Cleanup: Nuke moar G.main usages...
[blender.git] / source / blender / collada / SceneExporter.cpp
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  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/collada/SceneExporter.cpp
24  *  \ingroup collada
25  */
26
27 extern "C" {
28         #include "BLI_utildefines.h"
29         #include "BKE_object.h"
30         #include "BLI_listbase.h"
31 }
32
33 #include "SceneExporter.h"
34 #include "collada_utils.h"
35
36 SceneExporter::SceneExporter(COLLADASW::StreamWriter *sw, ArmatureExporter *arm, const ExportSettings *export_settings)
37         : COLLADASW::LibraryVisualScenes(sw), arm_exporter(arm), export_settings(export_settings)
38 {
39 }
40
41 void SceneExporter::exportScene(bContext *C, Scene *sce)
42 {
43         // <library_visual_scenes> <visual_scene>
44         std::string id_naming = id_name(sce);
45         openVisualScene(translate_id(id_naming), id_naming);
46         exportHierarchy(C, sce);
47         closeVisualScene();
48         closeLibrary();
49 }
50
51 void SceneExporter::exportHierarchy(bContext *C, Scene *sce)
52 {       
53         LinkNode *node;
54         std::vector<Object *> base_objects;
55
56         // Ensure all objects in the export_set are marked
57         for (node = this->export_settings->export_set; node; node = node->next) {
58                 Object *ob = (Object *) node->link;
59                 ob->id.tag |= LIB_TAG_DOIT;
60         }
61         
62         // Now find all exportable base ojects (highest in export hierarchy)
63         for (node = this->export_settings->export_set; node; node = node->next) {
64                 Object *ob = (Object *) node->link;
65                 if (bc_is_base_node(this->export_settings->export_set, ob)) {
66                         switch (ob->type) {
67                                 case OB_MESH:
68                                 case OB_CAMERA:
69                                 case OB_LAMP:
70                                 case OB_EMPTY:
71                                 case OB_ARMATURE:
72                                         base_objects.push_back(ob);
73                                         break;
74                         }
75                 }
76         }
77
78         // And now export the base objects:
79         for (int index = 0; index < base_objects.size(); index++) {
80                 Object *ob = base_objects[index];
81                 if (bc_is_marked(ob)) {
82                         bc_remove_mark(ob);
83                         writeNodes(C, ob, sce);
84                 }
85         }
86 }
87
88
89 void SceneExporter::writeNodes(bContext *C, Object *ob, Scene *sce)
90 {
91         // Add associated armature first if available
92         bool armature_exported = false;
93         Object *ob_arm = bc_get_assigned_armature(ob);
94         if (ob_arm != NULL) {
95                 armature_exported = bc_is_in_Export_set(this->export_settings->export_set, ob_arm);
96                 if (armature_exported && bc_is_marked(ob_arm)) {
97                         bc_remove_mark(ob_arm);
98                         writeNodes(C, ob_arm, sce);
99                         armature_exported = true;
100                 }
101         }
102
103         COLLADASW::Node colladaNode(mSW);
104         colladaNode.setNodeId(translate_id(id_name(ob)));
105         colladaNode.setNodeName(translate_id(id_name(ob)));
106         colladaNode.setType(COLLADASW::Node::NODE);
107
108         colladaNode.start();
109
110         std::list<Object *> child_objects;
111
112         // list child objects
113         LinkNode *node;
114         for (node=this->export_settings->export_set; node; node=node->next) {
115                 // cob - child object
116                 Object *cob = (Object *)node->link;
117
118                 if (cob->parent == ob) {
119                         switch (cob->type) {
120                                 case OB_MESH:
121                                 case OB_CAMERA:
122                                 case OB_LAMP:
123                                 case OB_EMPTY:
124                                 case OB_ARMATURE:
125                                         if (bc_is_marked(cob))
126                                                 child_objects.push_back(cob);
127                                         break;
128                         }
129                 }
130         }
131
132         if (ob->type == OB_MESH && armature_exported)
133                 // for skinned mesh we write obmat in <bind_shape_matrix>
134                 TransformWriter::add_node_transform_identity(colladaNode);
135         else {
136                 TransformWriter::add_node_transform_ob(colladaNode, ob, this->export_settings->export_transformation_type);
137         }
138
139         // <instance_geometry>
140         if (ob->type == OB_MESH) {
141                 bool instance_controller_created = false;
142                 if (armature_exported) {
143                         instance_controller_created = arm_exporter->add_instance_controller(ob);
144                 }
145                 if (!instance_controller_created) {
146                         COLLADASW::InstanceGeometry instGeom(mSW);
147                         instGeom.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob, this->export_settings->use_object_instantiation)));
148                         instGeom.setName(translate_id(id_name(ob)));
149                         InstanceWriter::add_material_bindings(instGeom.getBindMaterial(), 
150                                     ob, 
151                                         this->export_settings->active_uv_only, 
152                                         this->export_settings->export_texture_type);
153
154                         instGeom.add();
155                 }
156         }
157
158         // <instance_controller>
159         else if (ob->type == OB_ARMATURE) {
160                 arm_exporter->add_armature_bones(C, ob, sce, this, child_objects);
161         }
162
163         // <instance_camera>
164         else if (ob->type == OB_CAMERA) {
165                 COLLADASW::InstanceCamera instCam(mSW, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, get_camera_id(ob)));
166                 instCam.add();
167         }
168
169         // <instance_light>
170         else if (ob->type == OB_LAMP) {
171                 COLLADASW::InstanceLight instLa(mSW, COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, get_light_id(ob)));
172                 instLa.add();
173         }
174
175         // empty object
176         else if (ob->type == OB_EMPTY) { // TODO: handle groups (OB_DUPLIGROUP
177                 if ((ob->transflag & OB_DUPLIGROUP) == OB_DUPLIGROUP && ob->dup_group) {
178                         GroupObject *go = NULL;
179                         Group *gr = ob->dup_group;
180                         /* printf("group detected '%s'\n", gr->id.name + 2); */
181                         for (go = (GroupObject *)(gr->gobject.first); go; go = go->next) {
182                                 printf("\t%s\n", go->ob->id.name);
183                         }
184                 }
185         }
186
187         if (ob->type == OB_ARMATURE) {
188                 colladaNode.end();
189         }
190
191         if (BLI_listbase_is_empty(&ob->constraints) == false) {
192                 bConstraint *con = (bConstraint *) ob->constraints.first;
193                 while (con) {
194                         std::string con_name(translate_id(con->name));
195                         std::string con_tag = con_name + "_constraint";
196                         printf("%s\n", con_name.c_str());
197                         printf("%s\n\n", con_tag.c_str());
198                         colladaNode.addExtraTechniqueChildParameter("blender",con_tag,"type",con->type);
199                         colladaNode.addExtraTechniqueChildParameter("blender",con_tag,"enforce",con->enforce);
200                         colladaNode.addExtraTechniqueChildParameter("blender",con_tag,"flag",con->flag);
201                         colladaNode.addExtraTechniqueChildParameter("blender",con_tag,"headtail",con->headtail);
202                         colladaNode.addExtraTechniqueChildParameter("blender",con_tag,"lin_error",con->lin_error);
203                         colladaNode.addExtraTechniqueChildParameter("blender",con_tag,"own_space",con->ownspace);
204                         colladaNode.addExtraTechniqueChildParameter("blender",con_tag,"rot_error",con->rot_error);
205                         colladaNode.addExtraTechniqueChildParameter("blender",con_tag,"tar_space",con->tarspace);
206                         colladaNode.addExtraTechniqueChildParameter("blender",con_tag,"lin_error",con->lin_error);
207                         
208                         //not ideal: add the target object name as another parameter. 
209                         //No real mapping in the .dae
210                         //Need support for multiple target objects also.
211                         const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
212                         ListBase targets = {NULL, NULL};
213                         if (cti && cti->get_constraint_targets) {
214                         
215                                 bConstraintTarget *ct;
216                                 Object *obtar;
217                         
218                                 cti->get_constraint_targets(con, &targets);
219
220                                 for (ct = (bConstraintTarget *)targets.first; ct; ct = ct->next) {
221                                         obtar = ct->tar;
222                                         std::string tar_id((obtar) ? id_name(obtar) : "");
223                                         colladaNode.addExtraTechniqueChildParameter("blender",con_tag,"target_id",tar_id);
224                                 }
225
226                                 if (cti->flush_constraint_targets)
227                                         cti->flush_constraint_targets(con, &targets, 1);
228
229                         }
230
231                         con = con->next;
232                 }
233         }
234
235         for (std::list<Object *>::iterator i = child_objects.begin(); i != child_objects.end(); ++i) {
236                 if (bc_is_marked(*i)) {
237                         bc_remove_mark(*i);
238                         writeNodes(C, *i, sce);
239                 }
240         }
241
242         if (ob->type != OB_ARMATURE)
243                 colladaNode.end();
244 }
245