FBXExporter.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FBXExporter.h
  34. * Declares the exporter class to write a scene to an fbx file
  35. */
  36. #ifndef AI_FBXEXPORTER_H_INC
  37. #define AI_FBXEXPORTER_H_INC
  38. #ifndef ASSIMP_BUILD_NO_FBX_EXPORTER
  39. #include "FBXExportNode.h" // FBX::Node
  40. #include "FBXCommon.h" // FBX::TransformInheritance
  41. #include <assimp/types.h>
  42. #include <assimp/StreamWriter.h> // StreamWriterLE
  43. #include <assimp/Exceptional.h> // DeadlyExportError
  44. #include <vector>
  45. #include <map>
  46. #include <unordered_set>
  47. #include <memory> // shared_ptr
  48. #include <sstream> // stringstream
  49. struct aiScene;
  50. struct aiNode;
  51. struct aiLight;
  52. namespace Assimp {
  53. class IOSystem;
  54. class IOStream;
  55. class ExportProperties;
  56. // ---------------------------------------------------------------------
  57. /** Helper class to export a given scene to an FBX file. */
  58. // ---------------------------------------------------------------------
  59. class FBXExporter
  60. {
  61. public:
  62. /// Constructor for a specific scene to export
  63. FBXExporter(const aiScene* pScene, const ExportProperties* pProperties);
  64. // call one of these methods to export
  65. void ExportBinary(const char* pFile, IOSystem* pIOSystem);
  66. void ExportAscii(const char* pFile, IOSystem* pIOSystem);
  67. private:
  68. bool binary; // whether current export is in binary or ascii format
  69. const aiScene* mScene; // the scene to export
  70. const ExportProperties* mProperties; // currently unused
  71. std::shared_ptr<IOStream> outfile; // file to write to
  72. std::vector<FBX::Node> connections; // connection storage
  73. std::map<const aiNode*, int64_t> mesh_uids;
  74. std::vector<int64_t> blendshape_uids;
  75. std::vector<int64_t> material_uids;
  76. std::map<const aiNode*,int64_t> node_uids;
  77. std::map<std::string,int64_t> lights_uids;
  78. // this crude unique-ID system is actually fine
  79. int64_t last_uid = 999999;
  80. int64_t generate_uid() { return ++last_uid; }
  81. // binary files have a specific header and footer,
  82. // in addition to the actual data
  83. void WriteBinaryHeader();
  84. void WriteBinaryFooter();
  85. // ascii files have a comment at the top
  86. void WriteAsciiHeader();
  87. // WriteAllNodes does the actual export.
  88. // It just calls all the Write<Section> methods below in order.
  89. void WriteAllNodes();
  90. // Methods to write individual sections.
  91. // The order here matches the order inside an FBX file.
  92. // Each method corresponds to a top-level FBX section,
  93. // except WriteHeader which also includes some binary-only sections
  94. // and WriteFooter which is binary data only.
  95. void WriteHeaderExtension();
  96. // WriteFileId(); // binary-only, included in WriteHeader
  97. // WriteCreationTime(); // binary-only, included in WriteHeader
  98. // WriteCreator(); // binary-only, included in WriteHeader
  99. void WriteGlobalSettings();
  100. void WriteDocuments();
  101. void WriteReferences();
  102. void WriteDefinitions();
  103. void WriteObjects();
  104. void WriteConnections();
  105. // WriteTakes(); // deprecated since at least 2015 (fbx 7.4)
  106. // helpers
  107. void WriteAsciiSectionHeader(const std::string& title);
  108. void WriteModelNodes(
  109. Assimp::StreamWriterLE& s,
  110. const aiNode* node,
  111. int64_t parent_uid,
  112. const std::unordered_set<const aiNode*>& limbnodes
  113. );
  114. void WriteModelNodes( // usually don't call this directly
  115. StreamWriterLE& s,
  116. const aiNode* node,
  117. int64_t parent_uid,
  118. const std::unordered_set<const aiNode*>& limbnodes,
  119. std::vector<std::pair<std::string,aiVector3D>>& transform_chain
  120. );
  121. void WriteModelNode( // nor this
  122. StreamWriterLE& s,
  123. bool binary,
  124. const aiNode* node,
  125. int64_t node_uid,
  126. const std::string& type,
  127. const std::vector<std::pair<std::string,aiVector3D>>& xfm_chain,
  128. FBX::TransformInheritance ti_type=FBX::TransformInheritance_RSrs
  129. );
  130. void WriteAnimationCurveNode(
  131. StreamWriterLE &outstream,
  132. int64_t uid,
  133. const std::string &name, // "T", "R", or "S"
  134. aiVector3D default_value,
  135. const std::string &property_name, // "Lcl Translation" etc
  136. int64_t animation_layer_uid,
  137. int64_t node_uid);
  138. void WriteAnimationCurve(
  139. StreamWriterLE& outstream,
  140. double default_value,
  141. const std::vector<int64_t>& times,
  142. const std::vector<float>& values,
  143. int64_t curvenode_id,
  144. const std::string& property_link // "d|X", "d|Y", etc
  145. );
  146. };
  147. }
  148. #endif // ASSIMP_BUILD_NO_FBX_EXPORTER
  149. #endif // AI_FBXEXPORTER_H_INC