code cleanup: use const events for modal and invoke operators.
[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, const 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 triangulate;
96         int use_object_instantiation;
97         int sort_by_name;
98         int export_transformation_type;
99         int second_life; 
100
101         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
102                 BKE_report(op->reports, RPT_ERROR, "No filename given");
103                 return OPERATOR_CANCELLED;
104         }
105
106         RNA_string_get(op->ptr, "filepath", filepath);
107         BLI_ensure_extension(filepath, sizeof(filepath), ".dae");
108
109         /* Options panel */
110         apply_modifiers          = RNA_boolean_get(op->ptr, "apply_modifiers");
111         export_mesh_type         = RNA_enum_get(op->ptr,    "export_mesh_type_selection");
112         selected                 = RNA_boolean_get(op->ptr, "selected");
113         include_children         = RNA_boolean_get(op->ptr, "include_children");
114         include_armatures        = RNA_boolean_get(op->ptr, "include_armatures");
115         include_shapekeys        = RNA_boolean_get(op->ptr, "include_shapekeys");
116         deform_bones_only        = RNA_boolean_get(op->ptr, "deform_bones_only");
117
118         include_uv_textures      = RNA_boolean_get(op->ptr, "include_uv_textures");
119         include_material_textures = RNA_boolean_get(op->ptr, "include_material_textures");
120         use_texture_copies       = RNA_boolean_get(op->ptr, "use_texture_copies");
121         active_uv_only           = RNA_boolean_get(op->ptr, "active_uv_only");
122
123         triangulate                = RNA_boolean_get(op->ptr, "triangulate");
124         use_object_instantiation   = RNA_boolean_get(op->ptr, "use_object_instantiation");
125         sort_by_name               = RNA_boolean_get(op->ptr, "sort_by_name");
126         export_transformation_type = RNA_enum_get(op->ptr,    "export_transformation_type_selection");
127         second_life                = RNA_boolean_get(op->ptr, "second_life");
128
129         /* get editmode results */
130         ED_object_exit_editmode(C, 0);  /* 0 = does not exit editmode */
131
132         if (collada_export(CTX_data_scene(C),
133                            filepath,
134                            apply_modifiers,
135                            export_mesh_type,
136                            selected,
137                            include_children,
138                            include_armatures,
139                            include_shapekeys,
140                            deform_bones_only,
141
142                            active_uv_only,
143                            include_uv_textures,
144                            include_material_textures,
145                            use_texture_copies,
146
147                            triangulate,
148                            use_object_instantiation,
149                            sort_by_name,
150                            export_transformation_type,
151                            second_life))
152         {
153                 return OPERATOR_FINISHED;
154         }
155         else {
156                 BKE_report(op->reports, RPT_WARNING, "Export file not created");
157                 return OPERATOR_CANCELLED;
158         }
159 }
160
161 static void uiCollada_exportSettings(uiLayout *layout, PointerRNA *imfptr)
162 {
163         uiLayout *box, *row, *col, *split;
164
165         /* Export Options: */
166         box = uiLayoutBox(layout);
167         row = uiLayoutRow(box, FALSE);
168         uiItemL(row, IFACE_("Export Data Options:"), ICON_MESH_DATA);
169
170         row = uiLayoutRow(box, FALSE);
171         split = uiLayoutSplit(row, 0.6f, UI_LAYOUT_ALIGN_RIGHT);
172         col   = uiLayoutColumn(split, FALSE);
173         uiItemR(col, imfptr, "apply_modifiers", 0, NULL, ICON_NONE);
174         col   = uiLayoutColumn(split, FALSE);
175         uiItemR(col, imfptr, "export_mesh_type_selection", 0, "", ICON_NONE);
176         uiLayoutSetEnabled(col, RNA_boolean_get(imfptr, "apply_modifiers"));
177
178         row = uiLayoutRow(box, FALSE);
179         uiItemR(row, imfptr, "selected", 0, NULL, ICON_NONE);
180
181         row = uiLayoutRow(box, FALSE);
182         uiItemR(row, imfptr, "include_children", 0, NULL, ICON_NONE);
183         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
184
185         row = uiLayoutRow(box, FALSE);
186         uiItemR(row, imfptr, "include_armatures", 0, NULL, ICON_NONE);
187         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
188
189         row = uiLayoutRow(box, FALSE);
190         uiItemR(row, imfptr, "include_shapekeys", 0, NULL, ICON_NONE);
191         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
192
193         /* Texture options */
194         box = uiLayoutBox(layout);
195         row = uiLayoutRow(box, FALSE);
196         uiItemL(row, IFACE_("Texture Options:"), ICON_TEXTURE_DATA);
197
198         row = uiLayoutRow(box, FALSE);
199         uiItemR(row, imfptr, "active_uv_only", 0, NULL, ICON_NONE);
200
201         row = uiLayoutRow(box, FALSE);
202         uiItemR(row, imfptr, "include_uv_textures", 0, NULL, ICON_NONE);
203
204         row = uiLayoutRow(box, FALSE);
205         uiItemR(row, imfptr, "include_material_textures", 0, NULL, ICON_NONE);
206
207         row = uiLayoutRow(box, FALSE);
208         uiItemR(row, imfptr, "use_texture_copies", 1, NULL, ICON_NONE);
209
210
211         /* Armature options */
212         box = uiLayoutBox(layout);
213         row = uiLayoutRow(box, FALSE);
214         uiItemL(row, IFACE_("Armature Options:"), ICON_ARMATURE_DATA);
215
216         row = uiLayoutRow(box, FALSE);
217         uiItemR(row, imfptr, "deform_bones_only", 0, NULL, ICON_NONE);
218         row = uiLayoutRow(box, FALSE);
219         uiItemR(row, imfptr, "second_life", 0, NULL, ICON_NONE);
220
221         /* Collada options: */
222         box = uiLayoutBox(layout);
223         row = uiLayoutRow(box, FALSE);
224         uiItemL(row, IFACE_("Collada Options:"), ICON_MODIFIER);
225
226         row = uiLayoutRow(box, FALSE);
227         uiItemR(row, imfptr, "triangulate", 0, NULL, ICON_NONE);
228         row = uiLayoutRow(box, FALSE);
229         uiItemR(row, imfptr, "use_object_instantiation", 0, NULL, ICON_NONE);
230
231         row = uiLayoutRow(box, FALSE);
232         split = uiLayoutSplit(row, 0.6f, UI_LAYOUT_ALIGN_RIGHT);
233     uiItemL(split, IFACE_("Transformation Type"), ICON_NONE);
234         uiItemR(split, imfptr, "export_transformation_type_selection", 0, "", ICON_NONE);
235
236         row = uiLayoutRow(box, FALSE);
237         uiItemR(row, imfptr, "sort_by_name", 0, NULL, ICON_NONE);
238
239 }
240
241 static void wm_collada_export_draw(bContext *UNUSED(C), wmOperator *op)
242 {
243         PointerRNA ptr;
244
245         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
246         uiCollada_exportSettings(op->layout, &ptr);
247 }
248
249 void WM_OT_collada_export(wmOperatorType *ot)
250 {
251         static EnumPropertyItem prop_bc_export_mesh_type[] = {
252                 {BC_MESH_TYPE_VIEW, "view", 0, "View", "Apply modifier's view settings"},
253                 {BC_MESH_TYPE_RENDER, "render", 0, "Render", "Apply modifier's render settings"},
254                 {0, NULL, 0, NULL, NULL}
255         };
256
257         static EnumPropertyItem prop_bc_export_transformation_type[] = {
258                 {BC_TRANSFORMATION_TYPE_MATRIX, "matrix", 0, "Matrix", "Use <matrix> to specify transformations"},
259                 {BC_TRANSFORMATION_TYPE_TRANSROTLOC, "transrotloc", 0, "TransRotLoc", "Use <translate>, <rotate>, <scale> to specify transformations"},
260                 {BC_TRANSFORMATION_TYPE_BOTH, "both", 0, "Both", "Use <matrix> AND <translate>, <rotate>, <scale> to specify transformations"},
261                 {0, NULL, 0, NULL, NULL}
262         };
263
264         ot->name = "Export COLLADA";
265         ot->description = "Save a Collada file";
266         ot->idname = "WM_OT_collada_export";
267
268         ot->invoke = wm_collada_export_invoke;
269         ot->exec = wm_collada_export_exec;
270         ot->poll = WM_operator_winactive;
271
272         ot->flag |= OPTYPE_PRESET;
273
274         ot->ui = wm_collada_export_draw;
275
276         WM_operator_properties_filesel(ot, FOLDERFILE | COLLADAFILE, FILE_BLENDER, FILE_SAVE,
277                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
278
279         RNA_def_boolean(ot->srna,
280                         "apply_modifiers", 0, "Apply Modifiers",
281                         "Apply modifiers to exported mesh (non destructive))");
282
283         RNA_def_int(ot->srna, "export_mesh_type", 0, INT_MIN, INT_MAX,
284                     "Resolution", "Modifier resolution for export", INT_MIN, INT_MAX);
285
286         RNA_def_enum(ot->srna, "export_mesh_type_selection", prop_bc_export_mesh_type, 0,
287                      "Resolution", "Modifier resolution for export");
288
289         RNA_def_boolean(ot->srna, "selected", 0, "Selection Only",
290                         "Export only selected elements");
291
292         RNA_def_boolean(ot->srna, "include_children", 0, "Include Children",
293                         "Export all children of selected objects (even if not selected)");
294
295         RNA_def_boolean(ot->srna, "include_armatures", 0, "Include Armatures",
296                         "Export related armatures (even if not selected)");
297
298         RNA_def_boolean(ot->srna, "include_shapekeys", 1, "Include Shape Keys",
299                         "Export all Shape Keys from Mesh Objects");
300
301         RNA_def_boolean(ot->srna, "deform_bones_only", 0, "Deform Bones only",
302                         "Only export deforming bones with armatures");
303
304
305         RNA_def_boolean(ot->srna, "active_uv_only", 0, "Only Active UV layer",
306                         "Export textures assigned to the object UV maps");
307
308         RNA_def_boolean(ot->srna, "include_uv_textures", 0, "Include UV Textures",
309                         "Export textures assigned to the object UV maps");
310
311         RNA_def_boolean(ot->srna, "include_material_textures", 0, "Include Material Textures",
312                         "Export textures assigned to the object Materials");
313
314         RNA_def_boolean(ot->srna, "use_texture_copies", 1, "Copy",
315                         "Copy textures to same folder where the .dae file is exported");
316
317
318         RNA_def_boolean(ot->srna, "triangulate", 1, "Triangulate",
319                         "Export Polygons (Quads & NGons) as Triangles");
320
321         RNA_def_boolean(ot->srna, "use_object_instantiation", 1, "Use Object Instances",
322                         "Instantiate multiple Objects from same Data");
323
324         RNA_def_boolean(ot->srna, "sort_by_name", 0, "Sort by Object name",
325                         "Sort exported data by Object name");
326
327         RNA_def_int(ot->srna, "export_transformation_type", 0, INT_MIN, INT_MAX,
328                     "Transform", "Transformation type for translation, scale and rotation", INT_MIN, INT_MAX);
329
330         RNA_def_enum(ot->srna, "export_transformation_type_selection", prop_bc_export_transformation_type, 0,
331                      "Transform", "Transformation type for translation, scale and rotation");
332
333         RNA_def_boolean(ot->srna, "second_life", 0, "Export for Second Life",
334                         "Compatibility mode for Second Life");
335 }
336
337
338 /* function used for WM_OT_save_mainfile too */
339 static int wm_collada_import_exec(bContext *C, wmOperator *op)
340 {
341         char filename[FILE_MAX];
342         int import_units;
343
344         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
345                 BKE_report(op->reports, RPT_ERROR, "No filename given");
346                 return OPERATOR_CANCELLED;
347         }
348
349         /* Options panel */
350         import_units = RNA_boolean_get(op->ptr, "import_units");
351
352         RNA_string_get(op->ptr, "filepath", filename);
353         if (collada_import(C, filename, import_units)) {
354                 return OPERATOR_FINISHED;
355         }
356         else {
357                 BKE_report(op->reports, RPT_ERROR, "Errors found during parsing COLLADA document (see console for details)");
358                 return OPERATOR_CANCELLED;
359         }
360 }
361
362 static void uiCollada_importSettings(uiLayout *layout, PointerRNA *imfptr)
363 {
364         uiLayout *box, *row;
365
366         /* Import Options: */
367         box = uiLayoutBox(layout);
368         row = uiLayoutRow(box, FALSE);
369         uiItemL(row, IFACE_("Import Data Options:"), ICON_MESH_DATA);
370
371         row = uiLayoutRow(box, FALSE);
372         uiItemR(row, imfptr, "import_units", 0, NULL, ICON_NONE);
373 }
374
375 static void wm_collada_import_draw(bContext *UNUSED(C), wmOperator *op)
376 {
377         PointerRNA ptr;
378
379         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
380         uiCollada_importSettings(op->layout, &ptr);
381 }
382
383 void WM_OT_collada_import(wmOperatorType *ot)
384 {
385         ot->name = "Import COLLADA";
386         ot->description = "Load a Collada file";
387         ot->idname = "WM_OT_collada_import";
388
389         ot->invoke = WM_operator_filesel;
390         ot->exec = wm_collada_import_exec;
391         ot->poll = WM_operator_winactive;
392
393         //ot->flag |= OPTYPE_PRESET;
394
395         ot->ui = wm_collada_import_draw;
396
397         WM_operator_properties_filesel(ot, FOLDERFILE | COLLADAFILE, FILE_BLENDER, FILE_OPENFILE,
398                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
399
400         RNA_def_boolean(ot->srna,
401                         "import_units", 0, "Import Units",
402                         "If disabled match import to Blender's current Unit settings, "
403                         "otherwise use the settings from the Imported scene");
404
405 }
406 #endif