AL_USDMaya  0.16.6
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ProxyShape.h
1 #pragma once
2 #include "AL/usdmaya/Common.h"
3 #include "AL/maya/NodeHelper.h"
4 #include "AL/usdmaya/fileio/translators/TranslatorBase.h"
5 #include "AL/usdmaya/fileio/translators/TranslatorContext.h"
6 #include "AL/usdmaya/fileio/translators/TransformTranslator.h"
7 #include "AL/usdmaya/nodes/VariantDB.h"
8 
9 #include "maya/MPxSurfaceShape.h"
10 #include "maya/MEventMessage.h"
11 #include "maya/MNodeMessage.h"
12 #include "maya/MPxDrawOverride.h"
13 #include "pxr/usd/usd/prim.h"
14 #include "pxr/usd/usd/timeCode.h"
15 #include "pxr/usd/sdf/path.h"
16 #include "pxr/base/tf/weakBase.h"
17 #include "pxr/usd/usd/notice.h"
18 #include "pxr/usd/sdf/notice.h"
19 #include <stack>
20 
21 class UsdImagingGLHdEngine;
22 
23 namespace AL {
24 namespace usdmaya {
25 namespace nodes {
26 
27 //----------------------------------------------------------------------------------------------------------------------
32 //----------------------------------------------------------------------------------------------------------------------
34  : public MPxSurfaceShape,
35  public maya::NodeHelper,
36  public TfWeakBase
37 {
38 public:
39 
41  ProxyShape();
42 
44  ~ProxyShape();
45 
46  //--------------------------------------------------------------------------------------------------------------------
48  //--------------------------------------------------------------------------------------------------------------------
49  AL_MAYA_DECLARE_NODE();
50 
51  //--------------------------------------------------------------------------------------------------------------------
53  //--------------------------------------------------------------------------------------------------------------------
54 
58  Layer* findLayer(SdfLayerHandle handle);
59 
63  MString findLayerMayaName(SdfLayerHandle handle);
64 
67  Layer* getLayer();
68 
69  //--------------------------------------------------------------------------------------------------------------------
71  //--------------------------------------------------------------------------------------------------------------------
72 
74  AL_DECL_ATTRIBUTE(filePath);
75 
77  AL_DECL_ATTRIBUTE(primPath);
78 
80  AL_DECL_ATTRIBUTE(excludePrimPaths);
81 
83  AL_DECL_ATTRIBUTE(time);
84 
86  AL_DECL_ATTRIBUTE(timeOffset);
87 
90  AL_DECL_ATTRIBUTE(timeScalar);
91 
93  AL_DECL_ATTRIBUTE(complexity);
94 
96  AL_DECL_ATTRIBUTE(displayGuides);
97 
99  AL_DECL_ATTRIBUTE(displayRenderGuides);
100 
102  AL_DECL_ATTRIBUTE(layers);
103 
105  // TODO reset if the usd file path is updated via the ui
106  AL_DECL_ATTRIBUTE(serializedSessionLayer);
107 
108 
110  // @note currently not used
111  AL_DECL_ATTRIBUTE(serializedArCtx);
112 
114  AL_DECL_ATTRIBUTE(serializedTrCtx);
115 
117  AL_DECL_ATTRIBUTE(unloaded);
118 
120  AL_DECL_ATTRIBUTE(drivenPrimPaths);
121 
123  AL_DECL_ATTRIBUTE(drivenVisibility);
124 
126  AL_DECL_ATTRIBUTE(drivenTranslate);
127 
129  AL_DECL_ATTRIBUTE(drivenScale);
130 
132  AL_DECL_ATTRIBUTE(drivenRotate);
133 
135  AL_DECL_ATTRIBUTE(drivenRotateOrder);
136 
138  AL_DECL_ATTRIBUTE(ambient);
139 
141  AL_DECL_ATTRIBUTE(diffuse);
142 
144  AL_DECL_ATTRIBUTE(specular);
145 
147  AL_DECL_ATTRIBUTE(emission);
148 
150  AL_DECL_ATTRIBUTE(shininess);
151 
152  //--------------------------------------------------------------------------------------------------------------------
154  //--------------------------------------------------------------------------------------------------------------------
155 
157  AL_DECL_ATTRIBUTE(outTime);
158 
160  AL_DECL_ATTRIBUTE(outStageData);
161 
162  //--------------------------------------------------------------------------------------------------------------------
164  //--------------------------------------------------------------------------------------------------------------------
165 
168  UsdStageRefPtr getUsdStage() const;
169 
176  bool getRenderAttris(void* attribs, const MHWRender::MFrameContext& frameContext, const MDagPath& dagPath);
177 
179  MBoundingBox boundingBox() const override;
180 
181  //--------------------------------------------------------------------------------------------------------------------
188  //--------------------------------------------------------------------------------------------------------------------
189 
192  {
193  kSelection = 1 << 0,
194  kRequested = 1 << 1,
195  kRequired = 1 << 2
196  };
197 
201  inline bool isRequiredPath(const SdfPath& path) const
202  { return m_requiredPaths.find(path) != m_requiredPaths.end(); }
203 
207  inline MObject findRequiredPath(const SdfPath& path) const
208  {
209  const auto it = m_requiredPaths.find(path);
210  if(it != m_requiredPaths.end())
211  {
212  return it->second.m_node;
213  }
214  return MObject::kNullObj;
215  }
216 
222  std::vector<UsdPrim> huntForNativeNodesUnderPrim(
223  const MDagPath& proxyTransformPath,
224  SdfPath startPath);
225 
235  MObject makeUsdTransformChain(
236  const UsdPrim& usdPrim,
237  MDagModifier& modifier,
238  TransformReason reason,
239  MDGModifier* modifier2 = 0,
240  uint32_t* createCount = 0);
241 
248  void makeUsdTransforms(
249  const UsdPrim& usdPrim,
250  MDagModifier& modifier,
251  TransformReason reason,
252  MDGModifier* modifier2 = 0);
253 
260  const UsdPrim& usdPrim,
261  MDagModifier& modifier,
262  TransformReason reason);
263 
269  void removeUsdTransforms(
270  const UsdPrim& usdPrim,
271  MDagModifier& modifier,
272  TransformReason reason);
273 
276  void printRefCounts() const;
277 
280 
283 
286  { m_requiredPaths.clear(); }
287 
291  void unloadMayaReferences();
292 
296  { return m_schemaNodeDB; }
297 
300 
303 
306 
309  inline UsdImagingGLHdEngine* engine() const
310  { return m_engine; }
311 
314  SdfPathVector& selectedPaths()
315  { return m_selectedPaths; }
316 
318  void findExcludedGeometry();
319 
325  UsdPrim getRootPrim()
326  {
327  if(m_stage)
328  {
329  if(!m_path.IsEmpty())
330  {
331  UsdPrim prim = m_stage->GetPrimAtPath(m_path);
332  if(prim)
333  return prim;
334  }
335  return m_stage->GetPseudoRoot();
336  }
337  return UsdPrim();
338  }
339 
340 private:
341 
342  void constructExcludedPrims();
343  bool getInternalValueInContext(const MPlug& plug, MDataHandle& dataHandle, MDGContext& ctx) override;
344  bool setInternalValueInContext(const MPlug& plug, const MDataHandle& dataHandle, MDGContext& ctx) override;
345 
346  MObject makeUsdTransformChain(
347  UsdPrim usdPrim,
348  const MPlug& outStage,
349  const MPlug& outTime,
350  const MObject& parentXForm,
351  MDagModifier& modifier,
352  TransformReason reason,
353  MDGModifier* modifier2,
354  uint32_t* createCount);
355 
356  void makeUsdTransformsInternal(
357  const UsdPrim& usdPrim,
358  const MObject& parentXForm,
359  MDagModifier& modifier,
360  TransformReason reason,
361  MDGModifier* modifier2);
362 
363  void removeUsdTransformsInternal(
364  const UsdPrim& usdPrim,
365  MDagModifier& modifier,
366  TransformReason reason);
367 
368  struct TransformReference
369  {
370  TransformReference(const MObject& node, const TransformReason reason);
371  MObject m_node;
372  Transform* m_transform;
373 
374  // ref counting values
375  struct
376  {
377  uint32_t m_required:1;
378  uint32_t m_selected:1;
379  uint32_t m_refCount:30;
380  };
381 
382  bool decRef(const TransformReason reason);
383  void incRef(const TransformReason reason);
384  };
385 
390  typedef std::map<SdfPath, TransformReference> TransformReferenceMap;
391  TransformReferenceMap m_requiredPaths;
392 
393  // insert a new path into the requiredPaths map
394  void makeTransformReference(const SdfPath& path, const MObject& node, TransformReason reason);
395 
396  //--------------------------------------------------------------------------------------------------------------------
398  //--------------------------------------------------------------------------------------------------------------------
399 
400  void postConstructor() override;
401  MStatus compute(const MPlug& plug, MDataBlock& dataBlock) override;
402  MStatus setDependentsDirty(const MPlug& plugBeingDirtied, MPlugArray& plugs) override;
403  bool isBounded() const override;
404 
405  //--------------------------------------------------------------------------------------------------------------------
407  //--------------------------------------------------------------------------------------------------------------------
408 
409  // split out compute methods.
410  MStatus computeInStageDataCached(const MPlug& plug, MDataBlock& dataBlock);
411  MStatus computeOutStageData(const MPlug& plug, MDataBlock& dataBlock);
412  MStatus computeOutputTime(const MPlug& plug, MDataBlock& dataBlock);
413 
414  //--------------------------------------------------------------------------------------------------------------------
416  //--------------------------------------------------------------------------------------------------------------------
417 
418  UsdPrim getUsdPrim(MDataBlock& dataBlock) const;
419  SdfPathVector getExcludePrimPaths() const;
420  bool isStageValid() const;
421  bool primHasExcludedParent(UsdPrim prim);
422  bool initPrim(const uint32_t index, MDGContext& ctx);
423 
424  void reloadStage(MPlug& plug);
425  void onObjectsChanged(UsdNotice::ObjectsChanged const&, UsdStageWeakPtr const& sender);
426  void variantSelectionListener(SdfNotice::LayersDidChange const& notice, UsdStageWeakPtr const& sender);
427  void onEditTargetChanged(UsdNotice::StageEditTargetChanged const& notice, UsdStageWeakPtr const& sender);
428  static void onSelectionChanged(void* ptr);
429  static void onAttributeChanged(MNodeMessage::AttributeMessage, MPlug&, MPlug&, void*);
430  void validateTransforms();
431 
432 private:
433  SdfPathVector m_selectedPaths;
434  std::vector<SdfPath> m_paths;
435  std::vector<UsdPrim> m_prims;
436  TfNotice::Key m_objectsChangedNoticeKey;
437  TfNotice::Key m_variantChangedNoticeKey;
438  TfNotice::Key m_editTargetChanged;
439  mutable std::map<UsdTimeCode, MBoundingBox> m_boundingBoxCache;
440  MCallbackId m_onSelectChanged;
441  MCallbackId m_beforeSaveSceneId;
442  MCallbackId m_attributeChanged;
443  SdfPathVector m_excludedGeometry;
444  SdfPathVector m_excludedTaggedGeometry;
445  UsdStageRefPtr m_stage;
446  SdfPath m_path;
447  SchemaNodeRefDB m_schemaNodeDB;
448  SdfPath m_variantChangePath;
449  SdfPathVector m_variantSwitchedPrims;
450  UsdImagingGLHdEngine* m_engine = 0;
451  uint32_t m_engineRefCount = 0;
452  bool m_variantHasChanged = false;
453 };
454 
455 //----------------------------------------------------------------------------------------------------------------------
456 } // nodes
457 } // usdmaya
458 } // AL
459 //----------------------------------------------------------------------------------------------------------------------
the node exists for selection
Definition: ProxyShape.h:193
the node has been requested by a user
Definition: ProxyShape.h:194
void createSelectionChangedCallback()
create a callback to destroy unused transform nodes when the selection changes.
void unloadMayaReferences()
unloads all maya references
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:33
void destroySelectionChangedCallback()
destroys the selection callback
void findExcludedGeometry()
searches for the excluded geometry
UsdStageRefPtr getUsdStage() const
provides access to the UsdStage that this proxy shape is currently representing
UsdPrim getRootPrim()
if a root prim has been specified by the user in the proxy shape AE, then this method will return the...
Definition: ProxyShape.h:325
void makeUsdTransforms(const UsdPrim &usdPrim, MDagModifier &modifier, TransformReason reason, MDGModifier *modifier2=0)
Will construct AL_usdmaya_Transform nodes for all of the prims from the specified usdPrim and down...
void printRefCounts() const
Debugging util - prints out the reference counts for each AL_usdmaya_Transform that currently exists ...
void deserialiseTranslatorContext()
deserialises the translator context
nodes::SchemaNodeRefDB & schemaDB()
return a reference to the DB of schema prims imported via custom translators (used for variant switch...
Definition: ProxyShape.h:295
AL_DECL_ATTRIBUTE(filePath)
the input USD file path for this proxy
bool isRequiredPath(const SdfPath &path) const
returns true if the path is required for an imported schema prim
Definition: ProxyShape.h:201
Layer * getLayer()
return the node that represents the root layer
The proxy shape node needs to store a mapping of all the schema nodes it has brought into the Maya sc...
Definition: VariantDB.h:53
void destroyTransformReferences()
destroys all internal transform references
Definition: ProxyShape.h:285
Layer * findLayer(SdfLayerHandle handle)
Locate the maya node associated with the specified layer.
The AL::usdmaya::nodes::Transform node is a custom transform node that allows you to manipulate a USD...
Definition: Transform.h:62
MObject findRequiredPath(const SdfPath &path) const
returns the MObject of the maya transform for requested path (or MObject::kNullObj) ...
Definition: ProxyShape.h:207
UsdImagingGLHdEngine * engine() const
returns the usd imaging engine for this proxy shape
Definition: ProxyShape.h:309
std::vector< UsdPrim > huntForNativeNodesUnderPrim(const MDagPath &proxyTransformPath, SdfPath startPath)
traverses the UsdStage looking for the prims that are going to be handled by custom transformer plug-...
MObject makeUsdTransformChain(const UsdPrim &usdPrim, MDagModifier &modifier, TransformReason reason, MDGModifier *modifier2=0, uint32_t *createCount=0)
constructs a single chain of transform nodes from the usdPrim to the root of this proxy shape...
void constructGLImagingEngine()
constructs the USD imaging engine for this shape
SdfPathVector & selectedPaths()
returns the paths of the selected items within the proxy shape
Definition: ProxyShape.h:314
TransformReason
an enum describing the reason that a transform exists in the scene
Definition: ProxyShape.h:191
void removeUsdTransformChain(const UsdPrim &usdPrim, MDagModifier &modifier, TransformReason reason)
will destroy all of the AL_usdmaya_Transform nodes from the prim specified, up to the root (unless an...
the node is required for an imported schema prim
Definition: ProxyShape.h:195
The layer node stores a reference to an SdfLayer.
Definition: Layer.h:20
bool getRenderAttris(void *attribs, const MHWRender::MFrameContext &frameContext, const MDagPath &dagPath)
gets hold of the attributes on this node that control the rendering in some way
MString findLayerMayaName(SdfLayerHandle handle)
Locate the name of the maya node associated with the specified layer.
MBoundingBox boundingBox() const override
compute bounds
void serialiseTranslatorContext()
serialises the translator context
void removeUsdTransforms(const UsdPrim &usdPrim, MDagModifier &modifier, TransformReason reason)
Will destroy all AL_usdmaya_Transform nodes found underneath the prim (unless those nodes are require...
This is a little helper object designed to reduce the amount of boilerplate GUI code you need to jump...
Definition: NodeHelper.h:175