Merge branch 'master' into blender2.8
[blender.git] / source / blender / collada / DocumentImporter.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, Nathan Letwory.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/collada/DocumentImporter.cpp
24  *  \ingroup collada
25  */
26
27 // TODO:
28 // * name imported objects
29 // * import object rotation as euler
30
31 #include <string>
32 #include <map>
33 #include <algorithm> // sort()
34
35 #include "COLLADAFWRoot.h"
36 #include "COLLADAFWStableHeaders.h"
37 #include "COLLADAFWColorOrTexture.h"
38 #include "COLLADAFWIndexList.h"
39 #include "COLLADAFWMeshPrimitiveWithFaceVertexCount.h"
40 #include "COLLADAFWPolygons.h"
41 #include "COLLADAFWSampler.h"
42 #include "COLLADAFWTypes.h"
43 #include "COLLADAFWVisualScene.h"
44 #include "COLLADAFWArrayPrimitiveType.h"
45 #include "COLLADAFWLibraryNodes.h"
46 #include "COLLADAFWCamera.h"
47 #include "COLLADAFWLight.h"
48
49 #include "COLLADASaxFWLLoader.h"
50 #include "COLLADASaxFWLIExtraDataCallbackHandler.h"
51
52 extern "C" {
53 #include "BLI_listbase.h"
54 #include "BLI_math.h"
55 #include "BLI_string.h"
56 #include "BLI_utildefines.h"
57 #include "BLI_fileops.h"
58
59 #include "BKE_camera.h"
60 #include "BKE_collection.h"
61 #include "BKE_fcurve.h"
62 #include "BKE_global.h"
63 #include "BKE_image.h"
64 #include "BKE_layer.h"
65 #include "BKE_lamp.h"
66 #include "BKE_library.h"
67 #include "BKE_material.h"
68 #include "BKE_scene.h"
69
70 #include "BLI_path_util.h"
71
72 #include "DNA_camera_types.h"
73 #include "DNA_lamp_types.h"
74
75 #include "RNA_access.h"
76
77 #include "MEM_guardedalloc.h"
78
79 #include "WM_api.h"
80 #include "WM_types.h"
81
82 }
83
84 #include "DEG_depsgraph.h"
85 #include "DEG_depsgraph_build.h"
86
87 #include "ExtraHandler.h"
88 #include "ErrorHandler.h"
89 #include "DocumentImporter.h"
90 #include "TransformReader.h"
91
92 #include "collada_internal.h"
93 #include "collada_utils.h"
94 #include "Materials.h"
95
96 /*
97  * COLLADA Importer limitations:
98  * - no multiple scene import, all objects are added to active scene
99  */
100
101 // #define COLLADA_DEBUG
102 // creates empties for each imported bone on layer 2, for debugging
103 // #define ARMATURE_TEST
104
105 DocumentImporter::DocumentImporter(bContext *C, const ImportSettings *import_settings) :
106         import_settings(import_settings),
107         mImportStage(Fetching_Scene_data),
108         mContext(C),
109         view_layer(CTX_data_view_layer(mContext)),
110         armature_importer(&unit_converter, &mesh_importer, CTX_data_main(C), CTX_data_scene(C), view_layer, import_settings),
111         mesh_importer(&unit_converter, &armature_importer, CTX_data_main(C), CTX_data_scene(C), view_layer),
112         anim_importer(C, &unit_converter, &armature_importer, CTX_data_scene(C))
113 {
114 }
115
116 DocumentImporter::~DocumentImporter()
117 {
118         TagsMap::iterator etit;
119         etit = uid_tags_map.begin();
120         while (etit != uid_tags_map.end()) {
121                 delete etit->second;
122                 etit++;
123         }
124 }
125
126 bool DocumentImporter::import()
127 {
128         ErrorHandler errorHandler;
129         COLLADASaxFWL::Loader loader(&errorHandler);
130         COLLADAFW::Root root(&loader, this);
131         ExtraHandler *ehandler = new ExtraHandler(this, &(this->anim_importer));
132
133         loader.registerExtraDataCallbackHandler(ehandler);
134
135         // deselect all to select new objects
136         BKE_view_layer_base_deselect_all(view_layer);
137
138         std::string mFilename = std::string(this->import_settings->filepath);
139         const std::string encodedFilename = bc_url_encode(mFilename);
140         if (!root.loadDocument(encodedFilename)) {
141                 fprintf(stderr, "COLLADAFW::Root::loadDocument() returned false on 1st pass\n");
142                 delete ehandler;
143                 return false;
144         }
145
146         if (errorHandler.hasError()) {
147                 delete ehandler;
148                 return false;
149         }
150
151         /** TODO set up scene graph and such here */
152         mImportStage = Fetching_Controller_data;
153         COLLADASaxFWL::Loader loader2;
154         COLLADAFW::Root root2(&loader2, this);
155
156         if (!root2.loadDocument(encodedFilename)) {
157                 fprintf(stderr, "COLLADAFW::Root::loadDocument() returned false on 2nd pass\n");
158                 delete ehandler;
159                 return false;
160         }
161
162         delete ehandler;
163
164         return true;
165 }
166
167 void DocumentImporter::cancel(const COLLADAFW::String& errorMessage)
168 {
169         // TODO: if possible show error info
170         //
171         // Should we get rid of invisible Meshes that were created so far
172         // or maybe create objects at coordinate space origin?
173         //
174         // The latter sounds better.
175 }
176
177 void DocumentImporter::start()
178 {
179 }
180
181 void DocumentImporter::finish()
182 {
183         if (mImportStage == Fetching_Controller_data)
184                 return;
185
186         Main *bmain = CTX_data_main(mContext);
187         // TODO: create a new scene except the selected <visual_scene> - use current blender scene for it
188         Scene *sce = CTX_data_scene(mContext);
189         unit_converter.calculate_scale(*sce);
190
191         std::vector<Object *> *objects_to_scale = new std::vector<Object *>();
192
193         /** TODO Break up and put into 2-pass parsing of DAE */
194         std::vector<const COLLADAFW::VisualScene *>::iterator sit;
195         for (sit = vscenes.begin(); sit != vscenes.end(); sit++) {
196                 PointerRNA sceneptr, unit_settings;
197                 PropertyRNA *system, *scale;
198
199                 // for scene unit settings: system, scale_length
200
201                 RNA_id_pointer_create(&sce->id, &sceneptr);
202                 unit_settings = RNA_pointer_get(&sceneptr, "unit_settings");
203                 system = RNA_struct_find_property(&unit_settings, "system");
204                 scale = RNA_struct_find_property(&unit_settings, "scale_length");
205
206                 if (this->import_settings->import_units) {
207
208                         switch (unit_converter.isMetricSystem()) {
209                                 case UnitConverter::Metric:
210                                         RNA_property_enum_set(&unit_settings, system, USER_UNIT_METRIC);
211                                         break;
212                                 case UnitConverter::Imperial:
213                                         RNA_property_enum_set(&unit_settings, system, USER_UNIT_IMPERIAL);
214                                         break;
215                                 default:
216                                         RNA_property_enum_set(&unit_settings, system, USER_UNIT_NONE);
217                                         break;
218                         }
219                         float unit_factor = unit_converter.getLinearMeter();
220                         RNA_property_float_set(&unit_settings, scale, unit_factor);
221                         fprintf(stdout, "Collada: Adjusting Blender units to Importset units: %f.\n", unit_factor);
222
223                 }
224
225                 // Write nodes to scene
226                 fprintf(stderr, "+-- Import Scene --------\n");
227                 const COLLADAFW::NodePointerArray& roots = (*sit)->getRootNodes();
228                 for (unsigned int i = 0; i < roots.getCount(); i++) {
229                         std::vector<Object *> *objects_done = write_node(roots[i], NULL, sce, NULL, false);
230                         objects_to_scale->insert(objects_to_scale->end(), objects_done->begin(), objects_done->end());
231                         delete objects_done;
232                 }
233
234         }
235
236
237         mesh_importer.optimize_material_assignements();
238
239         armature_importer.set_tags_map(this->uid_tags_map);
240         armature_importer.make_armatures(mContext, *objects_to_scale);
241         armature_importer.make_shape_keys(mContext);
242
243 #if 0
244         armature_importer.fix_animation();
245 #endif
246
247         for (std::vector<const COLLADAFW::VisualScene *>::iterator vsit = vscenes.begin(); vsit != vscenes.end(); vsit++) {
248                 const COLLADAFW::NodePointerArray& roots = (*vsit)->getRootNodes();
249
250                 for (unsigned int i = 0; i < roots.getCount(); i++) {
251                         translate_anim_recursive(roots[i], NULL, NULL);
252                 }
253         }
254
255         if (libnode_ob.size()) {
256
257                 fprintf(stderr, "| Cleanup: free %d library nodes\n", (int)libnode_ob.size());
258                 // free all library_nodes
259                 std::vector<Object *>::iterator it;
260                 for (it = libnode_ob.begin(); it != libnode_ob.end(); it++) {
261                         Object *ob = *it;
262                         BKE_scene_collections_object_remove(bmain, sce, ob, true);
263                 }
264                 libnode_ob.clear();
265         }
266
267         bc_match_scale(objects_to_scale, unit_converter, !this->import_settings->import_units);
268
269         delete objects_to_scale;
270
271         // update scene
272         DEG_id_tag_update(&sce->id, DEG_TAG_COPY_ON_WRITE);
273         DEG_relations_tag_update(bmain);
274         WM_event_add_notifier(mContext, NC_OBJECT | ND_TRANSFORM, NULL);
275 }
276
277
278 void DocumentImporter::translate_anim_recursive(COLLADAFW::Node *node, COLLADAFW::Node *par = NULL, Object *parob = NULL)
279 {
280         // The split in #29246, rootmap must point at actual root when
281         // calculating bones in apply_curves_as_matrix. - actual root is the root node.
282         // This has to do with inverse bind poses being world space
283         // (the sources for skinned bones' restposes) and the way
284         // non-skinning nodes have their "restpose" recursively calculated.
285         // XXX TODO: design issue, how to support unrelated joints taking
286         // part in skinning.
287         if (par) { // && par->getType() == COLLADAFW::Node::JOINT) {
288                 // par is root if there's no corresp. key in root_map
289                 if (root_map.find(par->getUniqueId()) == root_map.end())
290                         root_map[node->getUniqueId()] = node;
291                 else
292                         root_map[node->getUniqueId()] = root_map[par->getUniqueId()];
293         }
294
295 #if 0
296         COLLADAFW::Transformation::TransformationType types[] = {
297                 COLLADAFW::Transformation::ROTATE,
298                 COLLADAFW::Transformation::SCALE,
299                 COLLADAFW::Transformation::TRANSLATE,
300                 COLLADAFW::Transformation::MATRIX
301         };
302
303         Object *ob;
304 #endif
305         unsigned int i;
306
307         if (node->getType() == COLLADAFW::Node::JOINT && par == NULL) {
308                 // For Skeletons without root node we have to simulate the
309                 // root node here and recursively enter the same function
310                 // XXX: maybe this can be made more elegant.
311                 translate_anim_recursive(node, node, parob);
312         }
313         else {
314                 anim_importer.translate_Animations(node, root_map, object_map, FW_object_map, uid_material_map);
315                 COLLADAFW::NodePointerArray &children = node->getChildNodes();
316                 for (i = 0; i < children.getCount(); i++) {
317                         translate_anim_recursive(children[i], node, NULL);
318                 }
319         }
320 }
321
322 /**
323  * If the imported file was made with Blender, return the Blender version used,
324  * otherwise return an empty std::string
325  */
326 std::string DocumentImporter::get_import_version(const COLLADAFW::FileInfo *asset)
327 {
328         const char AUTORING_TOOL[] = "authoring_tool";
329         const std::string BLENDER("Blender ");
330         const COLLADAFW::FileInfo::ValuePairPointerArray &valuePairs = asset->getValuePairArray();
331         for ( size_t i = 0, count = valuePairs.getCount(); i < count; ++i)
332         {
333                 const COLLADAFW::FileInfo::ValuePair* valuePair = valuePairs[i];
334                 const COLLADAFW::String& key = valuePair->first;
335                 const COLLADAFW::String& value = valuePair->second;
336                 if ( key == AUTORING_TOOL )
337                 {
338                         if (value.compare(0, BLENDER.length(), BLENDER) == 0)
339                         {
340                                 // Was made with Blender, now get version string
341                                 std::string v            = value.substr(BLENDER.length());
342                                 std::string::size_type n = v.find(" ");
343                                 if (n > 0) {
344                                         return v.substr(0,n);
345                                 }
346                         }
347                 }
348         }
349         return "";
350 }
351
352 /** When this method is called, the writer must write the global document asset.
353  * \return The writer should return true, if writing succeeded, false otherwise.*/
354 bool DocumentImporter::writeGlobalAsset(const COLLADAFW::FileInfo *asset)
355 {
356         unit_converter.read_asset(asset);
357         import_from_version = get_import_version(asset);
358         anim_importer.set_import_from_version(import_from_version);
359         return true;
360 }
361
362 /** When this method is called, the writer must write the scene.
363  * \return The writer should return true, if writing succeeded, false otherwise.*/
364 bool DocumentImporter::writeScene(const COLLADAFW::Scene *scene)
365 {
366         // XXX could store the scene id, but do nothing for now
367         return true;
368 }
369 Object *DocumentImporter::create_camera_object(COLLADAFW::InstanceCamera *camera, Scene *sce)
370 {
371         const COLLADAFW::UniqueId& cam_uid = camera->getInstanciatedObjectId();
372         if (uid_camera_map.find(cam_uid) == uid_camera_map.end()) {
373                 // fprintf(stderr, "Couldn't find camera by UID.\n");
374                 return NULL;
375         }
376
377         Main *bmain = CTX_data_main(mContext);
378         Object *ob = bc_add_object(bmain, sce, view_layer, OB_CAMERA, NULL);
379         Camera *cam = uid_camera_map[cam_uid];
380         Camera *old_cam = (Camera *)ob->data;
381         ob->data = cam;
382         BKE_libblock_free_us(bmain, old_cam);
383         return ob;
384 }
385
386 Object *DocumentImporter::create_lamp_object(COLLADAFW::InstanceLight *lamp, Scene *sce)
387 {
388         const COLLADAFW::UniqueId& lamp_uid = lamp->getInstanciatedObjectId();
389         if (uid_lamp_map.find(lamp_uid) == uid_lamp_map.end()) {
390                 fprintf(stderr, "Couldn't find light by UID.\n");
391                 return NULL;
392         }
393
394         Main *bmain = CTX_data_main(mContext);
395         Object *ob = bc_add_object(bmain, sce, view_layer, OB_LAMP, NULL);
396         Lamp *la = uid_lamp_map[lamp_uid];
397         Lamp *old_lamp = (Lamp *)ob->data;
398         ob->data = la;
399         BKE_libblock_free_us(bmain, old_lamp);
400         return ob;
401 }
402
403 Object *DocumentImporter::create_instance_node(Object *source_ob, COLLADAFW::Node *source_node, COLLADAFW::Node *instance_node, Scene *sce, bool is_library_node)
404 {
405         //fprintf(stderr, "create <instance_node> under node id=%s from node id=%s\n", instance_node ? instance_node->getOriginalId().c_str() : NULL, source_node ? source_node->getOriginalId().c_str() : NULL);
406
407         Main *bmain = CTX_data_main(mContext);
408         Object *obn = BKE_object_copy(bmain, source_ob);
409         DEG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
410         BKE_collection_object_add_from(bmain, sce, source_ob, obn);
411
412         if (instance_node) {
413                 anim_importer.read_node_transform(instance_node, obn);
414                 // if we also have a source_node (always ;), take its
415                 // transformation matrix and apply it to the newly instantiated
416                 // object to account for node hierarchy transforms in
417                 // .dae
418                 if (source_node) {
419                         COLLADABU::Math::Matrix4 mat4 = source_node->getTransformationMatrix();
420                         COLLADABU::Math::Matrix4 bmat4 = mat4.transpose(); // transpose to get blender row-major order
421                         float mat[4][4];
422                         for (int i = 0; i < 4; i++) {
423                                 for (int j = 0; j < 4; j++) {
424                                         mat[i][j] = bmat4[i][j];
425                                 }
426                         }
427                         // calc new matrix and apply
428                         mul_m4_m4m4(obn->obmat, obn->obmat, mat);
429                         BKE_object_apply_mat4(obn, obn->obmat, 0, 0);
430                 }
431         }
432         else {
433                 anim_importer.read_node_transform(source_node, obn);
434         }
435
436         /*DAG_relations_tag_update(CTX_data_main(mContext));*/
437
438         COLLADAFW::NodePointerArray &children = source_node->getChildNodes();
439         if (children.getCount()) {
440                 for (unsigned int i = 0; i < children.getCount(); i++) {
441                         COLLADAFW::Node *child_node = children[i];
442                         const COLLADAFW::UniqueId& child_id = child_node->getUniqueId();
443                         if (object_map.find(child_id) == object_map.end())
444                                 continue;
445                         COLLADAFW::InstanceNodePointerArray &inodes = child_node->getInstanceNodes();
446                         Object *new_child = NULL;
447                         if (inodes.getCount()) { // \todo loop through instance nodes
448                                 const COLLADAFW::UniqueId& id = inodes[0]->getInstanciatedObjectId();
449                                 fprintf(stderr, "Doing %d child nodes\n", (int)node_map.count(id));
450                                 new_child = create_instance_node(object_map.find(id)->second, node_map[id], child_node, sce, is_library_node);
451                         }
452                         else {
453                                 new_child = create_instance_node(object_map.find(child_id)->second, child_node, NULL, sce, is_library_node);
454                         }
455                         bc_set_parent(new_child, obn, mContext, true);
456
457                         if (is_library_node)
458                                 libnode_ob.push_back(new_child);
459                 }
460         }
461
462         return obn;
463 }
464
465 // to create constraints off node <extra> tags. Assumes only constraint data in
466 // current <extra> with blender profile.
467 void DocumentImporter::create_constraints(ExtraTags *et, Object *ob)
468 {
469         if (et && et->isProfile("blender")) {
470                 std::string name;
471                 short type = 0;
472                 et->setData("type", &type);
473                 BKE_constraint_add_for_object(ob, "Test_con", type);
474
475         }
476 }
477
478 void DocumentImporter::report_unknown_reference(const COLLADAFW::Node &node, const std::string object_type)
479 {
480         std::string id = node.getOriginalId();
481         std::string name = node.getName();
482         fprintf(stderr,
483                 "error: node id=\"%s\", name=\"%s\" refers to an undefined %s.\n",
484                 id.c_str(),
485                 name.c_str(),
486                 object_type.c_str());
487 }
488
489 std::vector<Object *> *DocumentImporter::write_node(COLLADAFW::Node *node, COLLADAFW::Node *parent_node, Scene *sce, Object *par, bool is_library_node)
490 {
491         Main *bmain = CTX_data_main(mContext);
492         Object *ob = NULL;
493         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
494         bool read_transform = true;
495         std::string id   = node->getOriginalId();
496         std::string name = node->getName();
497
498         // if node has child nodes write them
499         COLLADAFW::NodePointerArray &child_nodes = node->getChildNodes();
500
501         std::vector<Object *> *objects_done = new std::vector<Object *>();
502         std::vector<Object *> *root_objects = new std::vector<Object *>();
503
504         fprintf(stderr,
505                 "| %s id='%s', name='%s'\n",
506                 is_joint ? "JOINT" : "NODE ",
507                 id.c_str(),
508         name.c_str() );
509
510         if (is_joint) {
511                 if (parent_node == NULL && !is_library_node) {
512                         // A Joint on root level is a skeleton without root node.
513                         // Here we add the armature "on the fly":
514                         par = bc_add_object(bmain, sce, view_layer, OB_ARMATURE, std::string("Armature").c_str());
515                         objects_done->push_back(par);
516                         root_objects->push_back(par);
517                         object_map.insert(std::pair<COLLADAFW::UniqueId, Object *>(node->getUniqueId(), par));
518                         node_map[node->getUniqueId()] = node;
519                 }
520                 if (parent_node == NULL || parent_node->getType() != COLLADAFW::Node::JOINT) {
521                         armature_importer.add_root_joint(node, par);
522                 }
523
524                 if (parent_node == NULL) {
525                         // for skeletons without root node all has been done above.
526                         // Skeletons with root node are handled further down.
527                         goto finally;
528                 }
529         }
530         else {
531                 COLLADAFW::InstanceGeometryPointerArray &geom = node->getInstanceGeometries();
532                 COLLADAFW::InstanceCameraPointerArray &camera = node->getInstanceCameras();
533                 COLLADAFW::InstanceLightPointerArray &lamp = node->getInstanceLights();
534                 COLLADAFW::InstanceControllerPointerArray &controller = node->getInstanceControllers();
535                 COLLADAFW::InstanceNodePointerArray &inst_node = node->getInstanceNodes();
536                 size_t geom_done = 0;
537                 size_t camera_done = 0;
538                 size_t lamp_done = 0;
539                 size_t controller_done = 0;
540                 size_t inst_done = 0;
541
542                 // XXX linking object with the first <instance_geometry>, though a node may have more of them...
543                 // maybe join multiple <instance_...> meshes into 1, and link object with it? not sure...
544                 // <instance_geometry>
545                 while (geom_done < geom.getCount()) {
546                         ob = mesh_importer.create_mesh_object(node, geom[geom_done], false, uid_material_map);
547                         if (ob == NULL) {
548                                 report_unknown_reference(*node, "instance_mesh");
549                         }
550                         else {
551                                 objects_done->push_back(ob);
552                                 if (parent_node == NULL) {
553                                         root_objects->push_back(ob);
554                                 }
555                         }
556                         ++geom_done;
557                 }
558                 while (camera_done < camera.getCount()) {
559                         ob = create_camera_object(camera[camera_done], sce);
560                         if (ob == NULL) {
561                                 report_unknown_reference(*node, "instance_camera");
562                         }
563                         else {
564                                 objects_done->push_back(ob);
565                                 if (parent_node == NULL) {
566                                         root_objects->push_back(ob);
567                                 }
568                         }
569                         ++camera_done;
570                 }
571                 while (lamp_done < lamp.getCount()) {
572                         ob = create_lamp_object(lamp[lamp_done], sce);
573                         if (ob == NULL) {
574                                 report_unknown_reference(*node, "instance_lamp");
575                         }
576                         else {
577                                 objects_done->push_back(ob);
578                                 if (parent_node == NULL) {
579                                         root_objects->push_back(ob);
580                                 }
581                         }
582                         ++lamp_done;
583                 }
584                 while (controller_done < controller.getCount()) {
585                         COLLADAFW::InstanceGeometry *geometry = (COLLADAFW::InstanceGeometry *)controller[controller_done];
586                         ob = mesh_importer.create_mesh_object(node, geometry, true, uid_material_map);
587                         if (ob == NULL) {
588                                 report_unknown_reference(*node, "instance_controller");
589                         }
590                         else {
591                                 objects_done->push_back(ob);
592                                 if (parent_node == NULL) {
593                                         root_objects->push_back(ob);
594                                 }
595                         }
596                         ++controller_done;
597                 }
598                 // XXX instance_node is not supported yet
599                 while (inst_done < inst_node.getCount()) {
600                         const COLLADAFW::UniqueId& node_id = inst_node[inst_done]->getInstanciatedObjectId();
601                         if (object_map.find(node_id) == object_map.end()) {
602                                 fprintf(stderr, "Cannot find object for node referenced by <instance_node name=\"%s\">.\n", inst_node[inst_done]->getName().c_str());
603                                 ob = NULL;
604                         }
605                         else {
606                                 std::pair<std::multimap<COLLADAFW::UniqueId, Object *>::iterator, std::multimap<COLLADAFW::UniqueId, Object *>::iterator> pair_iter = object_map.equal_range(node_id);
607                                 for (std::multimap<COLLADAFW::UniqueId, Object *>::iterator it2 = pair_iter.first; it2 != pair_iter.second; it2++) {
608                                         Object *source_ob = (Object *)it2->second;
609                                         COLLADAFW::Node *source_node = node_map[node_id];
610                                         ob = create_instance_node(source_ob, source_node, node, sce, is_library_node);
611                                         objects_done->push_back(ob);
612                                         if (parent_node == NULL) {
613                                                 root_objects->push_back(ob);
614                                         }
615                                 }
616                         }
617                         ++inst_done;
618
619                         read_transform = false;
620                 }
621
622                 // if node is empty - create empty object
623                 // XXX empty node may not mean it is empty object, not sure about this
624                 if ( (geom_done + camera_done + lamp_done + controller_done + inst_done) < 1) {
625                         //Check if Object is armature, by checking if immediate child is a JOINT node.
626                         if (is_armature(node)) {
627                                 ob = bc_add_object(bmain, sce, view_layer, OB_ARMATURE, name.c_str());
628                         }
629                         else {
630                                 ob = bc_add_object(bmain, sce, view_layer, OB_EMPTY, NULL);
631                         }
632                         objects_done->push_back(ob);
633                         if (parent_node == NULL) {
634                                 root_objects->push_back(ob);
635                         }
636                 }
637
638                 // XXX: if there're multiple instances, only one is stored
639
640                 if (!ob) {
641                         goto finally;
642                 }
643
644                 for (std::vector<Object *>::iterator it = objects_done->begin(); it != objects_done->end(); ++it) {
645                         ob = *it;
646                         std::string nodename = node->getName().size() ? node->getName() : node->getOriginalId();
647                         BKE_libblock_rename(bmain, &ob->id, (char *)nodename.c_str());
648                         object_map.insert(std::pair<COLLADAFW::UniqueId, Object *>(node->getUniqueId(), ob));
649                         node_map[node->getUniqueId()] = node;
650
651                         if (is_library_node)
652                                 libnode_ob.push_back(ob);
653                 }
654
655
656                 //create_constraints(et,ob);
657
658         }
659
660         for (std::vector<Object *>::iterator it = objects_done->begin(); it != objects_done->end(); ++it) {
661                 ob = *it;
662
663                 if (read_transform)
664                         anim_importer.read_node_transform(node, ob);  // overwrites location set earlier
665
666                 if (!is_joint) {
667                         if (par && ob) {
668                                 ob->parent = par;
669                                 ob->partype = PAROBJECT;
670                                 ob->parsubstr[0] = 0;
671
672                                 //bc_set_parent(ob, par, mContext, false);
673                         }
674                 }
675         }
676
677         if (objects_done->size() > 0) {
678                 ob = *objects_done->begin();
679         }
680         else {
681                 ob = NULL;
682         }
683
684         for (unsigned int i = 0; i < child_nodes.getCount(); i++) {
685                 std::vector<Object *> *child_objects;
686                 child_objects = write_node(child_nodes[i], node, sce, ob, is_library_node);
687                 delete child_objects;
688         }
689
690
691 finally:
692         delete objects_done;
693
694         return root_objects;
695 }
696
697 /**
698  * When this method is called, the writer must write the entire visual scene.
699  * Return The writer should return true, if writing succeeded, false otherwise.
700  */
701 bool DocumentImporter::writeVisualScene(const COLLADAFW::VisualScene *visualScene)
702 {
703         if (mImportStage == Fetching_Controller_data)
704                 return true;
705
706         // this method called on post process after writeGeometry, writeMaterial, etc.
707
708         // for each <node> in <visual_scene>:
709         // create an Object
710         // if Mesh (previously created in writeGeometry) to which <node> corresponds exists, link Object with that mesh
711
712         // update: since we cannot link a Mesh with Object in
713         // writeGeometry because <geometry> does not reference <node>,
714         // we link Objects with Meshes here
715
716         vscenes.push_back(visualScene);
717
718         return true;
719 }
720
721 /** When this method is called, the writer must handle all nodes contained in the
722  * library nodes.
723  * \return The writer should return true, if writing succeeded, false otherwise.*/
724 bool DocumentImporter::writeLibraryNodes(const COLLADAFW::LibraryNodes *libraryNodes)
725 {
726         if (mImportStage == Fetching_Controller_data)
727                 return true;
728
729         Scene *sce = CTX_data_scene(mContext);
730
731         const COLLADAFW::NodePointerArray& nodes = libraryNodes->getNodes();
732
733         fprintf(stderr, "+-- Read Library nodes ----------\n");
734         for (unsigned int i = 0; i < nodes.getCount(); i++) {
735                 std::vector<Object *> *child_objects;
736                 child_objects = write_node(nodes[i], NULL, sce, NULL, true);
737                 delete child_objects;
738         }
739         return true;
740 }
741
742 /** When this method is called, the writer must write the geometry.
743  * \return The writer should return true, if writing succeeded, false otherwise.*/
744 bool DocumentImporter::writeGeometry(const COLLADAFW::Geometry *geom)
745 {
746         if (mImportStage == Fetching_Controller_data)
747                 return true;
748
749         return mesh_importer.write_geometry(geom);
750 }
751
752 /** When this method is called, the writer must write the material.
753  * \return The writer should return true, if writing succeeded, false otherwise.*/
754 bool DocumentImporter::writeMaterial(const COLLADAFW::Material *cmat)
755 {
756         if (mImportStage == Fetching_Controller_data)
757                 return true;
758
759         Main *bmain = CTX_data_main(mContext);
760         const std::string& str_mat_id = cmat->getName().size() ? cmat->getName() : cmat->getOriginalId();
761         Material *ma = BKE_material_add(bmain, (char *)str_mat_id.c_str());
762
763         this->uid_effect_map[cmat->getInstantiatedEffect()] = ma;
764         this->uid_material_map[cmat->getUniqueId()] = ma;
765
766         return true;
767 }
768
769
770 void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Material *ma)
771 {
772         MaterialNode matNode = MaterialNode(mContext, ef, ma, uid_image_map);
773         matNode.set_reflectivity(ef->getReflectivity().getFloatValue());
774         matNode.set_ior(ef->getIndexOfRefraction().getFloatValue());
775         matNode.set_diffuse(ef->getDiffuse(), "Diffuse");
776         matNode.set_ambient(ef->getAmbient(), "Ambient");
777         matNode.set_specular(ef->getSpecular(), "Specular");
778         matNode.set_reflective(ef->getReflective(), "Reflective");
779         matNode.set_emission(ef->getEmission(), "Emission");
780         matNode.set_opacity(ef->getOpacity(), "Opacity");
781 }
782
783 /** When this method is called, the writer must write the effect.
784  * \return The writer should return true, if writing succeeded, false otherwise.*/
785
786 bool DocumentImporter::writeEffect(const COLLADAFW::Effect *effect)
787 {
788         if (mImportStage == Fetching_Controller_data)
789                 return true;
790
791         const COLLADAFW::UniqueId& uid = effect->getUniqueId();
792
793         if (uid_effect_map.find(uid) == uid_effect_map.end()) {
794                 fprintf(stderr, "Couldn't find a material by UID.\n");
795                 return true;
796         }
797
798         Material *ma = uid_effect_map[uid];
799         std::map<COLLADAFW::UniqueId, Material *>::iterator iter;
800         for (iter = uid_material_map.begin(); iter != uid_material_map.end(); iter++) {
801                 if (iter->second == ma) {
802                         this->FW_object_map[iter->first] = effect;
803                         break;
804                 }
805         }
806         COLLADAFW::CommonEffectPointerArray common_efs = effect->getCommonEffects();
807         if (common_efs.getCount() < 1) {
808                 fprintf(stderr, "Couldn't find <profile_COMMON>.\n");
809                 return true;
810         }
811         // XXX TODO: Take all <profile_common>s
812         // Currently only first <profile_common> is supported
813         COLLADAFW::EffectCommon *ef = common_efs[0];
814         write_profile_COMMON(ef, ma);
815         this->FW_object_map[effect->getUniqueId()] = effect;
816
817         return true;
818 }
819
820
821 /** When this method is called, the writer must write the camera.
822  * \return The writer should return true, if writing succeeded, false otherwise.*/
823 bool DocumentImporter::writeCamera(const COLLADAFW::Camera *camera)
824 {
825         if (mImportStage == Fetching_Controller_data)
826                 return true;
827
828         Main *bmain = CTX_data_main(mContext);
829         Camera *cam = NULL;
830         std::string cam_id, cam_name;
831
832         ExtraTags *et=getExtraTags(camera->getUniqueId());
833         cam_id = camera->getOriginalId();
834         cam_name = camera->getName();
835         if (cam_name.size()) cam = (Camera *)BKE_camera_add(bmain, (char *)cam_name.c_str());
836         else cam = (Camera *)BKE_camera_add(bmain, (char *)cam_id.c_str());
837
838         if (!cam) {
839                 fprintf(stderr, "Cannot create camera.\n");
840                 return true;
841         }
842
843         if (et && et->isProfile("blender")) {
844                 et->setData("shiftx",&(cam->shiftx));
845                 et->setData("shifty",&(cam->shifty));
846                 et->setData("YF_dofdist",&(cam->YF_dofdist));
847         }
848         cam->clipsta = camera->getNearClippingPlane().getValue();
849         cam->clipend = camera->getFarClippingPlane().getValue();
850
851         COLLADAFW::Camera::CameraType type = camera->getCameraType();
852         switch (type) {
853                 case COLLADAFW::Camera::ORTHOGRAPHIC:
854                 {
855                         cam->type = CAM_ORTHO;
856                 }
857                 break;
858                 case COLLADAFW::Camera::PERSPECTIVE:
859                 {
860                         cam->type = CAM_PERSP;
861                 }
862                 break;
863                 case COLLADAFW::Camera::UNDEFINED_CAMERATYPE:
864                 {
865                         fprintf(stderr, "Current camera type is not supported.\n");
866                         cam->type = CAM_PERSP;
867                 }
868                 break;
869         }
870
871         switch (camera->getDescriptionType()) {
872                 case COLLADAFW::Camera::ASPECTRATIO_AND_Y:
873                 {
874                         switch (cam->type) {
875                                 case CAM_ORTHO:
876                                 {
877                                         double ymag = 2 * camera->getYMag().getValue();
878                                         double aspect = camera->getAspectRatio().getValue();
879                                         double xmag = aspect * ymag;
880                                         cam->ortho_scale = (float)xmag;
881                                 }
882                                 break;
883                                 case CAM_PERSP:
884                                 default:
885                                 {
886                                         double yfov = camera->getYFov().getValue();
887                                         double aspect = camera->getAspectRatio().getValue();
888
889                                         // NOTE: Needs more testing (As we curretnly have no official test data for this)
890
891                                         double xfov = 2.0f * atanf(aspect * tanf(DEG2RADF(yfov) * 0.5f));
892                                         cam->lens = fov_to_focallength(xfov, cam->sensor_x);
893                                 }
894                                 break;
895                         }
896                 }
897                 break;
898                 /* XXX correct way to do following four is probably to get also render
899                  * size and determine proper settings from that somehow */
900                 case COLLADAFW::Camera::ASPECTRATIO_AND_X:
901                 case COLLADAFW::Camera::SINGLE_X:
902                 case COLLADAFW::Camera::X_AND_Y:
903                 {
904                         switch (cam->type) {
905                                 case CAM_ORTHO:
906                                         cam->ortho_scale = (float)camera->getXMag().getValue() * 2;
907                                         break;
908                                 case CAM_PERSP:
909                                 default:
910                                 {
911                                         double x = camera->getXFov().getValue();
912                                         // x is in degrees, cam->lens is in millimiters
913                                         cam->lens = fov_to_focallength(DEG2RADF(x), cam->sensor_x);
914                                 }
915                                 break;
916                         }
917                 }
918                 break;
919                 case COLLADAFW::Camera::SINGLE_Y:
920                 {
921                         switch (cam->type) {
922                                 case CAM_ORTHO:
923                                         cam->ortho_scale = (float)camera->getYMag().getValue();
924                                         break;
925                                 case CAM_PERSP:
926                                 default:
927                                 {
928                                         double yfov = camera->getYFov().getValue();
929                                         // yfov is in degrees, cam->lens is in millimiters
930                                         cam->lens = fov_to_focallength(DEG2RADF(yfov), cam->sensor_x);
931                                 }
932                                 break;
933                         }
934                 }
935                 break;
936                 case COLLADAFW::Camera::UNDEFINED:
937                         // read nothing, use blender defaults.
938                         break;
939         }
940
941         this->uid_camera_map[camera->getUniqueId()] = cam;
942         this->FW_object_map[camera->getUniqueId()] = camera;
943         // XXX import camera options
944         return true;
945 }
946
947 /** When this method is called, the writer must write the image.
948  * \return The writer should return true, if writing succeeded, false otherwise.*/
949 bool DocumentImporter::writeImage(const COLLADAFW::Image *image)
950 {
951         if (mImportStage == Fetching_Controller_data)
952                 return true;
953
954         const std::string& imagepath = image->getImageURI().toNativePath();
955
956         char dir[FILE_MAX];
957         char absolute_path[FILE_MAX];
958         const char *workpath;
959
960         BLI_split_dir_part(this->import_settings->filepath, dir, sizeof(dir));
961         BLI_join_dirfile(absolute_path, sizeof(absolute_path), dir, imagepath.c_str());
962         if (BLI_exists(absolute_path)) {
963                 workpath = absolute_path;
964         }
965         else {
966                 // Maybe imagepath was already absolute ?
967                 if (!BLI_exists(imagepath.c_str())) {
968                         fprintf(stderr, "|! Image not found: %s\n", imagepath.c_str() );
969                         return true;
970                 }
971                 workpath = imagepath.c_str();
972         }
973
974         Image *ima = BKE_image_load_exists(CTX_data_main(mContext), workpath);
975         if (!ima) {
976                 fprintf(stderr, "|! Cannot create image: %s\n", workpath);
977                 return true;
978         }
979         this->uid_image_map[image->getUniqueId()] = ima;
980         fprintf(stderr, "| import Image: %s\n", workpath);
981         return true;
982 }
983
984 /** When this method is called, the writer must write the light.
985  * \return The writer should return true, if writing succeeded, false otherwise.*/
986 bool DocumentImporter::writeLight(const COLLADAFW::Light *light)
987 {
988         if (mImportStage == Fetching_Controller_data)
989                 return true;
990
991         Main *bmain = CTX_data_main(mContext);
992         Lamp *lamp = NULL;
993         std::string la_id, la_name;
994
995         ExtraTags *et = getExtraTags(light->getUniqueId());
996         /*TagsMap::iterator etit;
997         ExtraTags *et = 0;
998         etit = uid_tags_map.find(light->getUniqueId().toAscii());
999         if (etit != uid_tags_map.end())
1000                 et = etit->second;*/
1001
1002         la_id = light->getOriginalId();
1003         la_name = light->getName();
1004         if (la_name.size()) lamp = (Lamp *)BKE_lamp_add(bmain, (char *)la_name.c_str());
1005         else lamp = (Lamp *)BKE_lamp_add(bmain, (char *)la_id.c_str());
1006
1007         if (!lamp) {
1008                 fprintf(stderr, "Cannot create light.\n");
1009                 return true;
1010         }
1011
1012         // if we find an ExtraTags for this, use that instead.
1013         if (et && et->isProfile("blender")) {
1014                 et->setData("type", &(lamp->type));
1015                 et->setData("flag", &(lamp->flag));
1016                 et->setData("mode", &(lamp->mode));
1017                 et->setData("gamma", &(lamp->k));
1018                 et->setData("red", &(lamp->r));
1019                 et->setData("green", &(lamp->g));
1020                 et->setData("blue", &(lamp->b));
1021                 et->setData("shadow_r", &(lamp->shdwr));
1022                 et->setData("shadow_g", &(lamp->shdwg));
1023                 et->setData("shadow_b", &(lamp->shdwb));
1024                 et->setData("energy", &(lamp->energy));
1025                 et->setData("dist", &(lamp->dist));
1026                 et->setData("spotsize", &(lamp->spotsize));
1027                 lamp->spotsize = DEG2RADF(lamp->spotsize);
1028                 et->setData("spotblend", &(lamp->spotblend));
1029                 et->setData("att1", &(lamp->att1));
1030                 et->setData("att2", &(lamp->att2));
1031                 et->setData("falloff_type", &(lamp->falloff_type));
1032                 et->setData("clipsta", &(lamp->clipsta));
1033                 et->setData("clipend", &(lamp->clipend));
1034                 et->setData("bias", &(lamp->bias));
1035                 et->setData("soft", &(lamp->soft));
1036                 et->setData("bufsize", &(lamp->bufsize));
1037                 et->setData("buffers", &(lamp->buffers));
1038                 et->setData("area_shape", &(lamp->area_shape));
1039                 et->setData("area_size", &(lamp->area_size));
1040                 et->setData("area_sizey", &(lamp->area_sizey));
1041                 et->setData("area_sizez", &(lamp->area_sizez));
1042         }
1043         else {
1044                 float constatt = light->getConstantAttenuation().getValue();
1045                 float linatt = light->getLinearAttenuation().getValue();
1046                 float quadatt = light->getQuadraticAttenuation().getValue();
1047                 float d = 25.0f;
1048                 float att1 = 0.0f;
1049                 float att2 = 0.0f;
1050                 float e = 1.0f;
1051
1052                 if (light->getColor().isValid()) {
1053                         COLLADAFW::Color col = light->getColor();
1054                         lamp->r = col.getRed();
1055                         lamp->g = col.getGreen();
1056                         lamp->b = col.getBlue();
1057                 }
1058
1059                 if (IS_EQ(linatt, 0.0f) && quadatt > 0.0f) {
1060                         att2 = quadatt;
1061                         d = sqrt(1.0f / quadatt);
1062                 }
1063                 // linear light
1064                 else if (IS_EQ(quadatt, 0.0f) && linatt > 0.0f) {
1065                         att1 = linatt;
1066                         d = (1.0f / linatt);
1067                 }
1068                 else if (IS_EQ(constatt, 1.0f)) {
1069                         att1 = 1.0f;
1070                 }
1071                 else {
1072                         // assuming point light (const att = 1.0);
1073                         att1 = 1.0f;
1074                 }
1075
1076                 d *= (1.0f / unit_converter.getLinearMeter());
1077
1078                 lamp->energy = e;
1079                 lamp->dist = d;
1080
1081                 switch (light->getLightType()) {
1082                         case COLLADAFW::Light::AMBIENT_LIGHT:
1083                         {
1084                                 lamp->type = LA_SUN; //TODO needs more thoughts
1085                         }
1086                         break;
1087                         case COLLADAFW::Light::SPOT_LIGHT:
1088                         {
1089                                 lamp->type = LA_SPOT;
1090                                 lamp->att1 = att1;
1091                                 lamp->att2 = att2;
1092                                 if (IS_EQ(att1, 0.0f) && att2 > 0)
1093                                         lamp->falloff_type = LA_FALLOFF_INVSQUARE;
1094                                 if (IS_EQ(att2, 0.0f) && att1 > 0)
1095                                         lamp->falloff_type = LA_FALLOFF_INVLINEAR;
1096                                 lamp->spotsize = DEG2RADF(light->getFallOffAngle().getValue());
1097                                 lamp->spotblend = light->getFallOffExponent().getValue();
1098                         }
1099                         break;
1100                         case COLLADAFW::Light::DIRECTIONAL_LIGHT:
1101                         {
1102                                 /* our sun is very strong, so pick a smaller energy level */
1103                                 lamp->type = LA_SUN;
1104                         }
1105                         break;
1106                         case COLLADAFW::Light::POINT_LIGHT:
1107                         {
1108                                 lamp->type = LA_LOCAL;
1109                                 lamp->att1 = att1;
1110                                 lamp->att2 = att2;
1111                                 if (IS_EQ(att1, 0.0f) && att2 > 0)
1112                                         lamp->falloff_type = LA_FALLOFF_INVSQUARE;
1113                                 if (IS_EQ(att2, 0.0f) && att1 > 0)
1114                                         lamp->falloff_type = LA_FALLOFF_INVLINEAR;
1115                         }
1116                         break;
1117                         case COLLADAFW::Light::UNDEFINED:
1118                         {
1119                                 fprintf(stderr, "Current light type is not supported.\n");
1120                                 lamp->type = LA_LOCAL;
1121                         }
1122                         break;
1123                 }
1124         }
1125
1126         this->uid_lamp_map[light->getUniqueId()] = lamp;
1127         this->FW_object_map[light->getUniqueId()] = light;
1128         return true;
1129 }
1130
1131 // this function is called only for animations that pass COLLADAFW::validate
1132 bool DocumentImporter::writeAnimation(const COLLADAFW::Animation *anim)
1133 {
1134         if (mImportStage == Fetching_Controller_data)
1135                 return true;
1136
1137         // return true;
1138         return anim_importer.write_animation(anim);
1139 }
1140
1141 // called on post-process stage after writeVisualScenes
1142 bool DocumentImporter::writeAnimationList(const COLLADAFW::AnimationList *animationList)
1143 {
1144         if (mImportStage == Fetching_Controller_data)
1145                 return true;
1146
1147         // return true;
1148         return anim_importer.write_animation_list(animationList);
1149 }
1150
1151 #if OPENCOLLADA_WITH_ANIMATION_CLIP
1152 // Since opencollada 1.6.68
1153 // called on post-process stage after writeVisualScenes
1154 bool DocumentImporter::writeAnimationClip(const COLLADAFW::AnimationClip *AnimationClip)
1155 {
1156         if (mImportStage == Fetching_Controller_data)
1157                 return true;
1158
1159         return true;
1160         //return animation_clip_importer.write_animation_clip(animationClip); // TODO: implement import of AnimationClips
1161 }
1162 #endif
1163
1164 /** When this method is called, the writer must write the skin controller data.
1165  * \return The writer should return true, if writing succeeded, false otherwise.*/
1166 bool DocumentImporter::writeSkinControllerData(const COLLADAFW::SkinControllerData *skin)
1167 {
1168         return armature_importer.write_skin_controller_data(skin);
1169 }
1170
1171 // this is called on postprocess, before writeVisualScenes
1172 bool DocumentImporter::writeController(const COLLADAFW::Controller *controller)
1173 {
1174         if (mImportStage == Fetching_Controller_data)
1175                 return true;
1176
1177         return armature_importer.write_controller(controller);
1178 }
1179
1180 bool DocumentImporter::writeFormulas(const COLLADAFW::Formulas *formulas)
1181 {
1182         return true;
1183 }
1184
1185 bool DocumentImporter::writeKinematicsScene(const COLLADAFW::KinematicsScene *kinematicsScene)
1186 {
1187         return true;
1188 }
1189
1190 ExtraTags *DocumentImporter::getExtraTags(const COLLADAFW::UniqueId &uid)
1191 {
1192         if (uid_tags_map.find(uid.toAscii()) == uid_tags_map.end()) {
1193                 return NULL;
1194         }
1195         return uid_tags_map[uid.toAscii()];
1196 }
1197
1198 bool DocumentImporter::addExtraTags(const COLLADAFW::UniqueId &uid, ExtraTags *extra_tags)
1199 {
1200         uid_tags_map[uid.toAscii()] = extra_tags;
1201         return true;
1202 }
1203
1204 bool DocumentImporter::is_armature(COLLADAFW::Node *node)
1205 {
1206         COLLADAFW::NodePointerArray &child_nodes = node->getChildNodes();
1207         for (unsigned int i = 0; i < child_nodes.getCount(); i++) {
1208                 if (child_nodes[i]->getType() == COLLADAFW::Node::JOINT) {
1209                         return true;
1210                 }
1211                 else {
1212                         continue;
1213                 }
1214         }
1215
1216         //no child is JOINT
1217         return false;
1218 }