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