AL_USDMaya  0.16.6
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
TranslatorBase.h
1 #pragma once
2 
3 #include "maya/MDagPath.h"
4 
5 #include "pxr/base/tf/refBase.h"
6 #include "pxr/base/tf/type.h"
7 #include "pxr/base/tf/weakBase.h"
8 #include "pxr/base/tf/registryManager.h"
9 #include "pxr/usd/usd/prim.h"
10 
11 #include <iostream>
12 #include <unordered_map>
13 #include "AL/usdmaya/fileio/translators/TranslatorContext.h"
14 
15 namespace AL {
16 namespace usdmaya {
17 namespace fileio {
18 namespace translators {
19 
20 //----------------------------------------------------------------------------------------------------------------------
52 //----------------------------------------------------------------------------------------------------------------------
54  : public TfRefBase, public TfWeakBase
55 {
56 public:
58  typedef TfRefPtr<This> RefPtr;
59  typedef TfWeakPtr<This> Ptr;
60 
62  virtual ~TranslatorAbstract() {}
63 
66  virtual TfType getTranslatedType() const = 0;
67 
73  virtual bool needsTransformParent() const
74  { return true; }
75 
80  virtual MStatus initialize()
81  { return MS::kSuccess; }
82 
89  virtual MStatus import(const UsdPrim& prim, MObject& parent)
90  { return MS::kSuccess; }
91 
96  virtual MStatus postImport(const UsdPrim& prim)
97  { return MS::kSuccess; }
98 
103  virtual MStatus preTearDown(UsdPrim& prim)
104  { return MS::kSuccess; }
105 
113  virtual MStatus tearDown(const SdfPath& path)
114  { return MStatus::kNotImplemented; }
115 
118  virtual bool supportsUpdate() const
119  { return false; }
120 
125  virtual MStatus update(const UsdPrim& prim)
126  { return MStatus::kNotImplemented; }
127 
128 };
129 
130 //----------------------------------------------------------------------------------------------------------------------
134 //----------------------------------------------------------------------------------------------------------------------
136  : public TranslatorAbstract
137 {
138 public:
139  typedef TranslatorBase This;
140  typedef TfRefPtr<This> RefPtr;
141 
143  virtual ~TranslatorBase()
144  {}
145 
147  virtual TfType getTranslatedType() const override
148  { return m_translatedType; }
149 
152  virtual TranslatorContextPtr context() const
153  { return m_context; }
154 
159  static RefPtr manufacture(const std::string& primType, TranslatorContextPtr context) = delete;
160 
163  UsdStageRefPtr getUsdStage() const
164  { return context()->getUsdStage(); }
165 
166 protected:
167 
171  virtual void setTranslatedType(const TfType& translatedType)
172  { m_translatedType = translatedType; }
173 
176  virtual void setContext(const TranslatorContextPtr context)
177  { m_context = context; }
178 
179 private:
180 
181  TfType m_translatedType;
182  TranslatorContextPtr m_context;
183 };
184 
185 typedef TfRefPtr<TranslatorBase> TranslatorRefPtr;
186 
187 //----------------------------------------------------------------------------------------------------------------------
190 //----------------------------------------------------------------------------------------------------------------------
192 {
193 public:
194  typedef TfRefPtr<TranslatorBase> RefPtr;
195 
200  RefPtr get(const TfToken type_name, TranslatorContextPtr context);
201 
202 private:
203  std::unordered_map<std::string, TranslatorRefPtr> m_translatorsMap;
204 };
205 
206 //----------------------------------------------------------------------------------------------------------------------
209 //----------------------------------------------------------------------------------------------------------------------
211  : public TfType::FactoryBase
212 {
213 public:
217  virtual TfRefPtr<TranslatorBase> create(TranslatorContextPtr ctx) const = 0;
218 };
219 
220 //----------------------------------------------------------------------------------------------------------------------
223 //----------------------------------------------------------------------------------------------------------------------
224 template <typename T>
226 {
227 public:
231  virtual TfRefPtr<TranslatorBase> create(TranslatorContextPtr ctx) const
232  { return T::create(ctx); }
233 };
234 
235 //----------------------------------------------------------------------------------------------------------------------
238 //----------------------------------------------------------------------------------------------------------------------
239 #define AL_USDMAYA_DECLARE_TRANSLATOR(PlugClass) \
240 typedef PlugClass This; \
241 typedef TfRefPtr<This> RefPtr; \
242 typedef TfWeakPtr<This> Ptr; \
243 static RefPtr create(TranslatorContextPtr context);
244 
245 //----------------------------------------------------------------------------------------------------------------------
248 //----------------------------------------------------------------------------------------------------------------------
249 #define AL_USDMAYA_DEFINE_TRANSLATOR(PlugClass, TranslatedType) \
250 TfRefPtr<PlugClass> \
251 PlugClass::create(TranslatorContextPtr context) { \
252  TfType const &type = TfType::Find<TranslatedType>(); \
253  if(not type.IsUnknown()) { \
254  TfRefPtr<PlugClass> plugin = TfCreateRefPtr(new This()); \
255  plugin->setTranslatedType(type); \
256  plugin->setContext(context); \
257  plugin->initialize(); \
258  return plugin; \
259  } \
260  else { \
261  TF_CODING_ERROR( \
262  "Failed to get %s usd type, maybe the needed plugin is not loaded", \
263  typeid(TranslatedType).name()); \
264  return TfNullPtr; \
265  } \
266 } \
267  \
268 TF_REGISTRY_FUNCTION(TfType) \
269 { \
270  TfType::Define<PlugClass, TfType::Bases<TranslatorBase>>() \
271  .SetFactory<TranslatorFactory<PlugClass>>(); \
272 }
273 
274 //----------------------------------------------------------------------------------------------------------------------
275 } // translators
276 } // fileio
277 } // usdmaya
278 } // AL
279 //----------------------------------------------------------------------------------------------------------------------
280 
The base class interface of all translator plugins. The absolute minimum a translator plugin must imp...
Definition: TranslatorBase.h:53
the factory interface, used to create an instance of a particular translator type ...
Definition: TranslatorBase.h:210
virtual void setTranslatedType(const TfType &translatedType)
internal method. Used within AL_USDMAYA_DEFINE_TRANSLATOR macro to set the schema type of the node we...
Definition: TranslatorBase.h:171
virtual TfType getTranslatedType() const =0
Override to specify the schema type of the prim that this translator plugin is responsible for...
the factory instance for a given translator type
Definition: TranslatorBase.h:225
virtual ~TranslatorAbstract()
dtor
Definition: TranslatorBase.h:62
virtual MStatus update(const UsdPrim &prim)
Optionally override this method to copy the attribute values from the prim onto the Maya nodes you ha...
Definition: TranslatorBase.h:125
TfRefPtr< This > RefPtr
the type of a reference this type
Definition: TranslatorBase.h:58
virtual MStatus initialize()
Override this to do a one time initialization of your translator. Primarily this is to allow you to e...
Definition: TranslatorBase.h:80
virtual TfRefPtr< TranslatorBase > create(TranslatorContextPtr ctx) const
creates a new translator for a given type T
Definition: TranslatorBase.h:231
virtual bool needsTransformParent() const
if the custom node type you are importing requires a parent transform (e.g. you are importing a shape...
Definition: TranslatorBase.h:73
virtual TfRefPtr< TranslatorBase > create(TranslatorContextPtr ctx) const =0
overridden by the TranslatorFactory to create a new translator for a given type
TfWeakPtr< This > Ptr
weak pointer to this type
Definition: TranslatorBase.h:59
virtual TranslatorContextPtr context() const
returns the context currently being used to translate the USD prims. The context can be used to add r...
Definition: TranslatorBase.h:152
virtual TfType getTranslatedType() const override
returns the translated prim type
Definition: TranslatorBase.h:147
Forms a registry of all plug-in translator types registered.
Definition: TranslatorBase.h:191
virtual MStatus preTearDown(UsdPrim &prim)
This method will be called prior to the tear down process taking place. This is the last chance you h...
Definition: TranslatorBase.h:103
Base class for maya translator usd plugins. The TfType of these plugins has to be derived from the ba...
Definition: TranslatorBase.h:135
static RefPtr manufacture(const std::string &primType, TranslatorContextPtr context)=delete
Internal method used to create a new instance of a plugin translator.
TfRefPtr< TranslatorBase > RefPtr
handle to a plug-in translator
Definition: TranslatorBase.h:194
virtual MStatus tearDown(const SdfPath &path)
If your plugin creates any nodes within Maya, then this method should be overridden to remove those n...
Definition: TranslatorBase.h:113
UsdStageRefPtr getUsdStage() const
return the usd stage associated with this context
Definition: TranslatorBase.h:163
TranslatorAbstract This
this type
Definition: TranslatorBase.h:57
virtual ~TranslatorBase()
dtor
Definition: TranslatorBase.h:143
virtual MStatus postImport(const UsdPrim &prim)
If your node needs to set up any relationships after import (for example, adding the node to a set...
Definition: TranslatorBase.h:96
virtual bool supportsUpdate() const
override this method and return true if the translator supports update
Definition: TranslatorBase.h:118
virtual void setContext(const TranslatorContextPtr context)
internal method. Used within AL_USDMAYA_DEFINE_TRANSLATOR macro to set the translation context ...
Definition: TranslatorBase.h:176