0e6b8f7752822ba862270fe1938ceeef0297f27f
[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 static int wm_collada_export_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
58 {       
59         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
60                 char filepath[FILE_MAX];
61
62                 if (G.main->name[0] == 0)
63                         BLI_strncpy(filepath, "untitled", sizeof(filepath));
64                 else
65                         BLI_strncpy(filepath, G.main->name, sizeof(filepath));
66
67                 BLI_replace_extension(filepath, sizeof(filepath), ".dae");
68                 RNA_string_set(op->ptr, "filepath", filepath);
69         }
70
71         WM_event_add_fileselect(C, op);
72
73         return OPERATOR_RUNNING_MODAL;
74 }
75
76 /* function used for WM_OT_save_mainfile too */
77 static int wm_collada_export_exec(bContext *C, wmOperator *op)
78 {
79         char filepath[FILE_MAX];
80         int apply_modifiers;
81         int export_mesh_type;
82         int selected;
83         int include_children;
84         int include_armatures;
85         int deform_bones_only;
86
87         int include_uv_textures;
88         int include_material_textures;
89         int use_texture_copies;
90         int active_uv_only;
91
92         int use_object_instantiation;
93         int sort_by_name;
94         int second_life; 
95
96         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
97                 BKE_report(op->reports, RPT_ERROR, "No filename given");
98                 return OPERATOR_CANCELLED;
99         }
100
101         RNA_string_get(op->ptr, "filepath", filepath);
102         BLI_ensure_extension(filepath, sizeof(filepath), ".dae");
103
104         /* Options panel */
105         apply_modifiers          = RNA_boolean_get(op->ptr, "apply_modifiers");
106         export_mesh_type         = RNA_enum_get(op->ptr,    "export_mesh_type_selection");
107         selected                 = RNA_boolean_get(op->ptr, "selected");
108         include_children         = RNA_boolean_get(op->ptr, "include_children");
109         include_armatures        = RNA_boolean_get(op->ptr, "include_armatures");
110         deform_bones_only        = RNA_boolean_get(op->ptr, "deform_bones_only");
111
112         include_uv_textures      = RNA_boolean_get(op->ptr, "include_uv_textures");
113         include_material_textures= RNA_boolean_get(op->ptr, "include_material_textures");
114         use_texture_copies       = RNA_boolean_get(op->ptr, "use_texture_copies");
115         active_uv_only           = RNA_boolean_get(op->ptr, "active_uv_only");
116
117         use_object_instantiation = RNA_boolean_get(op->ptr, "use_object_instantiation");
118         sort_by_name             = RNA_boolean_get(op->ptr, "sort_by_name");
119         second_life              = RNA_boolean_get(op->ptr, "second_life");
120
121         /* get editmode results */
122         ED_object_exit_editmode(C, 0);  /* 0 = does not exit editmode */
123
124         if (collada_export(
125                 CTX_data_scene(C),
126                 filepath,
127                 apply_modifiers,
128                         export_mesh_type,
129                 selected,
130                 include_children,
131                 include_armatures,
132                 deform_bones_only,
133
134                         active_uv_only,
135                         include_uv_textures,
136                         include_material_textures,
137                         use_texture_copies,
138
139                 use_object_instantiation,
140                 sort_by_name,
141                 second_life)) {
142                 return OPERATOR_FINISHED;
143         }
144         else {
145                 return OPERATOR_CANCELLED;
146         }
147 }
148
149 void uiCollada_exportSettings(uiLayout *layout, PointerRNA *imfptr)
150 {
151         uiLayout *box, *row, *col, *split;
152
153         /* Export Options: */
154         box = uiLayoutBox(layout);
155         row = uiLayoutRow(box, FALSE);
156         uiItemL(row, IFACE_("Export Data Options:"), ICON_MESH_DATA);
157
158         row = uiLayoutRow(box, FALSE);
159         split = uiLayoutSplit(row, 0.6f, UI_LAYOUT_ALIGN_RIGHT);
160         col   = uiLayoutColumn(split, FALSE);
161         uiItemR(col, imfptr, "apply_modifiers", 0, NULL, ICON_NONE);
162         col   = uiLayoutColumn(split, FALSE);
163         uiItemR(col, imfptr, "export_mesh_type_selection", 0, "", ICON_NONE);
164         uiLayoutSetEnabled(col, RNA_boolean_get(imfptr, "apply_modifiers"));
165
166         row = uiLayoutRow(box, FALSE);
167         uiItemR(row, imfptr, "selected", 0, NULL, ICON_NONE);
168
169         row = uiLayoutRow(box, FALSE);
170         uiItemR(row, imfptr, "include_children", 0, NULL, ICON_NONE);
171         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
172
173         row = uiLayoutRow(box, FALSE);
174         uiItemR(row, imfptr, "include_armatures", 0, NULL, ICON_NONE);
175         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
176
177         /* Texture options */
178         box = uiLayoutBox(layout);
179         row = uiLayoutRow(box, FALSE);
180         uiItemL(row, IFACE_("Texture Options:"), ICON_TEXTURE_DATA);
181
182         row = uiLayoutRow(box, FALSE);
183         uiItemR(row, imfptr, "active_uv_only", 0, NULL, ICON_NONE);
184
185         row = uiLayoutRow(box, FALSE);
186         uiItemR(row, imfptr, "include_uv_textures", 0, NULL, ICON_NONE);
187
188         row = uiLayoutRow(box, FALSE);
189         uiItemR(row, imfptr, "include_material_textures", 0, NULL, ICON_NONE);
190
191         row = uiLayoutRow(box, FALSE);
192         uiItemR(row, imfptr, "use_texture_copies", 1, NULL, ICON_NONE);
193
194
195         /* Armature options */
196         box = uiLayoutBox(layout);
197         row = uiLayoutRow(box, FALSE);
198         uiItemL(row, IFACE_("Armature Options:"), ICON_ARMATURE_DATA);
199
200         row = uiLayoutRow(box, FALSE);
201         uiItemR(row, imfptr, "deform_bones_only", 0, NULL, ICON_NONE);
202         row = uiLayoutRow(box, FALSE);
203         uiItemR(row, imfptr, "second_life", 0, NULL, ICON_NONE);
204
205         /* Collada options: */
206         box = uiLayoutBox(layout);
207         row = uiLayoutRow(box, FALSE);
208         uiItemL(row, IFACE_("Collada Options:"), ICON_MODIFIER);
209
210         row = uiLayoutRow(box, FALSE);
211         uiItemR(row, imfptr, "use_object_instantiation", 0, NULL, ICON_NONE);
212         row = uiLayoutRow(box, FALSE);
213         uiItemR(row, imfptr, "sort_by_name", 0, NULL, ICON_NONE);
214
215 }
216
217 static void wm_collada_export_draw(bContext *UNUSED(C), wmOperator *op)
218 {
219         PointerRNA ptr;
220
221         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
222         uiCollada_exportSettings(op->layout, &ptr);
223 }
224
225 void WM_OT_collada_export(wmOperatorType *ot)
226 {
227         static EnumPropertyItem prop_bc_export_mesh_type[] = {
228                 {BC_MESH_TYPE_VIEW, "view", 0, "View", "Apply modifier's view settings"},
229                 {BC_MESH_TYPE_RENDER, "render", 0, "Render", "Apply modifier's render settings"},
230                 {0, NULL, 0, NULL, NULL}
231         };
232
233         ot->name = "Export COLLADA";
234         ot->description = "Save a Collada file";
235         ot->idname = "WM_OT_collada_export";
236         
237         ot->invoke = wm_collada_export_invoke;
238         ot->exec = wm_collada_export_exec;
239         ot->poll = WM_operator_winactive;
240
241         ot->flag |= OPTYPE_PRESET;
242
243         ot->ui = wm_collada_export_draw;
244         
245         WM_operator_properties_filesel(ot, FOLDERFILE | COLLADAFILE, FILE_BLENDER, FILE_SAVE,
246                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
247
248         RNA_def_boolean(ot->srna,
249                         "apply_modifiers", 0, "Apply Modifiers",
250                         "Apply modifiers to exported mesh (non destructive))");
251
252         RNA_def_int(ot->srna, "export_mesh_type", 0, INT_MIN, INT_MAX,
253                     "Resolution", "Modifier resolution for export", INT_MIN, INT_MAX);
254
255         RNA_def_enum(ot->srna, "export_mesh_type_selection", prop_bc_export_mesh_type, 0,
256                      "Resolution", "Modifier resolution for export");
257
258         RNA_def_boolean(ot->srna, "selected", 0, "Selection Only",
259                         "Export only selected elements");
260
261         RNA_def_boolean(ot->srna, "include_children", 0, "Include Children",
262                         "Export all children of selected objects (even if not selected)");
263
264         RNA_def_boolean(ot->srna, "include_armatures", 0, "Include Armatures",
265                         "Export related armatures (even if not selected)");
266
267         RNA_def_boolean(ot->srna, "deform_bones_only", 0, "Deform Bones only",
268                         "Only export deforming bones with armatures");
269
270
271         RNA_def_boolean(ot->srna, "active_uv_only", 0, "Only Active UV layer",
272                         "Export textures assigned to the object UV maps");
273
274         RNA_def_boolean(ot->srna, "include_uv_textures", 0, "Include UV Textures",
275                         "Export textures assigned to the object UV maps");
276
277         RNA_def_boolean(ot->srna, "include_material_textures", 0, "Include Material Textures",
278                         "Export textures assigned to the object Materials");
279
280         RNA_def_boolean(ot->srna, "use_texture_copies", 1, "Copy",
281                         "Copy textures to same folder where the .dae file is exported");
282
283
284         RNA_def_boolean(ot->srna, "use_object_instantiation", 1, "Use Object Instances",
285                         "Instantiate multiple Objects from same Data");
286
287         RNA_def_boolean(ot->srna, "sort_by_name", 0, "Sort by Object name",
288                         "Sort exported data by Object name");
289
290         RNA_def_boolean(ot->srna, "second_life", 0, "Export for Second Life",
291                         "Compatibility mode for Second Life");
292 }
293
294
295 /* function used for WM_OT_save_mainfile too */
296 static int wm_collada_import_exec(bContext *C, wmOperator *op)
297 {
298         char filename[FILE_MAX];
299         
300         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
301                 BKE_report(op->reports, RPT_ERROR, "No filename given");
302                 return OPERATOR_CANCELLED;
303         }
304
305         RNA_string_get(op->ptr, "filepath", filename);
306         if (collada_import(C, filename)) return OPERATOR_FINISHED;
307         
308         BKE_report(op->reports, RPT_ERROR, "Errors found during parsing COLLADA document. Please see console for error log.");
309         
310         return OPERATOR_FINISHED;
311 }
312
313 void WM_OT_collada_import(wmOperatorType *ot)
314 {
315         ot->name = "Import COLLADA";
316         ot->description = "Load a Collada file";
317         ot->idname = "WM_OT_collada_import";
318         
319         ot->invoke = WM_operator_filesel;
320         ot->exec = wm_collada_import_exec;
321         ot->poll = WM_operator_winactive;
322         
323         WM_operator_properties_filesel(ot, FOLDERFILE | COLLADAFILE, FILE_BLENDER, FILE_OPENFILE,
324                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
325 }
326 #endif