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