fix: Collada build warning about inconsistent usage of Camera structure/class
[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_utildefines.h"
103 #include "BKE_object.h"
104
105 #include "BLI_math.h"
106 #include "BLI_string.h"
107 #include "BLI_listbase.h"
108
109 #include "RNA_access.h"
110 }
111
112 #include "collada_internal.h"
113 #include "DocumentExporter.h"
114 #include "ExportSettings.h"
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 // TODO: it would be better to instantiate animations rather than create a new one per object
151 // COLLADA allows this through multiple <channel>s in <animation>.
152 // For this to work, we need to know objects that use a certain action.
153
154 void DocumentExporter::exportCurrentScene(Scene *sce)
155 {
156         PointerRNA sceneptr, unit_settings;
157         PropertyRNA *system; /* unused , *scale; */
158
159         clear_global_id_map();
160         
161         COLLADABU::NativeString native_filename =
162                 COLLADABU::NativeString(std::string(this->export_settings->filepath));
163         COLLADASW::StreamWriter sw(native_filename);
164
165         // open <collada>
166         sw.startDocument();
167
168         // <asset>
169         COLLADASW::Asset asset(&sw);
170
171         RNA_id_pointer_create(&(sce->id), &sceneptr);
172         unit_settings = RNA_pointer_get(&sceneptr, "unit_settings");
173         system = RNA_struct_find_property(&unit_settings, "system");
174         //scale = RNA_struct_find_property(&unit_settings, "scale_length");
175
176         std::string unitname = "meter";
177         float linearmeasure = RNA_float_get(&unit_settings, "scale_length");
178
179         switch (RNA_property_enum_get(&unit_settings, system)) {
180                 case USER_UNIT_NONE:
181                 case USER_UNIT_METRIC:
182                         if (linearmeasure == 0.001f) {
183                                 unitname = "millimeter";
184                         }
185                         else if (linearmeasure == 0.01f) {
186                                 unitname = "centimeter";
187                         }
188                         else if (linearmeasure == 0.1f) {
189                                 unitname = "decimeter";
190                         }
191                         else if (linearmeasure == 1.0f) {
192                                 unitname = "meter";
193                         }
194                         else if (linearmeasure == 1000.0f) {
195                                 unitname = "kilometer";
196                         }
197                         break;
198                 case USER_UNIT_IMPERIAL:
199                         if (linearmeasure == 0.0254f) {
200                                 unitname = "inch";
201                         }
202                         else if (linearmeasure == 0.3048f) {
203                                 unitname = "foot";
204                         }
205                         else if (linearmeasure == 0.9144f) {
206                                 unitname = "yard";
207                         }
208                         break;
209                 default:
210                         break;
211         }
212
213         asset.setUnit(unitname, linearmeasure);
214         asset.setUpAxisType(COLLADASW::Asset::Z_UP);
215         if (U.author[0] != '\0') {
216                 asset.getContributor().mAuthor = U.author;
217         }
218         else {
219                 asset.getContributor().mAuthor = "Blender User";
220         }
221         char version_buf[128];
222 #ifdef WITH_BUILDINFO
223         sprintf(version_buf, "Blender %d.%02d.%d r%s", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION, build_rev);
224 #else
225         sprintf(version_buf, "Blender %d.%02d.%d", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
226 #endif
227         asset.getContributor().mAuthoringTool = version_buf;
228         asset.add();
229         
230         // <library_cameras>
231         if (has_object_type(sce, OB_CAMERA)) {
232                 CamerasExporter ce(&sw, this->export_settings);
233                 ce.exportCameras(sce);
234         }
235         
236         // <library_lights>
237         if (has_object_type(sce, OB_LAMP)) {
238                 LightsExporter le(&sw, this->export_settings);
239                 le.exportLights(sce);
240         }
241
242         // <library_images>
243         ImagesExporter ie(&sw, this->export_settings);
244         ie.exportImages(sce);
245         
246         // <library_effects>
247         EffectsExporter ee(&sw, this->export_settings);
248         ee.exportEffects(sce);
249         
250         // <library_materials>
251         MaterialsExporter me(&sw, this->export_settings);
252         me.exportMaterials(sce);
253
254         // <library_geometries>
255         if (has_object_type(sce, OB_MESH)) {
256                 GeometryExporter ge(&sw, this->export_settings);
257                 ge.exportGeom(sce);
258         }
259
260         // <library_animations>
261         AnimationExporter ae(&sw, this->export_settings);
262         ae.exportAnimations(sce);
263
264         // <library_controllers>
265         ArmatureExporter arm_exporter(&sw, this->export_settings);
266         if (this->export_settings->include_armatures) {
267                 if (has_object_type(sce, OB_ARMATURE)) {
268                         arm_exporter.export_controllers(sce);
269                 }
270         }
271
272         // <library_visual_scenes>
273         SceneExporter se(&sw, &arm_exporter, this->export_settings);
274         se.exportScene(sce);
275         
276         // <scene>
277         std::string scene_name(translate_id(id_name(sce)));
278         COLLADASW::Scene scene(&sw, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING,
279                                                                                            scene_name));
280         scene.add();
281         
282         // close <Collada>
283         sw.endDocument();
284
285 }
286
287 void DocumentExporter::exportScenes(const char* filename)
288 {
289 }
290
291 /*
292
293 NOTES:
294
295 * AnimationExporter::sample_animation enables all curves on armature, this is undesirable for a user
296
297  */