AL_USDMaya  0.29.4
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TransformationMatrix.h
1 //
2 // Copyright 2017 Animal Logic
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.//
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #pragma once
17 
18 #include "../Api.h"
19 
20 #include "AL/usdmaya/TransformOperation.h"
21 
22 #include "maya/MPxTransformationMatrix.h"
23 #include "maya/MPxTransform.h"
24 
25 #include "pxr/pxr.h"
26 #include "pxr/usd/usdGeom/xform.h"
27 #include "pxr/usd/usdGeom/xformCommonAPI.h"
28 
29 PXR_NAMESPACE_USING_DIRECTIVE
30 
31 namespace AL {
32 namespace usdmaya {
33 namespace nodes {
34 
35 //----------------------------------------------------------------------------------------------------------------------
40 //----------------------------------------------------------------------------------------------------------------------
42  : public MPxTransformationMatrix
43 {
44  UsdPrim m_prim;
45  UsdGeomXform m_xform;
46  UsdTimeCode m_time;
47  std::vector<UsdGeomXformOp> m_xformops;
48  std::vector<TransformOperation> m_orderedOps;
49  MObject m_transformNode;
50 
51  // tweak values. These are applied on top of the USD transform values to produce the final result.
52  MVector m_scaleTweak;
53  MEulerRotation m_rotationTweak;
54  MVector m_translationTweak;
55  MVector m_shearTweak;
56  MPoint m_scalePivotTweak;
57  MVector m_scalePivotTranslationTweak;
58  MPoint m_rotatePivotTweak;
59  MVector m_rotatePivotTranslationTweak;
60  MQuaternion m_rotateOrientationTweak;
61 
62  // values read in from USD
63  MVector m_scaleFromUsd;
64  MEulerRotation m_rotationFromUsd;
65  MVector m_translationFromUsd;
66  MVector m_shearFromUsd;
67  MPoint m_scalePivotFromUsd;
68  MVector m_scalePivotTranslationFromUsd;
69  MPoint m_rotatePivotFromUsd;
70  MVector m_rotatePivotTranslationFromUsd;
71  MQuaternion m_rotateOrientationFromUsd;
72 
73  // post-transform translation value applied in object space after all other transformations
74  MVector m_localTranslateOffset;
75 
76  // methods that will insert a transform op into the ordered queue of operations, if for some.
77  void insertTranslateOp();
78  void insertScaleOp();
79  void insertShearOp();
80  void insertScalePivotOp();
81  void insertScalePivotTranslationOp();
82  void insertRotateOp();
83  void insertRotatePivotOp();
84  void insertRotatePivotTranslationOp();
85  void insertRotateAxesOp();
86 
87  enum Flags
88  {
89  // describe which components are animated
90  kAnimatedScale = 1 << 0,
91  kAnimatedRotation = 1 << 1,
92  kAnimatedTranslation = 1 << 2,
93  kAnimatedMatrix = 1 << 3,
94  kAnimatedShear = 1 << 4,
95 
96  // are the transform ops coming from a matrix, the PXR schema, or from the maya schema (no flags set)
97  kFromMatrix = 1 << 8,
98  kFromMayaSchema = 1 << 9,
99 
100  // which transform components are present in the prim?
101  kPrimHasScale = 1 << 16,
102  kPrimHasRotation = 1 << 17,
103  kPrimHasTranslation = 1 << 18,
104  kPrimHasShear = 1 << 19,
105  kPrimHasScalePivot = 1 << 20,
106  kPrimHasScalePivotTranslate = 1 << 21,
107  kPrimHasRotatePivot = 1 << 22,
108  kPrimHasRotatePivotTranslate = 1 << 23,
109  kPrimHasRotateAxes = 1 << 24,
110  kPrimHasPivot = 1 << 25,
111  kPrimHasTransform = 1 << 26,
112 
113  kPushToPrimEnabled = 1 << 28,
114  kInheritsTransform = 1 << 29,
115 
116  kPushPrimToMatrix = 1 << 30,
117  kReadAnimatedValues = 1 << 31,
118 
119  kAnimationMask = kAnimatedShear | kAnimatedScale | kAnimatedRotation | kAnimatedTranslation | kAnimatedMatrix,
120 
121  // Most of these flags are calculated based on reading the usd prim; however, a few are driven
122  // "externally" (ie, from attributes on the controlling transform node), and should NOT be reset
123  // when we're re-initializing (ie, in setPrim)
124  kPreservationMask = kPushToPrimEnabled | kReadAnimatedValues
125  };
126  uint32_t m_flags = 0;
127 
128  bool internal_readVector(MVector& result, const UsdGeomXformOp& op) { return readVector(result, op, getTimeCode()); }
129  bool internal_readShear(MVector& result, const UsdGeomXformOp& op) { return readShear(result, op, getTimeCode()); }
130  bool internal_readPoint(MPoint& result, const UsdGeomXformOp& op) { return readPoint(result, op, getTimeCode()); }
131  bool internal_readRotation(MEulerRotation& result, const UsdGeomXformOp& op) { return readRotation(result, op, getTimeCode()); }
132  double internal_readDouble(const UsdGeomXformOp& op) { return readDouble(op, getTimeCode()); }
133  bool internal_readMatrix(MMatrix& result, const UsdGeomXformOp& op) { return readMatrix(result, op, getTimeCode()); }
134 
135  bool internal_pushVector(const MVector& result, UsdGeomXformOp& op) { return pushVector(result, op, getTimeCode()); }
136  bool internal_pushPoint(const MPoint& result, UsdGeomXformOp& op) { return pushPoint(result, op, getTimeCode()); }
137  bool internal_pushRotation(const MEulerRotation& result, UsdGeomXformOp& op) { return pushRotation(result, op, getTimeCode()); }
138  void internal_pushDouble(const double result, UsdGeomXformOp& op) { pushDouble(result, op, getTimeCode()); }
139  bool internal_pushShear(const MVector& result, UsdGeomXformOp& op) { return pushShear(result, op, getTimeCode()); }
140  bool internal_pushMatrix(const MMatrix& result, UsdGeomXformOp& op) { return pushMatrix(result, op, getTimeCode()); }
141 
142 public:
143 
146  void setMObject(const MObject object)
147  { m_transformNode = object; }
148 
152  {
153  MPlug plug(m_transformNode, MPxTransform::translate);
154  return plug.isLocked() ||
155  plug.child(0).isLocked() ||
156  plug.child(1).isLocked() ||
157  plug.child(2).isLocked();
158  }
159 
163  {
164  MPlug plug(m_transformNode, MPxTransform::rotate);
165  return plug.isLocked() ||
166  plug.child(0).isLocked() ||
167  plug.child(1).isLocked() ||
168  plug.child(2).isLocked();
169  }
170 
174  {
175  MPlug plug(m_transformNode, MPxTransform::scale);
176  return plug.isLocked() ||
177  plug.child(0).isLocked() ||
178  plug.child(1).isLocked() ||
179  plug.child(2).isLocked();
180  }
181 
187  static bool readVector(MVector& result, const UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::EarliestTime());
188 
194  static bool readShear(MVector& result, const UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::EarliestTime());
195 
201  static bool readPoint(MPoint& result, const UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::EarliestTime());
202 
208  static bool readRotation(MEulerRotation& result, const UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::EarliestTime());
209 
214  static double readDouble(const UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::EarliestTime());
215 
221  static bool readMatrix(MMatrix& result, const UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::EarliestTime());
222 
228  static bool pushVector(const MVector& input, UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
229 
235  static bool pushPoint(const MPoint& input, UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
236 
242  static bool pushRotation(const MEulerRotation& input, UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
243 
249  static void pushDouble(const double input, UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
250 
256  static bool pushShear(const MVector& input, UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
257 
263  static bool pushMatrix(const MMatrix& input, UsdGeomXformOp& op, UsdTimeCode timeCode = UsdTimeCode::Default());
264 
266  AL_USDMAYA_PUBLIC
267  static const MTypeId kTypeId;
268 
271  AL_USDMAYA_PUBLIC
272  static MPxTransformationMatrix* creator();
273 
276 
279  TransformationMatrix(const UsdPrim& prim);
280 
283  void setPrim(const UsdPrim& prim, Transform* transformNode);
284 
289  void enablePushToPrim(bool enabled);
290 
294  void enableReadAnimatedValues(bool enabled);
295 
301  inline UsdTimeCode getTimeCode()
302  { return readAnimatedValues() ? m_time : UsdTimeCode::Default(); }
303 
307  inline void setLocalTranslationOffset(const MVector& localTranslateOffset)
308  { m_localTranslateOffset = localTranslateOffset; }
309 
312  inline const UsdPrim& prim() const
313  { return m_prim; }
314 
315  //--------------------------------------------------------------------------------------------------------------------
317  //--------------------------------------------------------------------------------------------------------------------
318 
320  inline bool hasAnimation() const
321  { return (kAnimationMask & m_flags) != 0; }
322 
324  inline bool hasAnimatedScale() const
325  { return (kAnimatedScale & m_flags) != 0; }
326 
328  inline bool hasAnimatedShear() const
329  { return (kAnimatedShear & m_flags) != 0; }
330 
332  inline bool hasAnimatedTranslation() const
333  { return (kAnimatedTranslation & m_flags) != 0; }
334 
336  inline bool hasAnimatedRotation() const
337  { return (kAnimatedRotation & m_flags) != 0; }
338 
340  inline bool hasAnimatedMatrix() const
341  { return (kAnimatedMatrix & m_flags) != 0; }
342 
344  inline bool primHasScale() const
345  { return (kPrimHasScale & m_flags) != 0; }
346 
348  inline bool primHasRotation() const
349  { return (kPrimHasRotation & m_flags) != 0; }
350 
352  inline bool primHasTranslation() const
353  { return (kPrimHasTranslation & m_flags) != 0; }
354 
356  inline bool primHasShear() const
357  { return (kPrimHasShear & m_flags) != 0; }
358 
360  inline bool primHasScalePivot() const
361  { return (kPrimHasScalePivot & m_flags) != 0; }
362 
364  inline bool primHasScalePivotTranslate() const
365  { return (kPrimHasScalePivotTranslate & m_flags) != 0; }
366 
368  inline bool primHasRotatePivot() const
369  { return (kPrimHasRotatePivot & m_flags) != 0; }
370 
372  inline bool primHasRotatePivotTranslate() const
373  { return (kPrimHasRotatePivotTranslate & m_flags) != 0; }
374 
376  inline bool primHasRotateAxes() const
377  { return (kPrimHasRotateAxes & m_flags) != 0; }
378 
380  inline bool primHasPivot() const
381  { return (kPrimHasPivot & m_flags) != 0; }
382 
384  inline bool primHasTransform() const
385  { return (kPrimHasTransform & m_flags) != 0; }
386 
388  inline bool readAnimatedValues() const
389  { return (kReadAnimatedValues & m_flags) != 0; }
390 
392  inline bool pushToPrimEnabled() const
393  { return (kPushToPrimEnabled & m_flags) != 0; }
394 
396  inline bool pushPrimToMatrix() const
397  { return (kPushPrimToMatrix & m_flags) != 0; }
398 
400  inline bool pushToPrimAvailable() const
401  { return pushToPrimEnabled() && m_prim.IsValid(); }
402 
403  //--------------------------------------------------------------------------------------------------------------------
405  //--------------------------------------------------------------------------------------------------------------------
406 
413  void initialiseToPrim(bool readFromPrim = true, Transform* node = 0);
414 
418  void updateToTime(const UsdTimeCode& time);
419 
421  void pushToPrim();
422 
423 private:
424  // Translation methods:
425  MStatus translateTo(const MVector &vector, MSpace::Space = MSpace::kTransform) override;
426 
427  // Scale methods:
428  MStatus scaleTo(const MVector &, MSpace::Space = MSpace::kTransform) override;
429 
430  // Shear methods:
431  MStatus shearTo(const MVector& shear, MSpace::Space = MSpace::kTransform) override;
432 
433  // Scale pivot methods:
434  MStatus setScalePivot(const MPoint &, MSpace::Space = MSpace::kTransform, bool balance = true) override;
435  MStatus setScalePivotTranslation(const MVector &vector, MSpace::Space = MSpace::kTransform) override;
436 
437  // Rotate pivot methods:
438  MStatus setRotatePivot(const MPoint &, MSpace::Space = MSpace::kTransform, bool balance = true) override;
439  MStatus setRotatePivotTranslation(const MVector &vector, MSpace::Space = MSpace::kTransform) override;
440 
441  // Rotate order methods:
442  MStatus setRotationOrder(MTransformationMatrix::RotationOrder, bool preserve = true) override;
443 
444  // Rotation methods:
445  MStatus rotateTo(const MQuaternion &q, MSpace::Space = MSpace::kTransform) override;
446  MStatus rotateTo(const MEulerRotation &e, MSpace::Space = MSpace::kTransform) override;
447  MStatus setRotateOrientation(const MQuaternion &q, MSpace::Space = MSpace::kTransform, bool balance = true) override;
448  MStatus setRotateOrientation(const MEulerRotation &euler, MSpace::Space = MSpace::kTransform, bool balance = true) override;
449 
450  // Compute matrix result
451  MMatrix asMatrix() const override;
452  MMatrix asMatrix(double percent) const override;
453 };
454 
455 //----------------------------------------------------------------------------------------------------------------------
456 } // nodes
457 } // usdmaya
458 } // AL
459 //----------------------------------------------------------------------------------------------------------------------
bool primHasScalePivotTranslate() const
does the UsdGeomXform have a scale pivot translate op?
Definition: TransformationMatrix.h:364
static bool readVector(MVector &result, const UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::EarliestTime())
helper method. Reads a vector from the transform op specified at the requested timecode ...
UsdTimeCode getTimeCode()
Returns the timecode to use when pushing the transform values to the USD prim. If readFromTimeline fl...
Definition: TransformationMatrix.h:301
bool primHasScale() const
does the UsdGeomXform have a scale transform op?
Definition: TransformationMatrix.h:344
bool hasAnimatedRotation() const
does the prim have animated rotation?
Definition: TransformationMatrix.h:336
void enableReadAnimatedValues(bool enabled)
If set to true, transform values will target the animated key-frame values in the prim...
static bool readRotation(MEulerRotation &result, const UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::EarliestTime())
helper method. Reads an euler rotation from the transform op specified at the requested timecode ...
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:376
static AL_USDMAYA_PUBLIC const MTypeId kTypeId
the type ID of the transformation matrix
Definition: TransformationMatrix.h:267
static AL_USDMAYA_PUBLIC MPxTransformationMatrix * creator()
create an instance of this transformation matrix
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:312
static double readDouble(const UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::EarliestTime())
helper method. Reads a double from the transform op specified at the requested timecode (typically Ro...
bool hasAnimatedShear() const
does the prim have animated shear?
Definition: TransformationMatrix.h:328
bool readAnimatedValues() const
should we read the animated keyframes or the defaults?
Definition: TransformationMatrix.h:388
bool pushToPrimAvailable() const
Is this transform set to write back onto the USD prim, and is it currently possible?
Definition: TransformationMatrix.h:400
bool primHasTranslation() const
does the UsdGeomXform have a translation transform op?
Definition: TransformationMatrix.h:352
bool isRotateLocked()
checks to see whether the rotate attribute is locked
Definition: TransformationMatrix.h:162
bool primHasPivot() const
does the UsdGeomXform have a pixar pivot op?
Definition: TransformationMatrix.h:380
static bool readMatrix(MMatrix &result, const UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::EarliestTime())
helper method. Reads a matrix from the transform op specified at the requested timecode ...
bool primHasTransform() const
does the UsdGeomXform have a transform matrix op?
Definition: TransformationMatrix.h:384
void pushToPrim()
pushes any modifications on the matrix back onto the UsdPrim
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 ...
bool pushToPrimEnabled() const
Is this transform set to write back onto the USD prim.
Definition: TransformationMatrix.h:392
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:348
bool hasAnimatedTranslation() const
does the prim have animated translation?
Definition: TransformationMatrix.h:332
static bool readPoint(MPoint &result, const UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::EarliestTime())
helper method. Reads a point 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:41
bool isScaleLocked()
checks to see whether the scale attribute is locked
Definition: TransformationMatrix.h:173
bool hasAnimatedMatrix() const
does the prim have an animated matrix only?
Definition: TransformationMatrix.h:340
The AL::usdmaya::nodes::Transform node is a custom transform node that allows you to manipulate a USD...
Definition: Transform.h:83
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:320
bool pushPrimToMatrix() const
Is this prim writing back to a matrix (true) or to components (false)
Definition: TransformationMatrix.h:396
bool isTranslateLocked()
checks to see whether the transform attribute is locked
Definition: TransformationMatrix.h:151
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:307
bool primHasRotatePivotTranslate() const
does the UsdGeomXform have a rotate pivot translate op?
Definition: TransformationMatrix.h:372
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 readShear(MVector &result, const UsdGeomXformOp &op, UsdTimeCode timeCode=UsdTimeCode::EarliestTime())
helper method. Reads a shear value from the transform op specified at the requested timecode ...
bool primHasScalePivot() const
does the UsdGeomXform have a scale pivot op?
Definition: TransformationMatrix.h:360
void setMObject(const MObject object)
sets the MObject for the transform
Definition: TransformationMatrix.h:146
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...
void setPrim(const UsdPrim &prim, Transform *transformNode)
set the prim that this transformation matrix will read/write to.
bool hasAnimatedScale() const
does the prim have animated scale?
Definition: TransformationMatrix.h:324
bool primHasRotatePivot() const
does the UsdGeomXform have a rotate pivot op?
Definition: TransformationMatrix.h:368
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:356