Changeset View
Changeset View
Standalone View
Standalone View
source/blender/collada/DocumentImporter.cpp
| Show First 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | |||||
| // creates empties for each imported bone on layer 2, for debugging | // creates empties for each imported bone on layer 2, for debugging | ||||
| // #define ARMATURE_TEST | // #define ARMATURE_TEST | ||||
| DocumentImporter::DocumentImporter(bContext *C, const ImportSettings *import_settings) : | DocumentImporter::DocumentImporter(bContext *C, const ImportSettings *import_settings) : | ||||
| import_settings(import_settings), | import_settings(import_settings), | ||||
| mImportStage(General), | mImportStage(General), | ||||
| mContext(C), | mContext(C), | ||||
| view_layer(CTX_data_view_layer(mContext)), | view_layer(CTX_data_view_layer(mContext)), | ||||
| armature_importer(&unit_converter, &mesh_importer, CTX_data_scene(C), view_layer, import_settings), | armature_importer(&unit_converter, &mesh_importer, CTX_data_scene(C), view_layer, CTX_wm_workspace(C), import_settings), | ||||
| mesh_importer(&unit_converter, &armature_importer, CTX_data_scene(C), view_layer), | mesh_importer(&unit_converter, &armature_importer, CTX_data_scene(C), view_layer, CTX_wm_workspace(C)), | ||||
| anim_importer(&unit_converter, &armature_importer, CTX_data_scene(C)) | anim_importer(&unit_converter, &armature_importer, CTX_data_scene(C)) | ||||
| { | { | ||||
| } | } | ||||
| DocumentImporter::~DocumentImporter() | DocumentImporter::~DocumentImporter() | ||||
| { | { | ||||
| TagsMap::iterator etit; | TagsMap::iterator etit; | ||||
| etit = uid_tags_map.begin(); | etit = uid_tags_map.begin(); | ||||
| ▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| void DocumentImporter::finish() | void DocumentImporter::finish() | ||||
| { | { | ||||
| if (mImportStage != General) | if (mImportStage != General) | ||||
| return; | return; | ||||
| Main *bmain = CTX_data_main(mContext); | Main *bmain = CTX_data_main(mContext); | ||||
| WorkSpace *workspace = CTX_wm_workspace(mContext); | |||||
| // TODO: create a new scene except the selected <visual_scene> - use current blender scene for it | // TODO: create a new scene except the selected <visual_scene> - use current blender scene for it | ||||
| Scene *sce = CTX_data_scene(mContext); | Scene *sce = CTX_data_scene(mContext); | ||||
| unit_converter.calculate_scale(*sce); | unit_converter.calculate_scale(*sce); | ||||
| std::vector<Object *> *objects_to_scale = new std::vector<Object *>(); | std::vector<Object *> *objects_to_scale = new std::vector<Object *>(); | ||||
| /** TODO Break up and put into 2-pass parsing of DAE */ | /** TODO Break up and put into 2-pass parsing of DAE */ | ||||
| std::vector<const COLLADAFW::VisualScene *>::iterator it; | std::vector<const COLLADAFW::VisualScene *>::iterator it; | ||||
| Show All 25 Lines | if (this->import_settings->import_units) { | ||||
| RNA_property_float_set(&unit_settings, scale, unit_factor); | RNA_property_float_set(&unit_settings, scale, unit_factor); | ||||
| fprintf(stdout, "Collada: Adjusting Blender units to Importset units: %f.\n", unit_factor); | fprintf(stdout, "Collada: Adjusting Blender units to Importset units: %f.\n", unit_factor); | ||||
| } | } | ||||
| // Write nodes to scene | // Write nodes to scene | ||||
| const COLLADAFW::NodePointerArray& roots = (*it)->getRootNodes(); | const COLLADAFW::NodePointerArray& roots = (*it)->getRootNodes(); | ||||
| for (unsigned int i = 0; i < roots.getCount(); i++) { | for (unsigned int i = 0; i < roots.getCount(); i++) { | ||||
| std::vector<Object *> *objects_done = write_node(roots[i], NULL, sce, NULL, false); | std::vector<Object *> *objects_done = write_node(roots[i], NULL, sce, workspace, NULL, false); | ||||
| objects_to_scale->insert(objects_to_scale->end(), objects_done->begin(), objects_done->end()); | objects_to_scale->insert(objects_to_scale->end(), objects_done->begin(), objects_done->end()); | ||||
| delete objects_done; | delete objects_done; | ||||
| } | } | ||||
| // update scene | // update scene | ||||
| DEG_relations_tag_update(bmain); | DEG_relations_tag_update(bmain); | ||||
| WM_event_add_notifier(mContext, NC_OBJECT | ND_TRANSFORM, NULL); | WM_event_add_notifier(mContext, NC_OBJECT | ND_TRANSFORM, NULL); | ||||
| ▲ Show 20 Lines • Show All 127 Lines • ▼ Show 20 Lines | |||||
| /** When this method is called, the writer must write the scene. | /** When this method is called, the writer must write the scene. | ||||
| * \return The writer should return true, if writing succeeded, false otherwise.*/ | * \return The writer should return true, if writing succeeded, false otherwise.*/ | ||||
| bool DocumentImporter::writeScene(const COLLADAFW::Scene *scene) | bool DocumentImporter::writeScene(const COLLADAFW::Scene *scene) | ||||
| { | { | ||||
| // XXX could store the scene id, but do nothing for now | // XXX could store the scene id, but do nothing for now | ||||
| return true; | return true; | ||||
| } | } | ||||
| Object *DocumentImporter::create_camera_object(COLLADAFW::InstanceCamera *camera, Scene *sce) | Object *DocumentImporter::create_camera_object(COLLADAFW::InstanceCamera *camera, Scene *sce, WorkSpace *workspace) | ||||
| { | { | ||||
| const COLLADAFW::UniqueId& cam_uid = camera->getInstanciatedObjectId(); | const COLLADAFW::UniqueId& cam_uid = camera->getInstanciatedObjectId(); | ||||
| if (uid_camera_map.find(cam_uid) == uid_camera_map.end()) { | if (uid_camera_map.find(cam_uid) == uid_camera_map.end()) { | ||||
| // fprintf(stderr, "Couldn't find camera by UID.\n"); | // fprintf(stderr, "Couldn't find camera by UID.\n"); | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| Object *ob = bc_add_object(sce, view_layer, OB_CAMERA, NULL); | Object *ob = bc_add_object(sce, view_layer, workspace, OB_CAMERA, NULL); | ||||
| Camera *cam = uid_camera_map[cam_uid]; | Camera *cam = uid_camera_map[cam_uid]; | ||||
| Camera *old_cam = (Camera *)ob->data; | Camera *old_cam = (Camera *)ob->data; | ||||
| ob->data = cam; | ob->data = cam; | ||||
| BKE_libblock_free_us(G.main, old_cam); | BKE_libblock_free_us(G.main, old_cam); | ||||
| return ob; | return ob; | ||||
| } | } | ||||
| Object *DocumentImporter::create_lamp_object(COLLADAFW::InstanceLight *lamp, Scene *sce) | Object *DocumentImporter::create_lamp_object(COLLADAFW::InstanceLight *lamp, Scene *sce, WorkSpace *workspace) | ||||
| { | { | ||||
| const COLLADAFW::UniqueId& lamp_uid = lamp->getInstanciatedObjectId(); | const COLLADAFW::UniqueId& lamp_uid = lamp->getInstanciatedObjectId(); | ||||
| if (uid_lamp_map.find(lamp_uid) == uid_lamp_map.end()) { | if (uid_lamp_map.find(lamp_uid) == uid_lamp_map.end()) { | ||||
| fprintf(stderr, "Couldn't find lamp by UID.\n"); | fprintf(stderr, "Couldn't find lamp by UID.\n"); | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| Object *ob = bc_add_object(sce, view_layer, OB_LAMP, NULL); | Object *ob = bc_add_object(sce, view_layer, workspace, OB_LAMP, NULL); | ||||
| Lamp *la = uid_lamp_map[lamp_uid]; | Lamp *la = uid_lamp_map[lamp_uid]; | ||||
| Lamp *old_lamp = (Lamp *)ob->data; | Lamp *old_lamp = (Lamp *)ob->data; | ||||
| ob->data = la; | ob->data = la; | ||||
| BKE_libblock_free_us(G.main, old_lamp); | BKE_libblock_free_us(G.main, old_lamp); | ||||
| return ob; | return ob; | ||||
| } | } | ||||
| Object *DocumentImporter::create_instance_node(Object *source_ob, COLLADAFW::Node *source_node, COLLADAFW::Node *instance_node, Scene *sce, bool is_library_node) | Object *DocumentImporter::create_instance_node(Object *source_ob, COLLADAFW::Node *source_node, COLLADAFW::Node *instance_node, Scene *sce, bool is_library_node) | ||||
| ▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | void DocumentImporter::report_unknown_reference(const COLLADAFW::Node &node, const std::string object_type) | ||||
| std::string name = node.getName(); | std::string name = node.getName(); | ||||
| fprintf(stderr, | fprintf(stderr, | ||||
| "error: node id=\"%s\", name=\"%s\" refers to an undefined %s.\n", | "error: node id=\"%s\", name=\"%s\" refers to an undefined %s.\n", | ||||
| id.c_str(), | id.c_str(), | ||||
| name.c_str(), | name.c_str(), | ||||
| object_type.c_str()); | object_type.c_str()); | ||||
| } | } | ||||
| std::vector<Object *> *DocumentImporter::write_node(COLLADAFW::Node *node, COLLADAFW::Node *parent_node, Scene *sce, Object *par, bool is_library_node) | std::vector<Object *> *DocumentImporter::write_node(COLLADAFW::Node *node, COLLADAFW::Node *parent_node, Scene *sce, Object *par, WorkSpace *workspace, bool is_library_node) | ||||
| { | { | ||||
| Object *ob = NULL; | Object *ob = NULL; | ||||
| bool is_joint = node->getType() == COLLADAFW::Node::JOINT; | bool is_joint = node->getType() == COLLADAFW::Node::JOINT; | ||||
| bool read_transform = true; | bool read_transform = true; | ||||
| std::string id = node->getOriginalId(); | std::string id = node->getOriginalId(); | ||||
| std::string name = node->getName(); | std::string name = node->getName(); | ||||
| // if node has child nodes write them | // if node has child nodes write them | ||||
| COLLADAFW::NodePointerArray &child_nodes = node->getChildNodes(); | COLLADAFW::NodePointerArray &child_nodes = node->getChildNodes(); | ||||
| std::vector<Object *> *objects_done = new std::vector<Object *>(); | std::vector<Object *> *objects_done = new std::vector<Object *>(); | ||||
| std::vector<Object *> *root_objects = new std::vector<Object *>(); | std::vector<Object *> *root_objects = new std::vector<Object *>(); | ||||
| fprintf(stderr, | fprintf(stderr, | ||||
| "Writing node id='%s', name='%s'\n", | "Writing node id='%s', name='%s'\n", | ||||
| id.c_str(), | id.c_str(), | ||||
| name.c_str()); | name.c_str()); | ||||
| if (is_joint) { | if (is_joint) { | ||||
| if (parent_node == NULL && !is_library_node) { | if (parent_node == NULL && !is_library_node) { | ||||
| // A Joint on root level is a skeleton without root node. | // A Joint on root level is a skeleton without root node. | ||||
| // Here we add the armature "on the fly": | // Here we add the armature "on the fly": | ||||
| par = bc_add_object(sce, view_layer, OB_ARMATURE, std::string("Armature").c_str()); | par = bc_add_object(sce, view_layer, workspace, OB_ARMATURE, std::string("Armature").c_str()); | ||||
| objects_done->push_back(par); | objects_done->push_back(par); | ||||
| root_objects->push_back(par); | root_objects->push_back(par); | ||||
| object_map.insert(std::pair<COLLADAFW::UniqueId, Object *>(node->getUniqueId(), par)); | object_map.insert(std::pair<COLLADAFW::UniqueId, Object *>(node->getUniqueId(), par)); | ||||
| node_map[node->getUniqueId()] = node; | node_map[node->getUniqueId()] = node; | ||||
| } | } | ||||
| if (parent_node == NULL || parent_node->getType() != COLLADAFW::Node::JOINT) { | if (parent_node == NULL || parent_node->getType() != COLLADAFW::Node::JOINT) { | ||||
| armature_importer.add_root_joint(node, par); | armature_importer.add_root_joint(node, par); | ||||
| } | } | ||||
| Show All 29 Lines | while (geom_done < geom.getCount()) { | ||||
| objects_done->push_back(ob); | objects_done->push_back(ob); | ||||
| if (parent_node == NULL) { | if (parent_node == NULL) { | ||||
| root_objects->push_back(ob); | root_objects->push_back(ob); | ||||
| } | } | ||||
| } | } | ||||
| ++geom_done; | ++geom_done; | ||||
| } | } | ||||
| while (camera_done < camera.getCount()) { | while (camera_done < camera.getCount()) { | ||||
| ob = create_camera_object(camera[camera_done], sce); | ob = create_camera_object(camera[camera_done], sce, workspace); | ||||
| if (ob == NULL) { | if (ob == NULL) { | ||||
| report_unknown_reference(*node, "instance_camera"); | report_unknown_reference(*node, "instance_camera"); | ||||
| } | } | ||||
| else { | else { | ||||
| objects_done->push_back(ob); | objects_done->push_back(ob); | ||||
| if (parent_node == NULL) { | if (parent_node == NULL) { | ||||
| root_objects->push_back(ob); | root_objects->push_back(ob); | ||||
| } | } | ||||
| } | } | ||||
| ++camera_done; | ++camera_done; | ||||
| } | } | ||||
| while (lamp_done < lamp.getCount()) { | while (lamp_done < lamp.getCount()) { | ||||
| ob = create_lamp_object(lamp[lamp_done], sce); | ob = create_lamp_object(lamp[lamp_done], sce, workspace); | ||||
| if (ob == NULL) { | if (ob == NULL) { | ||||
| report_unknown_reference(*node, "instance_lamp"); | report_unknown_reference(*node, "instance_lamp"); | ||||
| } | } | ||||
| else { | else { | ||||
| objects_done->push_back(ob); | objects_done->push_back(ob); | ||||
| if (parent_node == NULL) { | if (parent_node == NULL) { | ||||
| root_objects->push_back(ob); | root_objects->push_back(ob); | ||||
| } | } | ||||
| Show All 38 Lines | while (inst_done < inst_node.getCount()) { | ||||
| read_transform = false; | read_transform = false; | ||||
| } | } | ||||
| // if node is empty - create empty object | // if node is empty - create empty object | ||||
| // XXX empty node may not mean it is empty object, not sure about this | // XXX empty node may not mean it is empty object, not sure about this | ||||
| if ( (geom_done + camera_done + lamp_done + controller_done + inst_done) < 1) { | if ( (geom_done + camera_done + lamp_done + controller_done + inst_done) < 1) { | ||||
| //Check if Object is armature, by checking if immediate child is a JOINT node. | //Check if Object is armature, by checking if immediate child is a JOINT node. | ||||
| if (is_armature(node)) { | if (is_armature(node)) { | ||||
| ob = bc_add_object(sce, view_layer, OB_ARMATURE, name.c_str()); | ob = bc_add_object(sce, view_layer, workspace, OB_ARMATURE, name.c_str()); | ||||
| } | } | ||||
| else { | else { | ||||
| ob = bc_add_object(sce, view_layer, OB_EMPTY, NULL); | ob = bc_add_object(sce, view_layer, workspace, OB_EMPTY, NULL); | ||||
| } | } | ||||
| objects_done->push_back(ob); | objects_done->push_back(ob); | ||||
| if (parent_node == NULL) { | if (parent_node == NULL) { | ||||
| root_objects->push_back(ob); | root_objects->push_back(ob); | ||||
| } | } | ||||
| } | } | ||||
| // XXX: if there're multiple instances, only one is stored | // XXX: if there're multiple instances, only one is stored | ||||
| Show All 39 Lines | if (objects_done->size() > 0) { | ||||
| ob = *objects_done->begin(); | ob = *objects_done->begin(); | ||||
| } | } | ||||
| else { | else { | ||||
| ob = NULL; | ob = NULL; | ||||
| } | } | ||||
| for (unsigned int i = 0; i < child_nodes.getCount(); i++) { | for (unsigned int i = 0; i < child_nodes.getCount(); i++) { | ||||
| std::vector<Object *> *child_objects; | std::vector<Object *> *child_objects; | ||||
| child_objects = write_node(child_nodes[i], node, sce, ob, is_library_node); | child_objects = write_node(child_nodes[i], node, sce, ob, workspace, is_library_node); | ||||
| delete child_objects; | delete child_objects; | ||||
| } | } | ||||
| finally: | finally: | ||||
| delete objects_done; | delete objects_done; | ||||
| return root_objects; | return root_objects; | ||||
| Show All 25 Lines | |||||
| * library nodes. | * library nodes. | ||||
| * \return The writer should return true, if writing succeeded, false otherwise.*/ | * \return The writer should return true, if writing succeeded, false otherwise.*/ | ||||
| bool DocumentImporter::writeLibraryNodes(const COLLADAFW::LibraryNodes *libraryNodes) | bool DocumentImporter::writeLibraryNodes(const COLLADAFW::LibraryNodes *libraryNodes) | ||||
| { | { | ||||
| if (mImportStage != General) | if (mImportStage != General) | ||||
| return true; | return true; | ||||
| Scene *sce = CTX_data_scene(mContext); | Scene *sce = CTX_data_scene(mContext); | ||||
| WorkSpace *workspace = CTX_wm_workspace(mContext); | |||||
| const COLLADAFW::NodePointerArray& nodes = libraryNodes->getNodes(); | const COLLADAFW::NodePointerArray& nodes = libraryNodes->getNodes(); | ||||
| for (unsigned int i = 0; i < nodes.getCount(); i++) { | for (unsigned int i = 0; i < nodes.getCount(); i++) { | ||||
| std::vector<Object *> *child_objects; | std::vector<Object *> *child_objects; | ||||
| child_objects = write_node(nodes[i], NULL, sce, NULL, true); | child_objects = write_node(nodes[i], NULL, sce, NULL, workspace, true); | ||||
| delete child_objects; | delete child_objects; | ||||
| } | } | ||||
| return true; | return true; | ||||
| } | } | ||||
| /** When this method is called, the writer must write the geometry. | /** When this method is called, the writer must write the geometry. | ||||
| * \return The writer should return true, if writing succeeded, false otherwise.*/ | * \return The writer should return true, if writing succeeded, false otherwise.*/ | ||||
| ▲ Show 20 Lines • Show All 650 Lines • Show Last 20 Lines | |||||