HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FBX_SceneProxy.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: FBX_SceneProxy.h (FBX Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __FBX_SCENEPROXY_H_INCLUDED__
13 #define __FBX_SCENEPROXY_H_INCLUDED__
14 
15 #include "FBX_API.h"
16 #include "FBX_SceneProxySupport.h"
17 #include <GU/GU_DetailHandle.h>
18 #include <GA/GA_OffsetList.h>
19 #include <UT/UT_ArrayStringMap.h>
20 #include <UT/UT_StringArray.h>
21 #include <UT/UT_StringHolder.h>
22 #include <UT/UT_UniquePtr.h>
23 #include <UT/UT_VectorTypes.h>
24 #include <UT/UT_TransformUtil.h>
25 #include <SYS/SYS_Types.h>
26 
27 
28 class FBX_ErrorManager;
29 class GU_Detail;
30 enum class OP_OrientationMode;
31 
32 FBX_SCENEPROXY_DECLARE_ID(FBX_NodeID);
33 FBX_SCENEPROXY_DECLARE_ID(FBX_GeometryID);
34 FBX_SCENEPROXY_DECLARE_ID(FBX_ClipID);
35 FBX_SCENEPROXY_DECLARE_ID(FBX_AnimCurveID);
36 FBX_SCENEPROXY_DECLARE_ID(FBX_AnimLayerID);
37 
38 FBX_SCENEPROXY_DECLARE_ID(FBX_PropertyID);
39 
40 template <typename T>
41 class FBX_Property : public FBX_PropertyID
42 {
43 public:
44  using value_type = T;
45 
46  using FBX_PropertyID::FBX_PropertyID;
47 
48 private:
49  friend class FBX_SceneProxy;
50 };
52 
54 struct FBX_AxisSystem;
55 
57 {
58 public:
60  {
61  bool myConvertUnits = true;
62  bool myConvertAxisSystem = true;
63  bool myConvertAxisSystemForceYUp = true; /// only used when converting
64  bool myImportTemplates = true;
65  bool myImportPivots = true;
66  bool myImportGlobalSettings = true;
67  bool myImportCharacters = true;
68  bool myImportConstraints = true;
69  bool myImportGobos = true;
70  bool myImportShapes = true;
71  bool myImportLinks = true;
72  bool myImportTextures = true;
73  bool myImportModels = true;
74  bool myImportAnimations = true;
75  bool myImportMaterials = false; /// should also enable
76  /// myExtractEmbeddedMedia when
77  /// enabling this.
78  bool myExtractEmbeddedData = false; // evil!
79  };
80 
82  {
83  bool myBinary = true;
84  bool myAnimationOnly = false;
85  bool myEmbedData = false; /// only used when myBinary is true
86  };
87 
88  /// Construct wrapper around the FBX SDK
89  /// @note By default, no scene is set up. You must still call one of load()
90  /// or initInitNewScene().
92 
93  ~FBX_SceneProxy();
94 
95  /// Initialize a new empty scene
96  /// @note Resets mySceneScale/myShouldConvertMaterials to default.
97  bool initNewScene(FBX_ErrorManager& errors);
98 
99  /// Load a new scene
100  /// @{
101  bool load(const char* path,
102  const LoadOptions& options,
103  FBX_ErrorManager& errors);
104  inline bool load(const char* path,
105  bool convert_units,
106  FBX_ErrorManager& errors)
107  {
108  LoadOptions options;
109  options.myConvertUnits = convert_units;
110  return load(path, options, errors);
111  }
112  /// @}
113 
114  /// Save current scene. One of initNewScene() or load() must have been
115  /// called.
116  /// @{
117  bool save(const char* path,
118  FBX_ErrorManager& errors,
119  const SaveOptions& options) const;
120  inline bool save(const char* path,
121  FBX_ErrorManager& errors,
122  bool binary = true,
123  bool animation_only = false) const
124  {
125  SaveOptions options;
126  options.myBinary = binary;
127  options.myAnimationOnly = animation_only;
128  return save(path, errors, options);
129  }
130  // @}
131 
132  /// Stamp FbxDocumentInfo using the current $JOB, $HIPFILE and software
133  /// version.
134  void stampDocumentInfo(const char* orig_fbx_filename = nullptr);
135 
136  fpreal globalFrameRate() const;
137  void setGlobalFrameRate(fpreal fps);
138 
139  void getGlobalTimeRange(fpreal& start, fpreal &stop) const;
140  void setGlobalTimeRange(fpreal start, fpreal stop);
141 
142  /// Get the scene's current axis system
143  FBX_AxisSystem globalAxisSystem() const;
144 
145  /// Set the scene's current axis system
146  /// @{
147  void setGlobalAxisSystem(const FBX_AxisSystem& axis_system);
148  void setGlobalAxisSystem(OP_OrientationMode orientation_mode);
149  /// @}
150 
151  /// Convert the scene to the given axis system
152  void convertAxisSystem(const FBX_AxisSystem& target);
153 
154  /// Convert the scene to match Houdini.
155  /// Returns true if conversion was done, false if no conversion was needed
156  bool convertToHoudiniAxisSystem();
157 
158  /// Return name of node
159  const char* nodeName(FBX_NodeID node) const;
160 
161  /// Find first node in scene that has given name
162  FBX_NodeID findNode(const char* name) const;
163 
164  /// Return "path" from scene root of node. If node == nullptr, returns
165  /// empty string.
166  UT_StringHolder nodePath(FBX_NodeID node) const;
167 
168  /// Find node in scene by hierarchical path from the scene root.
169  /// If path does not begin with a '/' then it returns findNode(path).
170  FBX_NodeID findNodeByPath(const char* path) const;
171 
172  int nodeCount() const;
173  FBX_NodeID node(int i) const;
174 
175  FBX_NodeID rootNode() const;
176  FBX_NodeID parentNode(FBX_NodeID node) const;
177  int childNodeCount(FBX_NodeID node) const;
178  FBX_NodeID childNode(FBX_NodeID node, int i) const;
179 
180  enum NodeType
181  {
182  eUnknown, // Unknown
183  eNull, // Null object
184  eMarker, // Mocap marker OR IK/FK effector
185  eSkeleton, // Bone/Joint
186  eMesh, // Geometry
187  eNurbs, // Geometry
188  ePatch, // Geometry
189  eCamera, // Camera
190  eCameraStereo, // Stereo camera
191  eCameraSwitcher, // Switch between child cameras
192  eLight, // Light
193  eOpticalReference, // Mocap marker
194  eOpticalMarker, // Mocap marker OR IK/FK effector
195  eNurbsCurve, // Geometry
196  eTrimNurbsSurface, // Geometry
197  eBoundary, // Geometry
198  eNurbsSurface, // Geometry
199  eShape, // Blendshape
200  eLODGroup, // Group of objects with different levels of detail
201  eSubDiv, // Geometry
202  eCachedEffect, // Particles, fluids, hair, etc. cache
203  eLine // Geometry
204  };
205  NodeType nodeType(FBX_NodeID node) const;
206 
207  /// Add new FbxNode with new FbxSkeleton attribute as a joint
208  FBX_NodeID createJointNode(const char* name, bool is_root);
209 
210  /// Add child to parent. If child already has a parent, first it is removed
211  /// from current parent and then added to this one.
212  void nodeAddChild(FBX_NodeID parent, FBX_NodeID child);
213 
214  bool visibility(FBX_NodeID node) const;
215 
216  FBX_GeometryID geometry(FBX_NodeID node) const;
217  FBX_NodeID geometryNode(FBX_GeometryID geometry) const;
218  int geometrySkinDeformerCount(FBX_GeometryID geometry) const;
219 
220  /// Count of skin deformers in the file
221  int skinDeformerCount() const;
222 
223  /// Get geometry associated with skin deformer i
224  /// @note There might no geometry attached and this will return nullptr
225  FBX_GeometryID skinDeformerGeometry(int i) const;
226 
227  /// Convert geometry into GU_Detail, with skin deformers converted into
228  /// a boneCapture attribute. If world_xform is given, the result including
229  /// the transforms within boneCapture will have this baked into it.
230  /// When add_unused_joints is true, add ancestor joints for any captured
231  /// joints will also be added to the boneCapture attribute with identity
232  /// transforsm.
233  bool convertGeometry(
234  GU_Detail& detail,
235  FBX_GeometryID geometry,
236  FBX_ErrorManager& errors,
237  bool add_unused_joints,
238  const UT_Matrix4D* world_xform = nullptr) const;
239 
240  enum class SkinningType
241  {
242  eRigid,
243  eLinear,
244  eDualQuaternion,
245  eBlend
246  };
247  /// Returns the skinning type for the i'th skin deformer in the geometry.
248  SkinningType geometrySkinDeformerType(FBX_GeometryID geometry, int i) const;
249 
250  /// Returns the number of blend shape deformers for the geometry.
251  int blendshapeCount(FBX_GeometryID geometry) const;
252  /// Returns the number of channels for the i'th blend shape deformer.
253  int blendshapeChannelCount(
254  FBX_GeometryID geometry, int blendshape_idx) const;
255  /// Returns the name of the specified blendshape channel.
256  UT_StringHolder blendshapeChannelName(FBX_GeometryID geometry,
257  int blendshape_idx, int channel_idx) const;
258 
259  /// Returns the geometry, name, and weight (multiplied by 0.01) for each of
260  /// the channel's target shapes. Multiple targets imply that there are
261  /// in-between shapes, and the shapes are sorted by weight in ascending
262  /// order.
263  void blendShapeChannelTargets(
264  FBX_GeometryID geometry,
265  int blendshape_idx,
266  int channel_idx,
267  UT_Array<GU_DetailHandle>& details,
270  const UT_Matrix4D* world_xform = nullptr) const;
271 
272  /// Returns curve for the blend shape channel's values.
273  /// @note Result might be null for non-animated blendshapes.
274  /// @{
275  FBX_AnimCurveID blendshapeChannelCurve(
276  FBX_GeometryID geometry,
277  int blendshape_idx,
278  int channel_idx) const;
279  FBX_AnimCurveID blendshapeChannelCurve(
280  FBX_GeometryID geometry,
281  int blendshape_idx,
282  int channel_idx,
283  bool create_if_needed);
284  /// @}
285 
286  /// Evaluate an animation curve at the given time
287  fpreal animCurveEval(
288  FBX_AnimCurveID curve, fpreal time_seconds) const;
289 
290  /// Number of keys in anim curve
291  int animCurveKeyCount(FBX_AnimCurveID curve) const;
292 
293  /// Get the time range of the anim curve
294  void getAnimCurveTimeInterval(
295  FBX_AnimCurveID curve,
296  fpreal &start, fpreal &end) const;
297 
298  /// Local translation property of node
299  FBX_Property3D nodeLocalTranslate(FBX_NodeID node) const;
300 
301  /// Local rotation property of node
302  FBX_Property3D nodeLocalRotate(FBX_NodeID node) const;
303 
304  /// Local scale property of node
305  FBX_Property3D nodeLocalScale(FBX_NodeID node) const;
306 
307  /// Local pre-rotation property of node. Should only be used when
308  /// nodeIsRotationActive() is true.
309  FBX_Property3D nodeLocalPreRotation(FBX_NodeID node) const;
310 
311  /// When nodeIsRotationActive() is false, then these node properties are
312  /// ignored from the FBX file:
313  /// rotation order, pivots, pre/post rotation values, rotation limits
314  bool nodeIsRotationActive(FBX_NodeID node) const;
315 
316  /// Return the transform order for given node. For FBX, we always return a
317  /// transform order of SRT, so really only the rotation order is pertinent
318  /// here.
319  /// @note Returns a default value when !nodeIsRotationActive()
320  UT_XformOrder nodeTransformOrder(FBX_NodeID node) const;
321 
322  /// Return the rotation pivot for given node.
323  /// @note Returns a default value when !nodeIsRotationActive()
324  UT_Vector3D nodeRotationPivot(FBX_NodeID node) const;
325 
326  int animLayerCount(FBX_ClipID clip) const;
327  FBX_AnimLayerID animLayer(FBX_ClipID clip, int i) const;
328 
329  /// Create a new animation layer. If given clip, the layer is added to it.
330  FBX_AnimLayerID createAnimLayer(const char* name,
331  FBX_ClipID clip = FBX_ClipID{});
332 
333  /// Destroy give animation layer. If recurse_unused is true, then
334  /// recursively destroy all unused dst objects as a result.
335  void destroyAnimLayer(FBX_AnimLayerID layer,
336  bool recurse_unused = false);
337 
338  /// Add/remove a layer to clip as a src object
339  /// @{
340  void addAnimLayer(FBX_ClipID clip, FBX_AnimLayerID layer);
341  void removeAnimLayer(FBX_ClipID clip, FBX_AnimLayerID layer);
342  /// @}
343 
344  /// Return src object count of layer
345  int animLayerSrcCount(FBX_AnimLayerID layer) const;
346 
347  /// Return animation curve attached to property
348  FBX_AnimCurveID propertyAnimCurve(
349  FBX_PropertyID property,
350  FBX_AnimLayerID layer,
351  int component,
352  bool create = false);
353 
354  /// Evaluate animated value from property
355  /// @{
356  fpreal propertyEvalFloat(
357  FBX_PropertyID property,
358  fpreal time_seconds);
359  UT_Vector3R propertyEvalVector(
361  fpreal time_seconds);
362  /// @}
363 
364  /// Set non-animated value on property
365  /// @{
366  void propertySetFloat(
367  FBX_PropertyID property,
368  fpreal value);
369  void propertySetVector(
370  FBX_PropertyID property,
371  const UT_Vector3R& vector);
372  /// @}
373 
374  /// Scene scale to convert from internal FBX scene. Determined by load()
375  /// This is currently only used by nodeGlobalTransform()
376  double sceneScale() const;
377 
378  /// Set the scene scale
379  void setSceneScale(fpreal scale);
380 
381  /// Return a node's global transform, scaled by sceneScale()
382  UT_Matrix4D nodeGlobalTransform(
383  FBX_NodeID node, fpreal time_seconds) const;
384 
385  /// Get a node's local transform at given time.
386  /// @note This transform is NOT scaled by sceneScale()
387  UT_Matrix4D nodeLocalTransform(
388  FBX_NodeID node, fpreal time_seconds) const;
389 
390  /// Return global transform without animation, scaled by sceneScale()
391  UT_Matrix4D nodeGlobalRestTransform(FBX_NodeID node) const;
392 
393  /// Access node's local transform without animation
394  /// @note This transform is NOT scaled by sceneScale()
395  /// @{
396  UT_Matrix4D nodeLocalRestTransform(FBX_NodeID node) const;
397 
398  /// Set a node's local transform without anim
399  void setNodeLocalRestTransform(
400  FBX_NodeID node,
401  const UT_Matrix4D& local);
402  /// @}
403 
404  /// Get a node's geometric transform. Returns false if there is none.
405  /// A geometric transform is a transform that is only applied to the node's
406  /// geometry and is not part of its global transform.
407  /// @note This transform is NOT scaled by sceneScale()
408  bool nodeGeometricTransform(
409  FBX_NodeID node, UT_Matrix4D& transform) const;
410 
411  /// Remove all animation on the transform properties of given node
412  void removeNodeAnimation(FBX_NodeID node);
413 
414  /// Compute a scene scale needed convert transforms from
415  /// nodeGlobalTransform() to the Houdini unit length.
416  /// This will be 1.0 if the file was loaded with convert_units=true.
417  double computeConversionFactor() const;
418 
420  nodeScaleInheritanceMode(FBX_NodeID node) const;
421 
422  /// Set the scale inheritance mode for a node. Note that FBX does NOT
423  /// support everything, and only these modes:
424  /// DEFAULT, OFFSET_ONLY, and OFFSET_AND_SCALE
425  /// If its given an invalid type, it will map to the next closest thing
426  void setNodeScaleInheritanceMode(
427  FBX_NodeID node, UT_ScaleInheritanceMode inherit);
428 
429  int clipCount() const;
430  FBX_ClipID clip(int i) const;
431 
432  FBX_ClipID findClip(const char* name) const;
433  const char* clipName(FBX_ClipID clip) const;
434 
435  void setActiveClip(FBX_ClipID clip);
436  FBX_ClipID activeClip() const;
437 
438  FBX_ClipID createClip(const char* name);
439 
440  /// Get/set the *inclusive* time range for the given clip in seconds
441  /// @{
442  void getClipTimeRange(
443  FBX_ClipID clip, fpreal& start, fpreal &stop) const;
444  void setClipTimeRange(
445  FBX_ClipID clip, fpreal start, fpreal stop);
446  /// @}
447 
448  struct JointInfo
449  {
450  FBX_NodeID myNode;
452  };
455  {
458  };
459 
460  /// Export geometry to FBX scene, with automatic handling of shapes
461  /// partitioned by name and/or path primitive attributes. If the nodes
462  /// already exist, then their geometry will be destroyed and recreated.
463  /// In order to export them with an identity global transform, a time is
464  /// needed to evaluate its parent node. If not specified, the time at 0
465  /// seconds will be used.
466  /// @warn Currently, everything is first converted to polygons.
467  /// @{
468  bool exportGeometry(
469  UT_Array<FBX_NodeID>& exported_nodes,
470  const UT_Matrix4D& parent_xform,
471  const GU_Detail& geo,
472  const SkinningInfo* skinning_info,
473  FBX_ErrorManager& errors);
474  bool exportGeometry(
475  UT_Array<FBX_NodeID>& exported_nodes,
476  fpreal time_seconds,
477  const UT_Matrix4D& parent_xform,
478  const GU_Detail& geo,
479  const SkinningInfo* skinning_info,
480  FBX_ErrorManager& errors);
481  /// @}
482 
483  /// Iterates over a node's user properties.
485  {
486  public:
487  PropertyIterator(FBX_NodeID node);
488  ~PropertyIterator();
489 
490  bool isValid();
491  void advance();
492 
493  const char *getName() const;
494  fpreal evalFloat(fpreal time_seconds) const;
495 
496  enum DataType
497  {
521  eDateTime
522  };
523 
524  DataType getDataType() const;
525 
526  private:
527  struct Impl;
528  UT_UniquePtr<Impl> myImpl;
529  };
530 
531 private:
532  template <typename FBX_T, typename ID_T>
533  static inline FBX_T * castID(ID_T id);
534 
535 private:
536  struct Impl;
537  UT_UniquePtr<Impl> myImpl;
538 
539  friend FBX_AnimCurveModifier;
540 };
541 
542 enum class FBX_KeyInterp
543 {
544  CONSTANT,
545  LINEAR,
546  CUBIC
547 };
548 
550 {
551 public:
552  FBX_AnimCurveModifier() = default;
553  FBX_AnimCurveModifier(FBX_AnimCurveID curve);
555 
556  void reset(FBX_AnimCurveID curve);
557 
558  void clearKeys();
559  int addKey(fpreal time_seconds);
560 
561  void setKeyInterpolation(int key_i, FBX_KeyInterp interp);
562  FBX_KeyInterp keyInterpolation(int key_i) const;
563 
564  void setKeyValue(int key_i, fpreal val);
565  fpreal keyValue(int key_i) const;
566 
567  FBX_AnimCurveID curve() const { return myCurve; }
568  int lastKeyIndex() const { return myLastKeyIndex; }
569 
570 private:
571  template <typename FBX_T, typename ID_T>
572  static inline FBX_T * castID(ID_T id)
573  { return FBX_SceneProxy::castID<FBX_T,ID_T>(id); }
574 
575 private:
576  FBX_AnimCurveID myCurve = FBX_AnimCurveID{};
577  int myLastKeyIndex = 0;
578 };
579 
581 {
582  enum class UpVector
583  {
584  XAxis,
585  YAxis,
586  ZAxis
587  };
588  enum class FrontVector
589  {
590  ParityEven,
591  ParityOdd
592  };
593  enum class CoordSystem
594  {
595  RightHanded,
596  LeftHanded
597  };
598 
602 
603  bool operator==(const FBX_AxisSystem &x) const
604  {
605  return myUpVector == x.myUpVector &&
606  myFrontVector == x.myFrontVector &&
607  myCoordSystem == x.myCoordSystem;
608  }
609  bool operator!=(const FBX_AxisSystem &x) const
610  {
611  return !(*this == x);
612  }
613 };
614 
615 namespace UT
616 {
617  template <>
618  struct DefaultClearer<FBX_SceneProxy::JointInfo>
619  {
622  static void clear(T& v) { D::clear(v.myNode) ; }
623  static bool isClear(const T& v) { return D::isClear(v.myNode); }
624  static void clearConstruct(T* p) { D::clear(p->myNode); }
625  static const bool clearNeedsDestruction = false;
626  };
627 }
628 
629 #endif // __FBX_SCENEPROXY_H_INCLUDED__
GLbyte * weights
Definition: glew.h:7551
FrontVector myFrontVector
GLuint id
Definition: glew.h:1679
GLuint const GLchar * name
Definition: glew.h:1814
CoordSystem myCoordSystem
GLuint GLenum GLenum transform
Definition: glew.h:14742
GLenum target
Definition: glew.h:2865
GLuint const GLfloat * val
Definition: glew.h:2794
GLenum GLenum GLenum GLenum GLenum scale
Definition: glew.h:13880
int lastKeyIndex() const
const GLdouble * v
Definition: glew.h:1391
GLboolean reset
Definition: glew.h:4959
bool save(const char *path, FBX_ErrorManager &errors, bool binary=true, bool animation_only=false) const
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
bool operator!=(const FBX_AxisSystem &x) const
GLuint interp
Definition: glew.h:8277
FBX_KeyInterp
#define FBX_API
Definition: FBX_API.h:10
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
const GLuint GLenum const void * binary
Definition: glew.h:3502
GLuint GLuint end
Definition: glew.h:1253
INT property
Definition: wglew.h:145
GLuint const GLuint * names
Definition: glew.h:2690
UpVector myUpVector
bool load(const char *path, bool convert_units, FBX_ErrorManager &errors)
GLuint start
Definition: glew.h:1253
GLsizei const GLchar *const * path
Definition: glew.h:6461
GLfloat GLfloat p
Definition: glew.h:16321
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER T clip(const T &p, const Box< T > &box)
Definition: ImathBoxAlgo.h:89
fpreal64 fpreal
Definition: SYS_Types.h:277
bool operator==(const FBX_AxisSystem &x) const
GLint GLboolean GLint layer
Definition: glew.h:3601
OP_OrientationMode
Definition: OP_Director.h:60
FBX_AnimCurveID curve() const
Iterates over a node's user properties.
UT_ScaleInheritanceMode
Scale inheritance modes.
GLsizei const GLfloat * value
Definition: glew.h:1849
FBX_SCENEPROXY_DECLARE_ID(FBX_NodeID)