All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
util.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXRUSDMAYA_UTIL_H
25 #define PXRUSDMAYA_UTIL_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "usdMaya/api.h"
31 
32 #include "pxr/base/gf/vec2f.h"
33 #include "pxr/base/gf/vec3f.h"
34 #include "pxr/base/gf/vec4f.h"
35 #include "pxr/base/tf/declarePtrs.h"
36 #include "pxr/base/tf/refPtr.h"
37 #include "pxr/base/tf/token.h"
38 #include "pxr/base/vt/dictionary.h"
39 #include "pxr/base/vt/types.h"
40 #include "pxr/base/vt/value.h"
41 #include "pxr/usd/sdf/path.h"
42 #include "pxr/usd/usd/attribute.h"
43 #include "pxr/usd/usd/timeCode.h"
44 
45 #include <maya/MArgDatabase.h>
46 #include <maya/MBoundingBox.h>
47 #include <maya/MDagPath.h>
48 #include <maya/MDataHandle.h>
49 #include <maya/MDistance.h>
50 #include <maya/MFnDagNode.h>
51 #include <maya/MFnDependencyNode.h>
52 #include <maya/MFnMesh.h>
53 #include <maya/MFnNumericData.h>
54 #include <maya/MMatrix.h>
55 #include <maya/MObject.h>
56 #include <maya/MObjectHandle.h>
57 #include <maya/MPlug.h>
58 #include <maya/MStatus.h>
59 #include <maya/MString.h>
60 
61 #include <map>
62 #include <set>
63 #include <string>
64 #include <unordered_map>
65 #include <unordered_set>
66 #include <vector>
67 
68 
69 PXR_NAMESPACE_OPEN_SCOPE
70 
71 
73 namespace UsdMayaUtil
74 {
75 
76 struct _CmpDag
77 {
78  bool operator()(const MDagPath& lhs, const MDagPath& rhs) const
79  {
80  return strcmp(lhs.fullPathName().asChar(), rhs.fullPathName().asChar()) < 0;
81  }
82 };
83 
89 using MDagPathSet = std::set<MDagPath, _CmpDag>;
90 
96 template <typename V>
97 using MDagPathMap = std::map<MDagPath, V, _CmpDag>;
98 
99 struct _HashObjectHandle
100 {
101  unsigned long operator()(const MObjectHandle& handle) const
102  {
103  return handle.hashCode();
104  }
105 };
106 
109  std::unordered_set<MObjectHandle, _HashObjectHandle>;
110 
112 template <typename V>
114  std::unordered_map<MObjectHandle, V, _HashObjectHandle>;
115 
119 {
120  MPlug _plug;
121  MDataHandle _dataHandle;
122 
123 public:
124  PXRUSDMAYA_API
125  static TfRefPtr<MDataHandleHolder> New(const MPlug& plug);
126  PXRUSDMAYA_API
127  MDataHandle GetDataHandle() { return _dataHandle; }
128 
129 private:
130  MDataHandleHolder(const MPlug& plug, MDataHandle dataHandle);
131  ~MDataHandleHolder() override;
132 };
133 
134 const double MillimetersPerInch = 25.4;
135 
138 inline
139 double
140 ConvertMMToInches(const double mm)
141 {
142  return mm / MillimetersPerInch;
143 }
144 
147 inline
148 double
149 ConvertInchesToMM(const double inches)
150 {
151  return inches * MillimetersPerInch;
152 }
153 
154 const double MillimetersPerCentimeter = 10.0;
155 
158 inline
159 double
160 ConvertMMToCM(const double mm)
161 {
162  return mm / MillimetersPerCentimeter;
163 }
164 
167 inline
168 double
169 ConvertCMToMM(const double cm)
170 {
171  return cm * MillimetersPerCentimeter;
172 }
173 
176 PXRUSDMAYA_API
178  const MDistance::Unit mdistanceUnit);
179 
182 PXRUSDMAYA_API
184  const double linearUnit);
185 
196 PXRUSDMAYA_API
197 std::string GetMayaNodeName(const MObject& mayaNode);
198 
200 PXRUSDMAYA_API
201 MStatus GetMObjectByName(const std::string& nodeName, MObject& mObj);
202 
204 PXRUSDMAYA_API
205 MStatus GetDagPathByName(const std::string& nodeName, MDagPath& dagPath);
206 
210 PXRUSDMAYA_API
211 MStatus GetPlugByName(const std::string& attrPath, MPlug& plug);
212 
225 PXRUSDMAYA_API
226 MPlug GetMayaTimePlug();
227 
232 PXRUSDMAYA_API
233 MPlug GetMayaShaderListPlug();
234 
237 PXRUSDMAYA_API
238 MObject GetDefaultLightSetObject();
239 
240 PXRUSDMAYA_API
241 bool isAncestorDescendentRelationship(
242  const MDagPath& path1,
243  const MDagPath& path2);
244 
245 // returns 0 if static, 1 if sampled, and 2 if a curve
246 PXRUSDMAYA_API
247 int getSampledType(const MPlug& iPlug, const bool includeConnectedChildren);
248 
250 PXRUSDMAYA_API
251 bool isAnimated(const MObject& mayaObject, const bool checkParent = false);
252 
253 // Determine if a specific Maya plug is animated or not.
254 PXRUSDMAYA_API
255 bool isPlugAnimated(const MPlug& plug);
256 
262 PXRUSDMAYA_API
263 bool isIntermediate(const MObject& object);
264 
265 // returns true for visible and lod invisible and not templated objects
266 PXRUSDMAYA_API
267 bool isRenderable(const MObject& object);
268 
274 PXRUSDMAYA_API
275 bool isWritable(const MObject& object);
276 
279 const std::string MayaDagDelimiter("|");
280 
283 const std::string MayaNamespaceDelimiter(":");
284 
290 PXRUSDMAYA_API
291 std::string stripNamespaces(
292  const std::string& nodeName,
293  const int nsDepth = -1);
294 
295 PXRUSDMAYA_API
296 std::string SanitizeName(const std::string& name);
297 
298 // This to allow various pipeline to sanitize the colorset name for output
299 PXRUSDMAYA_API
300 std::string SanitizeColorSetName(const std::string& name);
301 
308 PXRUSDMAYA_API
310  const MFnDagNode& node,
311  PXR_NS::VtVec3fArray* RGBData,
312  PXR_NS::VtFloatArray* AlphaData,
313  PXR_NS::TfToken* interpolation,
314  PXR_NS::VtIntArray* assignmentIndices);
315 
329 PXRUSDMAYA_API
331  const MFnMesh& mesh,
332  PXR_NS::VtVec3fArray* RGBData,
333  PXR_NS::VtFloatArray* AlphaData,
334  PXR_NS::TfToken* interpolation,
335  PXR_NS::VtIntArray* assignmentIndices);
336 
339 PXRUSDMAYA_API
341  PXR_NS::VtFloatArray* valueData,
342  PXR_NS::VtIntArray* assignmentIndices);
343 
346 PXRUSDMAYA_API
348  PXR_NS::VtVec2fArray* valueData,
349  PXR_NS::VtIntArray* assignmentIndices);
350 
353 PXRUSDMAYA_API
355  PXR_NS::VtVec3fArray* valueData,
356  PXR_NS::VtIntArray* assignmentIndices);
357 
360 PXRUSDMAYA_API
362  PXR_NS::VtVec4fArray* valueData,
363  PXR_NS::VtIntArray* assignmentIndices);
364 
369 PXRUSDMAYA_API
371  const MFnMesh& mesh,
372  PXR_NS::TfToken* interpolation,
373  PXR_NS::VtIntArray* assignmentIndices);
374 
381 PXRUSDMAYA_API
382 bool IsAuthored(const MPlug& plug);
383 
384 PXRUSDMAYA_API
385 MPlug GetConnected(const MPlug& plug);
386 
387 PXRUSDMAYA_API
388 void Connect(
389  const MPlug& srcPlug,
390  const MPlug& dstPlug,
391  const bool clearDstPlug);
392 
394 PXRUSDMAYA_API
395 MPlug FindChildPlugByName(const MPlug& plug, const MString& name);
396 
402 PXRUSDMAYA_API
403 PXR_NS::SdfPath MayaNodeNameToSdfPath(
404  const std::string& nodeName,
405  const bool stripNamespaces);
406 
416 PXRUSDMAYA_API
417 PXR_NS::SdfPath MDagPathToUsdPath(
418  const MDagPath& dagPath,
419  const bool mergeTransformAndShape,
420  const bool stripNamespaces);
421 
423 PXRUSDMAYA_API
424 bool GetBoolCustomData(
425  const PXR_NS::UsdAttribute& obj,
426  const PXR_NS::TfToken& key,
427  const bool defaultValue);
428 
430 template <typename T>
432  const MFnDependencyNode& depNode,
433  const MString& attr,
434  T* val,
435  bool* isAnimated = nullptr)
436 {
437  MPlug plg = depNode.findPlug(attr, /* wantNetworkedPlug = */ true);
438  if (plg.isNull()) {
439  return false;
440  }
441 
442  if (isAnimated) {
443  *isAnimated = isPlugAnimated(plg);
444  }
445 
446  return plg.getValue(*val);
447 }
448 
450 PXRUSDMAYA_API
451 MMatrix GfMatrixToMMatrix(const GfMatrix4d& mx);
452 
453 // Like getPlugValue, but gets the matrix stored inside the MFnMatrixData on a
454 // plug.
455 // Returns true upon success, placing the matrix in the outVal parameter.
456 PXRUSDMAYA_API
457 bool getPlugMatrix(
458  const MFnDependencyNode& depNode,
459  const MString& attr,
460  MMatrix* outVal);
461 
464 PXRUSDMAYA_API
465 bool setPlugMatrix(
466  const MFnDependencyNode& depNode,
467  const MString& attr,
468  const GfMatrix4d& mx);
469 
470 PXRUSDMAYA_API
471 bool setPlugMatrix(const GfMatrix4d& mx, MPlug& plug);
472 
478 PXRUSDMAYA_API
479 bool setPlugValue(const PXR_NS::UsdAttribute& attr, MPlug& attrPlug);
480 
486 PXRUSDMAYA_API
487 bool setPlugValue(
488  const PXR_NS::UsdAttribute& attr,
489  const PXR_NS::UsdTimeCode time,
490  MPlug& attrPlug);
491 
494 template <typename T>
496  const MFnDependencyNode& depNode,
497  const MString& attr,
498  const T& val)
499 {
500  MPlug plg = depNode.findPlug(attr, /* findNetworked = */ false);
501  if (plg.isNull()) {
502  return false;
503  }
504 
505  return plg.setValue(val);
506 }
507 
512 PXRUSDMAYA_API
514 
515 PXRUSDMAYA_API
516 bool SetNotes(MFnDependencyNode& depNode, const std::string& notes);
517 
518 PXRUSDMAYA_API
519 bool SetHiddenInOutliner(MFnDependencyNode& depNode, const bool hidden);
520 
525 PXRUSDMAYA_API
527  const MArgDatabase& argData,
528  const VtDictionary& guideDict);
529 
536 PXRUSDMAYA_API
538  const std::string& key,
539  const std::string& value,
540  const VtDictionary& guideDict);
541 
548 PXRUSDMAYA_API
549 std::vector<std::string> GetAllAncestorMayaNodeTypes(const std::string& ty);
550 
554 PXRUSDMAYA_API
556  const MDagPath& dagPath,
557  MDagPath* assemblyPath = nullptr);
558 
559 PXRUSDMAYA_API
560 MBoundingBox GetInfiniteBoundingBox();
561 
562 } // namespace UsdMayaUtil
563 
564 
565 PXR_NAMESPACE_CLOSE_SCOPE
566 
567 
568 #endif
double ConvertMMToInches(const double mm)
Converts the given value mm in millimeters to the equivalent value in inches.
Definition: util.h:140
PXRUSDMAYA_API MStatus GetDagPathByName(const std::string &nodeName, MDagPath &dagPath)
Gets the Maya MDagPath for the node named nodeName.
PXRUSDMAYA_API MPlug GetMayaTimePlug()
Get the MPlug for the output time attribute of Maya&#39;s global time object.
PXRUSDMAYA_API TfRefPtr< MDataHandleHolder > GetPlugDataHandle(const MPlug &plug)
Obtains an RAII helper object for accessing the MDataHandle stored on the plug.
PXRUSDMAYA_API::SdfPath MayaNodeNameToSdfPath(const std::string &nodeName, const bool stripNamespaces)
Converts the given Maya node name nodeName into an SdfPath.
PXRUSDMAYA_API VtValue ParseArgumentValue(const std::string &key, const std::string &value, const VtDictionary &guideDict)
Parses value based on the type of key in guideDict, returning the parsed value wrapped in a VtValue...
std::map< MDagPath, V, _CmpDag > MDagPathMap
Mapping of DAG paths to an arbitrary type.
Definition: util.h:97
PXRUSDMAYA_API MPlug GetMayaShaderListPlug()
Get the MPlug for the shaders attribute of Maya&#39;s defaultShaderList.
A map with string keys and VtValue values.
Definition: dictionary.h:61
double ConvertCMToMM(const double cm)
Converts the given value cm in centimeters to the equivalent value in millimeters.
Definition: util.h:169
PXRUSDMAYA_API double ConvertMDistanceUnitToUsdGeomLinearUnit(const MDistance::Unit mdistanceUnit)
Converts the given value mdistance in Maya&#39;s MDistance units to the equivalent value in USD&#39;s metersP...
PXRUSDMAYA_API bool GetLinearShaderColor(const MFnDagNode &node, 1::VtVec3fArray *RGBData, 1::VtFloatArray *AlphaData, 1::TfToken *interpolation, 1::VtIntArray *assignmentIndices)
Get the base colors and opacities from the shader(s) bound to node.
PXRUSDMAYA_API MMatrix GfMatrixToMMatrix(const GfMatrix4d &mx)
Convert a Gf matrix to an MMatrix.
PXRUSDMAYA_API bool isAnimated(const MObject &mayaObject, const bool checkParent=false)
Determine if the Maya object mayaObject is animated or not.
PXRUSDMAYA_API bool isWritable(const MObject &object)
Determine whether a Maya object can be saved to or exported from the Maya scene.
PXRUSDMAYA_API void MergeEquivalentIndexedValues(1::VtFloatArray *valueData, 1::VtIntArray *assignmentIndices)
Combine distinct indices that point to the same values to all point to the same index for that value...
PXRUSDMAYA_API bool GetBoolCustomData(const 1::UsdAttribute &obj, const 1::TfToken &key, const bool defaultValue)
Convenience function to retrieve custom data.
PXRUSDMAYA_API bool IsAuthored(const MPlug &plug)
Get whether plug is authored in the Maya scene.
PXRUSDMAYA_API MStatus GetMObjectByName(const std::string &nodeName, MObject &mObj)
Gets the Maya MObject for the node named nodeName.
PXRUSDMAYA_API::SdfPath MDagPathToUsdPath(const MDagPath &dagPath, const bool mergeTransformAndShape, const bool stripNamespaces)
Converts the given Maya MDagPath dagPath into an SdfPath.
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:72
PXRUSDMAYA_API bool FindAncestorSceneAssembly(const MDagPath &dagPath, MDagPath *assemblyPath=nullptr)
If dagPath is a scene assembly node or is the descendant of one, populates the *assemblyPath with the...
PXRUSDMAYA_API VtDictionary GetDictionaryFromArgDatabase(const MArgDatabase &argData, const VtDictionary &guideDict)
Reads values from the given argData into a VtDictionary, using the guideDict to figure out which keys...
PXRUSDMAYA_API std::string stripNamespaces(const std::string &nodeName, const int nsDepth=-1)
Strip nsDepth namespaces from nodeName.
PXRUSDMAYA_API void CompressFaceVaryingPrimvarIndices(const MFnMesh &mesh, 1::TfToken *interpolation, 1::VtIntArray *assignmentIndices)
Attempt to compress faceVarying primvar indices to uniform, vertex, or constant interpolation if poss...
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
const std::string MayaNamespaceDelimiter(":")
This is the delimiter that Maya uses to separate levels of namespace in Maya node names...
PXRUSDMAYA_API MPlug FindChildPlugByName(const MPlug &plug, const MString &name)
Get a named child plug of plug by name.
PXRUSDMAYA_API std::string GetMayaNodeName(const MObject &mayaNode)
Get the full name of the Maya node mayaNode.
const std::string MayaDagDelimiter("|")
This is the delimiter that Maya uses to identify levels of hierarchy in the Maya DAG.
double ConvertInchesToMM(const double inches)
Converts the given value inches in inches to the equivalent value in millimeters. ...
Definition: util.h:149
PXRUSDMAYA_API MStatus GetPlugByName(const std::string &attrPath, MPlug &plug)
Gets the Maya MPlug for the given attrPath.
std::unordered_map< MObjectHandle, V, _HashObjectHandle > MObjectHandleUnorderedMap
Unordered mapping of Maya object handles to an arbitrary type.
Definition: util.h:114
PXRUSDMAYA_API bool setPlugValue(const 1::UsdAttribute &attr, MPlug &attrPlug)
Given an usdAttr , extract the value at the default timecode and write it on attrPlug.
std::set< MDagPath, _CmpDag > MDagPathSet
Set of DAG paths.
Definition: util.h:89
bool getPlugValue(const MFnDependencyNode &depNode, const MString &attr, T *val, bool *isAnimated=nullptr)
Compute the value of attr, returning true upon success.
Definition: util.h:431
PXRUSDMAYA_API bool setPlugMatrix(const MFnDependencyNode &depNode, const MString &attr, const GfMatrix4d &mx)
Set a matrix value on plug name attr, of depNode.
std::unordered_set< MObjectHandle, _HashObjectHandle > MObjectHandleUnorderedSet
Unordered set of Maya object handles.
Definition: util.h:109
PXRUSDMAYA_API std::vector< std::string > GetAllAncestorMayaNodeTypes(const std::string &ty)
Gets all Maya node types that are ancestors of the given Maya node type ty.
PXRUSDMAYA_API MDistance::Unit ConvertUsdGeomLinearUnitToMDistanceUnit(const double linearUnit)
Coverts the given value linearUnit in USD&#39;s metersPerUnit to the equivalent value in Maya&#39;s MDistance...
PXRUSDMAYA_API MObject GetDefaultLightSetObject()
Get the MObject for the DefaultLightSet, which should add any light nodes as members for them to take...
Reference-counted smart pointer utility class.
Definition: hash.h:45
PXRUSDMAYA_API bool isIntermediate(const MObject &object)
Determine if a Maya object is an intermediate object.
double ConvertMMToCM(const double mm)
Converts the given value mm in millimeters to the equivalent value in centimeters.
Definition: util.h:160
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
RAII-style helper for destructing an MDataHandle obtained from a plug once it goes out of scope...
Definition: util.h:118