77
78
return &joint_index_to_joint_info_map[joint_index];
80
void ArmatureImporter::create_unskinned_bone( COLLADAFW::Node *node, EditBone *parent, int totchild,
81
float parent_mat[][4], Object * ob_arm)
83
std::vector<COLLADAFW::Node*>::iterator it;
84
it = std::find(finished_joints.begin(),finished_joints.end(),node);
85
if ( it != finished_joints.end()) return;
91
get_node_mat(obmat, node, NULL, NULL);
93
EditBone *bone = ED_armature_edit_bone_add((bArmature*)ob_arm->data, (char*)bc_get_joint_name(node));
96
if (parent) bone->parent = parent;
102
mult_m4_m4m4(mat, parent_mat, obmat);
106
copy_m4_m4(mat, obmat);
109
float loc[3], size[3], rot[3][3];
110
mat4_to_loc_rot_size( loc, rot, size, obmat);
111
mat3_to_vec_roll(rot, NULL, &angle );
114
copy_v3_v3(bone->head, mat[3]);
116
// set tail, don't set it to head because 0-length bones are not allowed
117
float vec[3] = {0.0f, 0.5f, 0.0f};
118
add_v3_v3v3(bone->tail, bone->head, vec);
121
if (parent && totchild == 1) {
122
copy_v3_v3(parent->tail, bone->head);
124
// not setting BONE_CONNECTED because this would lock child bone location with respect to parent
125
// bone->flag |= BONE_CONNECTED;
127
// XXX increase this to prevent "very" small bones?
128
const float epsilon = 0.000001f;
130
// derive leaf bone length
131
float length = len_v3v3(parent->head, parent->tail);
132
if ((length < leaf_bone_length || totbone == 0) && length > epsilon) {
133
leaf_bone_length = length;
136
// treat zero-sized bone like a leaf bone
137
if (length <= epsilon) {
138
add_leaf_bone(parent_mat, parent, node);
143
COLLADAFW::NodePointerArray& children = node->getChildNodes();
144
for (unsigned int i = 0; i < children.getCount(); i++) {
145
create_unskinned_bone( children[i], bone, children.getCount(), mat, ob_arm);
148
// in second case it's not a leaf bone, but we handle it the same way
149
if (!children.getCount() || children.getCount() > 1) {
150
add_leaf_bone(mat, bone, node);
153
finished_joints.push_back(node);
157
void ArmatureImporter::create_bone(SkinInfo& skin, COLLADAFW::Node *node, EditBone *parent, int totchild,
158
float parent_mat[][4], bArmature *arm)
82
void ArmatureImporter::create_bone(SkinInfo* skin, COLLADAFW::Node *node, EditBone *parent, int totchild,
83
float parent_mat[4][4], bArmature *arm)
160
85
//Checking if bone is already made.
161
std::vector<COLLADAFW::Node*>::iterator it;
162
it = std::find(finished_joints.begin(),finished_joints.end(),node);
163
if ( it != finished_joints.end()) return;
86
std::vector<COLLADAFW::Node *>::iterator it;
87
it = std::find(finished_joints.begin(), finished_joints.end(), node);
88
if (it != finished_joints.end()) return;
165
90
float joint_inv_bind_mat[4][4];
167
92
// JointData* jd = get_joint_data(node);
171
97
// TODO rename from Node "name" attrs later
172
EditBone *bone = ED_armature_edit_bone_add(arm, (char*)bc_get_joint_name(node));
98
EditBone *bone = ED_armature_edit_bone_add(arm, (char *)bc_get_joint_name(node));
175
if (skin.get_joint_inv_bind_matrix(joint_inv_bind_mat, node)) {
101
if (skin && skin->get_joint_inv_bind_matrix(joint_inv_bind_mat, node)) {
176
102
// get original world-space matrix
177
103
invert_m4_m4(mat, joint_inv_bind_mat);
179
105
// create a bone even if there's no joint data for it (i.e. it has no influence)
184
108
get_node_mat(obmat, node, NULL, NULL);
186
110
// get world-space
188
112
mult_m4_m4m4(mat, parent_mat, obmat);
190
115
copy_m4_m4(mat, obmat);
192
float loc[3], size[3], rot[3][3] , angle;
193
mat4_to_loc_rot_size( loc, rot, size, obmat);
194
mat3_to_vec_roll(rot, NULL, &angle );
199
119
if (parent) bone->parent = parent;
121
float loc[3], size[3], rot[3][3];
123
float vec[3] = {0.0f, 0.5f, 0.0f};
124
mat4_to_loc_rot_size(loc, rot, size, mat);
125
//copy_m3_m4(bonemat,mat);
126
mat3_to_vec_roll(rot, vec, &angle);
202
130
copy_v3_v3(bone->head, mat[3]);
204
132
// set tail, don't set it to head because 0-length bones are not allowed
205
float vec[3] = {0.0f, 0.5f, 0.0f};
206
133
add_v3_v3v3(bone->tail, bone->head, vec);
208
135
// set parent tail
209
136
if (parent && totchild == 1) {
210
copy_v3_v3(parent->tail, bone->head);
137
copy_v3_v3(parent->tail, bone->head);
212
139
// not setting BONE_CONNECTED because this would lock child bone location with respect to parent
213
// bone->flag |= BONE_CONNECTED;
140
bone->flag |= BONE_CONNECTED;
215
142
// XXX increase this to prevent "very" small bones?
216
143
const float epsilon = 0.000001f;
408
310
void ArmatureImporter::create_armature_bones( )
410
std::vector<COLLADAFW::Node*>::iterator ri;
312
std::vector<COLLADAFW::Node *>::iterator ri;
314
leaf_bone_length = FLT_MAX;
411
315
//if there is an armature created for root_joint next root_joint
412
316
for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
413
if ( get_armature_for_joint(*ri) != NULL ) continue;
317
if (get_armature_for_joint(*ri) != NULL) continue;
415
//add armature object for current joint
416
//Object *ob_arm = bc_add_object(scene, OB_ARMATURE, NULL);
418
319
Object *ob_arm = joint_parent_map[(*ri)->getUniqueId()];
423
//ob_arm->type = OB_ARMATURE;
424
324
ED_armature_to_edit(ob_arm);
426
// min_angle = 360.0f; // minimum angle between bone head-tail and a row of bone matrix
428
// create unskinned bones
431
check if bones have already been created for a given joint
433
leaf_bone_length = FLT_MAX;
434
create_unskinned_bone(*ri, NULL, (*ri)->getChildNodes().getCount(), NULL, ob_arm);
328
* check if bones have already been created for a given joint
331
create_bone(NULL, *ri , NULL, (*ri)->getChildNodes().getCount(), NULL, (bArmature *)ob_arm->data);
333
//leaf bone tails are derived from the matrix, so no need of this.
436
334
fix_leaf_bones();
438
336
// exit armature edit mode
440
337
unskinned_armature_map[(*ri)->getUniqueId()] = ob_arm;
442
339
ED_armature_from_edit(ob_arm);
444
set_pose(ob_arm , *ri, NULL, NULL );
341
//This serves no purpose, as pose is automatically reset later, in BKE_where_is_bone()
342
//set_pose(ob_arm, *ri, NULL, NULL);
446
344
ED_armature_edit_free(ob_arm);
447
DAG_id_tag_update(&ob_arm->id, OB_RECALC_OB|OB_RECALC_DATA);
345
DAG_id_tag_update(&ob_arm->id, OB_RECALC_OB | OB_RECALC_DATA);
453
349
void ArmatureImporter::create_armature_bones(SkinInfo& skin)
559
467
// exit armature edit mode
560
468
ED_armature_from_edit(ob_arm);
561
469
ED_armature_edit_free(ob_arm);
562
DAG_id_tag_update(&ob_arm->id, OB_RECALC_OB|OB_RECALC_DATA);
470
DAG_id_tag_update(&ob_arm->id, OB_RECALC_OB | OB_RECALC_DATA);
564
// set_leaf_bone_shapes(ob_arm);
565
// set_euler_rotmode();
569
// root - if this joint is the top joint in hierarchy, if a joint
570
// is a child of a node (not joint), root should be true since
571
// this is where we build armature bones from
573
void ArmatureImporter::set_pose ( Object * ob_arm , COLLADAFW::Node * root_node , const char *parentname, float parent_mat[][4])
474
void ArmatureImporter::set_pose(Object *ob_arm, COLLADAFW::Node *root_node, const char *parentname, float parent_mat[4][4])
575
char * bone_name = (char *) bc_get_joint_name ( root_node);
476
char *bone_name = (char *) bc_get_joint_name(root_node);
577
478
float obmat[4][4];
583
481
get_node_mat(obmat, root_node, NULL, NULL);
586
bPoseChannel * pchan = get_pose_channel(ob_arm -> pose , bone_name);
484
bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, bone_name);
587
485
//else fprintf ( "",
589
487
// get world-space
590
488
if (parentname) {
591
489
mult_m4_m4m4(mat, parent_mat, obmat);
592
bPoseChannel *parchan = get_pose_channel(ob_arm->pose, parentname);
490
bPoseChannel *parchan = BKE_pose_channel_find_name(ob_arm->pose, parentname);
594
mult_m4_m4m4(pchan->pose_mat, parchan->pose_mat, mat );
492
mult_m4_m4m4(pchan->pose_mat, parchan->pose_mat, mat);
598
497
copy_m4_m4(mat, obmat);
599
498
float invObmat[4][4];
600
499
invert_m4_m4(invObmat, ob_arm->obmat);
601
500
mult_m4_m4m4(pchan->pose_mat, invObmat, mat);
604
mat4_to_axis_angle(ax,&angle,mat);
605
pchan->bone->roll = angle;
504
//float angle = 0.0f;
505
///*mat4_to_axis_angle(ax, &angle, mat);
506
//pchan->bone->roll = angle;*/
608
509
COLLADAFW::NodePointerArray& children = root_node->getChildNodes();
652
557
create_armature_bones(skin);
654
559
// link armature with a mesh object
655
Object *ob = mesh_importer->get_object_by_geom_uid(*get_geometry_uid(skin.get_controller_uid()));
657
skin.link_armature(C, ob, joint_by_uid, this);
560
const COLLADAFW::UniqueId &uid = skin.get_controller_uid();
561
const COLLADAFW::UniqueId *guid = get_geometry_uid(uid);
563
Object *ob = mesh_importer->get_object_by_geom_uid(*guid);
565
skin.link_armature(C, ob, joint_by_uid, this);
567
fprintf(stderr, "Cannot find object to link armature with.\n");
659
fprintf(stderr, "Cannot find object to link armature with.\n");
570
fprintf(stderr, "Cannot find geometry to link armature with.\n");
661
572
// set armature parent if any
662
573
Object *par = skin.get_parent();
664
bc_set_parent(skin.get_armature(), par, C, false);
575
bc_set_parent(skin.BKE_armature_from_object(), par, C, false);
666
577
// free memory stolen from SkinControllerData
738
648
skin_by_data_uid[data_uid].set_controller(co);
740
650
// morph controller
743
fprintf(stderr, "Morph controller is not supported yet.\n");
651
else if (controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_MORPH) {
652
COLLADAFW::MorphController *co = (COLLADAFW::MorphController *)controller;
653
// to be able to find geom id by controller id
654
geom_uid_by_controller_uid[con_id] = co->getSource();
655
//Shape keys are applied in DocumentImporter->finish()
656
morph_controllers.push_back(co);
662
void ArmatureImporter::make_shape_keys()
664
std::vector<COLLADAFW::MorphController *>::iterator mc;
667
for (mc = morph_controllers.begin(); mc != morph_controllers.end(); mc++) {
669
COLLADAFW::UniqueIdArray& morphTargetIds = (*mc)->getMorphTargets();
670
COLLADAFW::FloatOrDoubleArray& morphWeights = (*mc)->getMorphWeights();
672
//Prereq: all the geometries must be imported and mesh objects must be made
673
Object *source_ob = this->mesh_importer->get_object_by_geom_uid((*mc)->getSource());
677
Mesh *source_me = (Mesh*) source_ob->data;
678
//insert key to source mesh
679
Key *key = source_me->key = BKE_key_add((ID *)source_me);
680
key->type = KEY_RELATIVE;
684
kb = BKE_keyblock_add_ctime(key, "Basis", FALSE);
685
BKE_key_convert_from_mesh(source_me, kb);
687
//insert other shape keys
688
for (int i = 0 ; i < morphTargetIds.getCount() ; i++ ) {
689
//better to have a seperate map of morph objects,
690
//This'll do for now since only mesh morphing is imported
692
Mesh *me = this->mesh_importer->get_mesh_by_geom_uid(morphTargetIds[i]);
696
std::string morph_name = *this->mesh_importer->get_geometry_name(me->id.name);
698
kb = BKE_keyblock_add_ctime(key, morph_name.c_str(), FALSE);
699
BKE_key_convert_from_mesh(me, kb);
702
weight = morphWeights.getFloatValues()->getData()[i];
706
fprintf(stderr, "Morph target geometry not found.\n");
711
fprintf(stderr, "Morph target object not found.\n");
750
717
COLLADAFW::UniqueId *ArmatureImporter::get_geometry_uid(const COLLADAFW::UniqueId& controller_uid)