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