AL_USDMaya  0.29.4
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TranslatorContext.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/ForwardDeclares.h>
21 #include "maya/MPxData.h"
22 #include "maya/MGlobal.h"
23 #include "maya/MObject.h"
24 #include "maya/MObjectHandle.h"
25 #include "maya/MObjectArray.h"
26 #include "maya/MDGModifier.h"
27 #include "pxr/pxr.h"
28 #include "pxr/base/tf/refPtr.h"
29 #include "pxr/usd/usd/prim.h"
30 #include "pxr/base/tf/debug.h"
31 #include "AL/usdmaya/DebugCodes.h"
32 
33 #include <vector>
34 #include <string>
35 #include "AL/usd/utils/ForwardDeclares.h"
36 
37 PXR_NAMESPACE_USING_DIRECTIVE
38 
39 
40 namespace AL {
41 namespace usdmaya {
42 namespace fileio {
43 namespace translators {
44 
45 typedef std::vector<MObjectHandle> MObjectHandleArray;
46 
47 
48 //----------------------------------------------------------------------------------------------------------------------
53 //----------------------------------------------------------------------------------------------------------------------
55 {
57  inline void setForcePrimImport(bool forceImport)
58  { forcePrimImport = forceImport; }
59 
61  inline bool forceTranslatorImport() const
62  { return forcePrimImport; }
63 
64 private:
65  bool forcePrimImport = false;
66 };
67 
68 //----------------------------------------------------------------------------------------------------------------------
72 //----------------------------------------------------------------------------------------------------------------------
74  : public TfRefBase
75 {
76 public:
78  typedef TfRefPtr<This> RefPtr;
79 
83  static RefPtr create(nodes::ProxyShape* proxyShape)
84  {
85  RefPtr res = TfCreateRefPtr(new This(proxyShape));
86  res->setForceDefaultRead(false);
87  return res;
88  }
89 
93  { return m_proxyShape; }
94 
97  AL_USDMAYA_PUBLIC
98  UsdStageRefPtr getUsdStage() const;
99 
105  bool getTransform(const UsdPrim& prim, MObjectHandle& object)
106  { return getTransform(prim.GetPath(), object); }
107 
113  AL_USDMAYA_PUBLIC
114  bool getTransform(const SdfPath& path, MObjectHandle& object);
115 
125  bool getMObject(const UsdPrim& prim, MObjectHandle& object, MTypeId type)
126  { return getMObject(prim.GetPath(), object, type); }
127 
137  AL_USDMAYA_PUBLIC
138  bool getMObject(const SdfPath& path, MObjectHandle& object, MTypeId type);
139 
149  bool getMObject(const UsdPrim& prim, MObjectHandle& object, MFn::Type type)
150  { return getMObject(prim.GetPath(), object, type); }
151 
161  AL_USDMAYA_PUBLIC
162  bool getMObject(const SdfPath& path, MObjectHandle& object, MFn::Type type);
163 
168  bool getMObjects(const UsdPrim& prim, MObjectHandleArray& returned)
169  { return getMObjects(prim.GetPath(), returned); }
170 
175  AL_USDMAYA_PUBLIC
176  bool getMObjects(const SdfPath& path, MObjectHandleArray& returned);
177 
182  AL_USDMAYA_PUBLIC
183  void insertItem(const UsdPrim& prim, MObjectHandle object);
184 
188  void removeItems(const UsdPrim& prim)
189  { removeItems(prim.GetPath()); }
190 
194  AL_USDMAYA_PUBLIC
195  void removeItems(const SdfPath& path);
196 
198  AL_USDMAYA_PUBLIC
200 
204  TfToken getTypeForPath(SdfPath path) const
205  {
206  const auto it = find(path);
207  if(it != m_primMapping.end())
208  {
209  return it->type();
210  }
211  TF_DEBUG(ALUSDMAYA_TRANSLATORS).Msg("TranslatorContext::getTypeForPath did not find item in mapping.%s\n", path.GetText());
212  return TfToken();
213  }
214 
217  AL_USDMAYA_PUBLIC
218  void updatePrimTypes();
219 
225  AL_USDMAYA_PUBLIC
226  void registerItem(const UsdPrim& prim, MObjectHandle object);
227 
230  AL_USDMAYA_PUBLIC
231  MString serialise() const;
232 
235  AL_USDMAYA_PUBLIC
236  void deserialise(const MString& string);
237 
239  AL_USDMAYA_PUBLIC
240  void validatePrims();
241 
247  bool hasEntry(const SdfPath& path, const TfToken& type)
248  {
249  auto it = find(path);
250  if(it != m_primMapping.end())
251  {
252  return type == it->type();
253  }
254  return false;
255  }
256 
262  AL_USDMAYA_PUBLIC
263  void preRemoveEntry(const SdfPath& primPath, SdfPathVector& itemsToRemove, bool callPreUnload=true);
264 
267  AL_USDMAYA_PUBLIC
268  void removeEntries(const SdfPathVector& itemsToRemove);
269 
273  struct PrimLookup
274  {
280  PrimLookup(const SdfPath& path, const TfToken& type, MObject mayaObj)
281  : m_path(path), m_type(type), m_object(mayaObj), m_createdNodes() {}
282 
285 
288  const SdfPath& path() const
289  { return m_path; }
290 
293  MObjectHandle objectHandle() const
294  { return m_object; }
295 
298  MObject object() const
299  { return m_object.object(); }
300 
303  TfToken type() const
304  { return m_type; }
305 
308  MObjectHandleArray& createdNodes()
309  { return m_createdNodes; }
310 
313  const MObjectHandleArray& createdNodes() const
314  { return m_createdNodes; }
315 
316  private:
317  SdfPath m_path;
318  TfToken m_type;
319  MObjectHandle m_object;
320  MObjectHandleArray m_createdNodes;
321  };
322 
324  typedef std::vector<PrimLookup> PrimLookups;
325 
328  {
333  inline bool operator() (const PrimLookup& a, const SdfPath& b) const
334  { return a.path() < b; }
335 
340  inline bool operator() (const SdfPath& a, const PrimLookup& b) const
341  { return a < b.path(); }
342 
348  inline bool operator() (const PrimLookup& a, const PrimLookup& b) const
349  { return a.path() < b.path(); }
350  };
351 
354  { m_primMapping.clear(); }
355 
359  AL_USDMAYA_PUBLIC
360  bool addExcludedGeometry(const SdfPath& newPath)
361  {
362  auto foundPath = m_excludedGeometry.find(newPath);
363 
364  if(foundPath != m_excludedGeometry.end())
365  {
366  return false;
367  }
368 
369  m_excludedGeometry.insert(newPath);
370  m_isExcludedGeometryDirty = true;
371  return true;
372  }
373 
377  AL_USDMAYA_PUBLIC
378  bool removeExcludedGeometry(const SdfPath& newPath)
379  {
380  auto foundPath = m_excludedGeometry.find(newPath);
381 
382  if(foundPath == m_excludedGeometry.end())
383  {
384  return false;
385  }
386  m_excludedGeometry.erase(newPath);
387  m_isExcludedGeometryDirty = true;
388  return true;
389  }
390 
393  inline const SdfPathSet& excludedGeometry()
394  {return m_excludedGeometry;}
395 
399  {return m_isExcludedGeometryDirty;}
400 
401 private:
402  void unloadPrim(
403  const SdfPath& primPath,
404  const MObject& primObj);
405  void preUnloadPrim(
406  UsdPrim& primPath,
407  const MObject& primObj);
408 
409  bool isNodeAncestorOf(MObjectHandle ancestorHandle, MObjectHandle objectHandleToTest);
410 
413  bool isPrimInTransformChain(const SdfPath& path);
414 
415  inline PrimLookups::iterator find(const SdfPath& path)
416  {
417  PrimLookups::iterator end = m_primMapping.end();
418  PrimLookups::iterator it = std::lower_bound(m_primMapping.begin(), end, path, value_compare());
419  if(it != end)
420  {
421  if(it->path() == path)
422  return it;
423  }
424  return end;
425  }
426 
427  inline PrimLookups::const_iterator find(const SdfPath& path) const
428  {
429  PrimLookups::const_iterator end = m_primMapping.end();
430  PrimLookups::const_iterator it = std::lower_bound(m_primMapping.begin(), end, path, value_compare());
431  if(it != end)
432  {
433  if(it->path() == path)
434  return it;
435  }
436  return end;
437  }
438 
439  inline PrimLookups::iterator findLocation(const SdfPath& path)
440  {
441  PrimLookups::iterator end = m_primMapping.end();
442  PrimLookups::iterator it = std::lower_bound(m_primMapping.begin(), end, path, value_compare());
443  return it;
444  }
445 
446 
447  TranslatorContext(nodes::ProxyShape* proxyShape)
448  : m_proxyShape(proxyShape), m_primMapping()
449  {}
450 
451  nodes::ProxyShape* m_proxyShape;
452 
453  // map between a usd prim path and either a dag parent node or
454  // a dependency node
455  PrimLookups m_primMapping;
456 
457  // true to make all translators that default to not importing Prims to always import Prims via the translators
458  bool m_forcePrimImport;
459 
460  // list of geometry that has been request to be excluded during the translation
461  SdfPathSet m_excludedGeometry;
462  bool m_isExcludedGeometryDirty;
463 
464 public:
465  void setForceDefaultRead(bool forceDefaultRead)
466  { m_forceDefaultRead = forceDefaultRead; }
467 
468  bool getForceDefaultRead()
469  { return m_forceDefaultRead; }
470 
471 private:
472  // If true, will explicitly read default attribute values.
473  bool m_forceDefaultRead;
474 
475 };
476 
477 typedef TfRefPtr<TranslatorContext> TranslatorContextPtr;
478 
479 //----------------------------------------------------------------------------------------------------------------------
480 } // translators
481 } // fileio
482 } // usdmaya
483 } // AL
484 //----------------------------------------------------------------------------------------------------------------------
This class provides a context to store mappings between UsdPrims, and the Maya nodes that represent t...
Definition: TranslatorContext.h:73
TfRefPtr< This > RefPtr
pointer to this type
Definition: TranslatorContext.h:78
std::vector< PrimLookup > PrimLookups
a sorted array of prim mappings
Definition: TranslatorContext.h:324
AL_USDMAYA_PUBLIC void preRemoveEntry(const SdfPath &primPath, SdfPathVector &itemsToRemove, bool callPreUnload=true)
This is called during a variant switch to determine whether the variant switch will allow Maya nodes ...
AL_USDMAYA_PUBLIC void updatePrimTypes()
this method is used after a variant switch to check to see if the prim types have changed in the stag...
AL_USDMAYA_PUBLIC void validatePrims()
debugging utility to help keep track of prims during a variant switch
AL_USDMAYA_PUBLIC bool removeExcludedGeometry(const SdfPath &newPath)
remove geometry from the exclusion list
Definition: TranslatorContext.h:378
A custom proxy shape node that attaches itself to a USD file, and then renders it. The stage is held internally as a member variable, and it will be composed based on a change to the "filePath" attribute.
Definition: ProxyShape.h:244
An internal structure used to store a mapping between an SdfPath, the type of prim found at that loca...
Definition: TranslatorContext.h:273
bool hasEntry(const SdfPath &path, const TfToken &type)
This method is used to determine whether this DB has an entry for the specified prim path and the giv...
Definition: TranslatorContext.h:247
comparison utility (for sorting array of pointers to node references based on their path) ...
Definition: TranslatorContext.h:327
AL_USDMAYA_PUBLIC bool addExcludedGeometry(const SdfPath &newPath)
add geometry to the exclusion list
Definition: TranslatorContext.h:360
Transient aggregate of values that aims to direct the Translation of Prims. Typically an object of th...
Definition: TranslatorContext.h:54
const MObjectHandleArray & createdNodes() const
get created maya nodes
Definition: TranslatorContext.h:313
TranslatorContext This
this type
Definition: TranslatorContext.h:77
void setForcePrimImport(bool forceImport)
Flag that determines if all Prim schema types should be forced to be imported.
Definition: TranslatorContext.h:57
const SdfPathSet & excludedGeometry()
retrieve currently excluded translator geometries
Definition: TranslatorContext.h:393
MObject object() const
get the maya object of the node
Definition: TranslatorContext.h:298
AL_USDMAYA_PUBLIC void deserialise(const MString &string)
deserialises the string back into the translator context
AL_USDMAYA_PUBLIC void registerItem(const UsdPrim &prim, MObjectHandle object)
Internal method. If within your custom translator plug-in you need to create any maya nodes...
bool getMObjects(const UsdPrim &prim, MObjectHandleArray &returned)
returns all of the maya nodes that were created by the specific prim
Definition: TranslatorContext.h:168
void removeItems(const UsdPrim &prim)
during a variant switch, if we lose a prim, then it's path will be passed into this method...
Definition: TranslatorContext.h:188
AL_USDMAYA_PUBLIC void insertItem(const UsdPrim &prim, MObjectHandle object)
If within your custom translator plug-in you need to create any maya nodes, associate that maya node ...
AL_USDMAYA_PUBLIC UsdStageRefPtr getUsdStage() const
return the usd stage associated with this context
MObjectHandle objectHandle() const
get the maya object of the node
Definition: TranslatorContext.h:293
PrimLookup(const SdfPath &path, const TfToken &type, MObject mayaObj)
ctor
Definition: TranslatorContext.h:280
const nodes::ProxyShape * getProxyShape() const
return the proxy shape associated with this context
Definition: TranslatorContext.h:92
TfToken type() const
get the prim type
Definition: TranslatorContext.h:303
bool getTransform(const UsdPrim &prim, MObjectHandle &object)
given a USD prim, this will see whether a maya node exists for it. If it does, that will be returned ...
Definition: TranslatorContext.h:105
bool getMObject(const UsdPrim &prim, MObjectHandle &object, MTypeId type)
given a USD prim, this will see whether a maya node exists for it. If it does, that will be returned ...
Definition: TranslatorContext.h:125
AL_USDMAYA_PUBLIC MString serialise() const
serialises the content of the translator context to a text string.
const SdfPath & path() const
get the prim path of this reference
Definition: TranslatorContext.h:288
MObjectHandleArray & createdNodes()
get created maya nodes
Definition: TranslatorContext.h:308
static RefPtr create(nodes::ProxyShape *proxyShape)
construct a new context for the specified proxy shape node
Definition: TranslatorContext.h:83
AL_USDMAYA_PUBLIC void removeEntries(const SdfPathVector &itemsToRemove)
call this to remove a prim from the DB (you do not need to lock/unlock here).
bool getMObject(const UsdPrim &prim, MObjectHandle &object, MFn::Type type)
given a USD prim, this will see whether a maya node exists for it. If it does, that will be returned ...
Definition: TranslatorContext.h:149
~PrimLookup()
dtor
Definition: TranslatorContext.h:284
void clearPrimMappings()
This is used for testing only. Do not call.
Definition: TranslatorContext.h:353
TfToken getTypeForPath(SdfPath path) const
given a path to a prim, return the prim type we are aware of at that path
Definition: TranslatorContext.h:204
bool isExcludedGeometryDirty()
Retrieves if the the excluded geometry has been pushed to the renderer.
Definition: TranslatorContext.h:398
bool operator()(const PrimLookup &a, const SdfPath &b) const
compare schema node ref to path
Definition: TranslatorContext.h:333
bool forceTranslatorImport() const
Retrieves the flag that determines if all the Prim schema types should be imported.
Definition: TranslatorContext.h:61