Merging r50522 through r50572 from trunk into soc-2011-tomato
[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 "BKE_DerivedMesh.h"
83 #include "BKE_fcurve.h"
84 #include "BKE_animsys.h"
85 #include "BLI_path_util.h"
86 #include "BLI_fileops.h"
87 #include "ED_keyframing.h"
88 #ifdef WITH_BUILDINFO
89 extern char build_rev[];
90 #endif
91
92 #include "MEM_guardedalloc.h"
93
94 #include "BKE_blender.h" // version info
95 #include "BKE_scene.h"
96 #include "BKE_global.h"
97 #include "BKE_main.h"
98 #include "BKE_material.h"
99 #include "BKE_action.h" // pose functions
100 #include "BKE_armature.h"
101 #include "BKE_image.h"
102 #include "BKE_object.h"
103
104 #include "BLI_math.h"
105 #include "BLI_string.h"
106 #include "BLI_listbase.h"
107
108 #include "RNA_access.h"
109 }
110
111 #include "collada_internal.h"
112 #include "DocumentExporter.h"
113
114 extern bool bc_has_object_type(LinkNode *export_set, short obtype);
115
116 // can probably go after refactor is complete
117 #include "InstanceWriter.h"
118 #include "TransformWriter.h"
119
120 #include "SceneExporter.h"
121 #include "ArmatureExporter.h"
122 #include "AnimationExporter.h"
123 #include "CameraExporter.h"
124 #include "EffectExporter.h"
125 #include "GeometryExporter.h"
126 #include "ImageExporter.h"
127 #include "LightExporter.h"
128 #include "MaterialExporter.h"
129
130
131 char *bc_CustomData_get_layer_name(const struct CustomData *data, int type, int n)
132 {
133         int layer_index = CustomData_get_layer_index(data, type);
134         if (layer_index < 0) return NULL;
135
136         return data->layers[layer_index + n].name;
137 }
138
139 char *bc_CustomData_get_active_layer_name(const CustomData *data, int type)
140 {
141         /* get the layer index of the active layer of type */
142         int layer_index = CustomData_get_active_layer_index(data, type);
143         if (layer_index < 0) return NULL;
144
145         return data->layers[layer_index].name;
146 }
147
148 DocumentExporter::DocumentExporter(const ExportSettings *export_settings) : export_settings(export_settings) {
149 }
150
151 // TODO: it would be better to instantiate animations rather than create a new one per object
152 // COLLADA allows this through multiple <channel>s in <animation>.
153 // For this to work, we need to know objects that use a certain action.
154
155 void DocumentExporter::exportCurrentScene(Scene *sce)
156 {
157         PointerRNA sceneptr, unit_settings;
158         PropertyRNA *system; /* unused , *scale; */
159
160         clear_global_id_map();
161         
162         COLLADABU::NativeString native_filename =
163             COLLADABU::NativeString(std::string(this->export_settings->filepath));
164         COLLADASW::StreamWriter sw(native_filename);
165
166         fprintf(stdout, "Collada export: %s\n", this->export_settings->filepath);
167
168         // open <collada>
169         sw.startDocument();
170
171         // <asset>
172         COLLADASW::Asset asset(&sw);
173
174         RNA_id_pointer_create(&(sce->id), &sceneptr);
175         unit_settings = RNA_pointer_get(&sceneptr, "unit_settings");
176         system = RNA_struct_find_property(&unit_settings, "system");
177         //scale = RNA_struct_find_property(&unit_settings, "scale_length");
178
179         std::string unitname = "meter";
180         float linearmeasure = RNA_float_get(&unit_settings, "scale_length");
181
182         switch (RNA_property_enum_get(&unit_settings, system)) {
183                 case USER_UNIT_NONE:
184                 case USER_UNIT_METRIC:
185                         if (linearmeasure == 0.001f) {
186                                 unitname = "millimeter";
187                         }
188                         else if (linearmeasure == 0.01f) {
189                                 unitname = "centimeter";
190                         }
191                         else if (linearmeasure == 0.1f) {
192                                 unitname = "decimeter";
193                         }
194                         else if (linearmeasure == 1.0f) {
195                                 unitname = "meter";
196                         }
197                         else if (linearmeasure == 1000.0f) {
198                                 unitname = "kilometer";
199                         }
200                         break;
201                 case USER_UNIT_IMPERIAL:
202                         if (linearmeasure == 0.0254f) {
203                                 unitname = "inch";
204                         }
205                         else if (linearmeasure == 0.3048f) {
206                                 unitname = "foot";
207                         }
208                         else if (linearmeasure == 0.9144f) {
209                                 unitname = "yard";
210                         }
211                         break;
212                 default:
213                         break;
214         }
215
216         asset.setUnit(unitname, linearmeasure);
217         asset.setUpAxisType(COLLADASW::Asset::Z_UP);
218         if (U.author[0] != '\0') {
219                 asset.getContributor().mAuthor = U.author;
220         }
221         else {
222                 asset.getContributor().mAuthor = "Blender User";
223         }
224         char version_buf[128];
225 #ifdef WITH_BUILDINFO
226         sprintf(version_buf, "Blender %d.%02d.%d r%s", BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION, build_rev);
227 #else
228         sprintf(version_buf, "Blender %d.%02d.%d", BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION);
229 #endif
230         asset.getContributor().mAuthoringTool = version_buf;
231         asset.add();
232         
233         LinkNode *export_set = this->export_settings->export_set;
234         // <library_cameras>
235         if (bc_has_object_type(export_set, OB_CAMERA)) {
236                 CamerasExporter ce(&sw, this->export_settings);
237                 ce.exportCameras(sce);
238         }
239         
240         // <library_lights>
241         if (bc_has_object_type(export_set, OB_LAMP)) {
242                 LightsExporter le(&sw, this->export_settings);
243                 le.exportLights(sce);
244         }
245
246         // <library_images>
247         ImagesExporter ie(&sw, this->export_settings);
248         ie.exportImages(sce);
249         
250         // <library_effects>
251         EffectsExporter ee(&sw, this->export_settings);
252         ee.exportEffects(sce);
253         
254         // <library_materials>
255         MaterialsExporter me(&sw, this->export_settings);
256         me.exportMaterials(sce);
257
258         // <library_geometries>
259         if (bc_has_object_type(export_set, OB_MESH)) {
260                 GeometryExporter ge(&sw, this->export_settings);
261                 ge.exportGeom(sce);
262         }
263
264         // <library_animations>
265         AnimationExporter ae(&sw, this->export_settings);
266         ae.exportAnimations(sce);
267
268         // <library_controllers>
269         ArmatureExporter arm_exporter(&sw, this->export_settings);
270         if (bc_has_object_type(export_set, OB_ARMATURE)) {
271                 arm_exporter.export_controllers(sce);
272         }
273
274         // <library_visual_scenes>
275         SceneExporter se(&sw, &arm_exporter, this->export_settings);
276         se.exportScene(sce);
277         
278         // <scene>
279         std::string scene_name(translate_id(id_name(sce)));
280         COLLADASW::Scene scene(&sw, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING,
281                                                    scene_name));
282         scene.add();
283         
284         // close <Collada>
285         sw.endDocument();
286
287 }
288
289 void DocumentExporter::exportScenes(const char *filename)
290 {
291 }
292
293 /*
294  * NOTES:
295  *
296  * AnimationExporter::sample_animation enables all curves on armature, this is undesirable for a user
297  *
298  */