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 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/io/io_collada.c
31 #include "DNA_scene_types.h"
33 #include "BLF_translation.h"
35 #include "BLI_blenlib.h"
36 #include "BLI_utildefines.h"
38 #include "BKE_context.h"
39 #include "BKE_global.h"
41 #include "BKE_report.h"
43 #include "ED_screen.h"
44 #include "ED_object.h"
46 #include "RNA_access.h"
47 #include "RNA_define.h"
49 #include "UI_interface.h"
50 #include "UI_resources.h"
55 #include "../../collada/collada.h"
57 #include "io_collada.h"
59 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
61 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
62 char filepath[FILE_MAX];
64 if (G.main->name[0] == 0)
65 BLI_strncpy(filepath, "untitled", sizeof(filepath));
67 BLI_strncpy(filepath, G.main->name, sizeof(filepath));
69 BLI_replace_extension(filepath, sizeof(filepath), ".dae");
70 RNA_string_set(op->ptr, "filepath", filepath);
73 WM_event_add_fileselect(C, op);
75 return OPERATOR_RUNNING_MODAL;
78 /* function used for WM_OT_save_mainfile too */
79 static int wm_collada_export_exec(bContext *C, wmOperator *op)
81 char filepath[FILE_MAX];
86 int include_armatures;
87 int include_shapekeys;
88 int deform_bones_only;
90 int include_uv_textures;
91 int include_material_textures;
92 int use_texture_copies;
95 int use_object_instantiation;
99 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
100 BKE_report(op->reports, RPT_ERROR, "No filename given");
101 return OPERATOR_CANCELLED;
104 RNA_string_get(op->ptr, "filepath", filepath);
105 BLI_ensure_extension(filepath, sizeof(filepath), ".dae");
108 apply_modifiers = RNA_boolean_get(op->ptr, "apply_modifiers");
109 export_mesh_type = RNA_enum_get(op->ptr, "export_mesh_type_selection");
110 selected = RNA_boolean_get(op->ptr, "selected");
111 include_children = RNA_boolean_get(op->ptr, "include_children");
112 include_armatures = RNA_boolean_get(op->ptr, "include_armatures");
113 include_shapekeys = RNA_boolean_get(op->ptr, "include_shapekeys");
114 deform_bones_only = RNA_boolean_get(op->ptr, "deform_bones_only");
116 include_uv_textures = RNA_boolean_get(op->ptr, "include_uv_textures");
117 include_material_textures= RNA_boolean_get(op->ptr, "include_material_textures");
118 use_texture_copies = RNA_boolean_get(op->ptr, "use_texture_copies");
119 active_uv_only = RNA_boolean_get(op->ptr, "active_uv_only");
121 use_object_instantiation = RNA_boolean_get(op->ptr, "use_object_instantiation");
122 sort_by_name = RNA_boolean_get(op->ptr, "sort_by_name");
123 second_life = RNA_boolean_get(op->ptr, "second_life");
125 /* get editmode results */
126 ED_object_exit_editmode(C, 0); /* 0 = does not exit editmode */
128 if (collada_export(CTX_data_scene(C),
140 include_material_textures,
143 use_object_instantiation,
146 return OPERATOR_FINISHED;
149 BKE_report(op->reports, RPT_WARNING, "Export file not created");
150 return OPERATOR_CANCELLED;
154 static void uiCollada_exportSettings(uiLayout *layout, PointerRNA *imfptr)
156 uiLayout *box, *row, *col, *split;
158 /* Export Options: */
159 box = uiLayoutBox(layout);
160 row = uiLayoutRow(box, FALSE);
161 uiItemL(row, IFACE_("Export Data Options:"), ICON_MESH_DATA);
163 row = uiLayoutRow(box, FALSE);
164 split = uiLayoutSplit(row, 0.6f, UI_LAYOUT_ALIGN_RIGHT);
165 col = uiLayoutColumn(split, FALSE);
166 uiItemR(col, imfptr, "apply_modifiers", 0, NULL, ICON_NONE);
167 col = uiLayoutColumn(split, FALSE);
168 uiItemR(col, imfptr, "export_mesh_type_selection", 0, "", ICON_NONE);
169 uiLayoutSetEnabled(col, RNA_boolean_get(imfptr, "apply_modifiers"));
171 row = uiLayoutRow(box, FALSE);
172 uiItemR(row, imfptr, "selected", 0, NULL, ICON_NONE);
174 row = uiLayoutRow(box, FALSE);
175 uiItemR(row, imfptr, "include_children", 0, NULL, ICON_NONE);
176 uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
178 row = uiLayoutRow(box, FALSE);
179 uiItemR(row, imfptr, "include_armatures", 0, NULL, ICON_NONE);
180 uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
182 row = uiLayoutRow(box, FALSE);
183 uiItemR(row, imfptr, "include_shapekeys", 0, NULL, ICON_NONE);
184 uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
186 /* Texture options */
187 box = uiLayoutBox(layout);
188 row = uiLayoutRow(box, FALSE);
189 uiItemL(row, IFACE_("Texture Options:"), ICON_TEXTURE_DATA);
191 row = uiLayoutRow(box, FALSE);
192 uiItemR(row, imfptr, "active_uv_only", 0, NULL, ICON_NONE);
194 row = uiLayoutRow(box, FALSE);
195 uiItemR(row, imfptr, "include_uv_textures", 0, NULL, ICON_NONE);
197 row = uiLayoutRow(box, FALSE);
198 uiItemR(row, imfptr, "include_material_textures", 0, NULL, ICON_NONE);
200 row = uiLayoutRow(box, FALSE);
201 uiItemR(row, imfptr, "use_texture_copies", 1, NULL, ICON_NONE);
204 /* Armature options */
205 box = uiLayoutBox(layout);
206 row = uiLayoutRow(box, FALSE);
207 uiItemL(row, IFACE_("Armature Options:"), ICON_ARMATURE_DATA);
209 row = uiLayoutRow(box, FALSE);
210 uiItemR(row, imfptr, "deform_bones_only", 0, NULL, ICON_NONE);
211 row = uiLayoutRow(box, FALSE);
212 uiItemR(row, imfptr, "second_life", 0, NULL, ICON_NONE);
214 /* Collada options: */
215 box = uiLayoutBox(layout);
216 row = uiLayoutRow(box, FALSE);
217 uiItemL(row, IFACE_("Collada Options:"), ICON_MODIFIER);
219 row = uiLayoutRow(box, FALSE);
220 uiItemR(row, imfptr, "use_object_instantiation", 0, NULL, ICON_NONE);
221 row = uiLayoutRow(box, FALSE);
222 uiItemR(row, imfptr, "sort_by_name", 0, NULL, ICON_NONE);
226 static void wm_collada_export_draw(bContext *UNUSED(C), wmOperator *op)
230 RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
231 uiCollada_exportSettings(op->layout, &ptr);
234 void WM_OT_collada_export(wmOperatorType *ot)
236 static EnumPropertyItem prop_bc_export_mesh_type[] = {
237 {BC_MESH_TYPE_VIEW, "view", 0, "View", "Apply modifier's view settings"},
238 {BC_MESH_TYPE_RENDER, "render", 0, "Render", "Apply modifier's render settings"},
239 {0, NULL, 0, NULL, NULL}
242 ot->name = "Export COLLADA";
243 ot->description = "Save a Collada file";
244 ot->idname = "WM_OT_collada_export";
246 ot->invoke = wm_collada_export_invoke;
247 ot->exec = wm_collada_export_exec;
248 ot->poll = WM_operator_winactive;
250 ot->flag |= OPTYPE_PRESET;
252 ot->ui = wm_collada_export_draw;
254 WM_operator_properties_filesel(ot, FOLDERFILE | COLLADAFILE, FILE_BLENDER, FILE_SAVE,
255 WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
257 RNA_def_boolean(ot->srna,
258 "apply_modifiers", 0, "Apply Modifiers",
259 "Apply modifiers to exported mesh (non destructive))");
261 RNA_def_int(ot->srna, "export_mesh_type", 0, INT_MIN, INT_MAX,
262 "Resolution", "Modifier resolution for export", INT_MIN, INT_MAX);
264 RNA_def_enum(ot->srna, "export_mesh_type_selection", prop_bc_export_mesh_type, 0,
265 "Resolution", "Modifier resolution for export");
267 RNA_def_boolean(ot->srna, "selected", 0, "Selection Only",
268 "Export only selected elements");
270 RNA_def_boolean(ot->srna, "include_children", 0, "Include Children",
271 "Export all children of selected objects (even if not selected)");
273 RNA_def_boolean(ot->srna, "include_armatures", 0, "Include Armatures",
274 "Export related armatures (even if not selected)");
276 RNA_def_boolean(ot->srna, "include_shapekeys", 1, "Include Shape Keys",
277 "Export all Shape Keys from Mesh Objects");
279 RNA_def_boolean(ot->srna, "deform_bones_only", 0, "Deform Bones only",
280 "Only export deforming bones with armatures");
283 RNA_def_boolean(ot->srna, "active_uv_only", 0, "Only Active UV layer",
284 "Export textures assigned to the object UV maps");
286 RNA_def_boolean(ot->srna, "include_uv_textures", 0, "Include UV Textures",
287 "Export textures assigned to the object UV maps");
289 RNA_def_boolean(ot->srna, "include_material_textures", 0, "Include Material Textures",
290 "Export textures assigned to the object Materials");
292 RNA_def_boolean(ot->srna, "use_texture_copies", 1, "Copy",
293 "Copy textures to same folder where the .dae file is exported");
296 RNA_def_boolean(ot->srna, "use_object_instantiation", 1, "Use Object Instances",
297 "Instantiate multiple Objects from same Data");
299 RNA_def_boolean(ot->srna, "sort_by_name", 0, "Sort by Object name",
300 "Sort exported data by Object name");
302 RNA_def_boolean(ot->srna, "second_life", 0, "Export for Second Life",
303 "Compatibility mode for Second Life");
307 /* function used for WM_OT_save_mainfile too */
308 static int wm_collada_import_exec(bContext *C, wmOperator *op)
310 char filename[FILE_MAX];
313 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
314 BKE_report(op->reports, RPT_ERROR, "No filename given");
315 return OPERATOR_CANCELLED;
319 import_units = RNA_boolean_get(op->ptr, "import_units");
321 RNA_string_get(op->ptr, "filepath", filename);
322 if (collada_import( C,
325 return OPERATOR_FINISHED;
328 BKE_report(op->reports, RPT_ERROR, "Errors found during parsing COLLADA document (see console for details)");
329 return OPERATOR_CANCELLED;
333 static void uiCollada_importSettings(uiLayout *layout, PointerRNA *imfptr)
337 /* Import Options: */
338 box = uiLayoutBox(layout);
339 row = uiLayoutRow(box, FALSE);
340 uiItemL(row, IFACE_("Import Data Options:"), ICON_MESH_DATA);
342 row = uiLayoutRow(box, FALSE);
343 uiItemR(row, imfptr, "import_units", 0, NULL, ICON_NONE);
346 static void wm_collada_import_draw(bContext *UNUSED(C), wmOperator *op)
350 RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
351 uiCollada_importSettings(op->layout, &ptr);
354 void WM_OT_collada_import(wmOperatorType *ot)
356 ot->name = "Import COLLADA";
357 ot->description = "Load a Collada file";
358 ot->idname = "WM_OT_collada_import";
360 ot->invoke = WM_operator_filesel;
361 ot->exec = wm_collada_import_exec;
362 ot->poll = WM_operator_winactive;
364 //ot->flag |= OPTYPE_PRESET;
366 ot->ui = wm_collada_import_draw;
368 WM_operator_properties_filesel(ot, FOLDERFILE | COLLADAFILE, FILE_BLENDER, FILE_OPENFILE,
369 WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
371 RNA_def_boolean(ot->srna, "import_units", 0, "Import Units",
372 "If enabled use Units as defined in Collada Import, else keep Blender's current Units settings");