2004b18adf6ce6591f189420d0b8265e049a3143
[blender.git] / source / blender / editors / io / io_collada.c
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  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/io/io_collada.c
28  *  \ingroup collada
29  */
30 #ifdef WITH_COLLADA
31 #include "DNA_scene_types.h"
32
33 #include "BLF_translation.h"
34
35 #include "BLI_blenlib.h"
36 #include "BLI_utildefines.h"
37
38 #include "BKE_context.h"
39 #include "BKE_global.h"
40 #include "BKE_main.h"
41 #include "BKE_report.h"
42
43 #include "ED_screen.h"
44 #include "ED_object.h"
45
46 #include "RNA_access.h"
47 #include "RNA_define.h"
48
49 #include "UI_interface.h"
50 #include "UI_resources.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "../../collada/collada.h"
56
57 #include "io_collada.h"
58
59 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
60 {       
61         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
62                 char filepath[FILE_MAX];
63
64                 if (G.main->name[0] == 0)
65                         BLI_strncpy(filepath, "untitled", sizeof(filepath));
66                 else
67                         BLI_strncpy(filepath, G.main->name, sizeof(filepath));
68
69                 BLI_replace_extension(filepath, sizeof(filepath), ".dae");
70                 RNA_string_set(op->ptr, "filepath", filepath);
71         }
72
73         WM_event_add_fileselect(C, op);
74
75         return OPERATOR_RUNNING_MODAL;
76 }
77
78 /* function used for WM_OT_save_mainfile too */
79 static int wm_collada_export_exec(bContext *C, wmOperator *op)
80 {
81         char filepath[FILE_MAX];
82         int apply_modifiers;
83         int export_mesh_type;
84         int selected;
85         int include_children;
86         int include_armatures;
87         int include_shapekeys;
88         int deform_bones_only;
89
90         int include_uv_textures;
91         int include_material_textures;
92         int use_texture_copies;
93         int active_uv_only;
94
95         int use_object_instantiation;
96         int sort_by_name;
97         int second_life; 
98
99         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
100                 BKE_report(op->reports, RPT_ERROR, "No filename given");
101                 return OPERATOR_CANCELLED;
102         }
103
104         RNA_string_get(op->ptr, "filepath", filepath);
105         BLI_ensure_extension(filepath, sizeof(filepath), ".dae");
106
107         /* Options panel */
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");
115
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");
120
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");
124
125         /* get editmode results */
126         ED_object_exit_editmode(C, 0);  /* 0 = does not exit editmode */
127
128         if (collada_export(CTX_data_scene(C),
129                            filepath,
130                            apply_modifiers,
131                            export_mesh_type,
132                            selected,
133                            include_children,
134                            include_armatures,
135                            include_shapekeys,
136                            deform_bones_only,
137
138                            active_uv_only,
139                            include_uv_textures,
140                            include_material_textures,
141                            use_texture_copies,
142
143                            use_object_instantiation,
144                            sort_by_name,
145                            second_life)) {
146                 return OPERATOR_FINISHED;
147         }
148         else {
149                 BKE_report(op->reports, RPT_WARNING, "Export file not created");
150                 return OPERATOR_CANCELLED;
151         }
152 }
153
154 static void uiCollada_exportSettings(uiLayout *layout, PointerRNA *imfptr)
155 {
156         uiLayout *box, *row, *col, *split;
157
158         /* Export Options: */
159         box = uiLayoutBox(layout);
160         row = uiLayoutRow(box, FALSE);
161         uiItemL(row, IFACE_("Export Data Options:"), ICON_MESH_DATA);
162
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"));
170
171         row = uiLayoutRow(box, FALSE);
172         uiItemR(row, imfptr, "selected", 0, NULL, ICON_NONE);
173
174         row = uiLayoutRow(box, FALSE);
175         uiItemR(row, imfptr, "include_children", 0, NULL, ICON_NONE);
176         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
177
178         row = uiLayoutRow(box, FALSE);
179         uiItemR(row, imfptr, "include_armatures", 0, NULL, ICON_NONE);
180         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
181
182         row = uiLayoutRow(box, FALSE);
183         uiItemR(row, imfptr, "include_shapekeys", 0, NULL, ICON_NONE);
184         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
185
186         /* Texture options */
187         box = uiLayoutBox(layout);
188         row = uiLayoutRow(box, FALSE);
189         uiItemL(row, IFACE_("Texture Options:"), ICON_TEXTURE_DATA);
190
191         row = uiLayoutRow(box, FALSE);
192         uiItemR(row, imfptr, "active_uv_only", 0, NULL, ICON_NONE);
193
194         row = uiLayoutRow(box, FALSE);
195         uiItemR(row, imfptr, "include_uv_textures", 0, NULL, ICON_NONE);
196
197         row = uiLayoutRow(box, FALSE);
198         uiItemR(row, imfptr, "include_material_textures", 0, NULL, ICON_NONE);
199
200         row = uiLayoutRow(box, FALSE);
201         uiItemR(row, imfptr, "use_texture_copies", 1, NULL, ICON_NONE);
202
203
204         /* Armature options */
205         box = uiLayoutBox(layout);
206         row = uiLayoutRow(box, FALSE);
207         uiItemL(row, IFACE_("Armature Options:"), ICON_ARMATURE_DATA);
208
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);
213
214         /* Collada options: */
215         box = uiLayoutBox(layout);
216         row = uiLayoutRow(box, FALSE);
217         uiItemL(row, IFACE_("Collada Options:"), ICON_MODIFIER);
218
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);
223
224 }
225
226 static void wm_collada_export_draw(bContext *UNUSED(C), wmOperator *op)
227 {
228         PointerRNA ptr;
229
230         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
231         uiCollada_exportSettings(op->layout, &ptr);
232 }
233
234 void WM_OT_collada_export(wmOperatorType *ot)
235 {
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}
240         };
241
242         ot->name = "Export COLLADA";
243         ot->description = "Save a Collada file";
244         ot->idname = "WM_OT_collada_export";
245
246         ot->invoke = wm_collada_export_invoke;
247         ot->exec = wm_collada_export_exec;
248         ot->poll = WM_operator_winactive;
249
250         ot->flag |= OPTYPE_PRESET;
251
252         ot->ui = wm_collada_export_draw;
253
254         WM_operator_properties_filesel(ot, FOLDERFILE | COLLADAFILE, FILE_BLENDER, FILE_SAVE,
255                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
256
257         RNA_def_boolean(ot->srna,
258                         "apply_modifiers", 0, "Apply Modifiers",
259                         "Apply modifiers to exported mesh (non destructive))");
260
261         RNA_def_int(ot->srna, "export_mesh_type", 0, INT_MIN, INT_MAX,
262                     "Resolution", "Modifier resolution for export", INT_MIN, INT_MAX);
263
264         RNA_def_enum(ot->srna, "export_mesh_type_selection", prop_bc_export_mesh_type, 0,
265                      "Resolution", "Modifier resolution for export");
266
267         RNA_def_boolean(ot->srna, "selected", 0, "Selection Only",
268                         "Export only selected elements");
269
270         RNA_def_boolean(ot->srna, "include_children", 0, "Include Children",
271                         "Export all children of selected objects (even if not selected)");
272
273         RNA_def_boolean(ot->srna, "include_armatures", 0, "Include Armatures",
274                         "Export related armatures (even if not selected)");
275
276         RNA_def_boolean(ot->srna, "include_shapekeys", 1, "Include Shape Keys",
277                         "Export all Shape Keys from Mesh Objects");
278
279         RNA_def_boolean(ot->srna, "deform_bones_only", 0, "Deform Bones only",
280                         "Only export deforming bones with armatures");
281
282
283         RNA_def_boolean(ot->srna, "active_uv_only", 0, "Only Active UV layer",
284                         "Export textures assigned to the object UV maps");
285
286         RNA_def_boolean(ot->srna, "include_uv_textures", 0, "Include UV Textures",
287                         "Export textures assigned to the object UV maps");
288
289         RNA_def_boolean(ot->srna, "include_material_textures", 0, "Include Material Textures",
290                         "Export textures assigned to the object Materials");
291
292         RNA_def_boolean(ot->srna, "use_texture_copies", 1, "Copy",
293                         "Copy textures to same folder where the .dae file is exported");
294
295
296         RNA_def_boolean(ot->srna, "use_object_instantiation", 1, "Use Object Instances",
297                         "Instantiate multiple Objects from same Data");
298
299         RNA_def_boolean(ot->srna, "sort_by_name", 0, "Sort by Object name",
300                         "Sort exported data by Object name");
301
302         RNA_def_boolean(ot->srna, "second_life", 0, "Export for Second Life",
303                         "Compatibility mode for Second Life");
304 }
305
306
307 /* function used for WM_OT_save_mainfile too */
308 static int wm_collada_import_exec(bContext *C, wmOperator *op)
309 {
310         char filename[FILE_MAX];
311         int import_units;
312
313         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
314                 BKE_report(op->reports, RPT_ERROR, "No filename given");
315                 return OPERATOR_CANCELLED;
316         }
317
318         /* Options panel */
319         import_units = RNA_boolean_get(op->ptr, "import_units");
320
321         RNA_string_get(op->ptr, "filepath", filename);
322         if (collada_import( C,
323                                                 filename,
324                                                 import_units))  {
325                         return OPERATOR_FINISHED;
326         }
327         else {
328         BKE_report(op->reports, RPT_ERROR, "Errors found during parsing COLLADA document (see console for details)");
329         return OPERATOR_CANCELLED;
330         }
331 }
332
333 static void uiCollada_importSettings(uiLayout *layout, PointerRNA *imfptr)
334 {
335         uiLayout *box, *row;
336
337         /* Import Options: */
338         box = uiLayoutBox(layout);
339         row = uiLayoutRow(box, FALSE);
340         uiItemL(row, IFACE_("Import Data Options:"), ICON_MESH_DATA);
341
342         row = uiLayoutRow(box, FALSE);
343         uiItemR(row, imfptr, "import_units", 0, NULL, ICON_NONE);
344 }
345
346 static void wm_collada_import_draw(bContext *UNUSED(C), wmOperator *op)
347 {
348         PointerRNA ptr;
349
350         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
351         uiCollada_importSettings(op->layout, &ptr);
352 }
353
354 void WM_OT_collada_import(wmOperatorType *ot)
355 {
356         ot->name = "Import COLLADA";
357         ot->description = "Load a Collada file";
358         ot->idname = "WM_OT_collada_import";
359
360         ot->invoke = WM_operator_filesel;
361         ot->exec = wm_collada_import_exec;
362         ot->poll = WM_operator_winactive;
363
364         //ot->flag |= OPTYPE_PRESET;
365
366         ot->ui = wm_collada_import_draw;
367
368         WM_operator_properties_filesel(ot, FOLDERFILE | COLLADAFILE, FILE_BLENDER, FILE_OPENFILE,
369                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
370
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");
373
374 }
375 #endif