All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
adaptor.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 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_ADAPTOR_H
25 #define PXRUSDMAYA_ADAPTOR_H
26 
28 
29 #include "usdMaya/api.h"
30 
31 #include "pxr/pxr.h"
32 
33 #include "pxr/base/tf/registryManager.h"
34 #include "pxr/base/vt/value.h"
35 
36 #include "pxr/usd/sdf/attributeSpec.h"
37 #include "pxr/usd/sdf/primSpec.h"
38 #include "pxr/usd/usd/common.h"
39 #include "pxr/usd/usd/schemaRegistry.h"
40 
41 #include <maya/MDGModifier.h>
42 #include <maya/MObjectHandle.h>
43 #include <maya/MPlug.h>
44 
45 PXR_NAMESPACE_OPEN_SCOPE
46 
137 public:
153  MPlug _plug;
154  MObjectHandle _node;
155  MObjectHandle _attr;
156  SdfAttributeSpecHandle _attrDef;
157 
158  public:
159  PXRUSDMAYA_API
161 
162  PXRUSDMAYA_API
163  AttributeAdaptor(const MPlug& plug, SdfAttributeSpecHandle attrDef);
164 
165  PXRUSDMAYA_API
166  explicit operator bool() const;
167 
169  PXRUSDMAYA_API
171 
174  PXRUSDMAYA_API
175  TfToken GetName() const;
176 
184  template <typename T>
185  bool Get(T* value) const {
186  VtValue v;
187  if (Get(&v) && v.IsHolding<T>()) {
188  *value = v.Get<T>();
189  return true;
190  }
191  return false;
192  }
193 
200  PXRUSDMAYA_API
201  bool Get(VtValue* value) const;
202 
207  PXRUSDMAYA_API
208  bool Set(const VtValue& newValue);
209 
216  PXRUSDMAYA_API
217  bool Set(const VtValue& newValue, MDGModifier& modifier);
218 
221  PXRUSDMAYA_API
222  const SdfAttributeSpecHandle GetAttributeDefinition() const;
223  };
224 
231  MObjectHandle _handle;
232  SdfPrimSpecHandle _schemaDef;
233 
234  public:
235  PXRUSDMAYA_API
236  SchemaAdaptor();
237 
238  PXRUSDMAYA_API
239  SchemaAdaptor(const MObjectHandle& object, SdfPrimSpecHandle schemaDef);
240 
241  PXRUSDMAYA_API
242  explicit operator bool() const;
243 
245  PXRUSDMAYA_API
247 
250  PXRUSDMAYA_API
251  TfToken GetName() const;
252 
264  PXRUSDMAYA_API
265  AttributeAdaptor GetAttribute(const TfToken& attrName) const;
266 
274  PXRUSDMAYA_API
275  AttributeAdaptor CreateAttribute(const TfToken& attrName);
276 
286  PXRUSDMAYA_API
288  const TfToken& attrName,
289  MDGModifier& modifier);
290 
294  PXRUSDMAYA_API
295  void RemoveAttribute(const TfToken& attrName);
296 
302  PXRUSDMAYA_API
303  void RemoveAttribute(const TfToken& attrName, MDGModifier& modifier);
304 
308  PXRUSDMAYA_API
310 
314  PXRUSDMAYA_API
316 
319  PXRUSDMAYA_API
320  const SdfPrimSpecHandle GetSchemaDefinition() const;
321 
322  private:
326  std::string _GetMayaAttrNameOrAlias(
327  const SdfAttributeSpecHandle& attrSpec) const;
328  };
329 
330  PXRUSDMAYA_API
331  UsdMayaAdaptor(const MObject& obj);
332 
333  PXRUSDMAYA_API
334  explicit operator bool() const;
335 
338  PXRUSDMAYA_API
339  std::string GetMayaNodeName() const;
340 
343  PXRUSDMAYA_API
344  TfToken GetUsdTypeName() const;
345 
349  PXRUSDMAYA_API
350  TfType GetUsdType() const;
351 
356  PXRUSDMAYA_API
358 
371  PXRUSDMAYA_API
372  SchemaAdaptor GetSchema(const TfType& ty) const;
373 
386  PXRUSDMAYA_API
387  SchemaAdaptor GetSchemaByName(const TfToken& schemaName) const;
388 
389  template <typename T>
390  SchemaAdaptor GetSchemaOrInheritedSchema() const {
391  return GetSchemaOrInheritedSchema(TfType::Find<T>());
392  }
393 
412  PXRUSDMAYA_API
413  SchemaAdaptor GetSchemaOrInheritedSchema(const TfType& ty) const;
414 
422  PXRUSDMAYA_API
423  SchemaAdaptor ApplySchema(const TfType& ty);
424 
434  PXRUSDMAYA_API
435  SchemaAdaptor ApplySchema(const TfType& ty, MDGModifier& modifier);
436 
444  PXRUSDMAYA_API
445  SchemaAdaptor ApplySchemaByName(const TfToken& schemaName);
446 
455  PXRUSDMAYA_API
456  SchemaAdaptor ApplySchemaByName(
457  const TfToken& schemaName,
458  MDGModifier& modifier);
459 
462  PXRUSDMAYA_API
463  void UnapplySchema(const TfType& ty);
464 
469  PXRUSDMAYA_API
470  void UnapplySchema(const TfType& ty, MDGModifier& modifier);
471 
474  PXRUSDMAYA_API
475  void UnapplySchemaByName(const TfToken& schemaName);
476 
481  PXRUSDMAYA_API
482  void UnapplySchemaByName(const TfToken& schemaName, MDGModifier& modifier);
483 
488  PXRUSDMAYA_API
489  UsdMetadataValueMap GetAllAuthoredMetadata() const;
490 
498  PXRUSDMAYA_API
499  bool GetMetadata(const TfToken& key, VtValue* value) const;
500 
505  PXRUSDMAYA_API
506  bool SetMetadata(const TfToken& key, const VtValue& value);
507 
514  PXRUSDMAYA_API
515  bool SetMetadata(
516  const TfToken& key,
517  const VtValue& value,
518  MDGModifier& modifier);
519 
522  PXRUSDMAYA_API
523  void ClearMetadata(const TfToken& key);
524 
529  PXRUSDMAYA_API
530  void ClearMetadata(const TfToken& key, MDGModifier& modifier);
531 
533  PXRUSDMAYA_API
535 
537  PXRUSDMAYA_API
539 
541  PXRUSDMAYA_API
543 
548  PXRUSDMAYA_API
549  static void RegisterTypedSchemaConversion(
550  const std::string& nodeTypeName, const TfType& usdType);
551 
566  PXRUSDMAYA_API
567  static void RegisterAttributeAlias(
568  const TfToken& attributeName, const std::string& alias);
569 
574  PXRUSDMAYA_API
575  static std::vector<std::string> GetAttributeAliases(
576  const TfToken& attributeName);
577 
578 private:
579  MObjectHandle _handle;
580 
583  static std::map<std::string, TfType> _schemaLookup;
584 
586  static std::map<TfToken, std::vector<std::string>> _attributeAliases;
587 };
588 
605 #define PXRUSDMAYA_REGISTER_ADAPTOR_SCHEMA(mayaTypeName, schemaType)\
606 TF_REGISTRY_FUNCTION(UsdMayaAdaptor)\
607 {\
608  UsdMayaAdaptor::RegisterTypedSchemaConversion(\
609  #mayaTypeName, TfType::Find<schemaType>());\
610 }
611 
619 #define PXRUSDMAYA_REGISTER_ADAPTOR_ATTRIBUTE_ALIAS(attrName, alias)\
620 TF_REGISTRY_FUNCTION(UsdMayaAdaptor)\
621 {\
622  UsdMayaAdaptor::RegisterAttributeAlias(TfToken(attrName), alias);\
623 }
624 
625 PXR_NAMESPACE_CLOSE_SCOPE
626 
627 #endif
static PXRUSDMAYA_API void RegisterTypedSchemaConversion(const std::string &nodeTypeName, const TfType &usdType)
Registers the given Maya plugin type with a USD typed schema.
PXRUSDMAYA_API void UnapplySchemaByName(const TfToken &schemaName)
Removes the named API schema from the adaptor&#39;s apiSchemas metadata.
static PXRUSDMAYA_API TfToken::Set GetRegisteredTypedSchemas()
Gets the names of all known typed schemas.
The AttributeAdaptor stores a mapping between a USD schema attribute and a Maya plug, enabling conversions between the two.
Definition: adaptor.h:152
PXRUSDMAYA_API TfTokenVector GetAuthoredAttributeNames() const
Returns the names of only those schema attributes that are present on the Maya object, i.e., have been created via CreateAttribute().
PXRUSDMAYA_API TfTokenVector GetAppliedSchemas() const
Returns a vector containing the names of USD API schemas applied via adaptors on this Maya object...
PXRUSDMAYA_API AttributeAdaptor CreateAttribute(const TfToken &attrName)
Creates a Maya attribute corresponding to the given schema attribute and returns its adaptor...
The SchemaAdaptor is a wrapper around a Maya object associated with a particular USD schema...
Definition: adaptor.h:230
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:808
PXRUSDMAYA_API const SdfPrimSpecHandle GetSchemaDefinition() const
Gets the prim spec for this schema from the schema registry.
T const & Get() const
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:847
PXRUSDMAYA_API SchemaAdaptor ApplySchema(const TfType &ty)
Applies the given API schema type on this Maya object via the adaptor mechanism.
PXRUSDMAYA_API TfToken GetUsdTypeName() const
Gets the corresponding USD type name for this Maya node.
PXRUSDMAYA_API AttributeAdaptor GetAttribute(const TfToken &attrName) const
Gets the Maya attribute adaptor for the given schema attribute if it already exists.
PXRUSDMAYA_API SchemaAdaptor GetSchemaByName(const TfToken &schemaName) const
Returns a schema adaptor for this Maya object, bound to the named USD schema.
PXRUSDMAYA_API TfType GetUsdType() const
Gets the corresponding USD (Tf) type for this Maya node based on its Maya type and registered mapping...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
std::set< TfToken, TfTokenFastArbitraryLessThan > Set
Predefined type for set of tokens, for when faster lookup is desired, without paying the memory or in...
Definition: token.h:185
PXRUSDMAYA_API TfToken GetName() const
Gets the name of the attribute in the bound USD schema.
PXRUSDMAYA_API UsdMetadataValueMap GetAllAuthoredMetadata() const
Returns all metadata authored via the adaptor on this Maya object.
static PXRUSDMAYA_API TfTokenVector GetPrimMetadataFields()
Gets the names of all prim metadata fields registered in Sdf.
PXRUSDMAYA_API UsdMayaAdaptor GetNodeAdaptor() const
Gets the adaptor for the node that owns this attribute.
PXRUSDMAYA_API std::string GetMayaNodeName() const
Gets the full name of the underlying Maya node.
static PXRUSDMAYA_API std::vector< std::string > GetAttributeAliases(const TfToken &attributeName)
Gets the name of all possible Maya attribute names for the given USD schema attributeName, in the order in which the aliases were registered.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
PXRUSDMAYA_API bool Set(const VtValue &newValue)
Adapts the value to a Maya-compatible representation and sets it on the underlying Maya plug...
PXRUSDMAYA_API bool SetMetadata(const TfToken &key, const VtValue &value)
Sets the metadatum key&#39;s value to value on this Maya object, returning true on success.
static PXRUSDMAYA_API TfToken::Set GetRegisteredAPISchemas()
Gets the names of all known API schemas.
static PXRUSDMAYA_API void RegisterAttributeAlias(const TfToken &attributeName, const std::string &alias)
For backwards compatibility only: when upgrading any pre-existing code to use the adaptor mechanism...
bool Get(T *value) const
Gets the value of the underlying Maya plug and adapts it back into a the requested type...
Definition: adaptor.h:185
PXRUSDMAYA_API SchemaAdaptor ApplySchemaByName(const TfToken &schemaName)
Applies the named API schema on this Maya object via the adaptor mechanism.
PXRUSDMAYA_API SchemaAdaptor GetSchema(const TfType &ty) const
Returns a schema adaptor for this Maya object, bound to the given USD schema type.
PXRUSDMAYA_API void ClearMetadata(const TfToken &key)
Clears the authored key&#39;s value on this Maya object.
TfType represents a dynamic runtime type.
Definition: type.h:70
The UsdMayaAdaptor transparently adapts the interface for a Maya object to a UsdPrim-like interface...
Definition: adaptor.h:136
PXRUSDMAYA_API TfTokenVector GetAttributeNames() const
Returns the name of all schema attributes, including those that are unauthored on the Maya object...
PXRUSDMAYA_API void UnapplySchema(const TfType &ty)
Removes the given API schema from the adaptor&#39;s apiSchemas metadata.
PXRUSDMAYA_API TfToken GetName() const
Gets the name of the bound schema.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
PXRUSDMAYA_API bool GetMetadata(const TfToken &key, VtValue *value) const
Retrieves the requested metadatum if it has been authored on this Maya object, returning true on succ...
PXRUSDMAYA_API const SdfAttributeSpecHandle GetAttributeDefinition() const
Gets the defining spec for this attribute from the schema registry.
PXRUSDMAYA_API UsdMayaAdaptor GetNodeAdaptor() const
Gets the root adaptor for the underlying Maya node.
PXRUSDMAYA_API void RemoveAttribute(const TfToken &attrName)
Removes the named attribute adaptor from this Maya object.