AL_USDMaya  0.16.6
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
TransformationMatrix.h
1 #pragma once
2 #include "AL/usdmaya/TransformOperation.h"
3 
4 #include "maya/MPxTransformationMatrix.h"
5 
6 #include "pxr/usd/usdGeom/xform.h"
7 #include "pxr/usd/usdGeom/xformCommonAPI.h"
8 
9 namespace AL {
10 namespace usdmaya {
11 namespace nodes {
12 
13 //----------------------------------------------------------------------------------------------------------------------
18 //----------------------------------------------------------------------------------------------------------------------
20  : public MPxTransformationMatrix
21 {
22  UsdPrim m_prim;
23  UsdGeomXform m_xform;
24  UsdTimeCode m_time;
25  std::vector<UsdGeomXformOp> m_xformops;
26  std::vector<TransformOperation> m_orderedOps;
27 
28  // tweak values. These are applied on top of the USD transform values to produce the final result.
29  MVector m_scaleTweak;
30  MEulerRotation m_rotationTweak;
31  MVector m_translationTweak;
32  MVector m_shearTweak;
33  MPoint m_scalePivotTweak;
34  MVector m_scalePivotTranslationTweak;
35  MPoint m_rotatePivotTweak;
36  MVector m_rotatePivotTranslationTweak;
37  MQuaternion m_rotateOrientationTweak;
38 
39  // values read in from USD
40  MVector m_scaleFromUsd;
41  MEulerRotation m_rotationFromUsd;
42  MVector m_translationFromUsd;
43  MVector m_shearFromUsd;
44  MPoint m_scalePivotFromUsd;
45  MVector m_scalePivotTranslationFromUsd;
46  MPoint m_rotatePivotFromUsd;
47  MVector m_rotatePivotTranslationFromUsd;
48  MQuaternion m_rotateOrientationFromUsd;
49 
50  // post-transform translation value applied in object space after all other transformations
51  MVector m_localTranslateOffset;
52 
53  // methods that will insert a transform op into the ordered queue of operations, if for some.
54  void insertTranslateOp();
55  void insertScaleOp();
56  void insertShearOp();
57  void insertScalePivotOp();
58  void insertScalePivotTranslationOp();
59  void insertRotateOp();
60  void insertRotatePivotOp();
61  void insertRotatePivotTranslationOp();
62  void insertRotateAxesOp();
63 
64  enum Flags
65  {
66  // describe which components are animated
67  kAnimatedScale = 1 << 0,
68  kAnimatedRotation = 1 << 1,
69  kAnimatedTranslation = 1 << 2,
70  kAnimatedMatrix = 1 << 3,
71  kAnimatedShear = 1 << 4,
72 
73  // are the transform ops coming from a matrix, the PXR schema, or from the maya schema (no flags set)
74  kFromMatrix = 1 << 8,
75  kFromMayaSchema = 1 << 9,
76 
77  // which transform components are present in the prim?
78  kPrimHasScale = 1 << 16,
79  kPrimHasRotation = 1 << 17,
80  kPrimHasTranslation = 1 << 18,
81  kPrimHasShear = 1 << 19,
82  kPrimHasScalePivot = 1 << 20,
83  kPrimHasScalePivotTranslate = 1 << 21,
84  kPrimHasRotatePivot = 1 << 22,
85  kPrimHasRotatePivotTranslate = 1 << 23,
86  kPrimHasRotateAxes = 1 << 24,
87  kPrimHasPivot = 1 << 25,
88  kPrimHasTransform = 1 << 26,
89 
90  kPushToPrimEnabled = 1 << 28,
91  kInheritsTransform = 1 << 29,
92 
93  kPushPrimToMatrix = 1 << 30,
94  kReadAnimatedValues = 1 << 31,
95 
96  kAnimationMask = kAnimatedScale | kAnimatedRotation | kAnimatedTranslation | kAnimatedMatrix
97  };
98  uint32_t m_flags;
99 
100  bool internal_readVector(MVector& result, const UsdGeomXformOp& op) { return readVector(result, op, getTimeCode()); }
101  bool internal_readShear(MVector& result, const UsdGeomXformOp& op) { return readShear(result, op, getTimeCode()); }
102  bool internal_readPoint(MPoint& result, const UsdGeomXformOp& op) { return readPoint(result, op, getTimeCode()); }
103  bool internal_readRotation(MEulerRotation& result, const UsdGeomXformOp& op) { return readRotation(result, op, getTimeCode()); }
104  double internal_readDouble(const UsdGeomXformOp& op) { return readDouble(op, getTimeCode()); }
105  bool internal_readMatrix(MMatrix& result, const UsdGeomXformOp& op) { return readMatrix(result, op, getTimeCode()); }
106 
107  bool internal_pushVector(const MVector& result, UsdGeomXformOp& op) { return pushVector(result, op, getTimeCode()); }
108  bool internal_pushPoint(const MPoint& result, UsdGeomXformOp& op) { return pushPoint(result, op, getTimeCode()); }
109  bool internal_pushRotation(const MEulerRotation& result, UsdGeomXformOp& op) { return pushRotation(result, op, getTimeCode()); }
110  void internal_pushDouble(const double result, UsdGeomXformOp& op) { pushDouble(result, op, getTimeCode()); }
111  bool internal_pushShear(const MVector& result, UsdGeomXformOp& op) { return pushShear(result, op, getTimeCode()); }
112  bool internal_pushMatrix(const MMatrix& result, UsdGeomXformOp& op) { return pushMatrix(result, op, getTimeCode()); }
113 
114 public:
115 
121  static bool readVector(MVector& result, const UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
122 
128  static bool readShear(MVector& result, const UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
129 
135  static bool readPoint(MPoint& result, const UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
136 
142  static bool readRotation(MEulerRotation& result, const UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
143 
148  static double readDouble(const UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
149 
155  static bool readMatrix(MMatrix& result, const UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
156 
162  static bool pushVector(const MVector& input, UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
163 
169  static bool pushPoint(const MPoint& input, UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
170 
176  static bool pushRotation(const MEulerRotation& input, UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
177 
183  static void pushDouble(const double input, UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
184 
190  static bool pushShear(const MVector& input, UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
191 
197  static bool pushMatrix(const MMatrix& input, UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
198 
200  static const MTypeId kTypeId;
201 
204  static MPxTransformationMatrix* creator();
205 
208 
211  TransformationMatrix(const UsdPrim& prim);
212 
215  void setPrim(const UsdPrim& prim);
216 
221  void enablePushToPrim(bool enabled);
222 
226  void enableReadAnimatedValues(bool enabled);
227 
233  inline UsdTimeCode getTimeCode()
234  { return pushToPrimEnabled() ? m_time : UsdTimeCode::Default(); }
235 
239  inline void setLocalTranslationOffset(const MVector& localTranslateOffset)
240  { m_localTranslateOffset = localTranslateOffset; }
241 
244  inline const UsdPrim& prim() const
245  { return m_prim; }
246 
247  //--------------------------------------------------------------------------------------------------------------------
249  //--------------------------------------------------------------------------------------------------------------------
250 
252  inline bool hasAnimation() const
253  { return (kAnimationMask & m_flags) != 0; }
254 
256  inline bool hasAnimatedScale() const
257  { return (kAnimatedScale & m_flags) != 0; }
258 
260  inline bool hasAnimatedShear() const
261  { return (kAnimatedShear & m_flags) != 0; }
262 
264  inline bool hasAnimatedTranslation() const
265  { return (kAnimatedTranslation & m_flags) != 0; }
266 
268  inline bool hasAnimatedRotation() const
269  { return (kAnimatedRotation & m_flags) != 0; }
270 
272  inline bool hasAnimatedMatrix() const
273  { return (kAnimatedMatrix & m_flags) != 0; }
274 
276  inline bool primHasScale() const
277  { return (kPrimHasScale & m_flags) != 0; }
278 
280  inline bool primHasRotation() const
281  { return (kPrimHasRotation & m_flags) != 0; }
282 
284  inline bool primHasTranslation() const
285  { return (kPrimHasTranslation & m_flags) != 0; }
286 
288  inline bool primHasShear() const
289  { return (kPrimHasShear & m_flags) != 0; }
290 
292  inline bool primHasScalePivot() const
293  { return (kPrimHasScalePivot & m_flags) != 0; }
294 
296  inline bool primHasScalePivotTranslate() const
297  { return (kPrimHasScalePivotTranslate & m_flags) != 0; }
298 
300  inline bool primHasRotatePivot() const
301  { return (kPrimHasRotatePivot & m_flags) != 0; }
302 
304  inline bool primHasRotatePivotTranslate() const
305  { return (kPrimHasRotatePivotTranslate & m_flags) != 0; }
306 
308  inline bool primHasRotateAxes() const
309  { return (kPrimHasRotateAxes & m_flags) != 0; }
310 
312  inline bool primHasPivot() const
313  { return (kPrimHasPivot & m_flags) != 0; }
314 
316  inline bool primHasTransform() const
317  { return (kPrimHasTransform & m_flags) != 0; }
318 
320  inline bool readAnimatedValues() const
321  { return (kReadAnimatedValues & m_flags) != 0; }
322 
324  inline bool pushToPrimEnabled() const
325  { return (kPushToPrimEnabled & m_flags) != 0; }
326 
328  inline bool pushPrimToMatrix() const
329  { return (kPushPrimToMatrix & m_flags) != 0; }
330 
331  //--------------------------------------------------------------------------------------------------------------------
333  //--------------------------------------------------------------------------------------------------------------------
334 
341  void initialiseToPrim(bool readFromPrim = true, Transform* node = 0);
342 
346  void updateToTime(const UsdTimeCode& time);
347 
349  void pushToPrim();
350 
351 private:
352  // Translation methods:
353  MStatus translateTo(const MVector &vector, MSpace::Space = MSpace::kTransform) override;
354  MStatus translateBy(const MVector &vector, MSpace::Space = MSpace::kTransform) override;
355 
356  // Scale methods:
357  MStatus scaleTo(const MVector &, MSpace::Space = MSpace::kTransform) override;
358  MStatus scaleBy(const MVector &, MSpace::Space = MSpace::kTransform) override;
359 
360  // Shear methods:
361  MStatus shearTo(const MVector& shear, MSpace::Space = MSpace::kTransform) override;
362  MStatus shearBy(const MVector& shear, MSpace::Space = MSpace::kTransform) override;
363 
364  // Scale pivot methods:
365  MStatus setScalePivot(const MPoint &, MSpace::Space = MSpace::kTransform, bool balance = true) override;
366  MStatus setScalePivotTranslation(const MVector &vector, MSpace::Space = MSpace::kTransform) override;
367 
368  // Rotate pivot methods:
369  MStatus setRotatePivot(const MPoint &, MSpace::Space = MSpace::kTransform, bool balance = true) override;
370  MStatus setRotatePivotTranslation(const MVector &vector, MSpace::Space = MSpace::kTransform) override;
371 
372  // Rotate order methods:
373  MStatus setRotationOrder(MTransformationMatrix::RotationOrder, bool preserve = true) override;
374 
375  // Rotation methods:
376  MStatus rotateTo(const MQuaternion &q, MSpace::Space = MSpace::kTransform) override;
377  MStatus rotateBy(const MQuaternion &q, MSpace::Space = MSpace::kTransform) override;
378  MStatus rotateTo(const MEulerRotation &e, MSpace::Space = MSpace::kTransform) override;
379  MStatus rotateBy(const MEulerRotation &e, MSpace::Space = MSpace::kTransform) override;
380  MStatus setRotateOrientation(const MQuaternion &q, MSpace::Space = MSpace::kTransform, bool balance = true) override;
381  MStatus setRotateOrientation(const MEulerRotation &euler, MSpace::Space = MSpace::kTransform, bool balance = true) override;
382 
383  // Compute matrix result
384  MMatrix asMatrix() const;
385  MMatrix asMatrix(double percent) const;
386 };
387 
388 //----------------------------------------------------------------------------------------------------------------------
389 } // nodes
390 } // usdmaya
391 } // AL
392 //----------------------------------------------------------------------------------------------------------------------
bool primHasScalePivotTranslate() const
does the UsdGeomXform have a scale pivot translate op?
Definition: TransformationMatrix.h:296
UsdTimeCode getTimeCode()
Returns the timecode to use when pushing the transform values to the USD prim. If readFromTimeline fl...
Definition: TransformationMatrix.h:233
static MPxTransformationMatrix * creator()
create an instance of this transformation matrix
bool primHasScale() const
does the UsdGeomXform have a scale transform op?
Definition: TransformationMatrix.h:276
bool hasAnimatedRotation() const
does the prim have animated rotation?
Definition: TransformationMatrix.h:268
void enableReadAnimatedValues(bool enabled)
If set to true, transform values will target the animated key-frame values in the prim...
static bool pushShear(const MVector &input, UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::Default())
helper method. Pushes a shear into the transform op specified at the requested timecode ...
bool primHasRotateAxes() const
does the UsdGeomXform have a rotation axes op?
Definition: TransformationMatrix.h:308
static bool readMatrix(MMatrix &result, const UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::Default())
helper method. Reads a matrix from the transform op specified at the requested timecode ...
static const MTypeId kTypeId
the type ID of the transformation matrix
Definition: TransformationMatrix.h:200
void updateToTime(const UsdTimeCode &time)
this method updates the internal transformation components to the given time. Only the Transform node...
const UsdPrim & prim() const
return the prim this transform matrix is attached to
Definition: TransformationMatrix.h:244
bool hasAnimatedShear() const
does the prim have animated shear?
Definition: TransformationMatrix.h:260
bool readAnimatedValues() const
should we read the animated keyframes or the defaults?
Definition: TransformationMatrix.h:320
static bool readShear(MVector &result, const UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::Default())
helper method. Reads a shear value from the transform op specified at the requested timecode ...
bool primHasTranslation() const
does the UsdGeomXform have a translation transform op?
Definition: TransformationMatrix.h:284
bool primHasPivot() const
does the UsdGeomXform have a pixar pivot op?
Definition: TransformationMatrix.h:312
bool primHasTransform() const
does the UsdGeomXform have a transform matrix op?
Definition: TransformationMatrix.h:316
void setPrim(const UsdPrim &prim)
set the prim that this transformation matrix will read/write to.
void pushToPrim()
pushes any modifications on the matrix back onto the UsdPrim
static bool readRotation(MEulerRotation &result, const UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::Default())
helper method. Reads an euler rotation from the transform op specified at the requested timecode ...
static bool pushRotation(const MEulerRotation &input, UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::Default())
helper method. Pushes a vector into the transform op specified at the requested timecode ...
static double readDouble(const UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::Default())
helper method. Reads a double from the transform op specified at the requested timecode (typically Ro...
bool pushToPrimEnabled() const
Is this transform writing back onto the USD prim.
Definition: TransformationMatrix.h:324
void initialiseToPrim(bool readFromPrim=true, Transform *node=0)
this method inspects the UsdGeomXform to find out:what schema is being used (Maya, Pxr, or just a matrix)which transformation components are present (e.g. scale, rotate, etc).which, if any, of those components are animated.
bool primHasRotation() const
does the UsdGeomXform have a rotation transform op?
Definition: TransformationMatrix.h:280
bool hasAnimatedTranslation() const
does the prim have animated translation?
Definition: TransformationMatrix.h:264
static bool readVector(MVector &result, const UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::Default())
helper method. Reads a vector from the transform op specified at the requested timecode ...
This class provides a transformation matrix that allows you to apply tweaks over some read only trans...
Definition: TransformationMatrix.h:19
bool hasAnimatedMatrix() const
does the prim have an animated matrix only?
Definition: TransformationMatrix.h:272
The AL::usdmaya::nodes::Transform node is a custom transform node that allows you to manipulate a USD...
Definition: Transform.h:62
static bool pushVector(const MVector &input, UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::Default())
helper method. Pushes a vector into the transform op specified at the requested timecode ...
bool hasAnimation() const
does the prim have animated scale?
Definition: TransformationMatrix.h:252
bool pushPrimToMatrix() const
Is this prim writing back to a matrix (true) or to components (false)
Definition: TransformationMatrix.h:328
void enablePushToPrim(bool enabled)
If set to true, modifications to these transform attributes will be pushed back onto the original pri...
void setLocalTranslationOffset(const MVector &localTranslateOffset)
Applies a local space translation offset to the computed matrix. Useful for positioning objects on a ...
Definition: TransformationMatrix.h:239
bool primHasRotatePivotTranslate() const
does the UsdGeomXform have a rotate pivot translate op?
Definition: TransformationMatrix.h:304
static bool pushPoint(const MPoint &input, UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::Default())
helper method. Pushes a point into the transform op specified at the requested timecode ...
static bool readPoint(MPoint &result, const UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::Default())
helper method. Reads a point from the transform op specified at the requested timecode ...
bool primHasScalePivot() const
does the UsdGeomXform have a scale pivot op?
Definition: TransformationMatrix.h:292
static void pushDouble(const double input, UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::Default())
helper method. Pushes a double into the transform op specified at the requested timecode (typically f...
bool hasAnimatedScale() const
does the prim have animated scale?
Definition: TransformationMatrix.h:256
bool primHasRotatePivot() const
does the UsdGeomXform have a rotate pivot op?
Definition: TransformationMatrix.h:300
static bool pushMatrix(const MMatrix &input, UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::Default())
helper method. Pushes a matrix into the transform op specified at the requested timecode ...
bool primHasShear() const
does the UsdGeomXform have a shear transform op?
Definition: TransformationMatrix.h:288