|
Blender
V2.59
|
00001 /* 00002 * $Id: ArmatureImporter.cpp 37664 2011-06-20 12:43:10Z jesterking $ 00003 * 00004 * ***** BEGIN GPL LICENSE BLOCK ***** 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Nathan Letwory. 00021 * 00022 * ***** END GPL LICENSE BLOCK ***** 00023 */ 00024 00030 /* COLLADABU_ASSERT, may be able to remove later */ 00031 #include "COLLADABUPlatform.h" 00032 00033 #include <algorithm> 00034 00035 #include "COLLADAFWUniqueId.h" 00036 00037 #include "BKE_action.h" 00038 #include "BKE_depsgraph.h" 00039 #include "BKE_object.h" 00040 #include "BLI_string.h" 00041 #include "ED_armature.h" 00042 00043 #include "ArmatureImporter.h" 00044 00045 // use node name, or fall back to original id if not present (name is optional) 00046 template<class T> 00047 static const char *bc_get_joint_name(T *node) 00048 { 00049 const std::string& id = node->getName(); 00050 return id.size() ? id.c_str() : node->getOriginalId().c_str(); 00051 } 00052 00053 ArmatureImporter::ArmatureImporter(UnitConverter *conv, MeshImporterBase *mesh, AnimationImporterBase *anim, Scene *sce) : 00054 TransformReader(conv), scene(sce), empty(NULL), mesh_importer(mesh), anim_importer(anim) {} 00055 00056 ArmatureImporter::~ArmatureImporter() 00057 { 00058 // free skin controller data if we forget to do this earlier 00059 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it; 00060 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) { 00061 it->second.free(); 00062 } 00063 } 00064 00065 #if 0 00066 JointData *ArmatureImporter::get_joint_data(COLLADAFW::Node *node); 00067 { 00068 const COLLADAFW::UniqueId& joint_id = node->getUniqueId(); 00069 00070 if (joint_id_to_joint_index_map.find(joint_id) == joint_id_to_joint_index_map.end()) { 00071 fprintf(stderr, "Cannot find a joint index by joint id for %s.\n", 00072 node->getOriginalId().c_str()); 00073 return NULL; 00074 } 00075 00076 int joint_index = joint_id_to_joint_index_map[joint_id]; 00077 00078 return &joint_index_to_joint_info_map[joint_index]; 00079 } 00080 #endif 00081 00082 void ArmatureImporter::create_bone(SkinInfo& skin, COLLADAFW::Node *node, EditBone *parent, int totchild, 00083 float parent_mat[][4], bArmature *arm) 00084 { 00085 float joint_inv_bind_mat[4][4]; 00086 00087 // JointData* jd = get_joint_data(node); 00088 00089 float mat[4][4]; 00090 00091 if (skin.get_joint_inv_bind_matrix(joint_inv_bind_mat, node)) { 00092 // get original world-space matrix 00093 invert_m4_m4(mat, joint_inv_bind_mat); 00094 } 00095 // create a bone even if there's no joint data for it (i.e. it has no influence) 00096 else { 00097 float obmat[4][4]; 00098 00099 // object-space 00100 get_node_mat(obmat, node, NULL, NULL); 00101 00102 // get world-space 00103 if (parent) 00104 mul_m4_m4m4(mat, obmat, parent_mat); 00105 else 00106 copy_m4_m4(mat, obmat); 00107 } 00108 00109 // TODO rename from Node "name" attrs later 00110 EditBone *bone = ED_armature_edit_bone_add(arm, (char*)bc_get_joint_name(node)); 00111 totbone++; 00112 00113 if (parent) bone->parent = parent; 00114 00115 // set head 00116 copy_v3_v3(bone->head, mat[3]); 00117 00118 // set tail, don't set it to head because 0-length bones are not allowed 00119 float vec[3] = {0.0f, 0.5f, 0.0f}; 00120 add_v3_v3v3(bone->tail, bone->head, vec); 00121 00122 // set parent tail 00123 if (parent && totchild == 1) { 00124 copy_v3_v3(parent->tail, bone->head); 00125 00126 // not setting BONE_CONNECTED because this would lock child bone location with respect to parent 00127 // bone->flag |= BONE_CONNECTED; 00128 00129 // XXX increase this to prevent "very" small bones? 00130 const float epsilon = 0.000001f; 00131 00132 // derive leaf bone length 00133 float length = len_v3v3(parent->head, parent->tail); 00134 if ((length < leaf_bone_length || totbone == 0) && length > epsilon) { 00135 leaf_bone_length = length; 00136 } 00137 00138 // treat zero-sized bone like a leaf bone 00139 if (length <= epsilon) { 00140 add_leaf_bone(parent_mat, parent); 00141 } 00142 00143 /* 00144 #if 0 00145 // and which row in mat is bone direction 00146 float vec[3]; 00147 sub_v3_v3v3(vec, parent->tail, parent->head); 00148 #ifdef COLLADA_DEBUG 00149 print_v3("tail - head", vec); 00150 print_m4("matrix", parent_mat); 00151 #endif 00152 for (int i = 0; i < 3; i++) { 00153 #ifdef COLLADA_DEBUG 00154 char *axis_names[] = {"X", "Y", "Z"}; 00155 printf("%s-axis length is %f\n", axis_names[i], len_v3(parent_mat[i])); 00156 #endif 00157 float angle = angle_v2v2(vec, parent_mat[i]); 00158 if (angle < min_angle) { 00159 #ifdef COLLADA_DEBUG 00160 print_v3("picking", parent_mat[i]); 00161 printf("^ %s axis of %s's matrix\n", axis_names[i], get_dae_name(node)); 00162 #endif 00163 bone_direction_row = i; 00164 min_angle = angle; 00165 } 00166 } 00167 #endif 00168 */ 00169 } 00170 00171 COLLADAFW::NodePointerArray& children = node->getChildNodes(); 00172 for (unsigned int i = 0; i < children.getCount(); i++) { 00173 create_bone(skin, children[i], bone, children.getCount(), mat, arm); 00174 } 00175 00176 // in second case it's not a leaf bone, but we handle it the same way 00177 if (!children.getCount() || children.getCount() > 1) { 00178 add_leaf_bone(mat, bone); 00179 } 00180 } 00181 00182 void ArmatureImporter::add_leaf_bone(float mat[][4], EditBone *bone) 00183 { 00184 LeafBone leaf; 00185 00186 leaf.bone = bone; 00187 copy_m4_m4(leaf.mat, mat); 00188 BLI_strncpy(leaf.name, bone->name, sizeof(leaf.name)); 00189 00190 leaf_bones.push_back(leaf); 00191 } 00192 00193 void ArmatureImporter::fix_leaf_bones() 00194 { 00195 // just setting tail for leaf bones here 00196 00197 std::vector<LeafBone>::iterator it; 00198 for (it = leaf_bones.begin(); it != leaf_bones.end(); it++) { 00199 LeafBone& leaf = *it; 00200 00201 // pointing up 00202 float vec[3] = {0.0f, 0.0f, 1.0f}; 00203 00204 mul_v3_fl(vec, leaf_bone_length); 00205 00206 copy_v3_v3(leaf.bone->tail, leaf.bone->head); 00207 add_v3_v3v3(leaf.bone->tail, leaf.bone->head, vec); 00208 } 00209 } 00210 00211 #if 0 00212 void ArmatureImporter::set_leaf_bone_shapes(Object *ob_arm) 00213 { 00214 bPose *pose = ob_arm->pose; 00215 00216 std::vector<LeafBone>::iterator it; 00217 for (it = leaf_bones.begin(); it != leaf_bones.end(); it++) { 00218 LeafBone& leaf = *it; 00219 00220 bPoseChannel *pchan = get_pose_channel(pose, leaf.name); 00221 if (pchan) { 00222 pchan->custom = get_empty_for_leaves(); 00223 } 00224 else { 00225 fprintf(stderr, "Cannot find a pose channel for leaf bone %s\n", leaf.name); 00226 } 00227 } 00228 } 00229 00230 void ArmatureImporter::set_euler_rotmode() 00231 { 00232 // just set rotmode = ROT_MODE_EUL on pose channel for each joint 00233 00234 std::map<COLLADAFW::UniqueId, COLLADAFW::Node*>::iterator it; 00235 00236 for (it = joint_by_uid.begin(); it != joint_by_uid.end(); it++) { 00237 00238 COLLADAFW::Node *joint = it->second; 00239 00240 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator sit; 00241 00242 for (sit = skin_by_data_uid.begin(); sit != skin_by_data_uid.end(); sit++) { 00243 SkinInfo& skin = sit->second; 00244 00245 if (skin.uses_joint_or_descendant(joint)) { 00246 bPoseChannel *pchan = skin.get_pose_channel_from_node(joint); 00247 00248 if (pchan) { 00249 pchan->rotmode = ROT_MODE_EUL; 00250 } 00251 else { 00252 fprintf(stderr, "Cannot find pose channel for %s.\n", get_joint_name(joint)); 00253 } 00254 00255 break; 00256 } 00257 } 00258 } 00259 } 00260 #endif 00261 00262 Object *ArmatureImporter::get_empty_for_leaves() 00263 { 00264 if (empty) return empty; 00265 00266 empty = add_object(scene, OB_EMPTY); 00267 empty->empty_drawtype = OB_EMPTY_SPHERE; 00268 00269 return empty; 00270 } 00271 00272 #if 0 00273 Object *ArmatureImporter::find_armature(COLLADAFW::Node *node) 00274 { 00275 JointData* jd = get_joint_data(node); 00276 if (jd) return jd->ob_arm; 00277 00278 COLLADAFW::NodePointerArray& children = node->getChildNodes(); 00279 for (int i = 0; i < children.getCount(); i++) { 00280 Object *ob_arm = find_armature(children[i]); 00281 if (ob_arm) return ob_arm; 00282 } 00283 00284 return NULL; 00285 } 00286 00287 ArmatureJoints& ArmatureImporter::get_armature_joints(Object *ob_arm) 00288 { 00289 // try finding it 00290 std::vector<ArmatureJoints>::iterator it; 00291 for (it = armature_joints.begin(); it != armature_joints.end(); it++) { 00292 if ((*it).ob_arm == ob_arm) return *it; 00293 } 00294 00295 // not found, create one 00296 ArmatureJoints aj; 00297 aj.ob_arm = ob_arm; 00298 armature_joints.push_back(aj); 00299 00300 return armature_joints.back(); 00301 } 00302 #endif 00303 00304 void ArmatureImporter::create_armature_bones(SkinInfo& skin) 00305 { 00306 // just do like so: 00307 // - get armature 00308 // - enter editmode 00309 // - add edit bones and head/tail properties using matrices and parent-child info 00310 // - exit edit mode 00311 // - set a sphere shape to leaf bones 00312 00313 Object *ob_arm = NULL; 00314 00315 /* 00316 * find if there's another skin sharing at least one bone with this skin 00317 * if so, use that skin's armature 00318 */ 00319 00320 /* 00321 Pseudocode: 00322 00323 find_node_in_tree(node, root_joint) 00324 00325 skin::find_root_joints(root_joints): 00326 std::vector root_joints; 00327 for each root in root_joints: 00328 for each joint in joints: 00329 if find_node_in_tree(joint, root): 00330 if (std::find(root_joints.begin(), root_joints.end(), root) == root_joints.end()) 00331 root_joints.push_back(root); 00332 00333 for (each skin B with armature) { 00334 find all root joints for skin B 00335 00336 for each joint X in skin A: 00337 for each root joint R in skin B: 00338 if (find_node_in_tree(X, R)) { 00339 shared = 1; 00340 goto endloop; 00341 } 00342 } 00343 00344 endloop: 00345 */ 00346 00347 SkinInfo *a = &skin; 00348 Object *shared = NULL; 00349 std::vector<COLLADAFW::Node*> skin_root_joints; 00350 00351 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it; 00352 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) { 00353 SkinInfo *b = &it->second; 00354 if (b == a || b->get_armature() == NULL) 00355 continue; 00356 00357 skin_root_joints.clear(); 00358 00359 b->find_root_joints(root_joints, joint_by_uid, skin_root_joints); 00360 00361 std::vector<COLLADAFW::Node*>::iterator ri; 00362 for (ri = skin_root_joints.begin(); ri != skin_root_joints.end(); ri++) { 00363 if (a->uses_joint_or_descendant(*ri)) { 00364 shared = b->get_armature(); 00365 break; 00366 } 00367 } 00368 00369 if (shared != NULL) 00370 break; 00371 } 00372 00373 if (shared) 00374 ob_arm = skin.set_armature(shared); 00375 else 00376 ob_arm = skin.create_armature(scene); 00377 00378 // enter armature edit mode 00379 ED_armature_to_edit(ob_arm); 00380 00381 leaf_bones.clear(); 00382 totbone = 0; 00383 // bone_direction_row = 1; // TODO: don't default to Y but use asset and based on it decide on default row 00384 leaf_bone_length = FLT_MAX; 00385 // min_angle = 360.0f; // minimum angle between bone head-tail and a row of bone matrix 00386 00387 // create bones 00388 /* 00389 TODO: 00390 check if bones have already been created for a given joint 00391 */ 00392 00393 std::vector<COLLADAFW::Node*>::iterator ri; 00394 for (ri = root_joints.begin(); ri != root_joints.end(); ri++) { 00395 // for shared armature check if bone tree is already created 00396 if (shared && std::find(skin_root_joints.begin(), skin_root_joints.end(), *ri) != skin_root_joints.end()) 00397 continue; 00398 00399 // since root_joints may contain joints for multiple controllers, we need to filter 00400 if (skin.uses_joint_or_descendant(*ri)) { 00401 create_bone(skin, *ri, NULL, (*ri)->getChildNodes().getCount(), NULL, (bArmature*)ob_arm->data); 00402 00403 if (joint_parent_map.find((*ri)->getUniqueId()) != joint_parent_map.end() && !skin.get_parent()) 00404 skin.set_parent(joint_parent_map[(*ri)->getUniqueId()]); 00405 } 00406 } 00407 00408 fix_leaf_bones(); 00409 00410 // exit armature edit mode 00411 ED_armature_from_edit(ob_arm); 00412 ED_armature_edit_free(ob_arm); 00413 DAG_id_tag_update(&ob_arm->id, OB_RECALC_OB|OB_RECALC_DATA); 00414 00415 // set_leaf_bone_shapes(ob_arm); 00416 // set_euler_rotmode(); 00417 } 00418 00419 00420 // root - if this joint is the top joint in hierarchy, if a joint 00421 // is a child of a node (not joint), root should be true since 00422 // this is where we build armature bones from 00423 void ArmatureImporter::add_joint(COLLADAFW::Node *node, bool root, Object *parent) 00424 { 00425 joint_by_uid[node->getUniqueId()] = node; 00426 if (root) { 00427 root_joints.push_back(node); 00428 00429 if (parent) 00430 joint_parent_map[node->getUniqueId()] = parent; 00431 } 00432 } 00433 00434 #if 0 00435 void ArmatureImporter::add_root_joint(COLLADAFW::Node *node) 00436 { 00437 // root_joints.push_back(node); 00438 Object *ob_arm = find_armature(node); 00439 if (ob_arm) { 00440 get_armature_joints(ob_arm).root_joints.push_back(node); 00441 } 00442 #ifdef COLLADA_DEBUG 00443 else { 00444 fprintf(stderr, "%s cannot be added to armature.\n", get_joint_name(node)); 00445 } 00446 #endif 00447 } 00448 #endif 00449 00450 // here we add bones to armatures, having armatures previously created in write_controller 00451 void ArmatureImporter::make_armatures(bContext *C) 00452 { 00453 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it; 00454 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) { 00455 00456 SkinInfo& skin = it->second; 00457 00458 create_armature_bones(skin); 00459 00460 // link armature with a mesh object 00461 Object *ob = mesh_importer->get_object_by_geom_uid(*get_geometry_uid(skin.get_controller_uid())); 00462 if (ob) 00463 skin.link_armature(C, ob, joint_by_uid, this); 00464 else 00465 fprintf(stderr, "Cannot find object to link armature with.\n"); 00466 00467 // set armature parent if any 00468 Object *par = skin.get_parent(); 00469 if (par) 00470 bc_set_parent(skin.get_armature(), par, C, false); 00471 00472 // free memory stolen from SkinControllerData 00473 skin.free(); 00474 } 00475 } 00476 00477 #if 0 00478 // link with meshes, create vertex groups, assign weights 00479 void ArmatureImporter::link_armature(Object *ob_arm, const COLLADAFW::UniqueId& geom_id, const COLLADAFW::UniqueId& controller_data_id) 00480 { 00481 Object *ob = mesh_importer->get_object_by_geom_uid(geom_id); 00482 00483 if (!ob) { 00484 fprintf(stderr, "Cannot find object by geometry UID.\n"); 00485 return; 00486 } 00487 00488 if (skin_by_data_uid.find(controller_data_id) == skin_by_data_uid.end()) { 00489 fprintf(stderr, "Cannot find skin info by controller data UID.\n"); 00490 return; 00491 } 00492 00493 SkinInfo& skin = skin_by_data_uid[conroller_data_id]; 00494 00495 // create vertex groups 00496 } 00497 #endif 00498 00499 bool ArmatureImporter::write_skin_controller_data(const COLLADAFW::SkinControllerData* data) 00500 { 00501 // at this stage we get vertex influence info that should go into me->verts and ob->defbase 00502 // there's no info to which object this should be long so we associate it with skin controller data UID 00503 00504 // don't forget to call defgroup_unique_name before we copy 00505 00506 // controller data uid -> [armature] -> joint data, 00507 // [mesh object] 00508 // 00509 00510 SkinInfo skin(unit_converter); 00511 skin.borrow_skin_controller_data(data); 00512 00513 // store join inv bind matrix to use it later in armature construction 00514 const COLLADAFW::Matrix4Array& inv_bind_mats = data->getInverseBindMatrices(); 00515 for (unsigned int i = 0; i < data->getJointsCount(); i++) { 00516 skin.add_joint(inv_bind_mats[i]); 00517 } 00518 00519 skin_by_data_uid[data->getUniqueId()] = skin; 00520 00521 return true; 00522 } 00523 00524 bool ArmatureImporter::write_controller(const COLLADAFW::Controller* controller) 00525 { 00526 // - create and store armature object 00527 00528 const COLLADAFW::UniqueId& skin_id = controller->getUniqueId(); 00529 00530 if (controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_SKIN) { 00531 COLLADAFW::SkinController *co = (COLLADAFW::SkinController*)controller; 00532 // to be able to find geom id by controller id 00533 geom_uid_by_controller_uid[skin_id] = co->getSource(); 00534 00535 const COLLADAFW::UniqueId& data_uid = co->getSkinControllerData(); 00536 if (skin_by_data_uid.find(data_uid) == skin_by_data_uid.end()) { 00537 fprintf(stderr, "Cannot find skin by controller data UID.\n"); 00538 return true; 00539 } 00540 00541 skin_by_data_uid[data_uid].set_controller(co); 00542 } 00543 // morph controller 00544 else { 00545 // shape keys? :) 00546 fprintf(stderr, "Morph controller is not supported yet.\n"); 00547 } 00548 00549 return true; 00550 } 00551 00552 COLLADAFW::UniqueId *ArmatureImporter::get_geometry_uid(const COLLADAFW::UniqueId& controller_uid) 00553 { 00554 if (geom_uid_by_controller_uid.find(controller_uid) == geom_uid_by_controller_uid.end()) 00555 return NULL; 00556 00557 return &geom_uid_by_controller_uid[controller_uid]; 00558 } 00559 00560 Object *ArmatureImporter::get_armature_for_joint(COLLADAFW::Node *node) 00561 { 00562 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it; 00563 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) { 00564 SkinInfo& skin = it->second; 00565 00566 if (skin.uses_joint_or_descendant(node)) 00567 return skin.get_armature(); 00568 } 00569 00570 return NULL; 00571 } 00572 00573 void ArmatureImporter::get_rna_path_for_joint(COLLADAFW::Node *node, char *joint_path, size_t count) 00574 { 00575 BLI_snprintf(joint_path, count, "pose.bones[\"%s\"]", bc_get_joint_name(node)); 00576 } 00577 00578 // gives a world-space mat 00579 bool ArmatureImporter::get_joint_bind_mat(float m[][4], COLLADAFW::Node *joint) 00580 { 00581 std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it; 00582 bool found = false; 00583 for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) { 00584 SkinInfo& skin = it->second; 00585 if ((found = skin.get_joint_inv_bind_matrix(m, joint))) { 00586 invert_m4(m); 00587 break; 00588 } 00589 } 00590 00591 return found; 00592 }