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