Fix COLLADA build warning.
[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_space_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_object.h"
42 #include "BKE_report.h"
43
44 #include "DEG_depsgraph.h"
45
46 #include "ED_screen.h"
47 #include "ED_object.h"
48
49 #include "RNA_access.h"
50 #include "RNA_define.h"
51
52 #include "UI_interface.h"
53 #include "UI_resources.h"
54
55 #include "WM_api.h"
56 #include "WM_types.h"
57
58 #include "../../collada/collada.h"
59
60 #include "io_collada.h"
61
62 static int wm_collada_export_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
63 {
64         Main *bmain = CTX_data_main(C);
65
66         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
67                 char filepath[FILE_MAX];
68                 const char *blendfile_path = BKE_main_blendfile_path(bmain);
69
70                 if (blendfile_path[0] == '\0')
71                         BLI_strncpy(filepath, "untitled", sizeof(filepath));
72                 else
73                         BLI_strncpy(filepath, blendfile_path, sizeof(filepath));
74
75                 BLI_path_extension_replace(filepath, sizeof(filepath), ".dae");
76                 RNA_string_set(op->ptr, "filepath", filepath);
77         }
78
79         WM_event_add_fileselect(C, op);
80
81         return OPERATOR_RUNNING_MODAL;
82 }
83
84 /* function used for WM_OT_save_mainfile too */
85 static int wm_collada_export_exec(bContext *C, wmOperator *op)
86 {
87         char filepath[FILE_MAX];
88         int apply_modifiers;
89         int export_mesh_type;
90         int selected;
91         int include_children;
92         int include_armatures;
93         int include_shapekeys;
94         int deform_bones_only;
95
96         int include_animations;
97         int include_all_actions;
98         int sampling_rate;
99         int keep_smooth_curves;
100         int keep_keyframes;
101
102         int export_animation_type;
103         int use_texture_copies;
104         int active_uv_only;
105
106         int triangulate;
107         int use_object_instantiation;
108         int use_blender_profile;
109         int sort_by_name;
110         int export_transformation_type;
111
112         int open_sim;
113         int limit_precision;
114         int keep_bind_info;
115
116         int export_count;
117         int sample_animations;
118
119         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
120                 BKE_report(op->reports, RPT_ERROR, "No filename given");
121                 return OPERATOR_CANCELLED;
122         }
123
124         RNA_string_get(op->ptr, "filepath", filepath);
125         BLI_path_extension_ensure(filepath, sizeof(filepath), ".dae");
126
127
128         /* Avoid File write exceptions in Collada */
129         if (!BLI_exists(filepath)) {
130                 BLI_make_existing_file(filepath);
131                 if (!BLI_file_touch(filepath)) {
132                         BKE_report(op->reports, RPT_ERROR, "Can't create export file");
133                         fprintf(stdout, "Collada export: Can not create: %s\n", filepath);
134                         return OPERATOR_CANCELLED;
135                 }
136         }
137         else if (!BLI_file_is_writable(filepath)) {
138                 BKE_report(op->reports, RPT_ERROR, "Can't overwrite export file");
139                 fprintf(stdout, "Collada export: Can not modify: %s\n", filepath);
140                 return OPERATOR_CANCELLED;
141         }
142
143         /* Now the exporter can create and write the export file */
144
145         /* Options panel */
146         apply_modifiers          = RNA_boolean_get(op->ptr, "apply_modifiers");
147         export_mesh_type         = RNA_enum_get(op->ptr,    "export_mesh_type_selection");
148         selected                 = RNA_boolean_get(op->ptr, "selected");
149         include_children         = RNA_boolean_get(op->ptr, "include_children");
150         include_armatures        = RNA_boolean_get(op->ptr, "include_armatures");
151         include_shapekeys        = RNA_boolean_get(op->ptr, "include_shapekeys");
152
153         include_animations       = RNA_boolean_get(op->ptr, "include_animations");
154         include_all_actions      = RNA_boolean_get(op->ptr, "include_all_actions");
155         export_animation_type    = RNA_enum_get(op->ptr, "export_animation_type_selection");
156         sample_animations        = (export_animation_type == BC_ANIMATION_EXPORT_SAMPLES);
157         sampling_rate            = (sample_animations) ? RNA_int_get(op->ptr, "sampling_rate") : 0;
158         keep_smooth_curves       = RNA_boolean_get(op->ptr, "keep_smooth_curves");
159         keep_keyframes           = RNA_boolean_get(op->ptr, "keep_keyframes");
160
161         deform_bones_only        = RNA_boolean_get(op->ptr, "deform_bones_only");
162
163         use_texture_copies       = RNA_boolean_get(op->ptr, "use_texture_copies");
164         active_uv_only           = RNA_boolean_get(op->ptr, "active_uv_only");
165
166         triangulate                = RNA_boolean_get(op->ptr, "triangulate");
167         use_object_instantiation   = RNA_boolean_get(op->ptr, "use_object_instantiation");
168         use_blender_profile        = RNA_boolean_get(op->ptr, "use_blender_profile");
169         sort_by_name               = RNA_boolean_get(op->ptr, "sort_by_name");
170         export_transformation_type = RNA_enum_get(op->ptr,    "export_transformation_type_selection");
171         open_sim                   = RNA_boolean_get(op->ptr, "open_sim");
172
173         limit_precision = RNA_boolean_get(op->ptr, "limit_precision");
174         keep_bind_info = RNA_boolean_get(op->ptr, "keep_bind_info");
175
176         Main *bmain = CTX_data_main(C);
177
178         /* get editmode results */
179         ED_object_editmode_load(bmain, CTX_data_edit_object(C));
180
181         //Scene *scene = CTX_data_scene(C);
182
183         ExportSettings export_settings;
184
185         export_settings.filepath = filepath;
186
187         export_settings.apply_modifiers = apply_modifiers != 0;
188         export_settings.export_mesh_type = export_mesh_type;
189         export_settings.selected = selected != 0;
190         export_settings.include_children = include_children != 0;
191         export_settings.include_armatures = include_armatures != 0;
192         export_settings.include_shapekeys = include_shapekeys != 0;
193         export_settings.deform_bones_only = deform_bones_only != 0;
194         export_settings.include_animations = include_animations != 0;
195         export_settings.include_all_actions = include_all_actions != 0;
196         export_settings.sampling_rate = sampling_rate;
197         export_settings.keep_keyframes = keep_keyframes != 0 || sampling_rate < 1;
198
199         export_settings.active_uv_only = active_uv_only != 0;
200         export_settings.export_animation_type = export_animation_type;
201         export_settings.use_texture_copies = use_texture_copies != 0;
202
203         export_settings.triangulate = triangulate != 0;
204         export_settings.use_object_instantiation = use_object_instantiation != 0;
205         export_settings.use_blender_profile = use_blender_profile != 0;
206         export_settings.sort_by_name = sort_by_name != 0;
207
208         if (export_animation_type == BC_ANIMATION_EXPORT_SAMPLES) {
209                 export_settings.export_transformation_type = export_transformation_type;
210         }
211         else {
212                 // When curves are exported then we can not export as matrix
213                 export_settings.export_transformation_type = BC_TRANSFORMATION_TYPE_TRANSROTLOC;
214         }
215
216         if (export_settings.export_transformation_type == BC_TRANSFORMATION_TYPE_TRANSROTLOC) {
217                 export_settings.keep_smooth_curves = keep_smooth_curves != 0;
218         }
219         else {
220                 // Can not export smooth curves when Matrix export is enabled.
221                 export_settings.keep_smooth_curves = false;
222         }
223
224         export_settings.open_sim = open_sim != 0;
225         export_settings.limit_precision = limit_precision != 0;
226         export_settings.keep_bind_info = keep_bind_info != 0;
227
228         int includeFilter = OB_REL_NONE;
229         if (export_settings.include_armatures) includeFilter |= OB_REL_MOD_ARMATURE;
230         if (export_settings.include_children) includeFilter |= OB_REL_CHILDREN_RECURSIVE;
231
232         export_count = collada_export(
233                            C,
234                            &export_settings
235         );
236
237         if (export_count == 0) {
238                 BKE_report(op->reports, RPT_WARNING, "No objects selected -- Created empty export file");
239                 return OPERATOR_CANCELLED;
240         }
241         else if (export_count < 0) {
242                 BKE_report(op->reports, RPT_WARNING, "Error during export (see Console)");
243                 return OPERATOR_CANCELLED;
244         }
245         else {
246                 char buff[100];
247                 sprintf(buff, "Exported %d Objects", export_count);
248                 BKE_report(op->reports, RPT_INFO, buff);
249                 return OPERATOR_FINISHED;
250         }
251 }
252
253 static void uiCollada_exportSettings(uiLayout *layout, PointerRNA *imfptr)
254 {
255         uiLayout *box, *row, *col, *split;
256         bool include_animations = RNA_boolean_get(imfptr, "include_animations");
257         int ui_section = RNA_enum_get(imfptr, "prop_bc_export_ui_section");
258
259         BC_export_animation_type animation_type = RNA_enum_get(imfptr, "export_animation_type_selection");
260         BC_export_transformation_type transformation_type = RNA_enum_get(imfptr, "export_transformation_type_selection");
261
262         bool sampling = animation_type == BC_ANIMATION_EXPORT_SAMPLES;
263
264         /* Export Options: */
265         box = uiLayoutBox(layout);
266
267         row = uiLayoutRow(box, false);
268         uiItemR(row, imfptr, "prop_bc_export_ui_section", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
269
270         if (ui_section == BC_UI_SECTION_MAIN) {
271                 /* =================== */
272                 /* Export Data options */
273                 /* =================== */
274
275                 row = uiLayoutRow(box, false);
276                 uiItemR(row, imfptr, "selected", 0, NULL, ICON_NONE);
277
278                 row = uiLayoutRow(box, false);
279                 uiItemR(row, imfptr, "include_children", 0, NULL, ICON_NONE);
280                 uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
281
282                 row = uiLayoutRow(box, false);
283                 uiItemR(row, imfptr, "include_armatures", 0, NULL, ICON_NONE);
284                 uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
285
286                 row = uiLayoutRow(box, false);
287                 uiItemR(row, imfptr, "include_shapekeys", 0, NULL, ICON_NONE);
288                 uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
289
290                 /* Texture options */
291                 box = uiLayoutBox(layout);
292                 row = uiLayoutRow(box, false);
293                 uiItemL(row, IFACE_("Texture Options:"), ICON_TEXTURE_DATA);
294
295                 row = uiLayoutRow(box, false);
296                 uiItemR(row, imfptr, "active_uv_only", 0, NULL, ICON_NONE);
297
298                 row = uiLayoutRow(box, false);
299                 uiItemR(row, imfptr, "use_texture_copies", 1, NULL, ICON_NONE);
300
301         }
302         else if (ui_section == BC_UI_SECTION_GEOMETRY) {
303                 row = uiLayoutRow(box, false);
304                 uiItemL(row, IFACE_("Export Data Options:"), ICON_MESH_DATA);
305
306                 row = uiLayoutRow(box, false);
307                 split = uiLayoutSplit(row, 0.6f, UI_LAYOUT_ALIGN_RIGHT);
308                 col = uiLayoutColumn(split, false);
309                 uiItemR(col, imfptr, "apply_modifiers", 0, NULL, ICON_NONE);
310                 col = uiLayoutColumn(split, false);
311                 uiItemR(col, imfptr, "export_mesh_type_selection", 0, "", ICON_NONE);
312                 uiLayoutSetEnabled(col, RNA_boolean_get(imfptr, "apply_modifiers"));
313                 row = uiLayoutRow(box, false);
314                 uiItemR(row, imfptr, "triangulate", 1, NULL, ICON_NONE);
315         }
316         else if (ui_section == BC_UI_SECTION_ARMATURE) {
317                 /* Armature options */
318                 box = uiLayoutBox(layout);
319                 row = uiLayoutRow(box, false);
320                 uiItemL(row, IFACE_("Armature Options:"), ICON_ARMATURE_DATA);
321
322                 row = uiLayoutRow(box, false);
323                 uiItemR(row, imfptr, "deform_bones_only", 0, NULL, ICON_NONE);
324
325                 row = uiLayoutRow(box, false);
326                 uiItemR(row, imfptr, "open_sim", 0, NULL, ICON_NONE);
327         }
328         else if (ui_section == BC_UI_SECTION_ANIMATION) {
329
330                 /* ====================== */
331                 /* Animation Data options */
332                 /* ====================== */
333
334                 row = uiLayoutRow(box, false);
335                 uiItemR(row, imfptr, "include_animations", 0, NULL, ICON_NONE);
336
337
338                 row = uiLayoutRow(box, false);
339                 uiItemR(row, imfptr, "export_animation_type_selection", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
340
341                 row = uiLayoutRow(box, false);
342                 split = uiLayoutSplit(row, 0.6f, UI_LAYOUT_ALIGN_RIGHT);
343                 uiItemL(split, IFACE_("Transformation Type"), ICON_NONE);
344                 uiItemR(split, imfptr, "export_transformation_type_selection", 0, "", ICON_NONE);
345                 uiLayoutSetEnabled(row, animation_type == BC_ANIMATION_EXPORT_SAMPLES);
346
347                 row = uiLayoutColumn(box, false);
348                 uiItemR(row, imfptr, "keep_smooth_curves", 0, NULL, ICON_NONE);
349                 uiLayoutSetEnabled(row, include_animations &&
350                         (transformation_type == BC_TRANSFORMATION_TYPE_TRANSROTLOC || animation_type == BC_ANIMATION_EXPORT_KEYS));
351
352                 row = uiLayoutColumn(box, false);
353                 uiItemR(row, imfptr, "sampling_rate", 0, NULL, ICON_NONE);
354                 uiLayoutSetEnabled(row, sampling && include_animations);
355
356                 row = uiLayoutColumn(box, false);
357                 uiItemR(row, imfptr, "keep_keyframes", 0, NULL, ICON_NONE);
358                 uiLayoutSetEnabled(row, sampling && include_animations);
359
360                 row = uiLayoutRow(box, false);
361                 uiItemR(row, imfptr, "include_all_actions", 0, NULL, ICON_NONE);
362                 uiLayoutSetEnabled(row, include_animations);
363
364         }
365         else if (ui_section == BC_UI_SECTION_COLLADA) {
366                 /* Collada options: */
367                 box = uiLayoutBox(layout);
368                 row = uiLayoutRow(box, false);
369                 uiItemL(row, IFACE_("Collada Options:"), ICON_MODIFIER);
370
371                 row = uiLayoutRow(box, false);
372                 uiItemR(row, imfptr, "use_object_instantiation", 1, NULL, ICON_NONE);
373                 row = uiLayoutRow(box, false);
374                 uiItemR(row, imfptr, "use_blender_profile", 1, NULL, ICON_NONE);
375
376                 row = uiLayoutRow(box, false);
377                 uiItemR(row, imfptr, "sort_by_name", 0, NULL, ICON_NONE);
378
379                 row = uiLayoutRow(box, false);
380                 uiItemR(row, imfptr, "keep_bind_info", 0, NULL, ICON_NONE);
381
382                 row = uiLayoutRow(box, false);
383                 uiItemR(row, imfptr, "limit_precision", 0, NULL, ICON_NONE);
384         }
385 }
386
387 static void wm_collada_export_draw(bContext *UNUSED(C), wmOperator *op)
388 {
389         PointerRNA ptr;
390
391         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
392         uiCollada_exportSettings(op->layout, &ptr);
393 }
394
395 static bool wm_collada_export_check(bContext *UNUSED(C), wmOperator *op)
396 {
397         char filepath[FILE_MAX];
398         RNA_string_get(op->ptr, "filepath", filepath);
399
400         if (!BLI_path_extension_check(filepath, ".dae")) {
401                 BLI_path_extension_ensure(filepath, FILE_MAX, ".dae");
402                 RNA_string_set(op->ptr, "filepath", filepath);
403                 return true;
404         }
405
406         return false;
407 }
408
409 void WM_OT_collada_export(wmOperatorType *ot)
410 {
411         struct StructRNA *func = ot->srna;
412
413         static const EnumPropertyItem prop_bc_export_mesh_type[] = {
414                 {BC_MESH_TYPE_VIEW, "view", 0, "View", "Apply modifier's view settings"},
415                 {BC_MESH_TYPE_RENDER, "render", 0, "Render", "Apply modifier's render settings"},
416                 {0, NULL, 0, NULL, NULL}
417         };
418
419         static const EnumPropertyItem prop_bc_export_transformation_type[] = {
420                 { BC_TRANSFORMATION_TYPE_MATRIX, "matrix", 0, "Matrix", "Use <matrix> to specify transformations" },
421                 { BC_TRANSFORMATION_TYPE_TRANSROTLOC, "transrotloc", 0, "TransRotLoc", "Use <translate>, <rotate>, <scale> to specify transformations" },
422                 { 0, NULL, 0, NULL, NULL }
423         };
424
425         static const EnumPropertyItem prop_bc_export_animation_type[] = {
426         { BC_ANIMATION_EXPORT_SAMPLES, "sample", 0, "Samples", "Export Sampled points guided by sampling rate" },
427         { BC_ANIMATION_EXPORT_KEYS, "keys", 0, "Curves", "Export Curves\n Note: guided by curve keys" },
428         { 0, NULL, 0, NULL, NULL }
429         };
430
431         static const EnumPropertyItem prop_bc_export_ui_section[] = {
432         { BC_UI_SECTION_MAIN, "main", 0, "Main", "Data Export Section" },
433         { BC_UI_SECTION_GEOMETRY, "geometry", 0, "Geom", "Geometry Export Section" },
434         { BC_UI_SECTION_ARMATURE, "armature", 0, "Arm", "Armature Export Section" },
435         { BC_UI_SECTION_ANIMATION, "animation", 0, "Anim", "Animation Export Section" },
436         { BC_UI_SECTION_COLLADA, "collada", 0, "Extra", "Collada Export Section" },
437         { 0, NULL, 0, NULL, NULL }
438         };
439
440
441         ot->name = "Export COLLADA";
442         ot->description = "Save a Collada file";
443         ot->idname = "WM_OT_collada_export";
444
445         ot->invoke = wm_collada_export_invoke;
446         ot->exec = wm_collada_export_exec;
447         ot->poll = WM_operator_winactive;
448         ot->check = wm_collada_export_check;
449
450         ot->flag |= OPTYPE_PRESET;
451
452         ot->ui = wm_collada_export_draw;
453
454         WM_operator_properties_filesel(
455                 ot, FILE_TYPE_FOLDER | FILE_TYPE_COLLADA, FILE_BLENDER, FILE_SAVE,
456                 WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
457
458         RNA_def_enum(func, "prop_bc_export_ui_section", prop_bc_export_ui_section, 0,
459                 "Export Section", "Only for User Interface Organisation");
460
461         RNA_def_boolean(func,
462                         "apply_modifiers", 0, "Apply Modifiers",
463                         "Apply modifiers to exported mesh (non destructive))");
464
465         RNA_def_int(func, "export_mesh_type", 0, INT_MIN, INT_MAX,
466                     "Resolution", "Modifier resolution for export", INT_MIN, INT_MAX);
467
468         RNA_def_enum(func, "export_mesh_type_selection", prop_bc_export_mesh_type, 0,
469                      "Resolution", "Modifier resolution for export");
470
471         RNA_def_boolean(func, "selected", false, "Selection Only",
472                         "Export only selected elements");
473
474         RNA_def_boolean(func, "include_children", false, "Include Children",
475                         "Export all children of selected objects (even if not selected)");
476
477         RNA_def_boolean(func, "include_armatures", false, "Include Armatures",
478                         "Export related armatures (even if not selected)");
479
480         RNA_def_boolean(func, "include_shapekeys", false, "Include Shape Keys",
481                         "Export all Shape Keys from Mesh Objects");
482
483         RNA_def_boolean(func, "deform_bones_only", false, "Deform Bones only",
484                         "Only export deforming bones with armatures");
485
486
487
488         RNA_def_boolean(func, "include_animations", true,
489                 "Include Animations", "Export Animations if available.\nExporting Animations will enforce the decomposition of node transforms\ninto  <translation> <rotation> and <scale> components");
490
491         RNA_def_boolean(func, "include_all_actions", true,
492                 "Include all Actions", "Export also unassigned actions.\nThis allows you to export entire animation libraries for your charater(s)");
493
494         RNA_def_enum(func, "export_animation_type_selection", prop_bc_export_animation_type, 0,
495                 "Key Type", "Type for exported animations (use sample keys or Curve keys)");
496
497         RNA_def_int(func, "sampling_rate", 1, 1, INT_MAX,
498                 "Sampling Rate", "The distance between 2 keyframes. 1 means: Every frame is keyed", 1, INT_MAX);
499
500         RNA_def_boolean(func, "keep_smooth_curves", 0,
501                 "Keep Smooth curves", "Export also the curve handles (if available).\nThis does only work when the inverse parent matrix is the Unity matrix\nOtherwise you may end up with odd results\n");
502
503         RNA_def_boolean(func, "keep_keyframes", 0,
504                 "Keep Keyframes", "Use existing keyframes as additional sample points.\nThis helps when you want to keep manual tweeks");
505
506         RNA_def_boolean(func, "active_uv_only", 0, "Only Selected UV Map",
507                         "Export only the selected UV Map");
508
509         RNA_def_boolean(func, "use_texture_copies", 1, "Copy",
510                         "Copy textures to same folder where the .dae file is exported");
511
512
513         RNA_def_boolean(func, "triangulate", 1, "Triangulate",
514                         "Export Polygons (Quads & NGons) as Triangles");
515
516         RNA_def_boolean(func, "use_object_instantiation", 1, "Use Object Instances",
517                 "Instantiate multiple Objects from same Data");
518
519         RNA_def_boolean(func, "use_blender_profile", 1, "Use Blender Profile",
520                 "Export additional Blender specific information (for material, shaders, bones, etc.)");
521
522         RNA_def_boolean(func, "sort_by_name", 0, "Sort by Object name",
523                         "Sort exported data by Object name");
524
525         RNA_def_int(func, "export_transformation_type", 0, INT_MIN, INT_MAX,
526                     "Transform", "Transformation type for translation, scale and rotation", INT_MIN, INT_MAX);
527
528         RNA_def_enum(func, "export_transformation_type_selection", prop_bc_export_transformation_type, 0,
529                      "Transform", "Transformation type for translation, scale and rotation");
530
531         RNA_def_boolean(func, "open_sim", 0, "Export to SL/OpenSim",
532                         "Compatibility mode for SL, OpenSim and other compatible online worlds");
533
534         RNA_def_boolean(func, "limit_precision", 0,
535                 "Limit Precision", "Reduce the precision of the exported data to 6 digits");
536
537         RNA_def_boolean(func, "keep_bind_info", 0,
538                 "Keep Bind Info", "Store Bindpose information in custom bone properties for later use during Collada export");
539
540 }
541
542
543 /* function used for WM_OT_save_mainfile too */
544 static int wm_collada_import_exec(bContext *C, wmOperator *op)
545 {
546         char filename[FILE_MAX];
547         int import_units;
548         int find_chains;
549         int auto_connect;
550         int fix_orientation;
551         int min_chain_length;
552
553         int keep_bind_info;
554         ImportSettings import_settings;
555
556         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
557                 BKE_report(op->reports, RPT_ERROR, "No filename given");
558                 return OPERATOR_CANCELLED;
559         }
560
561         /* Options panel */
562         import_units     = RNA_boolean_get(op->ptr, "import_units");
563         find_chains      = RNA_boolean_get(op->ptr, "find_chains");
564         auto_connect     = RNA_boolean_get(op->ptr, "auto_connect");
565         fix_orientation  = RNA_boolean_get(op->ptr, "fix_orientation");
566
567         keep_bind_info = RNA_boolean_get(op->ptr, "keep_bind_info");
568
569         min_chain_length = RNA_int_get(op->ptr, "min_chain_length");
570
571         RNA_string_get(op->ptr, "filepath", filename);
572
573         import_settings.filepath = filename;
574         import_settings.import_units = import_units != 0;
575         import_settings.auto_connect = auto_connect != 0;
576         import_settings.find_chains = find_chains != 0;
577         import_settings.fix_orientation = fix_orientation != 0;
578         import_settings.min_chain_length = min_chain_length;
579         import_settings.keep_bind_info = keep_bind_info != 0;
580
581         if (collada_import(C, &import_settings)) {
582                 DEG_id_tag_update(&CTX_data_scene(C)->id, DEG_TAG_BASE_FLAGS_UPDATE);
583                 return OPERATOR_FINISHED;
584         }
585         else {
586                 BKE_report(op->reports, RPT_ERROR, "Parsing errors in Document (see Blender Console)");
587                 return OPERATOR_CANCELLED;
588         }
589 }
590
591 static void uiCollada_importSettings(uiLayout *layout, PointerRNA *imfptr)
592 {
593         uiLayout *box, *row;
594
595         /* Import Options: */
596         box = uiLayoutBox(layout);
597         row = uiLayoutRow(box, false);
598         uiItemL(row, IFACE_("Import Data Options:"), ICON_MESH_DATA);
599
600         row = uiLayoutRow(box, false);
601         uiItemR(row, imfptr, "import_units", 0, NULL, ICON_NONE);
602
603         box = uiLayoutBox(layout);
604         row = uiLayoutRow(box, false);
605         uiItemL(row, IFACE_("Armature Options:"), ICON_MESH_DATA);
606
607         row = uiLayoutRow(box, false);
608         uiItemR(row, imfptr, "fix_orientation", 0, NULL, ICON_NONE);
609
610         row = uiLayoutRow(box, false);
611         uiItemR(row, imfptr, "find_chains", 0, NULL, ICON_NONE);
612
613         row = uiLayoutRow(box, false);
614         uiItemR(row, imfptr, "auto_connect", 0, NULL, ICON_NONE);
615
616         row = uiLayoutRow(box, false);
617         uiItemR(row, imfptr, "min_chain_length", 0, NULL, ICON_NONE);
618
619         box = uiLayoutBox(layout);
620         row = uiLayoutRow(box, false);
621
622         row = uiLayoutRow(box, false);
623         uiItemR(row, imfptr, "keep_bind_info", 0, NULL, ICON_NONE);
624
625 }
626
627 static void wm_collada_import_draw(bContext *UNUSED(C), wmOperator *op)
628 {
629         PointerRNA ptr;
630
631         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
632         uiCollada_importSettings(op->layout, &ptr);
633 }
634
635 void WM_OT_collada_import(wmOperatorType *ot)
636 {
637         ot->name = "Import COLLADA";
638         ot->description = "Load a Collada file";
639         ot->idname = "WM_OT_collada_import";
640
641         ot->invoke = WM_operator_filesel;
642         ot->exec = wm_collada_import_exec;
643         ot->poll = WM_operator_winactive;
644
645         //ot->flag |= OPTYPE_PRESET;
646
647         ot->ui = wm_collada_import_draw;
648
649         WM_operator_properties_filesel(
650                 ot, FILE_TYPE_FOLDER | FILE_TYPE_COLLADA, FILE_BLENDER, FILE_OPENFILE,
651                 WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
652
653         RNA_def_boolean(ot->srna,
654                 "import_units", 0, "Import Units",
655                 "If disabled match import to Blender's current Unit settings, "
656                 "otherwise use the settings from the Imported scene");
657
658         RNA_def_boolean(ot->srna,
659                 "fix_orientation", 0, "Fix Leaf Bones",
660                 "Fix Orientation of Leaf Bones (Collada does only support Joints)");
661
662         RNA_def_boolean(ot->srna,
663                 "find_chains", 0, "Find Bone Chains",
664                 "Find best matching Bone Chains and ensure bones in chain are connected");
665
666         RNA_def_boolean(ot->srna,
667                 "auto_connect", 0, "Auto Connect",
668                 "Set use_connect for parent bones which have exactly one child bone");
669
670         RNA_def_int(ot->srna,
671                 "min_chain_length",
672                 0,
673                 0,
674                 INT_MAX,
675                 "Minimum Chain Length",
676                 "When searching Bone Chains disregard chains of length below this value",
677                 0,
678                 INT_MAX);
679
680         RNA_def_boolean(ot->srna,
681                 "keep_bind_info", 0, "Keep Bind Info",
682                 "Store Bindpose information in custom bone properties for later use during Collada export");
683
684 }
685 #endif