Blender V2.61 - r43446
|
00001 /* 00002 * ***** BEGIN GPL LICENSE BLOCK ***** 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software Foundation, 00016 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 * 00018 * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Nathan Letwory, Sukhitha Jayathilake. 00019 * 00020 * ***** END GPL LICENSE BLOCK ***** 00021 */ 00022 00027 #include <stddef.h> 00028 00029 /* COLLADABU_ASSERT, may be able to remove later */ 00030 #include "COLLADABUPlatform.h" 00031 00032 #include "DNA_armature_types.h" 00033 00034 #include "ED_keyframing.h" 00035 00036 #include "BLI_listbase.h" 00037 #include "BLI_math.h" 00038 #include "BLI_path_util.h" 00039 #include "BLI_string.h" 00040 00041 #include "BKE_action.h" 00042 #include "BKE_armature.h" 00043 #include "BKE_fcurve.h" 00044 #include "BKE_object.h" 00045 00046 #include "MEM_guardedalloc.h" 00047 00048 #include "collada_utils.h" 00049 #include "AnimationImporter.h" 00050 #include "ArmatureImporter.h" 00051 #include "MaterialExporter.h" 00052 00053 #include <algorithm> 00054 00055 // first try node name, if not available (since is optional), fall back to original id 00056 template<class T> 00057 static const char *bc_get_joint_name(T *node) 00058 { 00059 const std::string& id = node->getName(); 00060 return id.size() ? id.c_str() : node->getOriginalId().c_str(); 00061 } 00062 00063 FCurve *AnimationImporter::create_fcurve(int array_index, const char *rna_path) 00064 { 00065 FCurve *fcu = (FCurve*)MEM_callocN(sizeof(FCurve), "FCurve"); 00066 fcu->flag = (FCURVE_VISIBLE|FCURVE_AUTO_HANDLES|FCURVE_SELECTED); 00067 fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path)); 00068 fcu->array_index = array_index; 00069 return fcu; 00070 } 00071 00072 void AnimationImporter::create_bezt(FCurve *fcu, float frame, float output) 00073 { 00074 BezTriple bez; 00075 memset(&bez, 0, sizeof(BezTriple)); 00076 bez.vec[1][0] = frame; 00077 bez.vec[1][1] = output; 00078 bez.ipo = U.ipo_new; /* use default interpolation mode here... */ 00079 bez.f1 = bez.f2 = bez.f3 = SELECT; 00080 bez.h1 = bez.h2 = HD_AUTO; 00081 insert_bezt_fcurve(fcu, &bez, 0); 00082 calchandles_fcurve(fcu); 00083 } 00084 00085 // create one or several fcurves depending on the number of parameters being animated 00086 void AnimationImporter::animation_to_fcurves(COLLADAFW::AnimationCurve *curve) 00087 { 00088 COLLADAFW::FloatOrDoubleArray& input = curve->getInputValues(); 00089 COLLADAFW::FloatOrDoubleArray& output = curve->getOutputValues(); 00090 00091 float fps = (float)FPS; 00092 size_t dim = curve->getOutDimension(); 00093 unsigned int i; 00094 00095 std::vector<FCurve*>& fcurves = curve_map[curve->getUniqueId()]; 00096 00097 switch (dim) { 00098 case 1: // X, Y, Z or angle 00099 case 3: // XYZ 00100 case 4: 00101 case 16: // matrix 00102 { 00103 for (i = 0; i < dim; i++ ) { 00104 FCurve *fcu = (FCurve*)MEM_callocN(sizeof(FCurve), "FCurve"); 00105 00106 fcu->flag = (FCURVE_VISIBLE|FCURVE_AUTO_HANDLES|FCURVE_SELECTED); 00107 // fcu->rna_path = BLI_strdupn(path, strlen(path)); 00108 fcu->array_index = 0; 00109 fcu->totvert = curve->getKeyCount(); 00110 00111 // create beztriple for each key 00112 for (unsigned int j = 0; j < curve->getKeyCount(); j++) { 00113 BezTriple bez; 00114 memset(&bez, 0, sizeof(BezTriple)); 00115 00116 00117 // input, output 00118 bez.vec[1][0] = bc_get_float_value(input, j) * fps; 00119 bez.vec[1][1] = bc_get_float_value(output, j * dim + i); 00120 00121 00122 if( curve->getInterpolationType() == COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER || 00123 curve->getInterpolationType() == COLLADAFW::AnimationCurve::INTERPOLATION_STEP) 00124 { 00125 COLLADAFW::FloatOrDoubleArray& intan = curve->getInTangentValues(); 00126 COLLADAFW::FloatOrDoubleArray& outtan = curve->getOutTangentValues(); 00127 00128 // intangent 00129 bez.vec[0][0] = bc_get_float_value(intan, (j * 2 * dim ) + (2 * i)) * fps; 00130 bez.vec[0][1] = bc_get_float_value(intan, (j * 2 * dim )+ (2 * i) + 1); 00131 00132 // outtangent 00133 bez.vec[2][0] = bc_get_float_value(outtan, (j * 2 * dim ) + (2 * i)) * fps; 00134 bez.vec[2][1] = bc_get_float_value(outtan, (j * 2 * dim )+ (2 * i) + 1); 00135 if(curve->getInterpolationType() == COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER) 00136 bez.ipo = BEZT_IPO_BEZ; 00137 else 00138 bez.ipo = BEZT_IPO_CONST; 00139 //bez.h1 = bez.h2 = HD_AUTO; 00140 } 00141 else 00142 { 00143 bez.h1 = bez.h2 = HD_AUTO; 00144 bez.ipo = BEZT_IPO_LIN; 00145 } 00146 // bez.ipo = U.ipo_new; /* use default interpolation mode here... */ 00147 bez.f1 = bez.f2 = bez.f3 = SELECT; 00148 00149 insert_bezt_fcurve(fcu, &bez, 0); 00150 } 00151 00152 calchandles_fcurve(fcu); 00153 00154 fcurves.push_back(fcu); 00155 } 00156 } 00157 break; 00158 default: 00159 fprintf(stderr, "Output dimension of %d is not yet supported (animation id = %s)\n", (int)dim, curve->getOriginalId().c_str()); 00160 } 00161 00162 for (std::vector<FCurve*>::iterator it = fcurves.begin(); it != fcurves.end(); it++) 00163 unused_curves.push_back(*it); 00164 } 00165 00166 00167 void AnimationImporter::fcurve_deg_to_rad(FCurve *cu) 00168 { 00169 for (unsigned int i = 0; i < cu->totvert; i++) { 00170 // TODO convert handles too 00171 cu->bezt[i].vec[1][1] *= DEG2RADF(1.0f); 00172 cu->bezt[i].vec[0][1] *= DEG2RADF(1.0f); 00173 cu->bezt[i].vec[2][1] *= DEG2RADF(1.0f); 00174 } 00175 } 00176 00177 00178 void AnimationImporter::add_fcurves_to_object(Object *ob, std::vector<FCurve*>& curves, char *rna_path, int array_index, Animation *animated) 00179 { 00180 bAction *act; 00181 00182 if (!ob->adt || !ob->adt->action) act = verify_adt_action((ID*)&ob->id, 1); 00183 else act = ob->adt->action; 00184 00185 std::vector<FCurve*>::iterator it; 00186 int i; 00187 00188 #if 0 00189 char *p = strstr(rna_path, "rotation_euler"); 00190 bool is_rotation = p && *(p + strlen("rotation_euler")) == '\0'; 00191 00192 // convert degrees to radians for rotation 00193 if (is_rotation) 00194 fcurve_deg_to_rad(fcu); 00195 #endif 00196 00197 for (it = curves.begin(), i = 0; it != curves.end(); it++, i++) { 00198 FCurve *fcu = *it; 00199 fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path)); 00200 00201 if (array_index == -1) fcu->array_index = i; 00202 else fcu->array_index = array_index; 00203 00204 if (ob->type == OB_ARMATURE) { 00205 bActionGroup *grp = NULL; 00206 const char *bone_name = bc_get_joint_name(animated->node); 00207 00208 if (bone_name) { 00209 /* try to find group */ 00210 grp = action_groups_find_named(act, bone_name); 00211 00212 /* no matching groups, so add one */ 00213 if (grp == NULL) { 00214 /* Add a new group, and make it active */ 00215 grp = (bActionGroup*)MEM_callocN(sizeof(bActionGroup), "bActionGroup"); 00216 00217 grp->flag = AGRP_SELECTED; 00218 BLI_strncpy(grp->name, bone_name, sizeof(grp->name)); 00219 00220 BLI_addtail(&act->groups, grp); 00221 BLI_uniquename(&act->groups, grp, "Group", '.', offsetof(bActionGroup, name), 64); 00222 } 00223 00224 /* add F-Curve to group */ 00225 action_groups_add_channel(act, grp, fcu); 00226 00227 } 00228 #if 0 00229 if (is_rotation) { 00230 fcurves_actionGroup_map[grp].push_back(fcu); 00231 } 00232 #endif 00233 } 00234 else { 00235 BLI_addtail(&act->curves, fcu); 00236 } 00237 00238 // curve is used, so remove it from unused_curves 00239 unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), fcu), unused_curves.end()); 00240 } 00241 } 00242 00243 AnimationImporter::AnimationImporter(UnitConverter *conv, ArmatureImporter *arm, Scene *scene) : 00244 TransformReader(conv), armature_importer(arm), scene(scene) { } 00245 00246 AnimationImporter::~AnimationImporter() 00247 { 00248 // free unused FCurves 00249 for (std::vector<FCurve*>::iterator it = unused_curves.begin(); it != unused_curves.end(); it++) 00250 free_fcurve(*it); 00251 00252 if (unused_curves.size()) 00253 fprintf(stderr, "removed %d unused curves\n", (int)unused_curves.size()); 00254 } 00255 00256 bool AnimationImporter::write_animation(const COLLADAFW::Animation* anim) 00257 { 00258 if (anim->getAnimationType() == COLLADAFW::Animation::ANIMATION_CURVE) { 00259 COLLADAFW::AnimationCurve *curve = (COLLADAFW::AnimationCurve*)anim; 00260 00261 // XXX Don't know if it's necessary 00262 // Should we check outPhysicalDimension? 00263 if (curve->getInPhysicalDimension() != COLLADAFW::PHYSICAL_DIMENSION_TIME) { 00264 fprintf(stderr, "Inputs physical dimension is not time. \n"); 00265 return true; 00266 } 00267 00268 // a curve can have mixed interpolation type, 00269 // in this case curve->getInterpolationTypes returns a list of interpolation types per key 00270 COLLADAFW::AnimationCurve::InterpolationType interp = curve->getInterpolationType(); 00271 00272 if (interp != COLLADAFW::AnimationCurve::INTERPOLATION_MIXED) { 00273 switch (interp) { 00274 case COLLADAFW::AnimationCurve::INTERPOLATION_LINEAR: 00275 case COLLADAFW::AnimationCurve::INTERPOLATION_BEZIER: 00276 case COLLADAFW::AnimationCurve::INTERPOLATION_STEP: 00277 animation_to_fcurves(curve); 00278 break; 00279 default: 00280 // TODO there're also CARDINAL, HERMITE, BSPLINE and STEP types 00281 fprintf(stderr, "CARDINAL, HERMITE and BSPLINE anim interpolation types not supported yet.\n"); 00282 break; 00283 } 00284 } 00285 else { 00286 // not supported yet 00287 fprintf(stderr, "MIXED anim interpolation type is not supported yet.\n"); 00288 } 00289 } 00290 else { 00291 fprintf(stderr, "FORMULA animation type is not supported yet.\n"); 00292 } 00293 00294 return true; 00295 } 00296 00297 // called on post-process stage after writeVisualScenes 00298 bool AnimationImporter::write_animation_list(const COLLADAFW::AnimationList* animlist) 00299 { 00300 const COLLADAFW::UniqueId& animlist_id = animlist->getUniqueId(); 00301 00302 animlist_map[animlist_id] = animlist; 00303 00304 #if 0 00305 00306 // should not happen 00307 if (uid_animated_map.find(animlist_id) == uid_animated_map.end()) { 00308 return true; 00309 } 00310 00311 // for bones rna_path is like: pose.bones["bone-name"].rotation 00312 00313 00314 #endif 00315 00316 return true; 00317 } 00318 00319 // \todo refactor read_node_transform to not automatically apply anything, 00320 // but rather return the transform matrix, so caller can do with it what is 00321 // necessary. Same for \ref get_node_mat 00322 void AnimationImporter::read_node_transform(COLLADAFW::Node *node, Object *ob) 00323 { 00324 float mat[4][4]; 00325 TransformReader::get_node_mat(mat, node, &uid_animated_map, ob); 00326 if (ob) { 00327 copy_m4_m4(ob->obmat, mat); 00328 object_apply_mat4(ob, ob->obmat, 0, 0); 00329 } 00330 } 00331 00332 #if 0 00333 virtual void AnimationImporter::change_eul_to_quat(Object *ob, bAction *act) 00334 { 00335 bActionGroup *grp; 00336 int i; 00337 00338 for (grp = (bActionGroup*)act->groups.first; grp; grp = grp->next) { 00339 00340 FCurve *eulcu[3] = {NULL, NULL, NULL}; 00341 00342 if (fcurves_actionGroup_map.find(grp) == fcurves_actionGroup_map.end()) 00343 continue; 00344 00345 std::vector<FCurve*> &rot_fcurves = fcurves_actionGroup_map[grp]; 00346 00347 if (rot_fcurves.size() > 3) continue; 00348 00349 for (i = 0; i < rot_fcurves.size(); i++) 00350 eulcu[rot_fcurves[i]->array_index] = rot_fcurves[i]; 00351 00352 char joint_path[100]; 00353 char rna_path[100]; 00354 00355 BLI_snprintf(joint_path, sizeof(joint_path), "pose.bones[\"%s\"]", grp->name); 00356 BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_quaternion", joint_path); 00357 00358 FCurve *quatcu[4] = { 00359 create_fcurve(0, rna_path), 00360 create_fcurve(1, rna_path), 00361 create_fcurve(2, rna_path), 00362 create_fcurve(3, rna_path) 00363 }; 00364 00365 bPoseChannel *chan = get_pose_channel(ob->pose, grp->name); 00366 00367 float m4[4][4], irest[3][3]; 00368 invert_m4_m4(m4, chan->bone->arm_mat); 00369 copy_m3_m4(irest, m4); 00370 00371 for (i = 0; i < 3; i++) { 00372 00373 FCurve *cu = eulcu[i]; 00374 00375 if (!cu) continue; 00376 00377 for (int j = 0; j < cu->totvert; j++) { 00378 float frame = cu->bezt[j].vec[1][0]; 00379 00380 float eul[3] = { 00381 eulcu[0] ? evaluate_fcurve(eulcu[0], frame) : 0.0f, 00382 eulcu[1] ? evaluate_fcurve(eulcu[1], frame) : 0.0f, 00383 eulcu[2] ? evaluate_fcurve(eulcu[2], frame) : 0.0f 00384 }; 00385 00386 // make eul relative to bone rest pose 00387 float rot[3][3], rel[3][3], quat[4]; 00388 00389 /*eul_to_mat3(rot, eul); 00390 00391 mul_m3_m3m3(rel, irest, rot); 00392 00393 mat3_to_quat(quat, rel); 00394 */ 00395 00396 eul_to_quat(quat, eul); 00397 00398 for (int k = 0; k < 4; k++) 00399 create_bezt(quatcu[k], frame, quat[k]); 00400 } 00401 } 00402 00403 // now replace old Euler curves 00404 00405 for (i = 0; i < 3; i++) { 00406 if (!eulcu[i]) continue; 00407 00408 action_groups_remove_channel(act, eulcu[i]); 00409 free_fcurve(eulcu[i]); 00410 } 00411 00412 chan->rotmode = ROT_MODE_QUAT; 00413 00414 for (i = 0; i < 4; i++) 00415 action_groups_add_channel(act, grp, quatcu[i]); 00416 } 00417 00418 bPoseChannel *pchan; 00419 for (pchan = (bPoseChannel*)ob->pose->chanbase.first; pchan; pchan = pchan->next) { 00420 pchan->rotmode = ROT_MODE_QUAT; 00421 } 00422 } 00423 #endif 00424 00425 00426 //sets the rna_path and array index to curve 00427 void AnimationImporter::modify_fcurve(std::vector<FCurve*>* curves , const char* rna_path , int array_index ) 00428 { 00429 std::vector<FCurve*>::iterator it; 00430 int i; 00431 for (it = curves->begin(), i = 0; it != curves->end(); it++, i++) { 00432 FCurve *fcu = *it; 00433 fcu->rna_path = BLI_strdup(rna_path); 00434 00435 if (array_index == -1) fcu->array_index = i; 00436 else fcu->array_index = array_index; 00437 00438 unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), fcu), unused_curves.end()); 00439 } 00440 } 00441 00442 void AnimationImporter::find_frames( std::vector<float>* frames , std::vector<FCurve*>* curves) 00443 { 00444 std::vector<FCurve*>::iterator iter; 00445 for (iter = curves->begin(); iter != curves->end(); iter++) { 00446 FCurve *fcu = *iter; 00447 00448 for (unsigned int k = 0; k < fcu->totvert; k++) { 00449 //get frame value from bezTriple 00450 float fra = fcu->bezt[k].vec[1][0]; 00451 //if frame already not added add frame to frames 00452 if (std::find(frames->begin(), frames->end(), fra) == frames->end()) 00453 frames->push_back(fra); 00454 00455 } 00456 } 00457 } 00458 00459 //creates the rna_paths and array indices of fcurves from animations using transformation and bound animation class of each animation. 00460 void AnimationImporter:: Assign_transform_animations(COLLADAFW::Transformation * transform , 00461 const COLLADAFW::AnimationList::AnimationBinding * binding, 00462 std::vector<FCurve*>* curves, bool is_joint, char * joint_path) 00463 { 00464 COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType(); 00465 bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX; 00466 bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE; 00467 00468 //to check if the no of curves are valid 00469 bool xyz = ((tm_type == COLLADAFW::Transformation::TRANSLATE ||tm_type == COLLADAFW::Transformation::SCALE) && binding->animationClass == COLLADAFW::AnimationList::POSITION_XYZ); 00470 00471 00472 if (!((!xyz && curves->size() == 1) || (xyz && curves->size() == 3) || is_matrix)) { 00473 fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves->size()); 00474 return; 00475 } 00476 00477 char rna_path[100]; 00478 00479 switch (tm_type) { 00480 case COLLADAFW::Transformation::TRANSLATE: 00481 case COLLADAFW::Transformation::SCALE: 00482 { 00483 bool loc = tm_type == COLLADAFW::Transformation::TRANSLATE; 00484 if (is_joint) 00485 BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, loc ? "location" : "scale"); 00486 else 00487 BLI_strncpy(rna_path, loc ? "location" : "scale", sizeof(rna_path)); 00488 00489 switch (binding->animationClass) { 00490 case COLLADAFW::AnimationList::POSITION_X: 00491 modify_fcurve(curves, rna_path, 0 ); 00492 break; 00493 case COLLADAFW::AnimationList::POSITION_Y: 00494 modify_fcurve(curves, rna_path, 1 ); 00495 break; 00496 case COLLADAFW::AnimationList::POSITION_Z: 00497 modify_fcurve(curves, rna_path, 2 ); 00498 break; 00499 case COLLADAFW::AnimationList::POSITION_XYZ: 00500 modify_fcurve(curves, rna_path, -1 ); 00501 break; 00502 default: 00503 fprintf(stderr, "AnimationClass %d is not supported for %s.\n", 00504 binding->animationClass, loc ? "TRANSLATE" : "SCALE"); 00505 } 00506 break; 00507 } 00508 00509 00510 case COLLADAFW::Transformation::ROTATE: 00511 { 00512 if (is_joint) 00513 BLI_snprintf(rna_path, sizeof(rna_path), "%s.rotation_euler", joint_path); 00514 else 00515 BLI_strncpy(rna_path, "rotation_euler", sizeof(rna_path)); 00516 std::vector<FCurve*>::iterator iter; 00517 for (iter = curves->begin(); iter != curves->end(); iter++) { 00518 FCurve* fcu = *iter; 00519 00520 //if transform is rotation the fcurves values must be turned in to radian. 00521 if (is_rotation) 00522 fcurve_deg_to_rad(fcu); 00523 } 00524 COLLADAFW::Rotate* rot = (COLLADAFW::Rotate*)transform; 00525 COLLADABU::Math::Vector3& axis = rot->getRotationAxis(); 00526 00527 switch (binding->animationClass) { 00528 case COLLADAFW::AnimationList::ANGLE: 00529 if (COLLADABU::Math::Vector3::UNIT_X == axis) { 00530 modify_fcurve(curves, rna_path, 0 ); 00531 } 00532 else if (COLLADABU::Math::Vector3::UNIT_Y == axis) { 00533 modify_fcurve(curves, rna_path, 1 ); 00534 } 00535 else if (COLLADABU::Math::Vector3::UNIT_Z == axis) { 00536 modify_fcurve(curves, rna_path, 2 ); 00537 } 00538 break; 00539 case COLLADAFW::AnimationList::AXISANGLE: 00540 // TODO convert axis-angle to quat? or XYZ? 00541 default: 00542 fprintf(stderr, "AnimationClass %d is not supported for ROTATE transformation.\n", 00543 binding->animationClass); 00544 } 00545 break; 00546 } 00547 00548 case COLLADAFW::Transformation::MATRIX: 00549 /*{ 00550 COLLADAFW::Matrix* mat = (COLLADAFW::Matrix*)transform; 00551 COLLADABU::Math::Matrix4 mat4 = mat->getMatrix(); 00552 switch (binding->animationClass) { 00553 case COLLADAFW::AnimationList::TRANSFORM: 00554 00555 } 00556 }*/ 00557 break; 00558 case COLLADAFW::Transformation::SKEW: 00559 case COLLADAFW::Transformation::LOOKAT: 00560 fprintf(stderr, "Animation of SKEW and LOOKAT transformations is not supported yet.\n"); 00561 break; 00562 } 00563 00564 } 00565 00566 //creates the rna_paths and array indices of fcurves from animations using color and bound animation class of each animation. 00567 void AnimationImporter:: Assign_color_animations(const COLLADAFW::UniqueId& listid, ListBase *AnimCurves ,const char * anim_type) 00568 { 00569 char rna_path[100]; 00570 BLI_strncpy(rna_path,anim_type, sizeof(rna_path)); 00571 00572 const COLLADAFW::AnimationList *animlist = animlist_map[listid]; 00573 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings(); 00574 //all the curves belonging to the current binding 00575 std::vector<FCurve*> animcurves; 00576 for (unsigned int j = 0; j < bindings.getCount(); j++) { 00577 animcurves = curve_map[bindings[j].animation]; 00578 00579 switch (bindings[j].animationClass) { 00580 case COLLADAFW::AnimationList::COLOR_R: 00581 modify_fcurve(&animcurves, rna_path, 0 ); 00582 break; 00583 case COLLADAFW::AnimationList::COLOR_G: 00584 modify_fcurve(&animcurves, rna_path, 1 ); 00585 break; 00586 case COLLADAFW::AnimationList::COLOR_B: 00587 modify_fcurve(&animcurves, rna_path, 2 ); 00588 break; 00589 case COLLADAFW::AnimationList::COLOR_RGB: 00590 case COLLADAFW::AnimationList::COLOR_RGBA: // to do-> set intensity 00591 modify_fcurve(&animcurves, rna_path, -1 ); 00592 break; 00593 00594 default: 00595 fprintf(stderr, "AnimationClass %d is not supported for %s.\n", 00596 bindings[j].animationClass, "COLOR" ); 00597 } 00598 00599 std::vector<FCurve*>::iterator iter; 00600 //Add the curves of the current animation to the object 00601 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) { 00602 FCurve * fcu = *iter; 00603 BLI_addtail(AnimCurves, fcu); 00604 } 00605 } 00606 00607 00608 } 00609 00610 void AnimationImporter:: Assign_float_animations(const COLLADAFW::UniqueId& listid, ListBase *AnimCurves, const char * anim_type) 00611 { 00612 char rna_path[100]; 00613 if (animlist_map.find(listid) == animlist_map.end()) return ; 00614 else 00615 { 00616 //anim_type has animations 00617 const COLLADAFW::AnimationList *animlist = animlist_map[listid]; 00618 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings(); 00619 //all the curves belonging to the current binding 00620 std::vector<FCurve*> animcurves; 00621 for (unsigned int j = 0; j < bindings.getCount(); j++) { 00622 animcurves = curve_map[bindings[j].animation]; 00623 00624 BLI_strncpy(rna_path, anim_type , sizeof(rna_path)); 00625 modify_fcurve(&animcurves, rna_path, 0 ); 00626 std::vector<FCurve*>::iterator iter; 00627 //Add the curves of the current animation to the object 00628 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) { 00629 FCurve * fcu = *iter; 00630 BLI_addtail(AnimCurves, fcu); 00631 } 00632 } 00633 } 00634 00635 } 00636 00637 void AnimationImporter::apply_matrix_curves( Object * ob, std::vector<FCurve*>& animcurves, COLLADAFW::Node* root ,COLLADAFW::Node* node, 00638 COLLADAFW::Transformation * tm ) 00639 { 00640 bool is_joint = node->getType() == COLLADAFW::Node::JOINT; 00641 const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL; 00642 char joint_path[200]; 00643 if ( is_joint ) 00644 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path)); 00645 00646 std::vector<float> frames; 00647 find_frames(&frames, &animcurves); 00648 00649 float irest_dae[4][4]; 00650 float rest[4][4], irest[4][4]; 00651 00652 if (is_joint) { 00653 get_joint_rest_mat(irest_dae, root, node); 00654 invert_m4(irest_dae); 00655 00656 Bone *bone = get_named_bone((bArmature*)ob->data, bone_name); 00657 if (!bone) { 00658 fprintf(stderr, "cannot find bone \"%s\"\n", bone_name); 00659 return; 00660 } 00661 00662 unit_m4(rest); 00663 copy_m4_m4(rest, bone->arm_mat); 00664 invert_m4_m4(irest, rest); 00665 } 00666 // new curves to assign matrix transform animation 00667 FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale 00668 unsigned int totcu = 10 ; 00669 const char *tm_str = NULL; 00670 char rna_path[200]; 00671 for (int i = 0; i < totcu; i++) { 00672 00673 int axis = i; 00674 00675 if (i < 4) { 00676 tm_str = "rotation_quaternion"; 00677 axis = i; 00678 } 00679 else if (i < 7) { 00680 tm_str = "location"; 00681 axis = i - 4; 00682 } 00683 else { 00684 tm_str = "scale"; 00685 axis = i - 7; 00686 } 00687 00688 00689 if (is_joint) 00690 BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str); 00691 else 00692 BLI_strncpy(rna_path, tm_str, sizeof(rna_path)); 00693 newcu[i] = create_fcurve(axis, rna_path); 00694 newcu[i]->totvert = frames.size(); 00695 } 00696 00697 if (frames.size() == 0) 00698 return; 00699 00700 std::sort(frames.begin(), frames.end()); 00701 00702 std::vector<float>::iterator it; 00703 00704 // sample values at each frame 00705 for (it = frames.begin(); it != frames.end(); it++) { 00706 float fra = *it; 00707 00708 float mat[4][4]; 00709 float matfra[4][4]; 00710 00711 unit_m4(matfra); 00712 00713 // calc object-space mat 00714 evaluate_transform_at_frame(matfra, node, fra); 00715 00716 00717 // for joints, we need a special matrix 00718 if (is_joint) { 00719 // special matrix: iR * M * iR_dae * R 00720 // where R, iR are bone rest and inverse rest mats in world space (Blender bones), 00721 // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE) 00722 float temp[4][4], par[4][4]; 00723 00724 // calc M 00725 calc_joint_parent_mat_rest(par, NULL, root, node); 00726 mult_m4_m4m4(temp, par, matfra); 00727 00728 // evaluate_joint_world_transform_at_frame(temp, NULL, , node, fra); 00729 00730 // calc special matrix 00731 mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL); 00732 } 00733 else { 00734 copy_m4_m4(mat, matfra); 00735 } 00736 00737 float rot[4], loc[3], scale[3]; 00738 00739 mat4_to_quat(rot, mat); 00740 /*for ( int i = 0 ; i < 4 ; i ++ ) 00741 { 00742 rot[i] = RAD2DEGF(rot[i]); 00743 }*/ 00744 copy_v3_v3(loc, mat[3]); 00745 mat4_to_size(scale, mat); 00746 00747 // add keys 00748 for (int i = 0; i < totcu; i++) { 00749 if (i < 4) 00750 add_bezt(newcu[i], fra, rot[i]); 00751 else if (i < 7) 00752 add_bezt(newcu[i], fra, loc[i - 4]); 00753 else 00754 add_bezt(newcu[i], fra, scale[i - 7]); 00755 } 00756 } 00757 verify_adt_action((ID*)&ob->id, 1); 00758 00759 ListBase *curves = &ob->adt->action->curves; 00760 00761 // add curves 00762 for (int i= 0; i < totcu; i++) { 00763 if (is_joint) 00764 add_bone_fcurve(ob, node, newcu[i]); 00765 else 00766 BLI_addtail(curves, newcu[i]); 00767 } 00768 00769 if (is_joint) { 00770 bPoseChannel *chan = get_pose_channel(ob->pose, bone_name); 00771 chan->rotmode = ROT_MODE_QUAT; 00772 } 00773 else { 00774 ob->rotmode = ROT_MODE_QUAT; 00775 } 00776 00777 return; 00778 00779 } 00780 00781 void AnimationImporter::translate_Animations ( COLLADAFW::Node * node , 00782 std::map<COLLADAFW::UniqueId, COLLADAFW::Node*>& root_map, 00783 std::map<COLLADAFW::UniqueId, Object*>& object_map, 00784 std::map<COLLADAFW::UniqueId, const COLLADAFW::Object*> FW_object_map) 00785 { 00786 AnimationImporter::AnimMix* animType = get_animation_type(node, FW_object_map ); 00787 00788 bool is_joint = node->getType() == COLLADAFW::Node::JOINT; 00789 COLLADAFW::Node *root = root_map.find(node->getUniqueId()) == root_map.end() ? node : root_map[node->getUniqueId()]; 00790 Object *ob = is_joint ? armature_importer->get_armature_for_joint(root) : object_map[node->getUniqueId()]; 00791 if (!ob) 00792 { 00793 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str()); 00794 return; 00795 } 00796 00797 bAction * act; 00798 00799 if ( (animType->transform) != 0 ) 00800 { 00801 const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL; 00802 char joint_path[200]; 00803 00804 if ( is_joint ) 00805 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path)); 00806 00807 00808 if (!ob->adt || !ob->adt->action) act = verify_adt_action((ID*)&ob->id, 1); 00809 else act = ob->adt->action; 00810 00811 //Get the list of animation curves of the object 00812 ListBase *AnimCurves = &(act->curves); 00813 00814 const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations(); 00815 00816 //for each transformation in node 00817 for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) { 00818 COLLADAFW::Transformation *transform = nodeTransforms[i]; 00819 COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType(); 00820 00821 bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE; 00822 bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX; 00823 00824 const COLLADAFW::UniqueId& listid = transform->getAnimationList(); 00825 00826 //check if transformation has animations 00827 if (animlist_map.find(listid) == animlist_map.end()) continue ; 00828 else 00829 { 00830 //transformation has animations 00831 const COLLADAFW::AnimationList *animlist = animlist_map[listid]; 00832 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings(); 00833 //all the curves belonging to the current binding 00834 std::vector<FCurve*> animcurves; 00835 for (unsigned int j = 0; j < bindings.getCount(); j++) { 00836 animcurves = curve_map[bindings[j].animation]; 00837 if ( is_matrix ) 00838 apply_matrix_curves(ob, animcurves, root , node, transform ); 00839 else { 00840 //calculate rnapaths and array index of fcurves according to transformation and animation class 00841 Assign_transform_animations(transform, &bindings[j], &animcurves, is_joint, joint_path ); 00842 00843 std::vector<FCurve*>::iterator iter; 00844 //Add the curves of the current animation to the object 00845 for (iter = animcurves.begin(); iter != animcurves.end(); iter++) { 00846 FCurve * fcu = *iter; 00847 if ((ob->type == OB_ARMATURE)) 00848 add_bone_fcurve( ob, node , fcu ); 00849 else 00850 BLI_addtail(AnimCurves, fcu); 00851 } 00852 } 00853 } 00854 } 00855 if (is_rotation) { 00856 if (is_joint) 00857 { 00858 bPoseChannel *chan = get_pose_channel(ob->pose, bone_name); 00859 chan->rotmode = ROT_MODE_EUL; 00860 } 00861 else 00862 { 00863 ob->rotmode = ROT_MODE_EUL; 00864 } 00865 } 00866 } 00867 } 00868 00869 if ((animType->light) != 0) 00870 { 00871 Lamp * lamp = (Lamp*) ob->data; 00872 00873 if (!lamp->adt || !lamp->adt->action) act = verify_adt_action((ID*)&lamp->id, 1); 00874 else act = lamp->adt->action; 00875 00876 ListBase *AnimCurves = &(act->curves); 00877 const COLLADAFW::InstanceLightPointerArray& nodeLights = node->getInstanceLights(); 00878 00879 for (unsigned int i = 0; i < nodeLights.getCount(); i++) { 00880 const COLLADAFW::Light *light = (COLLADAFW::Light *) FW_object_map[nodeLights[i]->getInstanciatedObjectId()]; 00881 00882 if ((animType->light & LIGHT_COLOR) != 0) 00883 { 00884 const COLLADAFW::Color *col = &(light->getColor()); 00885 const COLLADAFW::UniqueId& listid = col->getAnimationList(); 00886 00887 Assign_color_animations(listid, AnimCurves, "color"); 00888 } 00889 if ((animType->light & LIGHT_FOA) != 0 ) 00890 { 00891 const COLLADAFW::AnimatableFloat *foa = &(light->getFallOffAngle()); 00892 const COLLADAFW::UniqueId& listid = foa->getAnimationList(); 00893 00894 Assign_float_animations( listid ,AnimCurves, "spot_size"); 00895 } 00896 if ( (animType->light & LIGHT_FOE) != 0 ) 00897 { 00898 const COLLADAFW::AnimatableFloat *foe = &(light->getFallOffExponent()); 00899 const COLLADAFW::UniqueId& listid = foe->getAnimationList(); 00900 00901 Assign_float_animations( listid ,AnimCurves, "spot_blend"); 00902 00903 } 00904 } 00905 } 00906 00907 if ( (animType->camera) != 0) 00908 { 00909 Camera * camera = (Camera*) ob->data; 00910 00911 if (!camera->adt || !camera->adt->action) act = verify_adt_action((ID*)&camera->id, 1); 00912 else act = camera->adt->action; 00913 00914 ListBase *AnimCurves = &(act->curves); 00915 const COLLADAFW::InstanceCameraPointerArray& nodeCameras= node->getInstanceCameras(); 00916 00917 for (unsigned int i = 0; i < nodeCameras.getCount(); i++) { 00918 const COLLADAFW::Camera *camera = (COLLADAFW::Camera *) FW_object_map[nodeCameras[i]->getInstanciatedObjectId()]; 00919 00920 if ((animType->camera & CAMERA_XFOV) != 0 ) 00921 { 00922 const COLLADAFW::AnimatableFloat *xfov = &(camera->getXFov()); 00923 const COLLADAFW::UniqueId& listid = xfov->getAnimationList(); 00924 Assign_float_animations( listid ,AnimCurves, "lens"); 00925 } 00926 00927 else if ((animType->camera & CAMERA_XMAG) != 0 ) 00928 { 00929 const COLLADAFW::AnimatableFloat *xmag = &(camera->getXMag()); 00930 const COLLADAFW::UniqueId& listid = xmag->getAnimationList(); 00931 Assign_float_animations( listid ,AnimCurves, "ortho_scale"); 00932 } 00933 00934 if ((animType->camera & CAMERA_ZFAR) != 0 ) 00935 { 00936 const COLLADAFW::AnimatableFloat *zfar = &(camera->getFarClippingPlane()); 00937 const COLLADAFW::UniqueId& listid = zfar->getAnimationList(); 00938 Assign_float_animations( listid ,AnimCurves, "clip_end"); 00939 } 00940 00941 if ((animType->camera & CAMERA_ZNEAR) != 0 ) 00942 { 00943 const COLLADAFW::AnimatableFloat *znear = &(camera->getNearClippingPlane()); 00944 const COLLADAFW::UniqueId& listid = znear->getAnimationList(); 00945 Assign_float_animations( listid ,AnimCurves, "clip_start"); 00946 } 00947 00948 } 00949 } 00950 if ( animType->material != 0){ 00951 Material *ma = give_current_material(ob, 1); 00952 if (!ma->adt || !ma->adt->action) act = verify_adt_action((ID*)&ma->id, 1); 00953 else act = ma->adt->action; 00954 00955 ListBase *AnimCurves = &(act->curves); 00956 00957 const COLLADAFW::InstanceGeometryPointerArray& nodeGeoms = node->getInstanceGeometries(); 00958 for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) { 00959 const COLLADAFW::MaterialBindingArray& matBinds = nodeGeoms[i]->getMaterialBindings(); 00960 for (unsigned int j = 0; j < matBinds.getCount(); j++) { 00961 const COLLADAFW::UniqueId & matuid = matBinds[j].getReferencedMaterial(); 00962 const COLLADAFW::Effect *ef = (COLLADAFW::Effect *) (FW_object_map[matuid]); 00963 if (ef != NULL) { /* can be NULL [#28909] */ 00964 const COLLADAFW::CommonEffectPointerArray& commonEffects = ef->getCommonEffects(); 00965 COLLADAFW::EffectCommon *efc = commonEffects[0]; 00966 if((animType->material & MATERIAL_SHININESS) != 0){ 00967 const COLLADAFW::FloatOrParam *shin = &(efc->getShininess()); 00968 const COLLADAFW::UniqueId& listid = shin->getAnimationList(); 00969 Assign_float_animations( listid, AnimCurves , "specular_hardness" ); 00970 } 00971 00972 if((animType->material & MATERIAL_IOR) != 0){ 00973 const COLLADAFW::FloatOrParam *ior = &(efc->getIndexOfRefraction()); 00974 const COLLADAFW::UniqueId& listid = ior->getAnimationList(); 00975 Assign_float_animations( listid, AnimCurves , "raytrace_transparency.ior" ); 00976 } 00977 00978 if((animType->material & MATERIAL_SPEC_COLOR) != 0){ 00979 const COLLADAFW::ColorOrTexture *cot = &(efc->getSpecular()); 00980 const COLLADAFW::UniqueId& listid = cot->getColor().getAnimationList(); 00981 Assign_color_animations( listid, AnimCurves , "specular_color" ); 00982 } 00983 00984 if((animType->material & MATERIAL_DIFF_COLOR) != 0){ 00985 const COLLADAFW::ColorOrTexture *cot = &(efc->getDiffuse()); 00986 const COLLADAFW::UniqueId& listid = cot->getColor().getAnimationList(); 00987 Assign_color_animations( listid, AnimCurves , "diffuse_color" ); 00988 } 00989 } 00990 } 00991 } 00992 } 00993 } 00994 00995 00996 //Check if object is animated by checking if animlist_map holds the animlist_id of node transforms 00997 AnimationImporter::AnimMix* AnimationImporter::get_animation_type ( const COLLADAFW::Node * node , 00998 std::map<COLLADAFW::UniqueId, const COLLADAFW::Object*> FW_object_map) 00999 { 01000 AnimMix *types = new AnimMix(); 01001 01002 const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations(); 01003 01004 //for each transformation in node 01005 for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) { 01006 COLLADAFW::Transformation *transform = nodeTransforms[i]; 01007 const COLLADAFW::UniqueId& listid = transform->getAnimationList(); 01008 01009 //check if transformation has animations 01010 if (animlist_map.find(listid) == animlist_map.end()) continue ; 01011 else 01012 { 01013 types->transform = types->transform|NODE_TRANSFORM; 01014 break; 01015 } 01016 } 01017 const COLLADAFW::InstanceLightPointerArray& nodeLights = node->getInstanceLights(); 01018 01019 for (unsigned int i = 0; i < nodeLights.getCount(); i++) { 01020 const COLLADAFW::Light *light = (COLLADAFW::Light *) FW_object_map[nodeLights[i]->getInstanciatedObjectId()]; 01021 types->light = setAnimType(&(light->getColor()),(types->light), LIGHT_COLOR); 01022 types->light = setAnimType(&(light->getFallOffAngle()),(types->light), LIGHT_FOA); 01023 types->light = setAnimType(&(light->getFallOffExponent()),(types->light), LIGHT_FOE); 01024 01025 if ( types->light != 0) break; 01026 01027 } 01028 01029 const COLLADAFW::InstanceCameraPointerArray& nodeCameras = node->getInstanceCameras(); 01030 for (unsigned int i = 0; i < nodeCameras.getCount(); i++) { 01031 const COLLADAFW::Camera *camera = (COLLADAFW::Camera *) FW_object_map[nodeCameras[i]->getInstanciatedObjectId()]; 01032 01033 if ( camera->getCameraType() == COLLADAFW::Camera::PERSPECTIVE ) 01034 { 01035 types->camera = setAnimType(&(camera->getXMag()),(types->camera), CAMERA_XFOV); 01036 } 01037 else 01038 { 01039 types->camera = setAnimType(&(camera->getXMag()),(types->camera), CAMERA_XMAG); 01040 } 01041 types->camera = setAnimType(&(camera->getFarClippingPlane()),(types->camera), CAMERA_ZFAR); 01042 types->camera = setAnimType(&(camera->getNearClippingPlane()),(types->camera), CAMERA_ZNEAR); 01043 01044 if ( types->camera != 0) break; 01045 01046 } 01047 01048 const COLLADAFW::InstanceGeometryPointerArray& nodeGeoms = node->getInstanceGeometries(); 01049 for (unsigned int i = 0; i < nodeGeoms.getCount(); i++) { 01050 const COLLADAFW::MaterialBindingArray& matBinds = nodeGeoms[i]->getMaterialBindings(); 01051 for (unsigned int j = 0; j < matBinds.getCount(); j++) { 01052 const COLLADAFW::UniqueId & matuid = matBinds[j].getReferencedMaterial(); 01053 const COLLADAFW::Effect *ef = (COLLADAFW::Effect *) (FW_object_map[matuid]); 01054 if (ef != NULL) { /* can be NULL [#28909] */ 01055 const COLLADAFW::CommonEffectPointerArray& commonEffects = ef->getCommonEffects(); 01056 if(!commonEffects.empty()) { 01057 COLLADAFW::EffectCommon *efc = commonEffects[0]; 01058 types->material = setAnimType(&(efc->getShininess()),(types->material), MATERIAL_SHININESS); 01059 types->material = setAnimType(&(efc->getSpecular().getColor()),(types->material), MATERIAL_SPEC_COLOR); 01060 types->material = setAnimType(&(efc->getDiffuse().getColor()),(types->material), MATERIAL_DIFF_COLOR); 01061 // types->material = setAnimType(&(efc->get()),(types->material), MATERIAL_TRANSPARENCY); 01062 types->material = setAnimType(&(efc->getIndexOfRefraction()),(types->material), MATERIAL_IOR); 01063 } 01064 } 01065 } 01066 } 01067 return types; 01068 } 01069 01070 int AnimationImporter::setAnimType ( const COLLADAFW::Animatable * prop , int types, int addition) 01071 { 01072 const COLLADAFW::UniqueId& listid = prop->getAnimationList(); 01073 if (animlist_map.find(listid) != animlist_map.end()) 01074 return types|addition; 01075 else return types; 01076 } 01077 01078 // Is not used anymore. 01079 void AnimationImporter::find_frames_old(std::vector<float> * frames, COLLADAFW::Node * node , COLLADAFW::Transformation::TransformationType tm_type) 01080 { 01081 bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX; 01082 bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE; 01083 // for each <rotate>, <translate>, etc. there is a separate Transformation 01084 const COLLADAFW::TransformationPointerArray& nodeTransforms = node->getTransformations(); 01085 01086 unsigned int i; 01087 // find frames at which to sample plus convert all rotation keys to radians 01088 for (i = 0; i < nodeTransforms.getCount(); i++) { 01089 COLLADAFW::Transformation *transform = nodeTransforms[i]; 01090 COLLADAFW::Transformation::TransformationType nodeTmType = transform->getTransformationType(); 01091 01092 01093 if (nodeTmType == tm_type) { 01094 //get animation bindings for the current transformation 01095 const COLLADAFW::UniqueId& listid = transform->getAnimationList(); 01096 //if transform is animated its animlist must exist. 01097 if (animlist_map.find(listid) != animlist_map.end()) { 01098 01099 const COLLADAFW::AnimationList *animlist = animlist_map[listid]; 01100 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings(); 01101 01102 if (bindings.getCount()) { 01103 //for each AnimationBinding get the fcurves which animate the transform 01104 for (unsigned int j = 0; j < bindings.getCount(); j++) { 01105 std::vector<FCurve*>& curves = curve_map[bindings[j].animation]; 01106 bool xyz = ((nodeTmType == COLLADAFW::Transformation::TRANSLATE || nodeTmType == COLLADAFW::Transformation::SCALE) && bindings[j].animationClass == COLLADAFW::AnimationList::POSITION_XYZ); 01107 01108 if ((!xyz && curves.size() == 1) || (xyz && curves.size() == 3) || is_matrix) { 01109 std::vector<FCurve*>::iterator iter; 01110 01111 for (iter = curves.begin(); iter != curves.end(); iter++) { 01112 FCurve *fcu = *iter; 01113 01114 //if transform is rotation the fcurves values must be turned in to radian. 01115 if (is_rotation) 01116 fcurve_deg_to_rad(fcu); 01117 01118 for (unsigned int k = 0; k < fcu->totvert; k++) { 01119 //get frame value from bezTriple 01120 float fra = fcu->bezt[k].vec[1][0]; 01121 //if frame already not added add frame to frames 01122 if (std::find(frames->begin(), frames->end(), fra) == frames->end()) 01123 frames->push_back(fra); 01124 } 01125 } 01126 } 01127 else { 01128 fprintf(stderr, "expected %d curves, got %d\n", xyz ? 3 : 1, (int)curves.size()); 01129 } 01130 } 01131 } 01132 } 01133 } 01134 } 01135 } 01136 01137 01138 01139 // prerequisites: 01140 // animlist_map - map animlist id -> animlist 01141 // curve_map - map anim id -> curve(s) 01142 Object *AnimationImporter::translate_animation_OLD(COLLADAFW::Node *node, 01143 std::map<COLLADAFW::UniqueId, Object*>& object_map, 01144 std::map<COLLADAFW::UniqueId, COLLADAFW::Node*>& root_map, 01145 COLLADAFW::Transformation::TransformationType tm_type, 01146 Object *par_job) 01147 { 01148 01149 bool is_rotation = tm_type == COLLADAFW::Transformation::ROTATE; 01150 bool is_matrix = tm_type == COLLADAFW::Transformation::MATRIX; 01151 bool is_joint = node->getType() == COLLADAFW::Node::JOINT; 01152 01153 COLLADAFW::Node *root = root_map.find(node->getUniqueId()) == root_map.end() ? node : root_map[node->getUniqueId()]; 01154 Object *ob = is_joint ? armature_importer->get_armature_for_joint(node) : object_map[node->getUniqueId()]; 01155 const char *bone_name = is_joint ? bc_get_joint_name(node) : NULL; 01156 if (!ob) { 01157 fprintf(stderr, "cannot find Object for Node with id=\"%s\"\n", node->getOriginalId().c_str()); 01158 return NULL; 01159 } 01160 01161 // frames at which to sample 01162 std::vector<float> frames; 01163 01164 find_frames_old(&frames, node , tm_type); 01165 01166 unsigned int i; 01167 01168 float irest_dae[4][4]; 01169 float rest[4][4], irest[4][4]; 01170 01171 if (is_joint) { 01172 get_joint_rest_mat(irest_dae, root, node); 01173 invert_m4(irest_dae); 01174 01175 Bone *bone = get_named_bone((bArmature*)ob->data, bone_name); 01176 if (!bone) { 01177 fprintf(stderr, "cannot find bone \"%s\"\n", bone_name); 01178 return NULL; 01179 } 01180 01181 unit_m4(rest); 01182 copy_m4_m4(rest, bone->arm_mat); 01183 invert_m4_m4(irest, rest); 01184 } 01185 01186 Object *job = NULL; 01187 01188 #ifdef ARMATURE_TEST 01189 FCurve *job_curves[10]; 01190 job = get_joint_object(root, node, par_job); 01191 #endif 01192 01193 if (frames.size() == 0) 01194 return job; 01195 01196 std::sort(frames.begin(), frames.end()); 01197 01198 const char *tm_str = NULL; 01199 switch (tm_type) { 01200 case COLLADAFW::Transformation::ROTATE: 01201 tm_str = "rotation_quaternion"; 01202 break; 01203 case COLLADAFW::Transformation::SCALE: 01204 tm_str = "scale"; 01205 break; 01206 case COLLADAFW::Transformation::TRANSLATE: 01207 tm_str = "location"; 01208 break; 01209 case COLLADAFW::Transformation::MATRIX: 01210 break; 01211 default: 01212 return job; 01213 } 01214 01215 char rna_path[200]; 01216 char joint_path[200]; 01217 01218 if (is_joint) 01219 armature_importer->get_rna_path_for_joint(node, joint_path, sizeof(joint_path)); 01220 01221 // new curves 01222 FCurve *newcu[10]; // if tm_type is matrix, then create 10 curves: 4 rot, 3 loc, 3 scale 01223 unsigned int totcu = is_matrix ? 10 : (is_rotation ? 4 : 3); 01224 01225 for (i = 0; i < totcu; i++) { 01226 01227 int axis = i; 01228 01229 if (is_matrix) { 01230 if (i < 4) { 01231 tm_str = "rotation_quaternion"; 01232 axis = i; 01233 } 01234 else if (i < 7) { 01235 tm_str = "location"; 01236 axis = i - 4; 01237 } 01238 else { 01239 tm_str = "scale"; 01240 axis = i - 7; 01241 } 01242 } 01243 01244 if (is_joint) 01245 BLI_snprintf(rna_path, sizeof(rna_path), "%s.%s", joint_path, tm_str); 01246 else 01247 BLI_strncpy(rna_path, tm_str, sizeof(rna_path)); 01248 newcu[i] = create_fcurve(axis, rna_path); 01249 01250 #ifdef ARMATURE_TEST 01251 if (is_joint) 01252 job_curves[i] = create_fcurve(axis, tm_str); 01253 #endif 01254 } 01255 01256 std::vector<float>::iterator it; 01257 01258 // sample values at each frame 01259 for (it = frames.begin(); it != frames.end(); it++) { 01260 float fra = *it; 01261 01262 float mat[4][4]; 01263 float matfra[4][4]; 01264 01265 unit_m4(matfra); 01266 01267 // calc object-space mat 01268 evaluate_transform_at_frame(matfra, node, fra); 01269 01270 // for joints, we need a special matrix 01271 if (is_joint) { 01272 // special matrix: iR * M * iR_dae * R 01273 // where R, iR are bone rest and inverse rest mats in world space (Blender bones), 01274 // iR_dae is joint inverse rest matrix (DAE) and M is an evaluated joint world-space matrix (DAE) 01275 float temp[4][4], par[4][4]; 01276 01277 // calc M 01278 calc_joint_parent_mat_rest(par, NULL, root, node); 01279 mult_m4_m4m4(temp, par, matfra); 01280 01281 // evaluate_joint_world_transform_at_frame(temp, NULL, , node, fra); 01282 01283 // calc special matrix 01284 mul_serie_m4(mat, irest, temp, irest_dae, rest, NULL, NULL, NULL, NULL); 01285 } 01286 else { 01287 copy_m4_m4(mat, matfra); 01288 } 01289 01290 float val[4], rot[4], loc[3], scale[3]; 01291 01292 switch (tm_type) { 01293 case COLLADAFW::Transformation::ROTATE: 01294 mat4_to_quat(val, mat); 01295 break; 01296 case COLLADAFW::Transformation::SCALE: 01297 mat4_to_size(val, mat); 01298 break; 01299 case COLLADAFW::Transformation::TRANSLATE: 01300 copy_v3_v3(val, mat[3]); 01301 break; 01302 case COLLADAFW::Transformation::MATRIX: 01303 mat4_to_quat(rot, mat); 01304 copy_v3_v3(loc, mat[3]); 01305 mat4_to_size(scale, mat); 01306 break; 01307 default: 01308 break; 01309 } 01310 01311 // add keys 01312 for (i = 0; i < totcu; i++) { 01313 if (is_matrix) { 01314 if (i < 4) 01315 add_bezt(newcu[i], fra, rot[i]); 01316 else if (i < 7) 01317 add_bezt(newcu[i], fra, loc[i - 4]); 01318 else 01319 add_bezt(newcu[i], fra, scale[i - 7]); 01320 } 01321 else { 01322 add_bezt(newcu[i], fra, val[i]); 01323 } 01324 } 01325 01326 #ifdef ARMATURE_TEST 01327 if (is_joint) { 01328 switch (tm_type) { 01329 case COLLADAFW::Transformation::ROTATE: 01330 mat4_to_quat(val, matfra); 01331 break; 01332 case COLLADAFW::Transformation::SCALE: 01333 mat4_to_size(val, matfra); 01334 break; 01335 case COLLADAFW::Transformation::TRANSLATE: 01336 copy_v3_v3(val, matfra[3]); 01337 break; 01338 case MATRIX: 01339 mat4_to_quat(rot, matfra); 01340 copy_v3_v3(loc, matfra[3]); 01341 mat4_to_size(scale, matfra); 01342 break; 01343 default: 01344 break; 01345 } 01346 01347 for (i = 0; i < totcu; i++) { 01348 if (is_matrix) { 01349 if (i < 4) 01350 add_bezt(job_curves[i], fra, rot[i]); 01351 else if (i < 7) 01352 add_bezt(job_curves[i], fra, loc[i - 4]); 01353 else 01354 add_bezt(job_curves[i], fra, scale[i - 7]); 01355 } 01356 else { 01357 add_bezt(job_curves[i], fra, val[i]); 01358 } 01359 } 01360 } 01361 #endif 01362 } 01363 01364 verify_adt_action((ID*)&ob->id, 1); 01365 01366 ListBase *curves = &ob->adt->action->curves; 01367 01368 // add curves 01369 for (i = 0; i < totcu; i++) { 01370 if (is_joint) 01371 add_bone_fcurve(ob, node, newcu[i]); 01372 else 01373 BLI_addtail(curves, newcu[i]); 01374 01375 #ifdef ARMATURE_TEST 01376 if (is_joint) 01377 BLI_addtail(&job->adt->action->curves, job_curves[i]); 01378 #endif 01379 } 01380 01381 if (is_rotation || is_matrix) { 01382 if (is_joint) { 01383 bPoseChannel *chan = get_pose_channel(ob->pose, bone_name); 01384 chan->rotmode = ROT_MODE_QUAT; 01385 } 01386 else { 01387 ob->rotmode = ROT_MODE_QUAT; 01388 } 01389 } 01390 01391 return job; 01392 } 01393 01394 // internal, better make it private 01395 // warning: evaluates only rotation and only assigns matrix transforms now 01396 // prerequisites: animlist_map, curve_map 01397 void AnimationImporter::evaluate_transform_at_frame(float mat[4][4], COLLADAFW::Node *node, float fra) 01398 { 01399 const COLLADAFW::TransformationPointerArray& tms = node->getTransformations(); 01400 01401 unit_m4(mat); 01402 01403 for (unsigned int i = 0; i < tms.getCount(); i++) { 01404 COLLADAFW::Transformation *tm = tms[i]; 01405 COLLADAFW::Transformation::TransformationType type = tm->getTransformationType(); 01406 float m[4][4]; 01407 01408 unit_m4(m); 01409 if ( type != COLLADAFW::Transformation::MATRIX ) 01410 continue; 01411 01412 std::string nodename = node->getName().size() ? node->getName() : node->getOriginalId(); 01413 if (!evaluate_animation(tm, m, fra, nodename.c_str())) { 01414 /*switch (type) { 01415 case COLLADAFW::Transformation::ROTATE: 01416 dae_rotate_to_mat4(tm, m); 01417 break; 01418 case COLLADAFW::Transformation::TRANSLATE: 01419 dae_translate_to_mat4(tm, m); 01420 break; 01421 case COLLADAFW::Transformation::SCALE: 01422 dae_scale_to_mat4(tm, m); 01423 break; 01424 case COLLADAFW::Transformation::MATRIX: 01425 dae_matrix_to_mat4(tm, m); 01426 break; 01427 default: 01428 fprintf(stderr, "unsupported transformation type %d\n", type); 01429 }*/ 01430 dae_matrix_to_mat4(tm, m); 01431 01432 } 01433 01434 float temp[4][4]; 01435 copy_m4_m4(temp, mat); 01436 01437 mult_m4_m4m4(mat, temp, m); 01438 } 01439 } 01440 01441 // return true to indicate that mat contains a sane value 01442 bool AnimationImporter::evaluate_animation(COLLADAFW::Transformation *tm, float mat[4][4], float fra, const char *node_id) 01443 { 01444 const COLLADAFW::UniqueId& listid = tm->getAnimationList(); 01445 COLLADAFW::Transformation::TransformationType type = tm->getTransformationType(); 01446 01447 if (type != COLLADAFW::Transformation::ROTATE && 01448 type != COLLADAFW::Transformation::SCALE && 01449 type != COLLADAFW::Transformation::TRANSLATE && 01450 type != COLLADAFW::Transformation::MATRIX) { 01451 fprintf(stderr, "animation of transformation %d is not supported yet\n", type); 01452 return false; 01453 } 01454 01455 if (animlist_map.find(listid) == animlist_map.end()) 01456 return false; 01457 01458 const COLLADAFW::AnimationList *animlist = animlist_map[listid]; 01459 const COLLADAFW::AnimationList::AnimationBindings& bindings = animlist->getAnimationBindings(); 01460 01461 if (bindings.getCount()) { 01462 float vec[3]; 01463 01464 bool is_scale = (type == COLLADAFW::Transformation::SCALE); 01465 bool is_translate = (type == COLLADAFW::Transformation::TRANSLATE); 01466 01467 if (is_scale) 01468 dae_scale_to_v3(tm, vec); 01469 else if (is_translate) 01470 dae_translate_to_v3(tm, vec); 01471 01472 for (unsigned int j = 0; j < bindings.getCount(); j++) { 01473 const COLLADAFW::AnimationList::AnimationBinding& binding = bindings[j]; 01474 std::vector<FCurve*>& curves = curve_map[binding.animation]; 01475 COLLADAFW::AnimationList::AnimationClass animclass = binding.animationClass; 01476 char path[100]; 01477 01478 switch (type) { 01479 case COLLADAFW::Transformation::ROTATE: 01480 BLI_snprintf(path, sizeof(path), "%s.rotate (binding %u)", node_id, j); 01481 break; 01482 case COLLADAFW::Transformation::SCALE: 01483 BLI_snprintf(path, sizeof(path), "%s.scale (binding %u)", node_id, j); 01484 break; 01485 case COLLADAFW::Transformation::TRANSLATE: 01486 BLI_snprintf(path, sizeof(path), "%s.translate (binding %u)", node_id, j); 01487 break; 01488 case COLLADAFW::Transformation::MATRIX: 01489 BLI_snprintf(path, sizeof(path), "%s.matrix (binding %u)", node_id, j); 01490 break; 01491 default: 01492 break; 01493 } 01494 01495 if (animclass == COLLADAFW::AnimationList::UNKNOWN_CLASS) { 01496 fprintf(stderr, "%s: UNKNOWN animation class\n", path); 01497 //continue; 01498 } 01499 01500 if (type == COLLADAFW::Transformation::ROTATE) { 01501 if (curves.size() != 1) { 01502 fprintf(stderr, "expected 1 curve, got %d\n", (int)curves.size()); 01503 return false; 01504 } 01505 01506 // TODO support other animclasses 01507 if (animclass != COLLADAFW::AnimationList::ANGLE) { 01508 fprintf(stderr, "%s: animation class %d is not supported yet\n", path, animclass); 01509 return false; 01510 } 01511 01512 COLLADABU::Math::Vector3& axis = ((COLLADAFW::Rotate*)tm)->getRotationAxis(); 01513 float ax[3] = {axis[0], axis[1], axis[2]}; 01514 float angle = evaluate_fcurve(curves[0], fra); 01515 axis_angle_to_mat4(mat, ax, angle); 01516 01517 return true; 01518 } 01519 else if (is_scale || is_translate) { 01520 bool is_xyz = animclass == COLLADAFW::AnimationList::POSITION_XYZ; 01521 01522 if ((!is_xyz && curves.size() != 1) || (is_xyz && curves.size() != 3)) { 01523 if (is_xyz) 01524 fprintf(stderr, "%s: expected 3 curves, got %d\n", path, (int)curves.size()); 01525 else 01526 fprintf(stderr, "%s: expected 1 curve, got %d\n", path, (int)curves.size()); 01527 return false; 01528 } 01529 01530 switch (animclass) { 01531 case COLLADAFW::AnimationList::POSITION_X: 01532 vec[0] = evaluate_fcurve(curves[0], fra); 01533 break; 01534 case COLLADAFW::AnimationList::POSITION_Y: 01535 vec[1] = evaluate_fcurve(curves[0], fra); 01536 break; 01537 case COLLADAFW::AnimationList::POSITION_Z: 01538 vec[2] = evaluate_fcurve(curves[0], fra); 01539 break; 01540 case COLLADAFW::AnimationList::POSITION_XYZ: 01541 vec[0] = evaluate_fcurve(curves[0], fra); 01542 vec[1] = evaluate_fcurve(curves[1], fra); 01543 vec[2] = evaluate_fcurve(curves[2], fra); 01544 break; 01545 default: 01546 fprintf(stderr, "%s: animation class %d is not supported yet\n", path, animclass); 01547 break; 01548 } 01549 } 01550 else if (type == COLLADAFW::Transformation::MATRIX) { 01551 // for now, of matrix animation, support only the case when all values are packed into one animation 01552 if (curves.size() != 16) { 01553 fprintf(stderr, "%s: expected 16 curves, got %d\n", path, (int)curves.size()); 01554 return false; 01555 } 01556 01557 COLLADABU::Math::Matrix4 matrix; 01558 int i = 0, j = 0; 01559 01560 for (std::vector<FCurve*>::iterator it = curves.begin(); it != curves.end(); it++) { 01561 matrix.setElement(i, j, evaluate_fcurve(*it, fra)); 01562 j++; 01563 if (j == 4) { 01564 i++; 01565 j = 0; 01566 } 01567 unused_curves.erase(std::remove(unused_curves.begin(), unused_curves.end(), *it), unused_curves.end()); 01568 } 01569 01570 COLLADAFW::Matrix tm(matrix); 01571 dae_matrix_to_mat4(&tm, mat); 01572 01573 std::vector<FCurve*>::iterator it; 01574 01575 return true; 01576 } 01577 } 01578 01579 if (is_scale) 01580 size_to_mat4(mat, vec); 01581 else 01582 copy_v3_v3(mat[3], vec); 01583 01584 return is_scale || is_translate; 01585 } 01586 01587 return false; 01588 } 01589 01590 // gives a world-space mat of joint at rest position 01591 void AnimationImporter::get_joint_rest_mat(float mat[4][4], COLLADAFW::Node *root, COLLADAFW::Node *node) 01592 { 01593 // if bind mat is not available, 01594 // use "current" node transform, i.e. all those tms listed inside <node> 01595 if (!armature_importer->get_joint_bind_mat(mat, node)) { 01596 float par[4][4], m[4][4]; 01597 01598 calc_joint_parent_mat_rest(par, NULL, root, node); 01599 get_node_mat(m, node, NULL, NULL); 01600 mult_m4_m4m4(mat, par, m); 01601 } 01602 } 01603 01604 // gives a world-space mat, end's mat not included 01605 bool AnimationImporter::calc_joint_parent_mat_rest(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end) 01606 { 01607 float m[4][4]; 01608 01609 if (node == end) { 01610 par ? copy_m4_m4(mat, par) : unit_m4(mat); 01611 return true; 01612 } 01613 01614 // use bind matrix if available or calc "current" world mat 01615 if (!armature_importer->get_joint_bind_mat(m, node)) { 01616 if (par) { 01617 float temp[4][4]; 01618 get_node_mat(temp, node, NULL, NULL); 01619 mult_m4_m4m4(m, par, temp); 01620 } 01621 else { 01622 get_node_mat(m, node, NULL, NULL); 01623 } 01624 } 01625 01626 COLLADAFW::NodePointerArray& children = node->getChildNodes(); 01627 for (unsigned int i = 0; i < children.getCount(); i++) { 01628 if (calc_joint_parent_mat_rest(mat, m, children[i], end)) 01629 return true; 01630 } 01631 01632 return false; 01633 } 01634 01635 #ifdef ARMATURE_TEST 01636 Object *AnimationImporter::get_joint_object(COLLADAFW::Node *root, COLLADAFW::Node *node, Object *par_job) 01637 { 01638 if (joint_objects.find(node->getUniqueId()) == joint_objects.end()) { 01639 Object *job = add_object(scene, OB_EMPTY); 01640 01641 rename_id((ID*)&job->id, (char*)get_joint_name(node)); 01642 01643 job->lay = object_in_scene(job, scene)->lay = 2; 01644 01645 mul_v3_fl(job->size, 0.5f); 01646 job->recalc |= OB_RECALC_OB; 01647 01648 verify_adt_action((ID*)&job->id, 1); 01649 01650 job->rotmode = ROT_MODE_QUAT; 01651 01652 float mat[4][4]; 01653 get_joint_rest_mat(mat, root, node); 01654 01655 if (par_job) { 01656 float temp[4][4], ipar[4][4]; 01657 invert_m4_m4(ipar, par_job->obmat); 01658 copy_m4_m4(temp, mat); 01659 mult_m4_m4m4(mat, ipar, temp); 01660 } 01661 01662 TransformBase::decompose(mat, job->loc, NULL, job->quat, job->size); 01663 01664 if (par_job) { 01665 job->parent = par_job; 01666 01667 par_job->recalc |= OB_RECALC_OB; 01668 job->parsubstr[0] = 0; 01669 } 01670 01671 where_is_object(scene, job); 01672 01673 // after parenting and layer change 01674 DAG_scene_sort(CTX_data_main(C), scene); 01675 01676 joint_objects[node->getUniqueId()] = job; 01677 } 01678 01679 return joint_objects[node->getUniqueId()]; 01680 } 01681 #endif 01682 01683 #if 0 01684 // recursively evaluates joint tree until end is found, mat then is world-space matrix of end 01685 // mat must be identity on enter, node must be root 01686 bool AnimationImporter::evaluate_joint_world_transform_at_frame(float mat[4][4], float par[4][4], COLLADAFW::Node *node, COLLADAFW::Node *end, float fra) 01687 { 01688 float m[4][4]; 01689 if (par) { 01690 float temp[4][4]; 01691 evaluate_transform_at_frame(temp, node, node == end ? fra : 0.0f); 01692 mult_m4_m4m4(m, par, temp); 01693 } 01694 else { 01695 evaluate_transform_at_frame(m, node, node == end ? fra : 0.0f); 01696 } 01697 01698 if (node == end) { 01699 copy_m4_m4(mat, m); 01700 return true; 01701 } 01702 else { 01703 COLLADAFW::NodePointerArray& children = node->getChildNodes(); 01704 for (int i = 0; i < children.getCount(); i++) { 01705 if (evaluate_joint_world_transform_at_frame(mat, m, children[i], end, fra)) 01706 return true; 01707 } 01708 } 01709 01710 return false; 01711 } 01712 #endif 01713 01714 void AnimationImporter::add_bone_fcurve(Object *ob, COLLADAFW::Node *node, FCurve *fcu) 01715 { 01716 const char *bone_name = bc_get_joint_name(node); 01717 bAction *act = ob->adt->action; 01718 01719 /* try to find group */ 01720 bActionGroup *grp = action_groups_find_named(act, bone_name); 01721 01722 /* no matching groups, so add one */ 01723 if (grp == NULL) { 01724 /* Add a new group, and make it active */ 01725 grp = (bActionGroup*)MEM_callocN(sizeof(bActionGroup), "bActionGroup"); 01726 01727 grp->flag = AGRP_SELECTED; 01728 BLI_strncpy(grp->name, bone_name, sizeof(grp->name)); 01729 01730 BLI_addtail(&act->groups, grp); 01731 BLI_uniquename(&act->groups, grp, "Group", '.', offsetof(bActionGroup, name), 64); 01732 } 01733 01734 /* add F-Curve to group */ 01735 action_groups_add_channel(act, grp, fcu); 01736 } 01737 01738 void AnimationImporter::add_bezt(FCurve *fcu, float fra, float value) 01739 { 01740 //float fps = (float)FPS; 01741 BezTriple bez; 01742 memset(&bez, 0, sizeof(BezTriple)); 01743 bez.vec[1][0] = fra ; 01744 bez.vec[1][1] = value; 01745 bez.ipo = BEZT_IPO_LIN ;/* use default interpolation mode here... */ 01746 bez.f1 = bez.f2 = bez.f3 = SELECT; 01747 bez.h1 = bez.h2 = HD_AUTO; 01748 insert_bezt_fcurve(fcu, &bez, 0); 01749 calchandles_fcurve(fcu); 01750 } 01751