BlenderLoader.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 BlenderLoader.h
  34. * @brief Declaration of the Blender 3D (*.blend) importer class.
  35. */
  36. #pragma once
  37. #ifndef INCLUDED_AI_BLEND_LOADER_H
  38. #define INCLUDED_AI_BLEND_LOADER_H
  39. #include <assimp/BaseImporter.h>
  40. #include <assimp/LogAux.h>
  41. #include <memory>
  42. struct aiNode;
  43. struct aiMesh;
  44. struct aiLight;
  45. struct aiCamera;
  46. struct aiMaterial;
  47. namespace Assimp {
  48. // TinyFormatter.h
  49. namespace Formatter {
  50. template <typename T, typename TR, typename A>
  51. class basic_formatter;
  52. typedef class basic_formatter<char, std::char_traits<char>, std::allocator<char>> format;
  53. } // namespace Formatter
  54. // BlenderDNA.h
  55. namespace Blender {
  56. class FileDatabase;
  57. struct ElemBase;
  58. } // namespace Blender
  59. // BlenderScene.h
  60. namespace Blender {
  61. struct Scene;
  62. struct Object;
  63. struct Collection;
  64. struct Mesh;
  65. struct Camera;
  66. struct Lamp;
  67. struct MTex;
  68. struct Image;
  69. struct Material;
  70. } // namespace Blender
  71. // BlenderIntermediate.h
  72. namespace Blender {
  73. struct ConversionData;
  74. template <template <typename, typename> class TCLASS, typename T>
  75. struct TempArray;
  76. } // namespace Blender
  77. // BlenderModifier.h
  78. namespace Blender {
  79. class BlenderModifierShowcase;
  80. class BlenderModifier;
  81. } // namespace Blender
  82. // -------------------------------------------------------------------------------------------
  83. /** Load blenders official binary format. The actual file structure (the `DNA` how they
  84. * call it is outsourced to BlenderDNA.cpp/BlenderDNA.h. This class only performs the
  85. * conversion from intermediate format to aiScene. */
  86. // -------------------------------------------------------------------------------------------
  87. class BlenderImporter : public BaseImporter, public LogFunctions<BlenderImporter> {
  88. public:
  89. BlenderImporter();
  90. ~BlenderImporter() override;
  91. bool CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const override;
  92. protected:
  93. const aiImporterDesc *GetInfo() const override;
  94. void SetupProperties(const Importer *pImp) override;
  95. void InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) override;
  96. void ParseBlendFile(Blender::FileDatabase &out, std::shared_ptr<IOStream> stream);
  97. void ExtractScene(Blender::Scene &out, const Blender::FileDatabase &file);
  98. void ParseSubCollection(const Blender::Scene &in, aiNode *root, const std::shared_ptr<Blender::Collection>& collection, Blender::ConversionData &conv_data);
  99. void ConvertBlendFile(aiScene *out, const Blender::Scene &in, const Blender::FileDatabase &file);
  100. private:
  101. aiNode *ConvertNode(const Blender::Scene &in,
  102. const Blender::Object *obj,
  103. Blender::ConversionData &conv_info,
  104. const aiMatrix4x4 &parentTransform);
  105. // --------------------
  106. void ConvertMesh(const Blender::Scene &in,
  107. const Blender::Object *obj,
  108. const Blender::Mesh *mesh,
  109. Blender::ConversionData &conv_data,
  110. Blender::TempArray<std::vector, aiMesh> &temp);
  111. // --------------------
  112. aiLight *ConvertLight(const Blender::Scene &in,
  113. const Blender::Object *obj,
  114. const Blender::Lamp *mesh,
  115. Blender::ConversionData &conv_data);
  116. // --------------------
  117. aiCamera *ConvertCamera(const Blender::Scene &in,
  118. const Blender::Object *obj,
  119. const Blender::Camera *mesh,
  120. Blender::ConversionData &conv_data);
  121. // --------------------
  122. void BuildDefaultMaterial(
  123. Blender::ConversionData &conv_data);
  124. // --------------------
  125. void AddBlendParams(
  126. aiMaterial *result,
  127. const Blender::Material *source);
  128. // --------------------
  129. void BuildMaterials(
  130. Blender::ConversionData &conv_data);
  131. // --------------------
  132. void ResolveTexture(
  133. aiMaterial *out,
  134. const Blender::Material *mat,
  135. const Blender::MTex *tex,
  136. Blender::ConversionData &conv_data);
  137. // --------------------
  138. void ResolveImage(
  139. aiMaterial *out,
  140. const Blender::Material *mat,
  141. const Blender::MTex *tex,
  142. const Blender::Image *img,
  143. Blender::ConversionData &conv_data);
  144. // --------------------
  145. void AddSentinelTexture(
  146. aiMaterial *out,
  147. const Blender::Material *mat,
  148. const Blender::MTex *tex,
  149. Blender::ConversionData &conv_data);
  150. // TODO: Move to a std::variant, once c++17 is supported.
  151. struct StreamOrError {
  152. std::shared_ptr<IOStream> stream;
  153. std::shared_ptr<std::vector<char>> input;
  154. std::string error;
  155. };
  156. // Returns either a stream (and optional input data for the stream) or
  157. // an error if it can't parse the magic token.
  158. StreamOrError ParseMagicToken(
  159. const std::string &pFile,
  160. IOSystem *pIOHandler) const;
  161. private: // static stuff, mostly logging and error reporting.
  162. // --------------------
  163. static void CheckActualType(const Blender::ElemBase *dt,
  164. const char *check);
  165. // --------------------
  166. static void NotSupportedObjectType(const Blender::Object *obj,
  167. const char *type);
  168. private:
  169. Blender::BlenderModifierShowcase *modifier_cache;
  170. }; // !class BlenderImporter
  171. } // end of namespace Assimp
  172. #endif // AI_UNREALIMPORTER_H_INC