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