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