AL_USDMaya  0.16.6
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
DgNodeTranslator.h
1 #pragma once
2 #include "AL/usdmaya/Common.h"
3 #include "AL/usdmaya/AttributeType.h"
4 #include "AL/maya/DgNodeHelper.h"
5 
6 #include "maya/MPlug.h"
7 #include "maya/MAngle.h"
8 #include "maya/MDistance.h"
9 #include "maya/MTime.h"
10 #include "maya/MFnAnimCurve.h"
11 
12 #include "maya/MGlobal.h"
13 #include "maya/MStatus.h"
14 
15 #include "pxr/base/gf/half.h"
16 #include "pxr/usd/usd/attribute.h"
17 #include "pxr/usd/usdGeom/xformOp.h"
18 
19 #include <string>
20 #include <vector>
21 
22 namespace AL {
23 namespace usdmaya {
24 namespace fileio {
25 namespace translators {
26 //----------------------------------------------------------------------------------------------------------------------
29 //----------------------------------------------------------------------------------------------------------------------
31  : public maya::DgNodeHelper
32 {
33 public:
34 
37  static MStatus registerType();
38 
45  virtual MObject createNode(const UsdPrim& from, MObject parent, const char* nodeType, const ImporterParams& params);
46 
52  MStatus copyAttributes(const UsdPrim& from, MObject to, const ImporterParams& params);
53 
59  static MStatus copyAttributes(const MObject& from, UsdPrim& to, const ExporterParams& params);
60 
66  virtual bool attributeHandled(const UsdAttribute& usdAttr);
67 
68  //--------------------------------------------------------------------------------------------------------------------
70  //--------------------------------------------------------------------------------------------------------------------
71 
78  template<typename T>
79  static MStatus setVec3Anim(MObject node, MObject attr, const UsdGeomXformOp op, double conversionFactor = 1.0);
80 
86  MStatus setAngleAnim(MObject node, MObject attr, const UsdGeomXformOp op);
87 
94  MStatus setFloatAttrAnim(MObject node, MObject attr, UsdAttribute usdAttr, double conversionFactor = 1.0);
95 
96  //--------------------------------------------------------------------------------------------------------------------
98  //--------------------------------------------------------------------------------------------------------------------
99 
105  static MStatus getUsdBoolArray(const MObject& node, const MObject& attr, VtArray<bool>& values);
106 
112  static MStatus getUsdInt8Array(const MObject& node, const MObject& attr, VtArray<int8_t>& values);
113 
119  static MStatus getUsdInt16Array(const MObject& node, const MObject& attr, VtArray<int16_t>& values);
120 
126  static MStatus getUsdInt32Array(const MObject& node, const MObject& attr, VtArray<int32_t>& values);
127 
133  static MStatus getUsdInt64Array(const MObject& node, const MObject& attr, VtArray<int64_t>& values);
134 
140  static MStatus getUsdHalfArray(const MObject& node, const MObject& attr, VtArray<half>& values);
141 
147  static MStatus getUsdFloatArray(const MObject& node, const MObject& attr, VtArray<float>& values);
148 
154  static MStatus getUsdDoubleArray(const MObject& node, const MObject& attr, VtArray<double>& values);
155 
161  static MStatus setUsdBoolArray(const MObject& node, const MObject& attr, const VtArray<bool>& values);
162 
168  static MStatus setUsdInt8Array(const MObject& node, const MObject& attr, const VtArray<int8_t>& values);
169 
175  static MStatus setUsdInt16Array(const MObject& node, const MObject& attr, const VtArray<int16_t>& values);
176 
182  static MStatus setUsdInt32Array(const MObject& node, const MObject& attr, const VtArray<int32_t>& values);
183 
189  static MStatus setUsdInt64Array(const MObject& node, const MObject& attr, const VtArray<int64_t>& values);
190 
196  static MStatus setUsdHalfArray(const MObject& node, const MObject& attr, const VtArray<half>& values);
197 
203  static MStatus setUsdFloatArray(const MObject& node, const MObject& attr, const VtArray<float>& values);
204 
210  static MStatus setUsdDoubleArray(const MObject& node, const MObject& attr, const VtArray<double>& values);
211 
212  //--------------------------------------------------------------------------------------------------------------------
214  //--------------------------------------------------------------------------------------------------------------------
215 
221  static MStatus copyBool(MObject node, MObject attr, const UsdAttribute& value);
222 
228  static MStatus copyFloat(MObject node, MObject attr, const UsdAttribute& value);
229 
235  static MStatus copyDouble(MObject node, MObject attr, const UsdAttribute& value);
236 
242  static MStatus copyInt(MObject node, MObject attr, const UsdAttribute& value);
243 
249  static MStatus copyVec3(MObject node, MObject attr, const UsdAttribute& value);
250 
251  //--------------------------------------------------------------------------------------------------------------------
253  //--------------------------------------------------------------------------------------------------------------------
254 
261  static MStatus setSingleMayaValue(MObject node, MObject attr, const UsdAttribute& usdAttr, const UsdDataType type);
262 
269  static MStatus setArrayMayaValue(MObject node, MObject attr, const UsdAttribute& usdAttr, const UsdDataType type);
270 
276  static MStatus setMayaValue(MObject node, MObject attr, const UsdAttribute& usdAttr);
277 
282  static MStatus addDynamicAttribute(MObject node, const UsdAttribute& usdAttr);
283 
288  static MStatus copyDynamicAttributes(MObject node, UsdPrim& prim);
289 
294  static void copyAttributeValue(const MPlug& attr, UsdAttribute& usdAttr, const UsdTimeCode& timeCode);
295 
300  static void copySimpleValue(const MPlug& plug, UsdAttribute& usdAttr, const UsdTimeCode& timeCode);
301 
307  static void copyAttributeValue(const MPlug& attr, UsdAttribute& usdAttr, float scale, const UsdTimeCode& timeCode);
308 
314  static void copySimpleValue(const MPlug& plug, UsdAttribute& usdAttr, float scale, const UsdTimeCode& timeCode);
315 };
316 
317 //----------------------------------------------------------------------------------------------------------------------
318 template<typename T>
319 MStatus DgNodeTranslator::setVec3Anim(MObject node, MObject attr, const UsdGeomXformOp op, double conversionFactor)
320 {
321  MPlug plug(node, attr);
322  MStatus status;
323  const char* const xformErrorCreate = "DgNodeTranslator:setVec3Anim error creating animation curve";
324 
325  MFnAnimCurve acFnSetX;
326  acFnSetX.create(plug.child(0), NULL, &status);
327  AL_MAYA_CHECK_ERROR(status, xformErrorCreate);
328 
329  MFnAnimCurve acFnSetY;
330  acFnSetY.create(plug.child(1), NULL, &status);
331  AL_MAYA_CHECK_ERROR(status, xformErrorCreate);
332 
333  MFnAnimCurve acFnSetZ;
334  acFnSetZ.create(plug.child(2), NULL, &status);
335  AL_MAYA_CHECK_ERROR(status, xformErrorCreate);
336 
337  std::vector<double> times;
338  op.GetTimeSamples(&times);
339 
340  const char* const xformErrorKey = "DgNodeTranslator:setVec3Anim error setting key on animation curve";
341 
342  T value(0);
343  for(auto const& timeValue: times)
344  {
345  const bool retValue = op.GetAs<T>(&value, timeValue);
346  if (!retValue) continue;
347 
348  MTime tm(timeValue, MTime::kFilm);
349 
350  switch (acFnSetX.animCurveType())
351  {
352  case MFnAnimCurve::kAnimCurveTL: // time->distance: translation
353  case MFnAnimCurve::kAnimCurveTA: // time->angle: rotation
354  case MFnAnimCurve::kAnimCurveTU: // time->double: scale
355  {
356  acFnSetX.addKey(tm, value[0] * conversionFactor, MFnAnimCurve::kTangentGlobal, MFnAnimCurve::kTangentGlobal, NULL, &status);
357  AL_MAYA_CHECK_ERROR(status, xformErrorKey);
358  acFnSetY.addKey(tm, value[1] * conversionFactor, MFnAnimCurve::kTangentGlobal, MFnAnimCurve::kTangentGlobal, NULL, &status);
359  AL_MAYA_CHECK_ERROR(status, xformErrorKey);
360  acFnSetZ.addKey(tm, value[2] * conversionFactor, MFnAnimCurve::kTangentGlobal, MFnAnimCurve::kTangentGlobal, NULL, &status);
361  AL_MAYA_CHECK_ERROR(status, xformErrorKey);
362  break;
363  }
364  default:
365  {
366  break;
367  }
368  }
369  }
370 
371  return MS::kSuccess;
372 }
373 
374 //----------------------------------------------------------------------------------------------------------------------
375 inline MStatus DgNodeTranslator::getUsdInt8Array(const MObject& node, const MObject& attr, VtArray<int8_t>& values)
376 {
377  MPlug plug(node, attr);
378  if(!plug || !plug.isArray())
379  return MS::kFailure;
380  const uint32_t num = plug.numElements();
381  values.resize(num);
382  return getInt8Array(node, attr, values.data(), num);
383 }
384 
385 //----------------------------------------------------------------------------------------------------------------------
386 inline MStatus DgNodeTranslator::getUsdInt16Array(const MObject& node, const MObject& attr, VtArray<int16_t>& values)
387 {
388  MPlug plug(node, attr);
389  if(!plug || !plug.isArray())
390  return MS::kFailure;
391  const uint32_t num = plug.numElements();
392  values.resize(num);
393  return getInt16Array(node, attr, values.data(), num);
394 }
395 
396 //----------------------------------------------------------------------------------------------------------------------
397 inline MStatus DgNodeTranslator::getUsdInt32Array(const MObject& node, const MObject& attr, VtArray<int32_t>& values)
398 {
399  MPlug plug(node, attr);
400  if(!plug || !plug.isArray())
401  return MS::kFailure;
402  const uint32_t num = plug.numElements();
403  values.resize(num);
404  return getInt32Array(node, attr, values.data(), num);
405 }
406 
407 //----------------------------------------------------------------------------------------------------------------------
408 inline MStatus DgNodeTranslator::getUsdInt64Array(const MObject& node, const MObject& attr, VtArray<int64_t>& values)
409 {
410  MPlug plug(node, attr);
411  if(!plug || !plug.isArray())
412  return MS::kFailure;
413  const uint32_t num = plug.numElements();
414  values.resize(num);
415  return getInt64Array(node, attr, values.data(), num);
416 }
417 
418 //----------------------------------------------------------------------------------------------------------------------
419 inline MStatus DgNodeTranslator::getUsdHalfArray(const MObject& node, const MObject& attr, VtArray<half>& values)
420 {
421  MPlug plug(node, attr);
422  if(!plug || !plug.isArray())
423  return MS::kFailure;
424  const uint32_t num = plug.numElements();
425  values.resize(num);
426  return getHalfArray(node, attr, values.data(), num);
427 }
428 
429 //----------------------------------------------------------------------------------------------------------------------
430 inline MStatus DgNodeTranslator::getUsdFloatArray(const MObject& node, const MObject& attr, VtArray<float>& values)
431 {
432  MPlug plug(node, attr);
433  if(!plug || !plug.isArray())
434  return MS::kFailure;
435  const uint32_t num = plug.numElements();
436  values.resize(num);
437  return getFloatArray(node, attr, values.data(), num);
438 }
439 
440 //----------------------------------------------------------------------------------------------------------------------
441 inline MStatus DgNodeTranslator::getUsdDoubleArray(const MObject& node, const MObject& attr, VtArray<double>& values)
442 {
443  MPlug plug(node, attr);
444  if(!plug || !plug.isArray())
445  return MS::kFailure;
446  const uint32_t num = plug.numElements();
447  values.resize(num);
448  return getDoubleArray(node, attr, values.data(), num);
449 }
450 
451 //----------------------------------------------------------------------------------------------------------------------
452 inline MStatus DgNodeTranslator::setUsdInt8Array(const MObject& node, const MObject& attr, const VtArray<int8_t>& values)
453 {
454  return setInt8Array(node, attr, values.cdata(), values.size());
455 }
456 
457 //----------------------------------------------------------------------------------------------------------------------
458 inline MStatus DgNodeTranslator::setUsdInt16Array(const MObject& node, const MObject& attr, const VtArray<int16_t>& values)
459 {
460  return setInt16Array(node, attr, values.cdata(), values.size());
461 }
462 
463 //----------------------------------------------------------------------------------------------------------------------
464 inline MStatus DgNodeTranslator::setUsdInt32Array(const MObject& node, const MObject& attr, const VtArray<int32_t>& values)
465 {
466  return setInt32Array(node, attr, values.cdata(), values.size());
467 }
468 
469 //----------------------------------------------------------------------------------------------------------------------
470 inline MStatus DgNodeTranslator::setUsdInt64Array(const MObject& node, const MObject& attr, const VtArray<int64_t>& values)
471 {
472  return setInt64Array(node, attr, values.cdata(), values.size());
473 }
474 
475 //----------------------------------------------------------------------------------------------------------------------
476 inline MStatus DgNodeTranslator::setUsdHalfArray(const MObject& node, const MObject& attr, const VtArray<half>& values)
477 {
478  return setHalfArray(node, attr, values.cdata(), values.size());
479 }
480 
481 //----------------------------------------------------------------------------------------------------------------------
482 inline MStatus DgNodeTranslator::setUsdFloatArray(const MObject& node, const MObject& attr, const VtArray<float>& values)
483 {
484  return setFloatArray(node, attr, values.cdata(), values.size());
485 }
486 
487 //----------------------------------------------------------------------------------------------------------------------
488 inline MStatus DgNodeTranslator::setUsdDoubleArray(const MObject& node, const MObject& attr, const VtArray<double>& values)
489 {
490  return setDoubleArray(node, attr, values.cdata(), values.size());
491 }
492 
493 //----------------------------------------------------------------------------------------------------------------------
494 } // translators
495 } // fileio
496 } // usdmaya
497 } // AL
498 //----------------------------------------------------------------------------------------------------------------------
static void copySimpleValue(const MPlug &plug, UsdAttribute &usdAttr, const UsdTimeCode &timeCode)
copy the attribute value from the plug specified, at the given time, and store the data on the usdAtt...
static MStatus copyInt(MObject node, MObject attr, const UsdAttribute &value)
copy a boolean value from USD and apply to Maya attribute
static MStatus getDoubleArray(const MObject &node, const MObject &attr, std::vector< double > &values)
retrieve an array of double values from an attribute in Maya
Definition: DgNodeHelper.h:1154
static MStatus setFloatArray(const MObject &node, const MObject &attr, const std::vector< float > &values)
sets all values on a float array attribute on the specified node
Definition: DgNodeHelper.h:1195
MStatus setFloatAttrAnim(MObject node, MObject attr, UsdAttribute usdAttr, double conversionFactor=1.0)
creates animation curves in maya for the specified attribute
static MStatus setSingleMayaValue(MObject node, MObject attr, const UsdAttribute &usdAttr, const UsdDataType type)
copy a non array value from a usd attribute into the maya attribute specified
static MStatus setInt32Array(const MObject &node, const MObject &attr, const std::vector< int32_t > &values)
sets all values on a 32bit integer array attribute on the specified node
Definition: DgNodeHelper.h:1177
static MStatus addDynamicAttribute(MObject node, const UsdAttribute &usdAttr)
creates a new dynamic attribute on the Maya node specified which will be initialized from the usdAttr...
parameters for the exporter. These parameters are constructed by any command or file translator that ...
Definition: ExportParams.h:17
parameters for the importer
Definition: ImportParams.h:18
static MStatus getInt16Array(const MObject &node, const MObject &attr, std::vector< int16_t > &values)
retrieve an array of 16bit integer values from an attribute in Maya
Definition: DgNodeHelper.h:1099
MStatus setAngleAnim(MObject node, MObject attr, const UsdGeomXformOp op)
creates animation curves to animate the specified angle attribute
static MStatus copyDynamicAttributes(MObject node, UsdPrim &prim)
copy all custom attributes from the usd primitive onto the maya node.
static MStatus setDoubleArray(const MObject &node, const MObject &attr, const std::vector< double > &values)
sets all values on a double array attribute on the specified node
Definition: DgNodeHelper.h:1201
virtual MObject createNode(const UsdPrim &from, MObject parent, const char *nodeType, const ImporterParams &params)
Creates a new maya node of the given type and set attributes based on input prim. ...
UsdDataType
A generalized set of USD attribute types that enable switch statements (instead of the if/else approa...
Definition: AttributeType.h:12
static MStatus getFloatArray(const MObject &node, const MObject &attr, std::vector< float > &values)
retrieve an array of float values from an attribute in Maya
Definition: DgNodeHelper.h:1143
static MStatus setInt64Array(const MObject &node, const MObject &attr, const std::vector< int64_t > &values)
sets all values on a 64bit integer array attribute on the specified node
Definition: DgNodeHelper.h:1183
static MStatus getInt64Array(const MObject &node, const MObject &attr, std::vector< int64_t > &values)
retrieve an array of 64bit integer values from an attribute in Maya
Definition: DgNodeHelper.h:1121
static MStatus setVec3Anim(MObject node, MObject attr, const UsdGeomXformOp op, double conversionFactor=1.0)
creates animation curves in maya for the specified attribute
Definition: DgNodeTranslator.h:319
Utility class that provides support for setting/getting attributes.
Definition: DgNodeHelper.h:21
static MStatus setInt8Array(const MObject &node, const MObject &attr, const std::vector< int8_t > &values)
sets all values on a 8bit integer array attribute on the specified node
Definition: DgNodeHelper.h:1165
static MStatus copyDouble(MObject node, MObject attr, const UsdAttribute &value)
copy a boolean value from USD and apply to Maya attribute
static MStatus copyVec3(MObject node, MObject attr, const UsdAttribute &value)
copy a boolean value from USD and apply to Maya attribute
static MStatus copyFloat(MObject node, MObject attr, const UsdAttribute &value)
copy a boolean value from USD and apply to Maya attribute
static MStatus getInt8Array(const MObject &node, const MObject &attr, std::vector< int8_t > &values)
retrieve an array of 8 bit char values from an attribute in Maya
Definition: DgNodeHelper.h:1088
static MStatus copyBool(MObject node, MObject attr, const UsdAttribute &value)
copy a boolean value from USD and apply to Maya attribute
static MStatus getHalfArray(const MObject &node, const MObject &attr, std::vector< half > &values)
retrieve an array of float values from an attribute in Maya (converted to halfs)
Definition: DgNodeHelper.h:1132
MStatus copyAttributes(const UsdPrim &from, MObject to, const ImporterParams &params)
helper method to copy attributes from the UsdPrim to the Maya node
virtual bool attributeHandled(const UsdAttribute &usdAttr)
A temporary solution. Given a custom attribute, if a translator handles it somehow (i...
static MStatus registerType()
static type registration
static MStatus setInt16Array(const MObject &node, const MObject &attr, const std::vector< int16_t > &values)
sets all values on a 16bit integer array attribute on the specified node
Definition: DgNodeHelper.h:1171
static MStatus getInt32Array(const MObject &node, const MObject &attr, std::vector< int32_t > &values)
retrieve an array of 32bit integer values from an attribute in Maya
Definition: DgNodeHelper.h:1110
static MStatus setHalfArray(const MObject &node, const MObject &attr, const std::vector< half > &values)
sets all values on a float array attribute on the specified node (but convert from half float data) ...
Definition: DgNodeHelper.h:1189
static MStatus setMayaValue(MObject node, MObject attr, const UsdAttribute &usdAttr)
copy the value from the usdAttribute onto the maya attribute value
Utility class that transfers DgNodes between Maya and USD.
Definition: DgNodeTranslator.h:30
static void copyAttributeValue(const MPlug &attr, UsdAttribute &usdAttr, const UsdTimeCode &timeCode)
copy the attribute value from the plug specified, at the given time, and store the data on the usdAtt...
static MStatus setArrayMayaValue(MObject node, MObject attr, const UsdAttribute &usdAttr, const UsdDataType type)
copy an array value from a usd attribute into the maya attribute specified
#define AL_MAYA_CHECK_ERROR(status, ErrorString)
Given the status, validates that the status is ok. If not, an error is logged using the specified err...
Definition: Common.h:75