AL_USDMaya  0.29.4
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AnimationTranslator.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/fileio/translators/DgNodeTranslator.h"
21 #include "AL/usdmaya/fileio/translators/TranslatorBase.h"
22 
23 #include "maya/MPlug.h"
24 #include "maya/MString.h"
25 #include "maya/MDagPath.h"
26 #include "maya/MObjectHandle.h"
27 
28 #include <vector>
29 #include <array>
30 #include <map>
31 
32 #include <utility>
33 
34 #include "pxr/pxr.h"
35 #include "pxr/usd/usd/stage.h"
36 
37 PXR_NAMESPACE_USING_DIRECTIVE
38 
41 {
42  bool operator () (const MPlug& a, const MPlug& b) const
43  {
44  return strcmp(a.name().asChar(), b.name().asChar()) < 0;;
45  }
46 };
47 
50 {
51  bool operator () (const MDagPath& a, const MDagPath& b) const
52  {
53  return strcmp(a.fullPathName().asChar(), b.fullPathName().asChar()) < 0;;
54  }
55 };
56 
57 namespace AL {
58 namespace usdmaya {
59 namespace fileio {
60 
62 struct ScaledPair
63 {
64  UsdAttribute attr;
65  float scale;
66 };
67 
68 typedef std::map<MPlug, UsdAttribute, compare_MPlug> PlugAttrVector;
69 typedef std::map<MDagPath, UsdAttribute, compare_MDagPath> MeshAttrVector;
70 typedef std::map<MPlug, ScaledPair, compare_MPlug> PlugAttrScaledVector;
71 
72 //----------------------------------------------------------------------------------------------------------------------
75 //----------------------------------------------------------------------------------------------------------------------
77 {
78 public:
79 
90  static bool isAnimated(const MObject& node, const MObject& attr, const bool assumeExpressionIsAnimated = true)
91  { return isAnimated(MPlug(node, attr), assumeExpressionIsAnimated); }
92 
102  AL_USDMAYA_PUBLIC
103  static bool isAnimated(MPlug attr, bool assumeExpressionIsAnimated = true);
104 
111  AL_USDMAYA_PUBLIC
112  static bool isAnimatedMesh(const MDagPath& mesh);
113 
121  AL_USDMAYA_PUBLIC
122  static bool isAnimatedTransform(const MObject& transformNode);
123 
129  inline void addPlug(const MPlug& plug, const UsdAttribute& attribute, const bool assumeExpressionIsAnimated)
130  {
131  if(m_animatedPlugs.find(plug) != m_animatedPlugs.end())
132  return;
133  if(isAnimated(plug, assumeExpressionIsAnimated))
134  m_animatedPlugs.emplace(plug, attribute);
135  }
136 
143  inline void addPlug(const MPlug& plug, const UsdAttribute& attribute, const float scale, const bool assumeExpressionIsAnimated)
144  {
145  if(m_scaledAnimatedPlugs.find(plug) != m_scaledAnimatedPlugs.end())
146  return;
147  if(isAnimated(plug, assumeExpressionIsAnimated))
148  m_scaledAnimatedPlugs.emplace(plug, ScaledPair{attribute, scale});
149  }
150 
157  inline void addTransformPlug(const MPlug& plug, const UsdAttribute& attribute, const bool assumeExpressionIsAnimated)
158  {
159  if(m_animatedTransformPlugs.find(plug) != m_animatedTransformPlugs.end())
160  return;
161  if (isAnimated(plug, assumeExpressionIsAnimated))
162  m_animatedTransformPlugs.emplace(plug, attribute);
163  }
164 
169  inline void forceAddTransformPlug(const MPlug& plug, const UsdAttribute& attribute)
170  {
171  if(m_animatedTransformPlugs.find(plug) == m_animatedTransformPlugs.end())
172  m_animatedTransformPlugs.emplace(plug, attribute);
173  }
174 
180  inline void forceAddPlug(const MPlug& plug, const UsdAttribute& attribute, const float scale)
181  {
182  if(m_scaledAnimatedPlugs.find(plug) == m_scaledAnimatedPlugs.end())
183  m_scaledAnimatedPlugs.emplace(plug, ScaledPair{ attribute, scale });
184  }
185 
190  inline void forceAddPlug(const MPlug& plug, const UsdAttribute& attribute)
191  {
192  if(m_animatedPlugs.find(plug) == m_animatedPlugs.end())
193  m_animatedPlugs.emplace(plug, attribute);
194  }
195 
199  inline void addMesh(const MDagPath& path, const UsdAttribute& attribute)
200  {
201  if(m_animatedMeshes.find(path) == m_animatedMeshes.end())
202  m_animatedMeshes.emplace(path, attribute);
203  }
204 
207  AL_USDMAYA_PUBLIC
208  void exportAnimation(const ExporterParams& params);
209 
214  inline void addCustomAnimNode(translators::TranslatorBase* translator, MDagPath dagPath, UsdPrim usdPrim)
215  { m_animatedNodes.push_back({translator, dagPath, usdPrim}); }
216 
217 private:
218  static bool considerToBeAnimation(const MFn::Type nodeType);
219  static bool inheritTransform(const MDagPath &path);
220  static bool areTransformAttributesConnected(const MDagPath &path);
221 private:
222  struct NodeExportInfo
223  {
224  translators::TranslatorBase* m_translator;
225  MDagPath m_path;
226  UsdPrim m_prim;
227  };
228  std::vector<NodeExportInfo> m_animatedNodes;
229  PlugAttrVector m_animatedPlugs;
230  PlugAttrScaledVector m_scaledAnimatedPlugs;
231  PlugAttrVector m_animatedTransformPlugs;
232  MeshAttrVector m_animatedMeshes;
233 };
234 
235 
236 //----------------------------------------------------------------------------------------------------------------------
239 //----------------------------------------------------------------------------------------------------------------------
241 {
242 private:
243  constexpr static int transformAttributesCount {13};
244 public:
245  AL_USDMAYA_PUBLIC
247  inline std::array<MObject, transformAttributesCount>::const_iterator begin() const {return m_commonTransformAttributes.cbegin();}
248  inline std::array<MObject, transformAttributesCount>::const_iterator end() const {return m_commonTransformAttributes.cend();}
249  inline MObject inheritTransformAttribute()const {return m_inheritTransformAttribute;}
250 
251 private:
252  MStatus initialise();
253  std::array<MObject, transformAttributesCount> m_commonTransformAttributes;
254  MObject m_inheritTransformAttribute;
255 };
256 
257 //----------------------------------------------------------------------------------------------------------------------
258 } // fileio
259 } // usdmaya
260 } // AL
261 //----------------------------------------------------------------------------------------------------------------------
parameters for the exporter. These parameters are constructed by any command or file translator that ...
Definition: ExportParams.h:35
void forceAddPlug(const MPlug &plug, const UsdAttribute &attribute)
add an animated plug to the animation translator (if the plug is animated)
Definition: AnimationTranslator.h:190
void forceAddTransformPlug(const MPlug &plug, const UsdAttribute &attribute)
add a transform plug to the animation translator (if the plug is animated)
Definition: AnimationTranslator.h:169
void addMesh(const MDagPath &path, const UsdAttribute &attribute)
add a mesh to the animation translator
Definition: AnimationTranslator.h:199
static AL_USDMAYA_PUBLIC bool isAnimatedTransform(const MObject &transformNode)
returns true if the transform node is animated
an attribute that has a scaling on it (due to unit differences)
Definition: AnimationTranslator.h:62
UsdAttribute attr
the attribute to export
Definition: AnimationTranslator.h:64
void forceAddPlug(const MPlug &plug, const UsdAttribute &attribute, const float scale)
add a scaled plug to the animation translator (if the plug is animated)
Definition: AnimationTranslator.h:180
void addPlug(const MPlug &plug, const UsdAttribute &attribute, const float scale, const bool assumeExpressionIsAnimated)
add a plug to the animation translator (if the plug is animated)
Definition: AnimationTranslator.h:143
AL_USDMAYA_PUBLIC void exportAnimation(const ExporterParams &params)
After the scene has been exported, call this method to export the animation data on various attribute...
void addCustomAnimNode(translators::TranslatorBase *translator, MDagPath dagPath, UsdPrim usdPrim)
insert a prim into the anim translator for custom anim export.
Definition: AnimationTranslator.h:214
Base class for maya translator usd plugins. The TfType of these plugins has to be derived from the ba...
Definition: TranslatorBase.h:177
static bool isAnimated(const MObject &node, const MObject &attr, const bool assumeExpressionIsAnimated=true)
returns true if the attribute is animated
Definition: AnimationTranslator.h:90
operator to compare MDagPaths with < operator
Definition: AnimationTranslator.h:49
A utility class to provide static transform attributes for testing if a transform node is animated or...
Definition: AnimationTranslator.h:240
static AL_USDMAYA_PUBLIC bool isAnimatedMesh(const MDagPath &mesh)
returns true if the mesh is animated
float scale
the scale to apply
Definition: AnimationTranslator.h:65
operator to compare MPlugs with < operator
Definition: AnimationTranslator.h:40
void addPlug(const MPlug &plug, const UsdAttribute &attribute, const bool assumeExpressionIsAnimated)
add a plug to the animation translator (if the plug is animated)
Definition: AnimationTranslator.h:129
void addTransformPlug(const MPlug &plug, const UsdAttribute &attribute, const bool assumeExpressionIsAnimated)
add a transform plug to the animation translator (if the plug is animated)
Definition: AnimationTranslator.h:157
A utility class to help with exporting animated plugs from maya.
Definition: AnimationTranslator.h:76