-
Notifications
You must be signed in to change notification settings - Fork 201
/
Utils.cpp
456 lines (383 loc) · 15.1 KB
/
Utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//
// Copyright 2019 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "Utils.h"
#include "private/Utils.h"
#include <mayaUsd/nodes/proxyShapeBase.h>
#include <mayaUsd/ufe/Global.h>
#include <mayaUsd/ufe/ProxyShapeHandler.h>
#include <mayaUsd/ufe/UsdStageMap.h>
#include <mayaUsd/utils/util.h>
#include <pxr/base/tf/hashset.h>
#include <pxr/base/tf/stringUtils.h>
#include <pxr/usd/pcp/layerStack.h>
#include <pxr/usd/pcp/site.h>
#include <pxr/usd/sdf/path.h>
#include <pxr/usd/sdf/tokens.h>
#include <pxr/usd/usd/prim.h>
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usdGeom/pointInstancer.h>
#include <pxr/usd/usdGeom/tokens.h>
#include <pxr/usdImaging/usdImaging/delegate.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MGlobal.h>
#include <maya/MObjectHandle.h>
#include <ufe/hierarchy.h>
#include <ufe/pathSegment.h>
#include <ufe/rtid.h>
#include <ufe/selection.h>
#include <cassert>
#include <cctype>
#include <memory>
#include <regex>
#include <stdexcept>
#include <string>
#include <unordered_map>
PXR_NAMESPACE_USING_DIRECTIVE
#ifndef MAYA_MSTRINGARRAY_ITERATOR_CATEGORY
// MStringArray::Iterator is not standard-compliant in Maya 2019, needs the
// following workaround. Fixed in Maya 2020. PPT, 20-Jun-2019.
namespace std {
template <> struct iterator_traits<MStringArray::Iterator>
{
typedef std::bidirectional_iterator_tag iterator_category;
};
} // namespace std
#endif
namespace {
constexpr auto kIllegalUSDPath = "Illegal USD run-time path %s.";
bool stringBeginsWithDigit(const std::string& inputString)
{
if (inputString.empty()) {
return false;
}
const char& firstChar = inputString.front();
if (std::isdigit(static_cast<unsigned char>(firstChar))) {
return true;
}
return false;
}
// This function calculates the position index for a given layer across all
// the site's local LayerStacks
uint32_t findLayerIndex(const UsdPrim& prim, const PXR_NS::SdfLayerHandle& layer)
{
uint32_t position { 0 };
const PXR_NS::PcpPrimIndex& primIndex = prim.GetPrimIndex();
// iterate through the expanded primIndex
for (PcpNodeRef node : primIndex.GetNodeRange()) {
TF_AXIOM(node);
const PcpLayerStackSite& site = node.GetSite();
const PcpLayerStackRefPtr& layerStack = site.layerStack;
// iterate through the "local" Layer stack for each site
// to find the layer
for (SdfLayerRefPtr const& l : layerStack->GetLayers()) {
if (l == layer) {
return position;
}
++position;
}
}
return position;
}
} // anonymous namespace
namespace MAYAUSD_NS_DEF {
namespace ufe {
//------------------------------------------------------------------------------
// Global variables & macros
//------------------------------------------------------------------------------
extern UsdStageMap g_StageMap;
extern Ufe::Rtid g_MayaRtid;
// Cache of Maya node types we've queried before for inheritance from the
// gateway node type.
std::unordered_map<std::string, bool> g_GatewayType;
//------------------------------------------------------------------------------
// Utility Functions
//------------------------------------------------------------------------------
UsdStageWeakPtr getStage(const Ufe::Path& path) { return g_StageMap.stage(path); }
Ufe::Path stagePath(UsdStageWeakPtr stage) { return g_StageMap.path(stage); }
TfHashSet<UsdStageWeakPtr, TfHash> getAllStages() { return g_StageMap.allStages(); }
Ufe::PathSegment usdPathToUfePathSegment(const SdfPath& usdPath, int instanceIndex)
{
const Ufe::Rtid usdRuntimeId = getUsdRunTimeId();
static const char separator = SdfPathTokens->childDelimiter.GetText()[0u];
if (usdPath.IsEmpty()) {
// Return an empty segment.
return Ufe::PathSegment(Ufe::PathSegment::Components(), usdRuntimeId, separator);
}
std::string pathString = usdPath.GetString();
if (instanceIndex >= 0) {
// Note here that we're taking advantage of the fact that identifiers
// in SdfPaths must be C/Python identifiers; that is, they must *not*
// begin with a digit. This means that when we see a path component at
// the end of a USD path segment that does begin with a digit, we can
// be sure that it represents an instance index and not a prim or other
// USD entity.
pathString += TfStringPrintf("%c%d", separator, instanceIndex);
}
return Ufe::PathSegment(pathString, usdRuntimeId, separator);
}
Ufe::Path stripInstanceIndexFromUfePath(const Ufe::Path& path)
{
if (path.empty()) {
return path;
}
// As with usdPathToUfePathSegment() above, we're taking advantage of the
// fact that identifiers in SdfPaths must be C/Python identifiers; that is,
// they must *not* begin with a digit. This means that when we see a path
// component at the end of a USD path segment that does begin with a digit,
// we can be sure that it represents an instance index and not a prim or
// other USD entity.
if (stringBeginsWithDigit(path.back().string())) {
return path.pop();
}
return path;
}
UsdPrim ufePathToPrim(const Ufe::Path& path)
{
const Ufe::Path ufePrimPath = stripInstanceIndexFromUfePath(path);
// Assume that there are only two segments in the path, the first a Maya
// Dag path segment to the proxy shape, which identifies the stage, and
// the second the USD segment.
// When called we do not make any assumption on whether or not the
// input path is valid.
const Ufe::Path::Segments& segments = ufePrimPath.getSegments();
if (!TF_VERIFY(segments.size() == 2u, kIllegalUSDPath, path.string().c_str())) {
return UsdPrim();
}
UsdPrim prim;
if (auto stage = getStage(Ufe::Path(segments[0]))) {
const SdfPath usdPath = SdfPath(segments[1].string());
prim = stage->GetPrimAtPath(usdPath.GetPrimPath());
}
return prim;
}
int ufePathToInstanceIndex(const Ufe::Path& path)
{
int instanceIndex = UsdImagingDelegate::ALL_INSTANCES;
const UsdPrim usdPrim = ufePathToPrim(path);
if (!usdPrim || !usdPrim.IsA<UsdGeomPointInstancer>()) {
return instanceIndex;
}
// Once more as above in usdPathToUfePathSegment() and
// stripInstanceIndexFromUfePath(), a path component at the tail of the
// path that begins with a digit is assumed to represent an instance index.
const std::string& tailComponentString = path.back().string();
if (stringBeginsWithDigit(path.back().string())) {
instanceIndex = std::stoi(tailComponentString);
}
return instanceIndex;
}
bool isRootChild(const Ufe::Path& path)
{
// When called we make the assumption that we are given a valid
// path and we are only testing whether or not we are a root child.
auto segments = path.getSegments();
if (segments.size() != 2) {
TF_RUNTIME_ERROR(kIllegalUSDPath, path.string().c_str());
}
return (segments[1].size() == 1);
}
UsdSceneItem::Ptr
createSiblingSceneItem(const Ufe::Path& ufeSrcPath, const std::string& siblingName)
{
auto ufeSiblingPath = ufeSrcPath.sibling(Ufe::PathComponent(siblingName));
return UsdSceneItem::create(ufeSiblingPath, ufePathToPrim(ufeSiblingPath));
}
std::string uniqueName(const TfToken::HashSet& existingNames, std::string srcName)
{
// Compiled regular expression to find a numerical suffix to a path component.
// It searches for any number of characters followed by a single non-numeric,
// then one or more digits at end of string.
std::regex re("(.*)([^0-9])([0-9]+)$");
std::string base { srcName };
int suffix { 1 };
std::smatch match;
if (std::regex_match(srcName, match, re)) {
base = match[1].str() + match[2].str();
suffix = std::stoi(match[3].str()) + 1;
}
std::string dstName = base + std::to_string(suffix);
while (existingNames.count(TfToken(dstName)) > 0) {
dstName = base + std::to_string(++suffix);
}
return dstName;
}
std::string uniqueChildName(const UsdPrim& usdParent, const std::string& name)
{
if (!usdParent.IsValid())
return std::string();
TfToken::HashSet childrenNames;
// The prim GetChildren method used the UsdPrimDefaultPredicate which includes
// active prims. We also need the inactive ones.
//
// const Usd_PrimFlagsConjunction UsdPrimDefaultPredicate =
// UsdPrimIsActive && UsdPrimIsDefined &&
// UsdPrimIsLoaded && !UsdPrimIsAbstract;
// Note: removed 'UsdPrimIsLoaded' from the predicate. When it is present the
// filter doesn't properly return the inactive prims. UsdView doesn't
// use loaded either in _computeDisplayPredicate().
//
// Note: our UsdHierarchy uses instance proxies, so we also use them here.
for (auto child : usdParent.GetFilteredChildren(
UsdTraverseInstanceProxies(UsdPrimIsDefined && !UsdPrimIsAbstract))) {
childrenNames.insert(child.GetName());
}
std::string childName { name };
if (childrenNames.find(TfToken(childName)) != childrenNames.end()) {
childName = uniqueName(childrenNames, childName);
}
return childName;
}
bool isAGatewayType(const std::string& mayaNodeType)
{
// If we've seen this node type before, return the cached value.
auto iter = g_GatewayType.find(mayaNodeType);
if (iter != std::end(g_GatewayType)) {
return iter->second;
}
// Note: we are calling the MEL interpreter to determine the inherited types,
// but we are then caching the result. So MEL will only be called once
// for each node type.
// Not seen before, so ask Maya.
// When the inherited flag is used, the command returns a string array containing
// the names of all the base node types inherited by the the given node.
MString cmd;
MStringArray inherited;
bool isInherited = false;
cmd.format("nodeType -inherited -isTypeName ^1s", mayaNodeType.c_str());
if (MS::kSuccess == MGlobal::executeCommand(cmd, inherited)) {
MString gatewayNodeType(ProxyShapeHandler::gatewayNodeType().c_str());
auto iter2 = std::find(inherited.begin(), inherited.end(), gatewayNodeType);
isInherited = (iter2 != inherited.end());
g_GatewayType[mayaNodeType] = isInherited;
}
return isInherited;
}
Ufe::Path dagPathToUfe(const MDagPath& dagPath)
{
// This function can only create UFE Maya scene items with a single
// segment, as it is only given a Dag path as input.
return Ufe::Path(dagPathToPathSegment(dagPath));
}
Ufe::PathSegment dagPathToPathSegment(const MDagPath& dagPath)
{
std::string fullPathName = dagPath.fullPathName().asChar();
return Ufe::PathSegment("world" + fullPathName, g_MayaRtid, '|');
}
UsdTimeCode getTime(const Ufe::Path& path)
{
// Path should not be empty.
if (!TF_VERIFY(!path.empty())) {
return UsdTimeCode::Default();
}
// Proxy shape node should not be null.
auto proxyShape = g_StageMap.proxyShapeNode(path);
if (!TF_VERIFY(proxyShape)) {
return UsdTimeCode::Default();
}
return proxyShape->getTime();
}
TfTokenVector getProxyShapePurposes(const Ufe::Path& path)
{
// Path should not be empty.
if (!TF_VERIFY(!path.empty())) {
return TfTokenVector();
}
// Proxy shape node should not be null.
auto proxyShape = g_StageMap.proxyShapeNode(path);
if (!TF_VERIFY(proxyShape)) {
return TfTokenVector();
}
bool renderPurpose, proxyPurpose, guidePurpose;
proxyShape->getDrawPurposeToggles(&renderPurpose, &proxyPurpose, &guidePurpose);
TfTokenVector purposes;
if (renderPurpose) {
purposes.emplace_back(UsdGeomTokens->render);
}
if (proxyPurpose) {
purposes.emplace_back(UsdGeomTokens->proxy);
}
if (guidePurpose) {
purposes.emplace_back(UsdGeomTokens->guide);
}
return purposes;
}
bool isAttributeEditAllowed(const PXR_NS::UsdAttribute& attr, std::string* errMsg)
{
// get the property spec in the edit target's layer
const auto& prim = attr.GetPrim();
const auto& stage = prim.GetStage();
const auto& editTarget = stage->GetEditTarget();
// get the index to edit target layer
const auto targetLayerIndex = findLayerIndex(prim, editTarget.GetLayer());
// HS March 22th,2021
// TODO: "Value Clips" are UsdStage-level feature, unknown to Pcp.So if the attribute in
// question is affected by Value Clips, we would will likely get the wrong answer. See Spiff
// comment for more information :
// https://groups.google.com/g/usd-interest/c/xTxFYQA_bRs/m/lX_WqNLoBAAJ
// Read on Value Clips here:
// https://graphics.pixar.com/usd/docs/api/_usd__page__value_clips.html
// get the strength-ordered ( strong-to-weak order ) list of property specs that provide
// opinions for this property.
const auto& propertyStack = attr.GetPropertyStack();
if (!propertyStack.empty()) {
// get the strongest layer that has the attr.
auto strongestLayer = attr.GetPropertyStack().front()->GetLayer();
// compare the calculated index between the "attr" and "edit target" layers.
if (findLayerIndex(prim, strongestLayer) < targetLayerIndex) {
if (errMsg) {
std::string err = TfStringPrintf(
"Cannot edit [%s] attribute because there is a stronger opinion in [%s].",
attr.GetBaseName().GetText(),
strongestLayer->GetDisplayName().c_str());
*errMsg = err;
}
return false;
}
}
return true;
}
Ufe::Selection removeDescendants(const Ufe::Selection& src, const Ufe::Path& filterPath)
{
// Filter the src selection, removing items below the filterPath
Ufe::Selection dst;
for (const auto& item : src) {
const auto& itemPath = item->path();
// The filterPath itself is still valid.
if (!itemPath.startsWith(filterPath) || itemPath == filterPath) {
dst.append(item);
}
}
return dst;
}
Ufe::Selection recreateDescendants(const Ufe::Selection& src, const Ufe::Path& filterPath)
{
// If a src selection item starts with the filterPath, re-create it.
Ufe::Selection dst;
for (const auto& item : src) {
const auto& itemPath = item->path();
// The filterPath itself is still valid.
if (!itemPath.startsWith(filterPath) || itemPath == filterPath) {
dst.append(item);
} else {
auto recreatedItem = Ufe::Hierarchy::createItem(item->path());
TF_AXIOM(recreatedItem);
dst.append(recreatedItem);
}
}
return dst;
}
} // namespace ufe
} // namespace MAYAUSD_NS_DEF