Small fixes to languages names (thx to Dalai Felinto for the hints), and enable build...
[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 "BLF_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, 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 deform_bones_only;
88
89         int include_uv_textures;
90         int include_material_textures;
91         int use_texture_copies;
92         int active_uv_only;
93
94         int use_object_instantiation;
95         int sort_by_name;
96         int second_life; 
97
98         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
99                 BKE_report(op->reports, RPT_ERROR, "No filename given");
100                 return OPERATOR_CANCELLED;
101         }
102
103         RNA_string_get(op->ptr, "filepath", filepath);
104         BLI_ensure_extension(filepath, sizeof(filepath), ".dae");
105
106         /* Options panel */
107         apply_modifiers          = RNA_boolean_get(op->ptr, "apply_modifiers");
108         export_mesh_type         = RNA_enum_get(op->ptr,    "export_mesh_type_selection");
109         selected                 = RNA_boolean_get(op->ptr, "selected");
110         include_children         = RNA_boolean_get(op->ptr, "include_children");
111         include_armatures        = RNA_boolean_get(op->ptr, "include_armatures");
112         deform_bones_only        = RNA_boolean_get(op->ptr, "deform_bones_only");
113
114         include_uv_textures      = RNA_boolean_get(op->ptr, "include_uv_textures");
115         include_material_textures= RNA_boolean_get(op->ptr, "include_material_textures");
116         use_texture_copies       = RNA_boolean_get(op->ptr, "use_texture_copies");
117         active_uv_only           = RNA_boolean_get(op->ptr, "active_uv_only");
118
119         use_object_instantiation = RNA_boolean_get(op->ptr, "use_object_instantiation");
120         sort_by_name             = RNA_boolean_get(op->ptr, "sort_by_name");
121         second_life              = RNA_boolean_get(op->ptr, "second_life");
122
123         /* get editmode results */
124         ED_object_exit_editmode(C, 0);  /* 0 = does not exit editmode */
125
126         if (collada_export(CTX_data_scene(C),
127                            filepath,
128                            apply_modifiers,
129                            export_mesh_type,
130                            selected,
131                            include_children,
132                            include_armatures,
133                            deform_bones_only,
134
135                            active_uv_only,
136                            include_uv_textures,
137                            include_material_textures,
138                            use_texture_copies,
139
140                            use_object_instantiation,
141                            sort_by_name,
142                            second_life)) {
143                 return OPERATOR_FINISHED;
144         }
145         else {
146                 return OPERATOR_CANCELLED;
147         }
148 }
149
150 /* Prototype now needed, it seems... Else gcc in paranoid warning=error mode fails to build! */
151 void uiCollada_exportSettings(uiLayout *layout, PointerRNA *imfptr);
152
153 void uiCollada_exportSettings(uiLayout *layout, PointerRNA *imfptr)
154 {
155         uiLayout *box, *row, *col, *split;
156
157         /* Export Options: */
158         box = uiLayoutBox(layout);
159         row = uiLayoutRow(box, FALSE);
160         uiItemL(row, IFACE_("Export Data Options:"), ICON_MESH_DATA);
161
162         row = uiLayoutRow(box, FALSE);
163         split = uiLayoutSplit(row, 0.6f, UI_LAYOUT_ALIGN_RIGHT);
164         col   = uiLayoutColumn(split, FALSE);
165         uiItemR(col, imfptr, "apply_modifiers", 0, NULL, ICON_NONE);
166         col   = uiLayoutColumn(split, FALSE);
167         uiItemR(col, imfptr, "export_mesh_type_selection", 0, "", ICON_NONE);
168         uiLayoutSetEnabled(col, RNA_boolean_get(imfptr, "apply_modifiers"));
169
170         row = uiLayoutRow(box, FALSE);
171         uiItemR(row, imfptr, "selected", 0, NULL, ICON_NONE);
172
173         row = uiLayoutRow(box, FALSE);
174         uiItemR(row, imfptr, "include_children", 0, NULL, ICON_NONE);
175         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
176
177         row = uiLayoutRow(box, FALSE);
178         uiItemR(row, imfptr, "include_armatures", 0, NULL, ICON_NONE);
179         uiLayoutSetEnabled(row, RNA_boolean_get(imfptr, "selected"));
180
181         /* Texture options */
182         box = uiLayoutBox(layout);
183         row = uiLayoutRow(box, FALSE);
184         uiItemL(row, IFACE_("Texture Options:"), ICON_TEXTURE_DATA);
185
186         row = uiLayoutRow(box, FALSE);
187         uiItemR(row, imfptr, "active_uv_only", 0, NULL, ICON_NONE);
188
189         row = uiLayoutRow(box, FALSE);
190         uiItemR(row, imfptr, "include_uv_textures", 0, NULL, ICON_NONE);
191
192         row = uiLayoutRow(box, FALSE);
193         uiItemR(row, imfptr, "include_material_textures", 0, NULL, ICON_NONE);
194
195         row = uiLayoutRow(box, FALSE);
196         uiItemR(row, imfptr, "use_texture_copies", 1, NULL, ICON_NONE);
197
198
199         /* Armature options */
200         box = uiLayoutBox(layout);
201         row = uiLayoutRow(box, FALSE);
202         uiItemL(row, IFACE_("Armature Options:"), ICON_ARMATURE_DATA);
203
204         row = uiLayoutRow(box, FALSE);
205         uiItemR(row, imfptr, "deform_bones_only", 0, NULL, ICON_NONE);
206         row = uiLayoutRow(box, FALSE);
207         uiItemR(row, imfptr, "second_life", 0, NULL, ICON_NONE);
208
209         /* Collada options: */
210         box = uiLayoutBox(layout);
211         row = uiLayoutRow(box, FALSE);
212         uiItemL(row, IFACE_("Collada Options:"), ICON_MODIFIER);
213
214         row = uiLayoutRow(box, FALSE);
215         uiItemR(row, imfptr, "use_object_instantiation", 0, NULL, ICON_NONE);
216         row = uiLayoutRow(box, FALSE);
217         uiItemR(row, imfptr, "sort_by_name", 0, NULL, ICON_NONE);
218
219 }
220
221 static void wm_collada_export_draw(bContext *UNUSED(C), wmOperator *op)
222 {
223         PointerRNA ptr;
224
225         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
226         uiCollada_exportSettings(op->layout, &ptr);
227 }
228
229 void WM_OT_collada_export(wmOperatorType *ot)
230 {
231         static EnumPropertyItem prop_bc_export_mesh_type[] = {
232                 {BC_MESH_TYPE_VIEW, "view", 0, "View", "Apply modifier's view settings"},
233                 {BC_MESH_TYPE_RENDER, "render", 0, "Render", "Apply modifier's render settings"},
234                 {0, NULL, 0, NULL, NULL}
235         };
236
237         ot->name = "Export COLLADA";
238         ot->description = "Save a Collada file";
239         ot->idname = "WM_OT_collada_export";
240
241         ot->invoke = wm_collada_export_invoke;
242         ot->exec = wm_collada_export_exec;
243         ot->poll = WM_operator_winactive;
244
245         ot->flag |= OPTYPE_PRESET;
246
247         ot->ui = wm_collada_export_draw;
248
249         WM_operator_properties_filesel(ot, FOLDERFILE | COLLADAFILE, FILE_BLENDER, FILE_SAVE,
250                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
251
252         RNA_def_boolean(ot->srna,
253                         "apply_modifiers", 0, "Apply Modifiers",
254                         "Apply modifiers to exported mesh (non destructive))");
255
256         RNA_def_int(ot->srna, "export_mesh_type", 0, INT_MIN, INT_MAX,
257                     "Resolution", "Modifier resolution for export", INT_MIN, INT_MAX);
258
259         RNA_def_enum(ot->srna, "export_mesh_type_selection", prop_bc_export_mesh_type, 0,
260                      "Resolution", "Modifier resolution for export");
261
262         RNA_def_boolean(ot->srna, "selected", 0, "Selection Only",
263                         "Export only selected elements");
264
265         RNA_def_boolean(ot->srna, "include_children", 0, "Include Children",
266                         "Export all children of selected objects (even if not selected)");
267
268         RNA_def_boolean(ot->srna, "include_armatures", 0, "Include Armatures",
269                         "Export related armatures (even if not selected)");
270
271         RNA_def_boolean(ot->srna, "deform_bones_only", 0, "Deform Bones only",
272                         "Only export deforming bones with armatures");
273
274
275         RNA_def_boolean(ot->srna, "active_uv_only", 0, "Only Active UV layer",
276                         "Export textures assigned to the object UV maps");
277
278         RNA_def_boolean(ot->srna, "include_uv_textures", 0, "Include UV Textures",
279                         "Export textures assigned to the object UV maps");
280
281         RNA_def_boolean(ot->srna, "include_material_textures", 0, "Include Material Textures",
282                         "Export textures assigned to the object Materials");
283
284         RNA_def_boolean(ot->srna, "use_texture_copies", 1, "Copy",
285                         "Copy textures to same folder where the .dae file is exported");
286
287
288         RNA_def_boolean(ot->srna, "use_object_instantiation", 1, "Use Object Instances",
289                         "Instantiate multiple Objects from same Data");
290
291         RNA_def_boolean(ot->srna, "sort_by_name", 0, "Sort by Object name",
292                         "Sort exported data by Object name");
293
294         RNA_def_boolean(ot->srna, "second_life", 0, "Export for Second Life",
295                         "Compatibility mode for Second Life");
296 }
297
298
299 /* function used for WM_OT_save_mainfile too */
300 static int wm_collada_import_exec(bContext *C, wmOperator *op)
301 {
302         char filename[FILE_MAX];
303
304         if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
305                 BKE_report(op->reports, RPT_ERROR, "No filename given");
306                 return OPERATOR_CANCELLED;
307         }
308
309         RNA_string_get(op->ptr, "filepath", filename);
310         if (collada_import(C, filename)) return OPERATOR_FINISHED;
311
312         BKE_report(op->reports, RPT_ERROR, "Errors found during parsing COLLADA document. Please see console for error log.");
313
314         return OPERATOR_FINISHED;
315 }
316
317 void WM_OT_collada_import(wmOperatorType *ot)
318 {
319         ot->name = "Import COLLADA";
320         ot->description = "Load a Collada file";
321         ot->idname = "WM_OT_collada_import";
322
323         ot->invoke = WM_operator_filesel;
324         ot->exec = wm_collada_import_exec;
325         ot->poll = WM_operator_winactive;
326
327         WM_operator_properties_filesel(ot, FOLDERFILE | COLLADAFILE, FILE_BLENDER, FILE_OPENFILE,
328                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
329 }
330 #endif