AL_USDMaya  0.29.4
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TranslatorBase.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 #include "AL/maya/utils/Api.h"
20 
21 #include "maya/MDagPath.h"
22 
23 #include "pxr/base/tf/refBase.h"
24 #include "pxr/base/tf/type.h"
25 #include "pxr/base/tf/weakBase.h"
26 #include "pxr/base/tf/registryManager.h"
27 #include "pxr/usd/usd/prim.h"
28 
29 #include <iostream>
30 #include <unordered_map>
31 #include <functional>
32 #include "AL/usdmaya/fileio/translators/TranslatorContext.h"
33 #include "AL/usdmaya/fileio/ExportParams.h"
34 
35 namespace AL {
36 namespace usdmaya {
37 namespace fileio {
38 namespace translators {
39 
40 enum class ExportFlag
41 {
42  kNotSupported,
43  kFallbackSupport,
44  kSupported
45 };
46 
47 
48 //----------------------------------------------------------------------------------------------------------------------
80 //----------------------------------------------------------------------------------------------------------------------
82  : public TfRefBase, public TfWeakBase
83 {
84 public:
86  typedef TfRefPtr<This> RefPtr;
87  typedef TfWeakPtr<This> Ptr;
88 
90  virtual ~TranslatorAbstract() {}
91 
94  virtual TfType getTranslatedType() const = 0;
95 
101  virtual bool needsTransformParent() const
102  { return true; }
103 
108  virtual MStatus initialize()
109  { return MS::kSuccess; }
110 
118  virtual MStatus import(const UsdPrim& prim, MObject& parent, MObject& createdObj)
119  { return MS::kSuccess; }
120 
121  virtual UsdPrim exportObject(UsdStageRefPtr stage, MDagPath dagPath, const SdfPath& usdPath, const ExporterParams& params)
122  { return UsdPrim(); }
123 
128  virtual MStatus postImport(const UsdPrim& prim)
129  { return MS::kSuccess; }
130 
135  virtual MStatus preTearDown(UsdPrim& prim)
136  { return MS::kSuccess; }
137 
145  virtual MStatus tearDown(const SdfPath& path)
146  { return MStatus::kNotImplemented; }
147 
150  virtual bool supportsUpdate() const
151  { return false; }
152 
156  virtual bool importableByDefault() const
157  { return true; }
158 
163  virtual MStatus update(const UsdPrim& prim)
164  { return MStatus::kNotImplemented; }
165 
167  virtual ExportFlag canExport(const MObject& obj)
168  { return ExportFlag::kNotSupported; }
169 
170 };
171 
172 //----------------------------------------------------------------------------------------------------------------------
176 //----------------------------------------------------------------------------------------------------------------------
178  : public TranslatorAbstract
179 {
180 public:
181  typedef TranslatorBase This;
182  typedef TfRefPtr<This> RefPtr;
183 
185  virtual ~TranslatorBase()
186  {}
187 
189  TfType getTranslatedType() const override
190  { return m_translatedType; }
191 
194  TranslatorContextPtr context() const
195  { return m_context; }
196 
201  AL_USDMAYA_PUBLIC
202  static RefPtr manufacture(const std::string& primType, TranslatorContextPtr context) = delete;
203 
206  UsdStageRefPtr getUsdStage() const
207  { return context()->getUsdStage(); }
208 
214  virtual void exportCustomAnim(const MDagPath& path, UsdPrim& prim, const UsdTimeCode& timeCode)
215  {
216  (void)path;
217  (void)prim;
218  (void)timeCode;
219  }
220 
221 protected:
222 
226  virtual void setTranslatedType(const TfType& translatedType)
227  { m_translatedType = translatedType; }
228 
231  virtual void setContext(const TranslatorContextPtr context)
232  { m_context = context; }
233 
234 private:
235  TfType m_translatedType;
236  TranslatorContextPtr m_context;
237 };
238 
239 typedef TfRefPtr<TranslatorBase> TranslatorRefPtr;
240 typedef std::vector<TranslatorRefPtr> TranslatorRefPtrVector;
241 
242 //----------------------------------------------------------------------------------------------------------------------
245 //----------------------------------------------------------------------------------------------------------------------
247 {
248 public:
249  typedef TfRefPtr<TranslatorBase> RefPtr;
250  typedef std::vector<RefPtr> RefPtrVector;
251 
255  AL_USDMAYA_PUBLIC
256  TranslatorManufacture(TranslatorContextPtr context);
257 
261  AL_USDMAYA_PUBLIC
262  RefPtr get(const TfToken type_name);
263 
267  AL_USDMAYA_PUBLIC
268  RefPtr get(const MObject& mayaObject);
269 
270 private:
271  std::unordered_map<std::string, TranslatorRefPtr> m_translatorsMap;
272 };
273 
274 //----------------------------------------------------------------------------------------------------------------------
277 //----------------------------------------------------------------------------------------------------------------------
279  : public TfType::FactoryBase
280 {
281 public:
285  AL_USDMAYA_PUBLIC
286  virtual TfRefPtr<TranslatorBase> create(TranslatorContextPtr ctx) const = 0;
287 };
288 
289 //----------------------------------------------------------------------------------------------------------------------
292 //----------------------------------------------------------------------------------------------------------------------
293 template <typename T>
295 {
296 public:
300  TfRefPtr<TranslatorBase> create(TranslatorContextPtr ctx) const override
301  { return T::create(ctx); }
302 };
303 
304 //----------------------------------------------------------------------------------------------------------------------
307 //----------------------------------------------------------------------------------------------------------------------
308 #define AL_USDMAYA_DECLARE_TRANSLATOR(PlugClass) \
309 typedef PlugClass This; \
310 typedef TfRefPtr<This> RefPtr; \
311 typedef TfWeakPtr<This> Ptr; \
312 AL_MAYA_MACROS_PUBLIC \
313 static RefPtr create(TranslatorContextPtr context);
314 
315 
316 //----------------------------------------------------------------------------------------------------------------------
319 //----------------------------------------------------------------------------------------------------------------------
320 #define AL_USDMAYA_DEFINE_TRANSLATOR(PlugClass, TranslatedType) \
321 TfRefPtr<PlugClass> \
322 PlugClass::create(TranslatorContextPtr context) { \
323  TfType const &type = TfType::Find<TranslatedType>(); \
324  if(!type.IsUnknown()) { \
325  TfRefPtr<PlugClass> plugin = TfCreateRefPtr(new This()); \
326  plugin->setTranslatedType(type); \
327  plugin->setContext(context); \
328  if(!plugin->initialize()) return TfRefPtr<PlugClass>(); \
329  return plugin; \
330  } \
331  else { \
332  TF_CODING_ERROR( \
333  "Failed to get %s usd type, maybe the needed plugin is not loaded", \
334  typeid(TranslatedType).name()); \
335  return TfNullPtr; \
336  } \
337 } \
338  \
339 TF_REGISTRY_FUNCTION(TfType) \
340 { \
341  TfType::Define<PlugClass, TfType::Bases<TranslatorBase>>() \
342  .SetFactory<TranslatorFactory<PlugClass>>(); \
343 }
344 
345 //----------------------------------------------------------------------------------------------------------------------
346 } // translators
347 } // fileio
348 } // usdmaya
349 } // AL
350 //----------------------------------------------------------------------------------------------------------------------
351 
The base class interface of all translator plugins. The absolute minimum a translator plugin must imp...
Definition: TranslatorBase.h:81
the factory interface, used to create an instance of a particular translator type ...
Definition: TranslatorBase.h:278
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:226
virtual TfType getTranslatedType() const =0
Override to specify the schema type of the prim that this translator plugin is responsible for...
parameters for the exporter. These parameters are constructed by any command or file translator that ...
Definition: ExportParams.h:35
virtual void exportCustomAnim(const MDagPath &path, UsdPrim &prim, const UsdTimeCode &timeCode)
override if you have a node that needs to generate animation that cannot be mapped between an MPlug a...
Definition: TranslatorBase.h:214
the factory instance for a given translator type
Definition: TranslatorBase.h:294
virtual ~TranslatorAbstract()
dtor
Definition: TranslatorBase.h:90
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:163
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:194
static AL_USDMAYA_PUBLIC RefPtr manufacture(const std::string &primType, TranslatorContextPtr context)=delete
Internal method used to create a new instance of a plugin translator.
TfRefPtr< This > RefPtr
the type of a reference this type
Definition: TranslatorBase.h:86
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:108
virtual ExportFlag canExport(const MObject &obj)
Method used to test a Maya node to see whether it can be exported.
Definition: TranslatorBase.h:167
AL_USDMAYA_PUBLIC TranslatorManufacture(TranslatorContextPtr context)
constructs a registry of translator plugins that are currently registered within usd maya...
TfType getTranslatedType() const override
returns the translated prim type
Definition: TranslatorBase.h:189
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:101
TfWeakPtr< This > Ptr
weak pointer to this type
Definition: TranslatorBase.h:87
virtual AL_USDMAYA_PUBLIC TfRefPtr< TranslatorBase > create(TranslatorContextPtr ctx) const =0
overridden by the TranslatorFactory to create a new translator for a given type
Forms a registry of all plug-in translator types registered.
Definition: TranslatorBase.h:246
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:135
Base class for maya translator usd plugins. The TfType of these plugins has to be derived from the ba...
Definition: TranslatorBase.h:177
TfRefPtr< TranslatorBase > create(TranslatorContextPtr ctx) const override
creates a new translator for a given type T
Definition: TranslatorBase.h:300
TfRefPtr< TranslatorBase > RefPtr
handle to a plug-in translator
Definition: TranslatorBase.h:249
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:145
UsdStageRefPtr getUsdStage() const
return the usd stage associated with this context
Definition: TranslatorBase.h:206
TranslatorAbstract This
this type
Definition: TranslatorBase.h:85
virtual ~TranslatorBase()
dtor
Definition: TranslatorBase.h:185
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:128
virtual bool importableByDefault() const
If a translator is importableByDefault=true, it will always be automatically imported on ProxyShape i...
Definition: TranslatorBase.h:156
virtual bool supportsUpdate() const
override this method and return true if the translator supports update
Definition: TranslatorBase.h:150
virtual void setContext(const TranslatorContextPtr context)
internal method. Used within AL_USDMAYA_DEFINE_TRANSLATOR macro to set the translation context ...
Definition: TranslatorBase.h:231