Blender  V3.3
DocumentExporter.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <algorithm> /* std::find */
8 #include <cmath>
9 #include <cstdio>
10 #include <cstdlib>
11 #include <vector>
12 
13 #include "COLLADASWAsset.h"
14 #include "COLLADASWBaseInputElement.h"
15 #include "COLLADASWBindMaterial.h"
16 #include "COLLADASWCamera.h"
17 #include "COLLADASWColorOrTexture.h"
18 #include "COLLADASWConstants.h"
19 #include "COLLADASWEffectProfile.h"
20 #include "COLLADASWImage.h"
21 #include "COLLADASWInputList.h"
22 #include "COLLADASWInstanceCamera.h"
23 #include "COLLADASWInstanceController.h"
24 #include "COLLADASWInstanceGeometry.h"
25 #include "COLLADASWInstanceLight.h"
26 #include "COLLADASWInstanceNode.h"
27 #include "COLLADASWLibraryAnimations.h"
28 #include "COLLADASWLibraryControllers.h"
29 #include "COLLADASWLibraryEffects.h"
30 #include "COLLADASWLibraryImages.h"
31 #include "COLLADASWLibraryMaterials.h"
32 #include "COLLADASWLibraryVisualScenes.h"
33 #include "COLLADASWNode.h"
34 #include "COLLADASWParamBase.h"
35 #include "COLLADASWParamTemplate.h"
36 #include "COLLADASWPrimitves.h"
37 #include "COLLADASWSampler.h"
38 #include "COLLADASWScene.h"
39 #include "COLLADASWSource.h"
40 #include "COLLADASWSurfaceInitOption.h"
41 #include "COLLADASWTechnique.h"
42 #include "COLLADASWTexture.h"
43 #include "COLLADASWVertices.h"
44 
45 #include "MEM_guardedalloc.h"
46 
47 #include "DNA_action_types.h"
48 #include "DNA_anim_types.h"
49 #include "DNA_armature_types.h"
50 #include "DNA_collection_types.h"
51 #include "DNA_curve_types.h"
52 #include "DNA_image_types.h"
53 #include "DNA_material_types.h"
54 #include "DNA_mesh_types.h"
55 #include "DNA_meshdata_types.h"
56 #include "DNA_modifier_types.h"
57 #include "DNA_object_types.h"
58 #include "DNA_scene_types.h"
59 #include "DNA_userdef_types.h"
60 
61 #include "BLI_fileops.h"
62 #include "BLI_listbase.h"
63 #include "BLI_math.h"
64 #include "BLI_path_util.h"
65 #include "BLI_string.h"
66 #include "BLI_utildefines.h"
67 
68 #include "BKE_action.h" /* pose functions */
69 #include "BKE_animsys.h"
70 #include "BKE_appdir.h"
71 #include "BKE_armature.h"
72 #include "BKE_blender_version.h"
73 #include "BKE_customdata.h"
74 #include "BKE_fcurve.h"
75 #include "BKE_global.h"
76 #include "BKE_image.h"
77 #include "BKE_main.h"
78 #include "BKE_material.h"
79 #include "BKE_object.h"
80 #include "BKE_scene.h"
81 
82 #include "ED_keyframing.h"
83 #ifdef WITH_BUILDINFO
84 extern "C" char build_commit_date[];
85 extern "C" char build_commit_time[];
86 extern "C" char build_hash[];
87 #endif
88 
89 #include "RNA_access.h"
90 
91 #include "DocumentExporter.h"
92 #include "collada_internal.h"
93 #include "collada_utils.h"
94 
95 /* can probably go after refactor is complete */
96 #include "InstanceWriter.h"
97 #include "TransformWriter.h"
98 
99 #include "AnimationExporter.h"
100 #include "ArmatureExporter.h"
101 #include "CameraExporter.h"
102 #include "ControllerExporter.h"
103 #include "EffectExporter.h"
104 #include "GeometryExporter.h"
105 #include "ImageExporter.h"
106 #include "LightExporter.h"
107 #include "MaterialExporter.h"
108 #include "SceneExporter.h"
109 
110 #include <cerrno>
111 
112 char *bc_CustomData_get_layer_name(const struct CustomData *data, int type, int n)
113 {
114  int layer_index = CustomData_get_layer_index(data, type);
115  if (layer_index < 0) {
116  return nullptr;
117  }
118 
119  return data->layers[layer_index + n].name;
120 }
121 
123 {
124  /* get the layer index of the active layer of type */
125  int layer_index = CustomData_get_active_layer_index(data, type);
126  if (layer_index < 0) {
127  return nullptr;
128  }
129 
130  return data->layers[layer_index].name;
131 }
132 
133 DocumentExporter::DocumentExporter(BlenderContext &blender_context,
134  ExportSettings *export_settings)
135  : blender_context(blender_context),
136  export_settings(BCExportSettings(export_settings, blender_context))
137 {
138 }
139 
140 static COLLADABU::NativeString make_temp_filepath(const char *name, const char *extension)
141 {
142  char tempfile[FILE_MAX];
143 
144  if (name == nullptr) {
145  name = "untitled";
146  }
147 
148  BLI_join_dirfile(tempfile, sizeof(tempfile), BKE_tempdir_session(), name);
149 
150  if (extension) {
151  BLI_path_extension_ensure(tempfile, FILE_MAX, extension);
152  }
153 
154  COLLADABU::NativeString native_filename = COLLADABU::NativeString(
155  tempfile, COLLADABU::NativeString::ENCODING_UTF8);
156  return native_filename;
157 }
158 
159 /* TODO: it would be better to instantiate animations rather than create a new one per object
160  * COLLADA allows this through multiple <channel>s in <animation>.
161  * For this to work, we need to know objects that use a certain action. */
162 
164 {
165  Scene *sce = blender_context.get_scene();
166  bContext *C = blender_context.get_context();
167 
168  PointerRNA sceneptr, unit_settings;
169  PropertyRNA *system; /* unused, *scale; */
170 
172 
173  COLLADABU::NativeString native_filename = make_temp_filepath(nullptr, ".dae");
174  COLLADASW::StreamWriter *writer = new COLLADASW::StreamWriter(native_filename);
175 
176  /* open <collada> */
177  writer->startDocument();
178 
179  /* <asset> */
180  COLLADASW::Asset asset(writer);
181 
182  RNA_id_pointer_create(&(sce->id), &sceneptr);
183  unit_settings = RNA_pointer_get(&sceneptr, "unit_settings");
184  system = RNA_struct_find_property(&unit_settings, "system");
185  // scale = RNA_struct_find_property(&unit_settings, "scale_length");
186 
187  std::string unitname = "meter";
188  float linearmeasure = RNA_float_get(&unit_settings, "scale_length");
189 
190  switch (RNA_property_enum_get(&unit_settings, system)) {
191  case USER_UNIT_NONE:
192  case USER_UNIT_METRIC:
193  if (linearmeasure == 0.001f) {
194  unitname = "millimeter";
195  }
196  else if (linearmeasure == 0.01f) {
197  unitname = "centimeter";
198  }
199  else if (linearmeasure == 0.1f) {
200  unitname = "decimeter";
201  }
202  else if (linearmeasure == 1.0f) {
203  unitname = "meter";
204  }
205  else if (linearmeasure == 1000.0f) {
206  unitname = "kilometer";
207  }
208  break;
209  case USER_UNIT_IMPERIAL:
210  if (linearmeasure == 0.0254f) {
211  unitname = "inch";
212  }
213  else if (linearmeasure == 0.3048f) {
214  unitname = "foot";
215  }
216  else if (linearmeasure == 0.9144f) {
217  unitname = "yard";
218  }
219  break;
220  default:
221  break;
222  }
223 
224  asset.setUnit(unitname, linearmeasure);
225  asset.setUpAxisType(COLLADASW::Asset::Z_UP);
226  asset.getContributor().mAuthor = "Blender User";
227  char version_buf[128];
228 #ifdef WITH_BUILDINFO
229  BLI_snprintf(version_buf,
230  sizeof(version_buf),
231  "Blender %s commit date:%s, commit time:%s, hash:%s",
235  build_hash);
236 #else
237  BLI_snprintf(version_buf, sizeof(version_buf), "Blender %s", BKE_blender_version_string());
238 #endif
239  asset.getContributor().mAuthoringTool = version_buf;
240  asset.add();
241 
242  LinkNode *export_set = this->export_settings.get_export_set();
243  /* <library_cameras> */
244  if (bc_has_object_type(export_set, OB_CAMERA)) {
245  CamerasExporter ce(writer, this->export_settings);
246  ce.exportCameras(sce);
247  }
248 
249  /* <library_lights> */
250  if (bc_has_object_type(export_set, OB_LAMP)) {
251  LightsExporter le(writer, this->export_settings);
252  le.exportLights(sce);
253  }
254 
255  /* <library_effects> */
256  EffectsExporter ee(writer, this->export_settings, key_image_map);
257  ee.exportEffects(C, sce);
258 
259  /* <library_images> */
260  ImagesExporter ie(writer, this->export_settings, key_image_map);
261  ie.exportImages(sce);
262 
263  /* <library_materials> */
264  MaterialsExporter me(writer, this->export_settings);
265  me.exportMaterials(sce);
266 
267  /* <library_geometries> */
268  if (bc_has_object_type(export_set, OB_MESH)) {
269  GeometryExporter ge(blender_context, writer, this->export_settings);
270  ge.exportGeom();
271  }
272 
273  /* <library_controllers> */
274  ArmatureExporter arm_exporter(blender_context, writer, this->export_settings);
275  ControllerExporter controller_exporter(blender_context, writer, this->export_settings);
276  if (bc_has_object_type(export_set, OB_ARMATURE) ||
277  this->export_settings.get_include_shapekeys()) {
278  controller_exporter.export_controllers();
279  }
280 
281  /* <library_visual_scenes> */
282 
283  SceneExporter se(blender_context, writer, &arm_exporter, this->export_settings);
284 
285  if (this->export_settings.get_include_animations()) {
286  /* <library_animations> */
287  AnimationExporter ae(writer, this->export_settings);
288  ae.exportAnimations();
289  }
290 
291  se.exportScene();
292 
293  /* <scene> */
294  std::string scene_name(translate_id(id_name(sce)));
295  COLLADASW::Scene scene(writer, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, scene_name));
296  scene.add();
297 
298  /* close <Collada> */
299  writer->endDocument();
300  delete writer;
301 
302  /* Finally move the created document into place */
303  fprintf(stdout, "Collada export to: %s\n", this->export_settings.get_filepath());
304  int status = BLI_rename(native_filename.c_str(), this->export_settings.get_filepath());
305  if (status != 0) {
306  status = BLI_copy(native_filename.c_str(), this->export_settings.get_filepath());
307  BLI_delete(native_filename.c_str(), false, false);
308  }
309  return status;
310 }
311 
312 void DocumentExporter::exportScenes(const char *filename)
313 {
314 }
315 
316 /*
317  * NOTES:
318  *
319  * AnimationExporter::sample_animation enables all curves on armature, this is undesirable for a
320  * user
321  */
std::string EMPTY_STRING
Blender kernel action and pose functionality.
const char * BKE_blender_version_string(void)
Definition: blender.c:124
CustomData interface, see also DNA_customdata_types.h.
int CustomData_get_active_layer_index(const struct CustomData *data, int type)
int CustomData_get_layer_index(const struct CustomData *data, int type)
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
File and directory operations.
int BLI_delete(const char *file, bool dir, bool recursive) ATTR_NONNULL()
Definition: fileops.c:934
int BLI_rename(const char *from, const char *to) ATTR_NONNULL()
Definition: fileops.c:1268
int BLI_copy(const char *file, const char *to) ATTR_NONNULL()
Definition: fileops.c:1198
#define FILE_MAX
bool BLI_path_extension_ensure(char *path, size_t maxlen, const char *ext) ATTR_NONNULL()
Definition: path_util.c:1420
void BLI_join_dirfile(char *__restrict dst, size_t maxlen, const char *__restrict dir, const char *__restrict file) ATTR_NONNULL()
Definition: path_util.c:1531
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
@ OB_CAMERA
@ OB_ARMATURE
@ OB_LAMP
@ OB_MESH
#define USER_UNIT_METRIC
#define USER_UNIT_NONE
struct Scene Scene
#define USER_UNIT_IMPERIAL
char * bc_CustomData_get_active_layer_name(const CustomData *data, int type)
static COLLADABU::NativeString make_temp_filepath(const char *name, const char *extension)
char * bc_CustomData_get_layer_name(const struct CustomData *data, int type, int n)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
char build_hash[]
Definition: buildinfo.c:31
char build_commit_date[]
Definition: buildinfo.c:33
char build_commit_time[]
Definition: buildinfo.c:34
void exportCameras(Scene *sce)
void exportScenes(const char *filename)
DocumentExporter(BlenderContext &blender_context, ExportSettings *export_settings)
void exportEffects(bContext *C, Scene *sce)
void exportImages(Scene *sce)
void exportLights(Scene *sce)
void exportMaterials(Scene *sce)
std::string translate_id(const char *idString)
void clear_global_id_map()
std::string id_name(void *id)
bool bc_has_object_type(LinkNode *export_set, short obtype)
Scene scene
void * BKE_tempdir_session
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5167
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:112
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:717
float RNA_float_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4957
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:3402