All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
referenceAssembly.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXRUSDMAYA_REFERENCE_ASSEMBLY_H
25 #define PXRUSDMAYA_REFERENCE_ASSEMBLY_H
26 
28 
29 #include "usdMaya/api.h"
30 #include "usdMaya/proxyShape.h"
31 #include "usdMaya/usdPrimProvider.h"
32 
33 #include "pxr/pxr.h"
34 
35 #include "pxr/base/tf/envSetting.h"
36 #include "pxr/base/tf/staticTokens.h"
37 
38 #include "pxr/usd/sdf/layer.h"
39 #include "pxr/usd/usd/stage.h"
40 
41 #include <maya/MDagModifier.h>
42 #include <maya/MDGModifier.h>
43 #include <maya/MPxAssembly.h>
44 #include <maya/MPxRepresentation.h>
45 #include <maya/MString.h>
46 #include <maya/MTypeId.h>
47 
48 #include <map>
49 #include <string>
50 #include <vector>
51 
52 
53 PXR_NAMESPACE_OPEN_SCOPE
54 
55 
56 #define PXRUSDMAYA_REFERENCE_ASSEMBLY_TOKENS \
57  ((MayaTypeName, "pxrUsdReferenceAssembly"))
58 
59 TF_DECLARE_PUBLIC_TOKENS(UsdMayaReferenceAssemblyTokens,
60  PXRUSDMAYA_API,
61  PXRUSDMAYA_REFERENCE_ASSEMBLY_TOKENS);
62 
63 
64 #define PXRUSDMAYA_VARIANT_SET_TOKENS \
65  ((PlugNamePrefix, "usdVariantSet_"))
66 
67 TF_DECLARE_PUBLIC_TOKENS(UsdMayaVariantSetTokens,
68  PXRUSDMAYA_API,
69  PXRUSDMAYA_VARIANT_SET_TOKENS);
70 
71 
73 PXRUSDMAYA_API
75 
76 
77 class UsdMayaReferenceAssembly : public MPxAssembly,
78  public UsdMayaUsdPrimProvider
79 {
80 public:
81  PXRUSDMAYA_API
82  static const MTypeId typeId;
83  PXRUSDMAYA_API
84  static const MString typeName;
85  PXRUSDMAYA_API
86  static const MString _classification;
87 
88  // Attributes
89  PXRUSDMAYA_API
90  static MObject filePathAttr;
91  PXRUSDMAYA_API
92  static MObject primPathAttr;
93  PXRUSDMAYA_API
94  static MObject excludePrimPathsAttr;
95  PXRUSDMAYA_API
96  static MObject timeAttr;
97  PXRUSDMAYA_API
98  static MObject complexityAttr;
99  PXRUSDMAYA_API
100  static MObject kindAttr;
101  PXRUSDMAYA_API
102  static MObject initialRepAttr;
103  PXRUSDMAYA_API
104  static MObject repNamespaceAttr;
105  PXRUSDMAYA_API
106  static MObject drawModeAttr;
107  PXRUSDMAYA_API
108  static MObject inStageDataAttr;
109  PXRUSDMAYA_API
110  static MObject inStageDataCachedAttr;
111  PXRUSDMAYA_API
112  static MObject outStageDataAttr;
113  PXRUSDMAYA_API
114  static std::vector<MObject> attrsAffectingRepresentation;
115 
116  // Static Member Functions ==
117  PXRUSDMAYA_API
118  static void* creator();
119  PXRUSDMAYA_API
120  static MStatus initialize();
121 
122  // == Base Class Virtuals ==
123  PXRUSDMAYA_API
124  MStatus compute(const MPlug& plug, MDataBlock& dataBlock) override;
125 
126  PXRUSDMAYA_API
127  bool setInternalValueInContext(
128  const MPlug& plug,
129  const MDataHandle& dataHandle,
130  MDGContext& ctx) override;
131 
132  // Required overrides
133  PXRUSDMAYA_API
134  MString createRepresentation(
135  const MString& input,
136  const MString& type,
137  const MString& representation,
138  MDagModifier* undoRedo = nullptr,
139  MStatus* ReturnStatus = nullptr) override;
140 
141  PXRUSDMAYA_API
142  MString getActive() const override;
143  PXRUSDMAYA_API
144  MStringArray getRepresentations(
145  MStatus* ReturnStatus = nullptr) const override;
146  PXRUSDMAYA_API
147  MString getRepType(const MString& representation) const override;
148  PXRUSDMAYA_API
149  MString getRepLabel(const MString& representation) const override;
150  PXRUSDMAYA_API
151  MStringArray repTypes() const override;
152  PXRUSDMAYA_API
153  MStatus deleteRepresentation(
154  const MString& representation) override;
155  PXRUSDMAYA_API
156  MStatus deleteAllRepresentations() override;
157  PXRUSDMAYA_API
158  MString setRepName(
159  const MString& representation,
160  const MString& newName,
161  MStatus* ReturnStatus = nullptr) override;
162  PXRUSDMAYA_API
163  MStatus setRepLabel(
164  const MString& representation,
165  const MString& label) override;
166  PXRUSDMAYA_API
167  bool activateRep(const MString& representation) override;
168 
169  // Optional overrides
170  PXRUSDMAYA_API
171  bool supportsEdits() const override { return true; };
172  PXRUSDMAYA_API
173  bool supportsMemberChanges() const override { return false; };
174  PXRUSDMAYA_API
175  bool canRepApplyEdits(const MString& rep) const override {
176  return (rep.length() > 0);
177  };
178 
179  PXRUSDMAYA_API
180  void postLoad() override;
181  PXRUSDMAYA_API
182  bool inactivateRep() override;
183  PXRUSDMAYA_API
184  MString getRepNamespace() const override;
185  PXRUSDMAYA_API
186  void updateRepNamespace(const MString& repNamespace) override;
187 
188  PXRUSDMAYA_API
189  MStatus setDependentsDirty(
190  const MPlug& plug,
191  MPlugArray& plugArray) override;
192 
193  PXRUSDMAYA_API
194  MStatus connectionMade(
195  const MPlug& plug,
196  const MPlug& otherPlug,
197  bool asSrc) override;
198 
199  PXRUSDMAYA_API
200  MStatus connectionBroken(
201  const MPlug& plug,
202  const MPlug& otherPlug,
203  bool asSrc) override;
204 
205  // UsdMayaUsdPrimProvider overrides:
206  PXRUSDMAYA_API
207  UsdPrim usdPrim() const override;
208 
209  // Additional public functions
210  bool HasEdits() const { return _hasEdits; }
211  void SetHasEdits(bool val) { _hasEdits = val; }
212 
218  PXRUSDMAYA_API
219  std::map<std::string, std::string> GetVariantSetSelections() const;
220 
226  PXRUSDMAYA_API
227  void ConnectMayaTimeToAssemblyTime();
228 
235  PXRUSDMAYA_API
236  void DisconnectAssemblyTimeFromMayaTime();
237 
238  private:
239 
240  friend class UsdMayaRepresentationBase;
241 
242  UsdMayaReferenceAssembly();
243  ~UsdMayaReferenceAssembly() override;
244 
245  // Private Class functions
246  MStatus computeInStageDataCached(MDataBlock& dataBlock);
247  MStatus computeOutStageData(MDataBlock& dataBlock);
248 
249  // After discussion with Autodesk, we've decided to adopt the namespace
250  // handling functionality from their sample assembly reference
251  // implementation here:
252  //
253  // http://help.autodesk.com/view/MAYAUL/2017/ENU/?guid=__cpp_ref_scene_assembly_2assembly_reference_8h_example_html
254  //
255  // This should really be implemented internally as built-in functionality
256  // of Maya assemblies rather than having to deal with it in plugin code,
257  // but there are currently no plans to make that happen, so we're forced to
258  // do it ourselves. This helps ensure that assembly edits do not fall off
259  // when assembly nodes are renamed/duplicated/etc.
260 
267  MString getDefaultRepNamespace() const;
268 
269  // This variable is used to tell if we're in the process of updating the
270  // repNamespace. It helps distinguish between cases when the namespace
271  // change was initiated by Maya or via the namespace editor (in which case
272  // _updatingRepNamespace == true) versus when the repNamespace attribute
273  // was edited directly (in which case _updatingRepNamespace == false).
274  bool _updatingRepNamespace;
275 
276  std::map<std::string, std::shared_ptr<MPxRepresentation> > _representations;
277  bool _activateRepOnFileLoad;
278  std::shared_ptr<MPxRepresentation> _activeRep;
279  bool _inSetInternalValue;
280  bool _hasEdits;
281 };
282 
283 
284 
285 // ===========================================================
286 //
287 // Base Class for UsdMayaRepresentations
288 //
289 class UsdMayaRepresentationBase : public MPxRepresentation
290 {
291  public:
292  // == Overrides for MPxRepresentation ==
293  PXRUSDMAYA_API
294  UsdMayaRepresentationBase(MPxAssembly *assembly, const MString &name);
295  PXRUSDMAYA_API
296  ~UsdMayaRepresentationBase() override {};
297 
298  bool activate() override = 0;
299  PXRUSDMAYA_API
300  bool inactivate() override;
301 
302  // == Required Virtual Overrides
303  MString getType() const override = 0;
304 
305  // == Optional Virtual Overrides
306  //virtual bool canApplyEdits() const;
307 
308  // == New functions for UsdMayaRepresentationBase ==
309  // Expose protected function getAssembly() as public
310  MPxAssembly* GetAssembly() { return getAssembly(); };
311 };
312 
313 class UsdMayaRepresentationProxyBase : public UsdMayaRepresentationBase
314 {
315  public:
316  UsdMayaRepresentationProxyBase(
317  MPxAssembly *assembly,
318  const MString &name,
319  bool proxyIsSoftSelectable) :
320  UsdMayaRepresentationBase(assembly, name),
321  _proxyIsSoftSelectable(proxyIsSoftSelectable) {};
322 
323  PXRUSDMAYA_API
324  bool activate() override;
325  PXRUSDMAYA_API
326  bool inactivate() override;
327 
328  protected:
329  PXRUSDMAYA_API
330  virtual void _OverrideProxyPlugs(
331  MFnDependencyNode &shapeFn,
332  MDGModifier &dgMod);
333 
334  private:
335  void _PushEditsToProxy();
336 
337  private:
338  SdfLayerRefPtr _sessionSublayer;
339  bool _proxyIsSoftSelectable;
340 };
341 
342 // ===========================================================
343 //
344 // Render a collapsed USD File
345 // Draw the subgraph using a single UsdMayaProxyShape.
346 //
347 class UsdMayaRepresentationCollapsed : public UsdMayaRepresentationProxyBase
348 {
349  public:
350  // == Statics
351  PXRUSDMAYA_API
352  static const MString _assemblyType;
353 
354  // == Overrides for MPxRepresentation ==
355  PXRUSDMAYA_API
356  UsdMayaRepresentationCollapsed(MPxAssembly *assembly, const MString &name) :
357 
358  // We only support soft selection on "collapsed" proxies. While we may
359  // want to move proxies that are not root of the model, we suspect this
360  // is more likely to lead to undesired behavior.
361  UsdMayaRepresentationProxyBase(assembly, name, true) {};
362 
363  MString getType() const override {
364  return UsdMayaRepresentationCollapsed::_assemblyType;
365  };
366 
367  protected:
368  PXRUSDMAYA_API
369  void _OverrideProxyPlugs(MFnDependencyNode &shapeFn,
370  MDGModifier &dgMod) override;
371 };
372 
373 // ===========================================================
374 //
375 // Render a USD model as a single set of collapsed cards.
376 // Draw the subgraph using a single UsdMayaProxyShape.
377 //
378 class UsdMayaRepresentationCards : public UsdMayaRepresentationProxyBase
379 {
380  public:
381  // == Statics
382  PXRUSDMAYA_API
383  static const MString _assemblyType;
384 
385  // == Overrides for MPxRepresentation ==
386  PXRUSDMAYA_API
387  UsdMayaRepresentationCards(MPxAssembly *assembly, const MString &name) :
388 
389  // We only support soft selection on "collapsed" proxies. While we may
390  // want to move proxies that are not root of the model, we suspect this
391  // is more likely to lead to undesired behavior.
392  UsdMayaRepresentationProxyBase(assembly, name, true) {};
393 
394  MString getType() const override {
395  return UsdMayaRepresentationCards::_assemblyType;
396  };
397 
398  PXRUSDMAYA_API
399  bool activate() override;
400  PXRUSDMAYA_API
401  bool inactivate() override;
402 
403  protected:
404  PXRUSDMAYA_API
405  void _OverrideProxyPlugs(MFnDependencyNode &shapeFn,
406  MDGModifier &dgMod) override;
407 };
408 
409 // ===========================================================
410 //
411 // Render a collapsed USD File
412 // Draw the subgraph using a single UsdMayaProxyShape.
413 //
414 class UsdMayaRepresentationPlayback : public UsdMayaRepresentationProxyBase
415 {
416  public:
417  // == Statics
418  PXRUSDMAYA_API
419  static const MString _assemblyType;
420 
421  // == Overrides for MPxRepresentation ==
422  UsdMayaRepresentationPlayback(MPxAssembly *assembly, const MString &name) :
423  UsdMayaRepresentationProxyBase(assembly, name, false) {};
424 
425  MString getType() const override {
426  return UsdMayaRepresentationPlayback::_assemblyType;
427  };
428 
429  PXRUSDMAYA_API
430  bool activate() override;
431  PXRUSDMAYA_API
432  bool inactivate() override;
433 
434  protected:
435  PXRUSDMAYA_API
436  void _OverrideProxyPlugs(MFnDependencyNode &shapeFn,
437  MDGModifier &dgMod) override;
438 };
439 
440 // Base class for representations that unroll a hierarchy.
441 
442 class UsdMayaRepresentationHierBase : public UsdMayaRepresentationBase
443 {
444  public:
445  // == Overrides for MPxRepresentation ==
446  UsdMayaRepresentationHierBase(MPxAssembly *assembly, const MString &name) :
447  UsdMayaRepresentationBase(assembly, name) {};
448 
449  PXRUSDMAYA_API
450  bool activate() override;
451 
452  protected:
453  PXRUSDMAYA_API
454  void _ConnectSubAssemblyPlugs();
455  PXRUSDMAYA_API
456  void _ConnectProxyPlugs();
457 
458  virtual bool _ShouldImportWithProxies() const { return false; };
459 };
460 
461 // ===========================================================
462 //
463 // Expand a USD hierarchy into sub-assemblies
464 // Imports xforms as maya groups and other prims as usdPrimShapes.
465 // Children that are models, model groups, and sets will be imported as UsdAssemblies
466 //
467 class UsdMayaRepresentationExpanded : public UsdMayaRepresentationHierBase
468 {
469  public:
470  // == Statics
471  PXRUSDMAYA_API
472  static const MString _assemblyType;
473 
474  // == Overrides for MPxRepresentation ==
475  UsdMayaRepresentationExpanded(MPxAssembly *assembly, const MString &name) :
476  UsdMayaRepresentationHierBase(assembly, name) {};
477 
478  MString getType() const override {
479  return UsdMayaRepresentationExpanded::_assemblyType;
480  };
481 
482  protected:
483  bool _ShouldImportWithProxies() const override { return true; };
484 };
485 
486 // ===========================================================
487 //
488 //
489 // Imports the USD subgraph (via usdImport command) as full maya geometry.
490 //
491 class UsdMayaRepresentationFull : public UsdMayaRepresentationHierBase
492 {
493  public:
494  // == Statics
495  PXRUSDMAYA_API
496  static const MString _assemblyType;
497 
498  // == Overrides for MPxRepresentation ==
499  UsdMayaRepresentationFull(MPxAssembly *assembly, const MString &name) :
500  UsdMayaRepresentationHierBase(assembly, name) {};
501 
502  MString getType() const override {
503  return UsdMayaRepresentationFull::_assemblyType;
504  };
505 };
506 
507 
508 PXR_NAMESPACE_CLOSE_SCOPE
509 
510 
511 #endif
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:118
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a &quot;Prim&quot; as ...
Definition: prim.h:131
PXRUSDMAYA_API bool UsdMayaUseUsdAssemblyNamespace()
Returns the PIXMAYA_USE_USD_ASSEM_NAMESPACE env setting.