2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed.
20 * ***** END GPL LICENSE BLOCK *****
23 /** \file blender/collada/DocumentExporter.cpp
31 #include <algorithm> // std::find
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"
67 #include "DNA_scene_types.h"
68 #include "DNA_object_types.h"
69 #include "DNA_collection_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_anim_types.h"
75 #include "DNA_action_types.h"
76 #include "DNA_curve_types.h"
77 #include "DNA_armature_types.h"
78 #include "DNA_modifier_types.h"
79 #include "DNA_userdef_types.h"
81 #include "BLI_path_util.h"
82 #include "BLI_fileops.h"
84 #include "BLI_string.h"
85 #include "BLI_listbase.h"
86 #include "BLI_utildefines.h"
88 #include "BKE_action.h" // pose functions
89 #include "BKE_animsys.h"
90 #include "BKE_armature.h"
91 #include "BKE_blender_version.h"
92 #include "BKE_customdata.h"
93 #include "BKE_fcurve.h"
94 #include "BKE_global.h"
95 #include "BKE_image.h"
97 #include "BKE_material.h"
98 #include "BKE_object.h"
99 #include "BKE_scene.h"
100 #include "BKE_appdir.h"
102 #include "ED_keyframing.h"
103 #ifdef WITH_BUILDINFO
104 extern char build_commit_date[];
105 extern char build_commit_time[];
106 extern char build_hash[];
109 #include "MEM_guardedalloc.h"
111 #include "RNA_access.h"
114 #include "collada_internal.h"
115 #include "collada_utils.h"
116 #include "DocumentExporter.h"
118 extern bool bc_has_object_type(LinkNode *export_set, short obtype);
120 // can probably go after refactor is complete
121 #include "InstanceWriter.h"
122 #include "TransformWriter.h"
124 #include "SceneExporter.h"
125 #include "ArmatureExporter.h"
126 #include "AnimationExporter.h"
127 #include "CameraExporter.h"
128 #include "ControllerExporter.h"
129 #include "EffectExporter.h"
130 #include "GeometryExporter.h"
131 #include "ImageExporter.h"
132 #include "LightExporter.h"
133 #include "MaterialExporter.h"
137 char *bc_CustomData_get_layer_name(const struct CustomData *data, int type, int n)
139 int layer_index = CustomData_get_layer_index(data, type);
140 if (layer_index < 0) return NULL;
142 return data->layers[layer_index + n].name;
145 char *bc_CustomData_get_active_layer_name(const CustomData *data, int type)
147 /* get the layer index of the active layer of type */
148 int layer_index = CustomData_get_active_layer_index(data, type);
149 if (layer_index < 0) return NULL;
151 return data->layers[layer_index].name;
154 DocumentExporter::DocumentExporter(BlenderContext &blender_context, const ExportSettings *export_settings) :
155 blender_context(blender_context),
156 export_settings(export_settings) {
159 static COLLADABU::NativeString make_temp_filepath(const char *name, const char *extension)
161 char tempfile[FILE_MAX];
162 const char *tempdir = BKE_tempdir_session();
168 BLI_make_file_string(NULL, tempfile, tempdir, name);
171 BLI_path_extension_ensure(tempfile, FILE_MAX, extension);
174 COLLADABU::NativeString native_filename =
175 COLLADABU::NativeString(tempfile, COLLADABU::NativeString::ENCODING_UTF8);
176 return native_filename;
179 // TODO: it would be better to instantiate animations rather than create a new one per object
180 // COLLADA allows this through multiple <channel>s in <animation>.
181 // For this to work, we need to know objects that use a certain action.
183 int DocumentExporter::exportCurrentScene()
185 Scene *sce = blender_context.get_scene();
186 bContext *C = blender_context.get_context();
188 PointerRNA sceneptr, unit_settings;
189 PropertyRNA *system; /* unused , *scale; */
191 clear_global_id_map();
193 COLLADABU::NativeString native_filename = make_temp_filepath(NULL, ".dae");
194 COLLADASW::StreamWriter *writer = new COLLADASW::StreamWriter(native_filename);
197 writer->startDocument();
200 COLLADASW::Asset asset(writer);
202 RNA_id_pointer_create(&(sce->id), &sceneptr);
203 unit_settings = RNA_pointer_get(&sceneptr, "unit_settings");
204 system = RNA_struct_find_property(&unit_settings, "system");
205 //scale = RNA_struct_find_property(&unit_settings, "scale_length");
207 std::string unitname = "meter";
208 float linearmeasure = RNA_float_get(&unit_settings, "scale_length");
210 switch (RNA_property_enum_get(&unit_settings, system)) {
212 case USER_UNIT_METRIC:
213 if (linearmeasure == 0.001f) {
214 unitname = "millimeter";
216 else if (linearmeasure == 0.01f) {
217 unitname = "centimeter";
219 else if (linearmeasure == 0.1f) {
220 unitname = "decimeter";
222 else if (linearmeasure == 1.0f) {
225 else if (linearmeasure == 1000.0f) {
226 unitname = "kilometer";
229 case USER_UNIT_IMPERIAL:
230 if (linearmeasure == 0.0254f) {
233 else if (linearmeasure == 0.3048f) {
236 else if (linearmeasure == 0.9144f) {
244 asset.setUnit(unitname, linearmeasure);
245 asset.setUpAxisType(COLLADASW::Asset::Z_UP);
246 asset.getContributor().mAuthor = "Blender User";
247 char version_buf[128];
248 #ifdef WITH_BUILDINFO
249 BLI_snprintf(version_buf, sizeof(version_buf), "Blender %d.%02d.%d commit date:%s, commit time:%s, hash:%s",
250 BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION,
251 build_commit_date, build_commit_time, build_hash);
253 BLI_snprintf(version_buf, sizeof(version_buf), "Blender %d.%02d.%d",
254 BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION);
256 asset.getContributor().mAuthoringTool = version_buf;
259 LinkNode *export_set = this->export_settings->export_set;
261 if (bc_has_object_type(export_set, OB_CAMERA)) {
262 CamerasExporter ce(writer, this->export_settings);
263 ce.exportCameras(sce);
267 if (bc_has_object_type(export_set, OB_LAMP)) {
268 LightsExporter le(writer, this->export_settings);
269 le.exportLights(sce);
273 EffectsExporter ee(writer, this->export_settings, key_image_map);
274 ee.exportEffects(C, sce);
277 ImagesExporter ie(writer, this->export_settings, key_image_map);
278 ie.exportImages(sce);
280 // <library_materials>
281 MaterialsExporter me(writer, this->export_settings);
282 me.exportMaterials(sce);
284 // <library_geometries>
285 if (bc_has_object_type(export_set, OB_MESH)) {
286 GeometryExporter ge(blender_context, writer, this->export_settings);
290 // <library_controllers>
291 ArmatureExporter arm_exporter(blender_context, writer, this->export_settings);
292 ControllerExporter controller_exporter(blender_context, writer, this->export_settings);
293 if (bc_has_object_type(export_set, OB_ARMATURE) || this->export_settings->include_shapekeys)
295 controller_exporter.export_controllers();
298 // <library_visual_scenes>
300 SceneExporter se(blender_context, writer, &arm_exporter, this->export_settings);
302 if (this->export_settings->include_animations) {
303 // <library_animations>
304 AnimationExporter ae(blender_context, writer, this->export_settings);
305 ae.exportAnimations();
311 std::string scene_name(translate_id(id_name(sce)));
312 COLLADASW::Scene scene(writer, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING,
317 writer->endDocument();
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);
324 status = BLI_copy(native_filename.c_str(), this->export_settings->filepath);
325 BLI_delete(native_filename.c_str(), false, false);
330 void DocumentExporter::exportScenes(const char *filename)
337 * AnimationExporter::sample_animation enables all curves on armature, this is undesirable for a user