6e04a1773bb6118e715fb3ce22e9390272bafb9e
[blender-staging.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
31 extern "C" 
32 {
33 #include "DNA_scene_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_group_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_mesh_types.h"
38 #include "DNA_image_types.h"
39 #include "DNA_material_types.h"
40 #include "DNA_texture_types.h"
41 #include "DNA_anim_types.h"
42 #include "DNA_action_types.h"
43 #include "DNA_curve_types.h"
44 #include "DNA_armature_types.h"
45 #include "DNA_modifier_types.h"
46 #include "DNA_userdef_types.h"
47
48 #include "BKE_DerivedMesh.h"
49 #include "BKE_fcurve.h"
50 #include "BKE_animsys.h"
51 #include "BLI_path_util.h"
52 #include "BLI_fileops.h"
53 #include "ED_keyframing.h"
54 #ifdef WITH_BUILDINFO
55 extern char build_rev[];
56 #endif
57 }
58
59 #include "MEM_guardedalloc.h"
60
61 #include "BKE_blender.h" // version info
62 #include "BKE_scene.h"
63 #include "BKE_global.h"
64 #include "BKE_main.h"
65 #include "BKE_material.h"
66 #include "BKE_action.h" // pose functions
67 #include "BKE_armature.h"
68 #include "BKE_image.h"
69 #include "BKE_utildefines.h"
70 #include "BKE_object.h"
71
72 #include "BLI_math.h"
73 #include "BLI_string.h"
74 #include "BLI_listbase.h"
75
76 #include "RNA_access.h"
77
78 #include "COLLADASWAsset.h"
79 #include "COLLADASWLibraryVisualScenes.h"
80 #include "COLLADASWNode.h"
81 #include "COLLADASWSource.h"
82 #include "COLLADASWInstanceGeometry.h"
83 #include "COLLADASWInputList.h"
84 #include "COLLADASWPrimitves.h"
85 #include "COLLADASWVertices.h"
86 #include "COLLADASWLibraryAnimations.h"
87 #include "COLLADASWLibraryImages.h"
88 #include "COLLADASWLibraryEffects.h"
89 #include "COLLADASWImage.h"
90 #include "COLLADASWEffectProfile.h"
91 #include "COLLADASWColorOrTexture.h"
92 #include "COLLADASWParamTemplate.h"
93 #include "COLLADASWParamBase.h"
94 #include "COLLADASWSurfaceInitOption.h"
95 #include "COLLADASWSampler.h"
96 #include "COLLADASWScene.h"
97 #include "COLLADASWTechnique.h"
98 #include "COLLADASWTexture.h"
99 #include "COLLADASWLibraryMaterials.h"
100 #include "COLLADASWBindMaterial.h"
101 #include "COLLADASWInstanceCamera.h"
102 #include "COLLADASWInstanceLight.h"
103 #include "COLLADASWConstants.h"
104 #include "COLLADASWLibraryControllers.h"
105 #include "COLLADASWInstanceController.h"
106 #include "COLLADASWInstanceNode.h"
107 #include "COLLADASWBaseInputElement.h"
108
109 #include "collada_internal.h"
110 #include "DocumentExporter.h"
111 #include "ExportSettings.h"
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 "EffectExporter.h"
122 #include "GeometryExporter.h"
123 #include "ImageExporter.h"
124 #include "LightExporter.h"
125 #include "MaterialExporter.h"
126
127 #include <vector>
128 #include <algorithm> // std::find
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(const ExportSettings *export_settings) : export_settings(export_settings) {}
148
149 // TODO: it would be better to instantiate animations rather than create a new one per object
150 // COLLADA allows this through multiple <channel>s in <animation>.
151 // For this to work, we need to know objects that use a certain action.
152
153 void DocumentExporter::exportCurrentScene(Scene *sce)
154 {
155         PointerRNA sceneptr, unit_settings;
156         PropertyRNA *system; /* unused , *scale; */
157
158         clear_global_id_map();
159         
160         COLLADABU::NativeString native_filename =
161                 COLLADABU::NativeString(std::string(this->export_settings->filepath));
162         COLLADASW::StreamWriter sw(native_filename);
163
164         // open <collada>
165         sw.startDocument();
166
167         // <asset>
168         COLLADASW::Asset asset(&sw);
169
170         RNA_id_pointer_create(&(sce->id), &sceneptr);
171         unit_settings = RNA_pointer_get(&sceneptr, "unit_settings");
172         system = RNA_struct_find_property(&unit_settings, "system");
173         //scale = RNA_struct_find_property(&unit_settings, "scale_length");
174
175         std::string unitname = "meter";
176         float linearmeasure = RNA_float_get(&unit_settings, "scale_length");
177
178         switch(RNA_property_enum_get(&unit_settings, system)) {
179                 case USER_UNIT_NONE:
180                 case USER_UNIT_METRIC:
181                         if(linearmeasure == 0.001f) {
182                                 unitname = "millimeter";
183                         }
184                         else if(linearmeasure == 0.01f) {
185                                 unitname = "centimeter";
186                         }
187                         else if(linearmeasure == 0.1f) {
188                                 unitname = "decimeter";
189                         }
190                         else if(linearmeasure == 1.0f) {
191                                 unitname = "meter";
192                         }
193                         else if(linearmeasure == 1000.0f) {
194                                 unitname = "kilometer";
195                         }
196                         break;
197                 case USER_UNIT_IMPERIAL:
198                         if(linearmeasure == 0.0254f) {
199                                 unitname = "inch";
200                         }
201                         else if(linearmeasure == 0.3048f) {
202                                 unitname = "foot";
203                         }
204                         else if(linearmeasure == 0.9144f) {
205                                 unitname = "yard";
206                         }
207                         break;
208                 default:
209                         break;
210         }
211
212         asset.setUnit(unitname, linearmeasure);
213         asset.setUpAxisType(COLLADASW::Asset::Z_UP);
214         if(U.author[0] != '\0') {
215                 asset.getContributor().mAuthor = U.author;
216         }
217         else {
218                 asset.getContributor().mAuthor = "Blender User";
219         }
220 #ifdef WITH_BUILDINFO
221         char version_buf[128];
222         sprintf(version_buf, "Blender %d.%02d.%d r%s", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION, build_rev);
223         asset.getContributor().mAuthoringTool = version_buf;
224 #else
225         asset.getContributor().mAuthoringTool = "Blender 2.5x";
226 #endif
227         asset.add();
228         
229         // <library_cameras>
230         if(has_object_type(sce, OB_CAMERA)) {
231                 CamerasExporter ce(&sw, this->export_settings);
232                 ce.exportCameras(sce);
233         }
234         
235         // <library_lights>
236         if(has_object_type(sce, OB_LAMP)) {
237                 LightsExporter le(&sw, this->export_settings);
238                 le.exportLights(sce);
239         }
240
241         // <library_images>
242         ImagesExporter ie(&sw, this->export_settings);
243         ie.exportImages(sce);
244         
245         // <library_effects>
246         EffectsExporter ee(&sw, this->export_settings);
247         ee.exportEffects(sce);
248         
249         // <library_materials>
250         MaterialsExporter me(&sw, this->export_settings);
251         me.exportMaterials(sce);
252
253         // <library_geometries>
254         if(has_object_type(sce, OB_MESH)) {
255                 GeometryExporter ge(&sw, this->export_settings);
256                 ge.exportGeom(sce);
257         }
258
259         // <library_animations>
260         AnimationExporter ae(&sw, this->export_settings);
261         ae.exportAnimations(sce);
262
263         // <library_controllers>
264         ArmatureExporter arm_exporter(&sw, this->export_settings);
265         if(has_object_type(sce, OB_ARMATURE)) {
266                 arm_exporter.export_controllers(sce);
267         }
268
269         // <library_visual_scenes>
270         SceneExporter se(&sw, &arm_exporter, this->export_settings);
271         se.exportScene(sce);
272         
273         // <scene>
274         std::string scene_name(translate_id(id_name(sce)));
275         COLLADASW::Scene scene(&sw, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING,
276                                                                                            scene_name));
277         scene.add();
278         
279         // close <Collada>
280         sw.endDocument();
281
282 }
283
284 void DocumentExporter::exportScenes(const char* filename)
285 {
286 }
287
288 /*
289
290 NOTES:
291
292 * AnimationExporter::sample_animation enables all curves on armature, this is undesirable for a user
293
294  */