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