This commit makes it possible to select the sort mode whenever we invoke an operator.
[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 "BLT_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 open_sim;
100
101         int export_count;
102
103         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
104                 BKE_report(op->reports, RPT_ERROR, "No filename given");
105                 return OPERATOR_CANCELLED;
106         }
107
108         RNA_string_get(op->ptr, "filepath", filepath);
109         BLI_ensure_extension(filepath, sizeof(filepath), ".dae");
110
111
112         /* Avoid File write exceptions in Collada */
113         if (!BLI_exists(filepath)) {
114                 BLI_make_existing_file(filepath);
115                 if (!BLI_file_touch(filepath)) {
116                         BKE_report(op->reports, RPT_ERROR, "Can't create export file");
117                         fprintf(stdout, "Collada export: Can not create: %s\n", filepath);
118                         return OPERATOR_CANCELLED;
119                 }
120         }
121         else if (!BLI_file_is_writable(filepath)) {
122                 BKE_report(op->reports, RPT_ERROR, "Can't overwrite export file");
123                 fprintf(stdout, "Collada export: Can not modify: %s\n", filepath);
124                 return OPERATOR_CANCELLED;
125         }
126
127         /* Now the exporter can create and write the export file */
128
129         /* Options panel */
130         apply_modifiers          = RNA_boolean_get(op->ptr, "apply_modifiers");
131         export_mesh_type         = RNA_enum_get(op->ptr,    "export_mesh_type_selection");
132         selected                 = RNA_boolean_get(op->ptr, "selected");
133         include_children         = RNA_boolean_get(op->ptr, "include_children");
134         include_armatures        = RNA_boolean_get(op->ptr, "include_armatures");
135         include_shapekeys        = RNA_boolean_get(op->ptr, "include_shapekeys");
136         deform_bones_only        = RNA_boolean_get(op->ptr, "deform_bones_only");
137
138         include_uv_textures      = RNA_boolean_get(op->ptr, "include_uv_textures");
139         include_material_textures = RNA_boolean_get(op->ptr, "include_material_textures");
140         use_texture_copies       = RNA_boolean_get(op->ptr, "use_texture_copies");
141         active_uv_only           = RNA_boolean_get(op->ptr, "active_uv_only");
142
143         triangulate                = RNA_boolean_get(op->ptr, "triangulate");
144         use_object_instantiation   = RNA_boolean_get(op->ptr, "use_object_instantiation");
145         sort_by_name               = RNA_boolean_get(op->ptr, "sort_by_name");
146         export_transformation_type = RNA_enum_get(op->ptr,    "export_transformation_type_selection");
147         open_sim                   = RNA_boolean_get(op->ptr, "open_sim");
148
149         /* get editmode results */
150         ED_object_editmode_load(CTX_data_edit_object(C));
151
152
153         export_count = collada_export(CTX_data_scene(C),
154                 filepath,
155                 apply_modifiers,
156                 export_mesh_type,
157                 selected,
158                 include_children,
159                 include_armatures,
160                 include_shapekeys,
161                 deform_bones_only,
162
163                 active_uv_only,
164                 include_uv_textures,
165                 include_material_textures,
166                 use_texture_copies,
167
168                 triangulate,
169                 use_object_instantiation,
170                 sort_by_name,
171                 export_transformation_type,
172                 open_sim);
173
174         if (export_count == 0) {
175                 BKE_report(op->reports, RPT_WARNING, "Export file is empty");
176                 return OPERATOR_CANCELLED;
177         }
178         else {
179                 char buff[100];
180                 sprintf(buff, "Exported %d Objects", export_count);
181                 BKE_report(op->reports, RPT_INFO, buff);
182                 return OPERATOR_FINISHED;
183         }
184 }
185
186 static void uiCollada_exportSettings(uiLayout *layout, PointerRNA *imfptr)
187 {
188         uiLayout *box, *row, *col, *split;
189
190         /* Export Options: */
191         box = uiLayoutBox(layout);
192         row = uiLayoutRow(box, false);
193         uiItemL(row, IFACE_("Export Data Options:"), ICON_MESH_DATA);
194
195         row = uiLayoutRow(box, false);
196         split = uiLayoutSplit(row, 0.6f, UI_LAYOUT_ALIGN_RIGHT);
197         col   = uiLayoutColumn(split, false);
198         uiItemR(col, imfptr, "apply_modifiers", 0, NULL, ICON_NONE);
199         col   = uiLayoutColumn(split, false);
200         uiItemR(col, imfptr, "export_mesh_type_selection", 0, "", ICON_NONE);
201         uiLayoutSetEnabled(col, RNA_boolean_get(imfptr, "apply_modifiers"));
202
203         row = uiLayoutRow(box, false);
204         uiItemR(row, imfptr, "selected", 0, NULL, ICON_NONE);
205
206         row = uiLayoutRow(box, false);
207         uiItemR(row, imfptr, "include_children", 0, NULL, ICON_NONE);
208         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
209
210         row = uiLayoutRow(box, false);
211         uiItemR(row, imfptr, "include_armatures", 0, NULL, ICON_NONE);
212         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
213
214         row = uiLayoutRow(box, false);
215         uiItemR(row, imfptr, "include_shapekeys", 0, NULL, ICON_NONE);
216         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
217
218         /* Texture options */
219         box = uiLayoutBox(layout);
220         row = uiLayoutRow(box, false);
221         uiItemL(row, IFACE_("Texture Options:"), ICON_TEXTURE_DATA);
222
223         row = uiLayoutRow(box, false);
224         uiItemR(row, imfptr, "active_uv_only", 0, NULL, ICON_NONE);
225
226         row = uiLayoutRow(box, false);
227         uiItemR(row, imfptr, "include_uv_textures", 0, NULL, ICON_NONE);
228
229         row = uiLayoutRow(box, false);
230         uiItemR(row, imfptr, "include_material_textures", 0, NULL, ICON_NONE);
231
232         row = uiLayoutRow(box, false);
233         uiItemR(row, imfptr, "use_texture_copies", 1, NULL, ICON_NONE);
234
235
236         /* Armature options */
237         box = uiLayoutBox(layout);
238         row = uiLayoutRow(box, false);
239         uiItemL(row, IFACE_("Armature Options:"), ICON_ARMATURE_DATA);
240
241         row = uiLayoutRow(box, false);
242         uiItemR(row, imfptr, "deform_bones_only", 0, NULL, ICON_NONE);
243         row = uiLayoutRow(box, false);
244         uiItemR(row, imfptr, "open_sim", 0, NULL, ICON_NONE);
245
246         /* Collada options: */
247         box = uiLayoutBox(layout);
248         row = uiLayoutRow(box, false);
249         uiItemL(row, IFACE_("Collada Options:"), ICON_MODIFIER);
250
251         row = uiLayoutRow(box, false);
252         uiItemR(row, imfptr, "triangulate", 0, NULL, ICON_NONE);
253         row = uiLayoutRow(box, false);
254         uiItemR(row, imfptr, "use_object_instantiation", 0, NULL, ICON_NONE);
255
256         row = uiLayoutRow(box, false);
257         split = uiLayoutSplit(row, 0.6f, UI_LAYOUT_ALIGN_RIGHT);
258         uiItemL(split, IFACE_("Transformation Type"), ICON_NONE);
259         uiItemR(split, imfptr, "export_transformation_type_selection", 0, "", ICON_NONE);
260
261         row = uiLayoutRow(box, false);
262         uiItemR(row, imfptr, "sort_by_name", 0, NULL, ICON_NONE);
263
264 }
265
266 static void wm_collada_export_draw(bContext *UNUSED(C), wmOperator *op)
267 {
268         PointerRNA ptr;
269
270         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
271         uiCollada_exportSettings(op->layout, &ptr);
272 }
273
274 void WM_OT_collada_export(wmOperatorType *ot)
275 {
276         static EnumPropertyItem prop_bc_export_mesh_type[] = {
277                 {BC_MESH_TYPE_VIEW, "view", 0, "View", "Apply modifier's view settings"},
278                 {BC_MESH_TYPE_RENDER, "render", 0, "Render", "Apply modifier's render settings"},
279                 {0, NULL, 0, NULL, NULL}
280         };
281
282         static EnumPropertyItem prop_bc_export_transformation_type[] = {
283                 {BC_TRANSFORMATION_TYPE_MATRIX, "matrix", 0, "Matrix", "Use <matrix> to specify transformations"},
284                 {BC_TRANSFORMATION_TYPE_TRANSROTLOC, "transrotloc", 0, "TransRotLoc", "Use <translate>, <rotate>, <scale> to specify transformations"},
285                 {BC_TRANSFORMATION_TYPE_BOTH, "both", 0, "Both", "Use <matrix> AND <translate>, <rotate>, <scale> to specify transformations"},
286                 {0, NULL, 0, NULL, NULL}
287         };
288
289         ot->name = "Export COLLADA";
290         ot->description = "Save a Collada file";
291         ot->idname = "WM_OT_collada_export";
292
293         ot->invoke = wm_collada_export_invoke;
294         ot->exec = wm_collada_export_exec;
295         ot->poll = WM_operator_winactive;
296
297         ot->flag |= OPTYPE_PRESET;
298
299         ot->ui = wm_collada_export_draw;
300
301         WM_operator_properties_filesel(ot, FILE_TYPE_FOLDER | FILE_TYPE_COLLADA, FILE_BLENDER, FILE_SAVE,
302                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
303
304         RNA_def_boolean(ot->srna,
305                         "apply_modifiers", 0, "Apply Modifiers",
306                         "Apply modifiers to exported mesh (non destructive))");
307
308         RNA_def_int(ot->srna, "export_mesh_type", 0, INT_MIN, INT_MAX,
309                     "Resolution", "Modifier resolution for export", INT_MIN, INT_MAX);
310
311         RNA_def_enum(ot->srna, "export_mesh_type_selection", prop_bc_export_mesh_type, 0,
312                      "Resolution", "Modifier resolution for export");
313
314         RNA_def_boolean(ot->srna, "selected", 0, "Selection Only",
315                         "Export only selected elements");
316
317         RNA_def_boolean(ot->srna, "include_children", 0, "Include Children",
318                         "Export all children of selected objects (even if not selected)");
319
320         RNA_def_boolean(ot->srna, "include_armatures", 0, "Include Armatures",
321                         "Export related armatures (even if not selected)");
322
323         RNA_def_boolean(ot->srna, "include_shapekeys", 1, "Include Shape Keys",
324                         "Export all Shape Keys from Mesh Objects");
325
326         RNA_def_boolean(ot->srna, "deform_bones_only", 0, "Deform Bones only",
327                         "Only export deforming bones with armatures");
328
329
330         RNA_def_boolean(ot->srna, "active_uv_only", 0, "Only Selected UV Map",
331                         "Export only the selected UV Map");
332
333         RNA_def_boolean(ot->srna, "include_uv_textures", 0, "Include UV Textures",
334                         "Export textures assigned to the object UV Maps");
335
336         RNA_def_boolean(ot->srna, "include_material_textures", 0, "Include Material Textures",
337                         "Export textures assigned to the object Materials");
338
339         RNA_def_boolean(ot->srna, "use_texture_copies", 1, "Copy",
340                         "Copy textures to same folder where the .dae file is exported");
341
342
343         RNA_def_boolean(ot->srna, "triangulate", 1, "Triangulate",
344                         "Export Polygons (Quads & NGons) as Triangles");
345
346         RNA_def_boolean(ot->srna, "use_object_instantiation", 1, "Use Object Instances",
347                         "Instantiate multiple Objects from same Data");
348
349         RNA_def_boolean(ot->srna, "sort_by_name", 0, "Sort by Object name",
350                         "Sort exported data by Object name");
351
352         RNA_def_int(ot->srna, "export_transformation_type", 0, INT_MIN, INT_MAX,
353                     "Transform", "Transformation type for translation, scale and rotation", INT_MIN, INT_MAX);
354
355         RNA_def_enum(ot->srna, "export_transformation_type_selection", prop_bc_export_transformation_type, 0,
356                      "Transform", "Transformation type for translation, scale and rotation");
357
358         RNA_def_boolean(ot->srna, "open_sim", 0, "Export to SL/OpenSim",
359                         "Compatibility mode for SL, OpenSim and other compatible online worlds");
360 }
361
362
363 /* function used for WM_OT_save_mainfile too */
364 static int wm_collada_import_exec(bContext *C, wmOperator *op)
365 {
366         char filename[FILE_MAX];
367         int import_units;
368         int find_chains;
369         int fix_orientation;
370         int  min_chain_length;
371
372         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
373                 BKE_report(op->reports, RPT_ERROR, "No filename given");
374                 return OPERATOR_CANCELLED;
375         }
376
377         /* Options panel */
378         import_units     = RNA_boolean_get(op->ptr, "import_units");
379         find_chains      = RNA_boolean_get(op->ptr, "find_chains");
380         fix_orientation  = RNA_boolean_get(op->ptr, "fix_orientation");
381         min_chain_length = RNA_int_get(op->ptr, "min_chain_length");
382
383         RNA_string_get(op->ptr, "filepath", filename);
384         if (collada_import(
385                 C, filename,
386                 import_units,
387                 find_chains,
388                 fix_orientation,
389                 min_chain_length))
390         {
391                 return OPERATOR_FINISHED;
392         }
393         else {
394                 BKE_report(op->reports, RPT_ERROR, "Errors found during parsing COLLADA document (see console for details)");
395                 return OPERATOR_CANCELLED;
396         }
397 }
398
399 static void uiCollada_importSettings(uiLayout *layout, PointerRNA *imfptr)
400 {
401         uiLayout *box, *row;
402
403         /* Import Options: */
404         box = uiLayoutBox(layout);
405         row = uiLayoutRow(box, false);
406         uiItemL(row, IFACE_("Import Data Options:"), ICON_MESH_DATA);
407
408         row = uiLayoutRow(box, false);
409         uiItemR(row, imfptr, "import_units", 0, NULL, ICON_NONE);
410
411         box = uiLayoutBox(layout);
412         row = uiLayoutRow(box, false);
413         uiItemL(row, IFACE_("Armature Options:"), ICON_MESH_DATA);
414
415         row = uiLayoutRow(box, false);
416         uiItemR(row, imfptr, "fix_orientation", 0, NULL, ICON_NONE);
417
418         row = uiLayoutRow(box, false);
419         uiItemR(row, imfptr, "find_chains", 0, NULL, ICON_NONE);
420
421         row = uiLayoutRow(box, false);
422         uiItemR(row, imfptr, "min_chain_length", 0, NULL, ICON_NONE);
423 }
424
425 static void wm_collada_import_draw(bContext *UNUSED(C), wmOperator *op)
426 {
427         PointerRNA ptr;
428
429         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
430         uiCollada_importSettings(op->layout, &ptr);
431 }
432
433 void WM_OT_collada_import(wmOperatorType *ot)
434 {
435         ot->name = "Import COLLADA";
436         ot->description = "Load a Collada file";
437         ot->idname = "WM_OT_collada_import";
438
439         ot->invoke = WM_operator_filesel;
440         ot->exec = wm_collada_import_exec;
441         ot->poll = WM_operator_winactive;
442
443         //ot->flag |= OPTYPE_PRESET;
444
445         ot->ui = wm_collada_import_draw;
446
447         WM_operator_properties_filesel(ot, FILE_TYPE_FOLDER | FILE_TYPE_COLLADA, FILE_BLENDER, FILE_OPENFILE,
448                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
449
450         RNA_def_boolean(ot->srna,
451                 "import_units", 0, "Import Units",
452                 "If disabled match import to Blender's current Unit settings, "
453                 "otherwise use the settings from the Imported scene");
454
455         RNA_def_boolean(ot->srna,
456                 "fix_orientation", 0, "Fix Leaf Bones",
457                 "Fix Orientation of Leaf Bones (Collada does only support Joints)");
458
459         RNA_def_boolean(ot->srna,
460                 "find_chains", 0, "Find Bone Chains",
461                 "Find best matching Bone Chains and ensure bones in chain are connected");
462
463         RNA_def_int(ot->srna,
464                 "min_chain_length",
465                 0,
466                 0,
467                 INT_MAX,
468                 "Minimum Chain Length",
469                 "When searching Bone Chains disregard chains of length below this value",
470                 0,
471                 INT_MAX);
472
473 }
474 #endif