~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/collada/ArmatureImporter.cpp

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
}
51
51
 
52
52
ArmatureImporter::ArmatureImporter(UnitConverter *conv, MeshImporterBase *mesh, AnimationImporterBase *anim, Scene *sce) :
53
 
        TransformReader(conv), scene(sce), empty(NULL), mesh_importer(mesh), anim_importer(anim) {}
 
53
        TransformReader(conv), scene(sce), empty(NULL), mesh_importer(mesh), anim_importer(anim) {
 
54
}
54
55
 
55
56
ArmatureImporter::~ArmatureImporter()
56
57
{
68
69
 
69
70
        if (joint_id_to_joint_index_map.find(joint_id) == joint_id_to_joint_index_map.end()) {
70
71
                fprintf(stderr, "Cannot find a joint index by joint id for %s.\n",
71
 
                                node->getOriginalId().c_str());
 
72
                        node->getOriginalId().c_str());
72
73
                return NULL;
73
74
        }
74
75
 
77
78
        return &joint_index_to_joint_info_map[joint_index];
78
79
}
79
80
#endif
80
 
void ArmatureImporter::create_unskinned_bone( COLLADAFW::Node *node, EditBone *parent, int totchild,
81
 
                                 float parent_mat[][4], Object * ob_arm)
82
 
{
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; 
86
 
 
87
 
        float mat[4][4];
88
 
        float obmat[4][4];
89
 
 
90
 
        // object-space
91
 
        get_node_mat(obmat, node, NULL, NULL);
92
 
 
93
 
        EditBone *bone = ED_armature_edit_bone_add((bArmature*)ob_arm->data, (char*)bc_get_joint_name(node));
94
 
        totbone++;
95
 
        
96
 
        if (parent) bone->parent = parent;
97
 
 
98
 
        float angle = 0;
99
 
 
100
 
        // get world-space
101
 
        if (parent) {
102
 
                mult_m4_m4m4(mat, parent_mat, obmat);
103
 
 
104
 
        }
105
 
        else {
106
 
                copy_m4_m4(mat, obmat);
107
 
 
108
 
        }
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 );
112
 
        bone->roll=angle;
113
 
        // set head
114
 
        copy_v3_v3(bone->head, mat[3]);
115
 
 
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);
119
 
 
120
 
        // set parent tail
121
 
        if (parent && totchild == 1) {
122
 
                copy_v3_v3(parent->tail, bone->head);
123
 
                
124
 
                // not setting BONE_CONNECTED because this would lock child bone location with respect to parent
125
 
                // bone->flag |= BONE_CONNECTED;
126
 
        
127
 
                // XXX increase this to prevent "very" small bones?
128
 
                const float epsilon = 0.000001f;
129
 
 
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;
134
 
                }
135
 
 
136
 
                // treat zero-sized bone like a leaf bone
137
 
                if (length <= epsilon) {
138
 
                        add_leaf_bone(parent_mat, parent, node);
139
 
                }
140
 
 
141
 
        }
142
 
 
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);
146
 
        }
147
 
 
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);
151
 
        }
152
 
 
153
 
        finished_joints.push_back(node);
154
 
 
155
 
}
156
 
 
157
 
void ArmatureImporter::create_bone(SkinInfo& skin, COLLADAFW::Node *node, EditBone *parent, int totchild,
158
 
                                 float parent_mat[][4], bArmature *arm)
 
81
 
 
82
void ArmatureImporter::create_bone(SkinInfo* skin, COLLADAFW::Node *node, EditBone *parent, int totchild,
 
83
                                   float parent_mat[4][4], bArmature *arm)
159
84
{
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;
164
89
 
165
90
        float joint_inv_bind_mat[4][4];
166
91
 
167
92
        // JointData* jd = get_joint_data(node);
168
93
 
169
94
        float mat[4][4];
170
 
 
 
95
        float obmat[4][4];
 
96
        
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));
173
99
        totbone++;
174
100
 
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);
178
104
        }
179
105
        // create a bone even if there's no joint data for it (i.e. it has no influence)
180
106
        else {
181
 
                float obmat[4][4];
182
 
 
183
 
                // object-space
 
107
                // bone-space
184
108
                get_node_mat(obmat, node, NULL, NULL);
185
109
 
186
110
                // get world-space
187
 
                if (parent)
 
111
                if (parent) {
188
112
                        mult_m4_m4m4(mat, parent_mat, obmat);
189
 
                else
 
113
                }
 
114
                else {
190
115
                        copy_m4_m4(mat, obmat);
191
 
 
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 );
195
 
                bone->roll=angle;
 
116
                }
196
117
        }
197
118
 
198
 
        
199
119
        if (parent) bone->parent = parent;
200
120
 
 
121
        float loc[3], size[3], rot[3][3]; 
 
122
        float angle;
 
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);
 
127
 
 
128
        bone->roll = angle;
201
129
        // set head
202
130
        copy_v3_v3(bone->head, mat[3]);
203
131
 
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);
207
134
 
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);
211
138
 
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;
214
141
 
215
142
                // XXX increase this to prevent "very" small bones?
216
143
                const float epsilon = 0.000001f;
226
153
                        add_leaf_bone(parent_mat, parent, node);
227
154
                }
228
155
 
229
 
                /*
230
 
#if 0
231
 
                // and which row in mat is bone direction
232
 
                float vec[3];
233
 
                sub_v3_v3v3(vec, parent->tail, parent->head);
234
 
#ifdef COLLADA_DEBUG
235
 
                print_v3("tail - head", vec);
236
 
                print_m4("matrix", parent_mat);
237
 
#endif
238
 
                for (int i = 0; i < 3; i++) {
239
 
#ifdef COLLADA_DEBUG
240
 
                        char *axis_names[] = {"X", "Y", "Z"};
241
 
                        printf("%s-axis length is %f\n", axis_names[i], len_v3(parent_mat[i]));
242
 
#endif
243
 
                        float angle = angle_v2v2(vec, parent_mat[i]);
244
 
                        if (angle < min_angle) {
245
 
#ifdef COLLADA_DEBUG
246
 
                                print_v3("picking", parent_mat[i]);
247
 
                                printf("^ %s axis of %s's matrix\n", axis_names[i], get_dae_name(node));
248
 
#endif
249
 
                                bone_direction_row = i;
250
 
                                min_angle = angle;
251
 
                        }
252
 
                }
253
 
#endif
254
 
                */
255
156
        }
256
157
 
257
158
        COLLADAFW::NodePointerArray& children = node->getChildNodes();
261
162
 
262
163
        // in second case it's not a leaf bone, but we handle it the same way
263
164
        if (!children.getCount() || children.getCount() > 1) {
264
 
                add_leaf_bone(mat, bone , node);
 
165
                add_leaf_bone(mat, bone, node);
265
166
        }
266
167
 
 
168
        bone->length = len_v3v3(bone->head, bone->tail);
 
169
 
267
170
        finished_joints.push_back(node);
268
171
}
269
172
 
270
 
void ArmatureImporter::add_leaf_bone(float mat[][4], EditBone *bone,  COLLADAFW::Node * node)
 
173
void ArmatureImporter::add_leaf_bone(float mat[4][4], EditBone *bone,  COLLADAFW::Node *node)
271
174
{
272
175
        LeafBone leaf;
273
176
 
278
181
        TagsMap::iterator etit;
279
182
        ExtraTags *et = 0;
280
183
        etit = uid_tags_map.find(node->getUniqueId().toAscii());
281
 
        if (etit !=  uid_tags_map.end())
282
 
        {
 
184
        if (etit != uid_tags_map.end()) {
283
185
                et = etit->second;
284
186
                //else return;
285
187
 
286
 
                float x,y,z;
287
 
                et->setData("tip_x",&x);
288
 
                et->setData("tip_y",&y);
289
 
                et->setData("tip_z",&z);
290
 
                float vec[3] = {x,y,z};
 
188
                float x, y, z;
 
189
                et->setData("tip_x", &x);
 
190
                et->setData("tip_y", &y);
 
191
                et->setData("tip_z", &z);
 
192
                float vec[3] = {x, y, z};
291
193
                copy_v3_v3(leaf.bone->tail, leaf.bone->head);
292
194
                add_v3_v3v3(leaf.bone->tail, leaf.bone->head, vec);
293
 
        }else
 
195
        }
 
196
        else {
294
197
                leaf_bones.push_back(leaf);
 
198
        }
295
199
}
296
200
 
297
201
void ArmatureImporter::fix_leaf_bones( )
298
202
{
299
203
        // just setting tail for leaf bones here
300
 
 
301
204
        std::vector<LeafBone>::iterator it;
302
205
        for (it = leaf_bones.begin(); it != leaf_bones.end(); it++) {
303
206
                LeafBone& leaf = *it;
305
208
                // pointing up
306
209
                float vec[3] = {0.0f, 0.0f, 0.1f};
307
210
                
308
 
                // if parent: take parent length and direction
309
 
                if (leaf.bone->parent) sub_v3_v3v3(vec, leaf.bone->parent->tail, leaf.bone->parent->head);
 
211
                sub_v3_v3v3(vec, leaf.bone->tail , leaf.bone->head);
 
212
                mul_v3_fl(vec, leaf_bone_length);
 
213
                add_v3_v3v3(leaf.bone->tail, leaf.bone->head , vec);
310
214
 
311
 
                copy_v3_v3(leaf.bone->tail, leaf.bone->head);
312
 
                add_v3_v3v3(leaf.bone->tail, leaf.bone->head, vec);
313
215
        }
314
216
}
315
217
 
322
224
        for (it = leaf_bones.begin(); it != leaf_bones.end(); it++) {
323
225
                LeafBone& leaf = *it;
324
226
 
325
 
                bPoseChannel *pchan = get_pose_channel(pose, leaf.name);
 
227
                bPoseChannel *pchan = BKE_pose_channel_find_name(pose, leaf.name);
326
228
                if (pchan) {
327
229
                        pchan->custom = get_empty_for_leaves();
328
230
                }
336
238
{
337
239
        // just set rotmode = ROT_MODE_EUL on pose channel for each joint
338
240
 
339
 
        std::map<COLLADAFW::UniqueId, COLLADAFW::Node*>::iterator it;
 
241
        std::map<COLLADAFW::UniqueId, COLLADAFW::Node *>::iterator it;
340
242
 
341
243
        for (it = joint_by_uid.begin(); it != joint_by_uid.end(); it++) {
342
244
 
377
279
#if 0
378
280
Object *ArmatureImporter::find_armature(COLLADAFW::Node *node)
379
281
{
380
 
        JointData* jd = get_joint_data(node);
 
282
        JointData *jd = get_joint_data(node);
381
283
        if (jd) return jd->ob_arm;
382
284
 
383
285
        COLLADAFW::NodePointerArray& children = node->getChildNodes();
407
309
#endif
408
310
void ArmatureImporter::create_armature_bones( )
409
311
{
410
 
        std::vector<COLLADAFW::Node*>::iterator ri;
 
312
        std::vector<COLLADAFW::Node *>::iterator ri;
 
313
 
 
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;
414
318
                
415
 
                //add armature object for current joint
416
 
                //Object *ob_arm = bc_add_object(scene, OB_ARMATURE, NULL);
417
 
 
418
319
                Object *ob_arm = joint_parent_map[(*ri)->getUniqueId()];
419
320
 
420
321
                if (!ob_arm)
421
322
                        continue;
422
323
 
423
 
                //ob_arm->type = OB_ARMATURE;
424
324
                ED_armature_to_edit(ob_arm);
425
325
 
426
 
                // min_angle = 360.0f;          // minimum angle between bone head-tail and a row of bone matrix
427
 
 
428
 
                // create unskinned bones
429
326
                /*
430
 
                  TODO:
431
 
                  check if bones have already been created for a given joint
432
 
                */
433
 
                leaf_bone_length = FLT_MAX;
434
 
                create_unskinned_bone(*ri, NULL, (*ri)->getChildNodes().getCount(), NULL, ob_arm);
435
 
 
 
327
                 * TODO:
 
328
                 * check if bones have already been created for a given joint
 
329
                 */
 
330
 
 
331
                create_bone(NULL, *ri , NULL, (*ri)->getChildNodes().getCount(), NULL, (bArmature *)ob_arm->data);
 
332
 
 
333
                //leaf bone tails are derived from the matrix, so no need of this.
436
334
                fix_leaf_bones();
437
335
 
438
336
                // exit armature edit mode
439
 
        
440
337
                unskinned_armature_map[(*ri)->getUniqueId()] = ob_arm;
441
338
 
442
339
                ED_armature_from_edit(ob_arm);
443
340
 
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);
445
343
 
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);
448
346
        }
449
 
 
450
 
        
451
347
}
452
348
 
453
349
void ArmatureImporter::create_armature_bones(SkinInfo& skin)
495
391
 
496
392
        SkinInfo *a = &skin;
497
393
        Object *shared = NULL;
498
 
        std::vector<COLLADAFW::Node*> skin_root_joints;
 
394
        std::vector<COLLADAFW::Node *> skin_root_joints;
499
395
 
500
396
        std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
501
397
        for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
502
398
                SkinInfo *b = &it->second;
503
 
                if (b == a || b->get_armature() == NULL)
 
399
                if (b == a || b->BKE_armature_from_object() == NULL)
504
400
                        continue;
505
401
 
506
402
                skin_root_joints.clear();
507
403
 
508
404
                b->find_root_joints(root_joints, joint_by_uid, skin_root_joints);
509
405
 
510
 
                std::vector<COLLADAFW::Node*>::iterator ri;
 
406
                std::vector<COLLADAFW::Node *>::iterator ri;
511
407
                for (ri = skin_root_joints.begin(); ri != skin_root_joints.end(); ri++) {
512
408
                        if (a->uses_joint_or_descendant(*ri)) {
513
 
                                shared = b->get_armature();
 
409
                                shared = b->BKE_armature_from_object();
514
410
                                break;
515
411
                        }
516
412
                }
519
415
                        break;
520
416
        }
521
417
 
522
 
        if (shared)
 
418
        if (!shared && this->joint_parent_map.size() > 0) {
 
419
                // All armatures have been created while creating the Node tree. 
 
420
                // The Collada exporter currently does not create a 
 
421
                // strict relationship between geometries and armatures
 
422
                // So when we reimport a Blender collada file, then we have
 
423
                // to guess what is meant.
 
424
                // XXX This is not safe when we have more than one armatures
 
425
                // in the import.
 
426
                shared = this->joint_parent_map.begin()->second;
 
427
        }
 
428
 
 
429
        if (shared) {
523
430
                ob_arm = skin.set_armature(shared);
524
 
        else
525
 
                ob_arm = skin.create_armature(scene); //once for every armature
 
431
        }
 
432
        else {
 
433
                ob_arm = skin.create_armature(scene);  //once for every armature
 
434
        }
526
435
 
527
436
        // enter armature edit mode
528
437
        ED_armature_to_edit(ob_arm);
531
440
        totbone = 0;
532
441
        // bone_direction_row = 1; // TODO: don't default to Y but use asset and based on it decide on default row
533
442
        leaf_bone_length = FLT_MAX;
534
 
        // min_angle = 360.0f;          // minimum angle between bone head-tail and a row of bone matrix
535
443
 
536
444
        // create bones
537
445
        /*
538
446
           TODO:
539
447
           check if bones have already been created for a given joint
540
 
        */
 
448
         */
541
449
 
542
 
        std::vector<COLLADAFW::Node*>::iterator ri;
 
450
        std::vector<COLLADAFW::Node *>::iterator ri;
543
451
        for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
544
452
                // for shared armature check if bone tree is already created
545
453
                if (shared && std::find(skin_root_joints.begin(), skin_root_joints.end(), *ri) != skin_root_joints.end())
547
455
 
548
456
                // since root_joints may contain joints for multiple controllers, we need to filter
549
457
                if (skin.uses_joint_or_descendant(*ri)) {
550
 
                        create_bone(skin, *ri, NULL, (*ri)->getChildNodes().getCount(), NULL, (bArmature*)ob_arm->data);
 
458
                        create_bone(&skin, *ri, NULL, (*ri)->getChildNodes().getCount(), NULL, (bArmature *)ob_arm->data);
551
459
 
552
460
                        if (joint_parent_map.find((*ri)->getUniqueId()) != joint_parent_map.end() && !skin.get_parent())
553
461
                                skin.set_parent(joint_parent_map[(*ri)->getUniqueId()]);
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);
563
471
 
564
 
        // set_leaf_bone_shapes(ob_arm);
565
 
        // set_euler_rotmode();
566
472
}
567
473
 
568
 
 
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
572
 
 
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])
574
475
575
 
        char * bone_name = (char *) bc_get_joint_name ( root_node);
 
476
        char *bone_name = (char *) bc_get_joint_name(root_node);
576
477
        float mat[4][4];
577
478
        float obmat[4][4];
578
479
 
579
 
        float ax[3];
580
 
        float angle = 0.0f;
581
 
        
582
480
        // object-space
583
481
        get_node_mat(obmat, root_node, NULL, NULL);
584
482
 
585
483
        //if (*edbone)
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 ( "",
588
486
 
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);
593
491
 
594
 
                mult_m4_m4m4(pchan->pose_mat, parchan->pose_mat, mat );
 
492
                mult_m4_m4m4(pchan->pose_mat, parchan->pose_mat, mat);
595
493
 
596
494
        }
597
495
        else {
 
496
                
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);
 
501
                
602
502
        }
603
503
 
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;*/
606
507
 
607
508
 
608
509
        COLLADAFW::NodePointerArray& children = root_node->getChildNodes();
612
513
 
613
514
}
614
515
 
 
516
 
 
517
// root - if this joint is the top joint in hierarchy, if a joint
 
518
// is a child of a node (not joint), root should be true since
 
519
// this is where we build armature bones from
615
520
void ArmatureImporter::add_joint(COLLADAFW::Node *node, bool root, Object *parent, Scene *sce)
616
521
{
617
522
        joint_by_uid[node->getUniqueId()] = node;
630
535
{
631
536
        // root_joints.push_back(node);
632
537
        Object *ob_arm = find_armature(node);
633
 
        if (ob_arm)     {
 
538
        if (ob_arm) {
634
539
                get_armature_joints(ob_arm).root_joints.push_back(node);
635
540
        }
636
541
#ifdef COLLADA_DEBUG
652
557
                create_armature_bones(skin);
653
558
 
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()));
656
 
                if (ob)
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);
 
562
                if (guid != NULL) {
 
563
                        Object *ob = mesh_importer->get_object_by_geom_uid(*guid);
 
564
                        if (ob)
 
565
                                skin.link_armature(C, ob, joint_by_uid, this);
 
566
                        else
 
567
                                fprintf(stderr, "Cannot find object to link armature with.\n");
 
568
                }
658
569
                else
659
 
                        fprintf(stderr, "Cannot find object to link armature with.\n");
 
570
                        fprintf(stderr, "Cannot find geometry to link armature with.\n");
660
571
 
661
572
                // set armature parent if any
662
573
                Object *par = skin.get_parent();
663
574
                if (par)
664
 
                        bc_set_parent(skin.get_armature(), par, C, false);
 
575
                        bc_set_parent(skin.BKE_armature_from_object(), par, C, false);
665
576
 
666
577
                // free memory stolen from SkinControllerData
667
578
                skin.free();
693
604
}
694
605
#endif
695
606
 
696
 
bool ArmatureImporter::write_skin_controller_data(const COLLADAFW::SkinControllerData* data)
 
607
bool ArmatureImporter::write_skin_controller_data(const COLLADAFW::SkinControllerData *data)
697
608
{
698
609
        // at this stage we get vertex influence info that should go into me->verts and ob->defbase
699
610
        // there's no info to which object this should be long so we associate it with skin controller data UID
718
629
        return true;
719
630
}
720
631
 
721
 
bool ArmatureImporter::write_controller(const COLLADAFW::Controller* controller)
 
632
bool ArmatureImporter::write_controller(const COLLADAFW::Controller *controller)
722
633
{
723
634
        // - create and store armature object
724
 
 
725
 
        const COLLADAFW::UniqueId& skin_id = controller->getUniqueId();
 
635
        const COLLADAFW::UniqueId& con_id = controller->getUniqueId();
726
636
 
727
637
        if (controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_SKIN) {
728
 
                COLLADAFW::SkinController *co = (COLLADAFW::SkinController*)controller;
 
638
                COLLADAFW::SkinController *co = (COLLADAFW::SkinController *)controller;
729
639
                // to be able to find geom id by controller id
730
 
                geom_uid_by_controller_uid[skin_id] = co->getSource();
 
640
                geom_uid_by_controller_uid[con_id] = co->getSource();
731
641
 
732
642
                const COLLADAFW::UniqueId& data_uid = co->getSkinControllerData();
733
643
                if (skin_by_data_uid.find(data_uid) == skin_by_data_uid.end()) {
738
648
                skin_by_data_uid[data_uid].set_controller(co);
739
649
        }
740
650
        // morph controller
741
 
        else {
742
 
                // shape keys? :)
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);
744
657
        }
745
658
 
746
659
        return true;
747
660
}
748
661
 
 
662
void ArmatureImporter::make_shape_keys()
 
663
{
 
664
        std::vector<COLLADAFW::MorphController *>::iterator mc;
 
665
        float weight;
 
666
 
 
667
        for (mc = morph_controllers.begin(); mc != morph_controllers.end(); mc++) {
 
668
                //Controller data
 
669
                COLLADAFW::UniqueIdArray& morphTargetIds = (*mc)->getMorphTargets();
 
670
                COLLADAFW::FloatOrDoubleArray& morphWeights = (*mc)->getMorphWeights();
 
671
 
 
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());
 
674
                
 
675
                if (source_ob) {
 
676
 
 
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;
 
681
                        KeyBlock *kb;
 
682
                        
 
683
                        //insert basis key
 
684
                        kb = BKE_keyblock_add_ctime(key, "Basis", FALSE);
 
685
                        BKE_key_convert_from_mesh(source_me, kb);
 
686
 
 
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
 
691
 
 
692
                                Mesh *me = this->mesh_importer->get_mesh_by_geom_uid(morphTargetIds[i]);
 
693
                                
 
694
                                if (me) {
 
695
                                        me->key = key;
 
696
                                        std::string morph_name = *this->mesh_importer->get_geometry_name(me->id.name);
 
697
 
 
698
                                        kb = BKE_keyblock_add_ctime(key, morph_name.c_str(), FALSE);
 
699
                                        BKE_key_convert_from_mesh(me, kb);
 
700
                                        
 
701
                                        //apply weights
 
702
                                        weight =  morphWeights.getFloatValues()->getData()[i];
 
703
                                        kb->curval = weight;
 
704
                                }
 
705
                                else {
 
706
                                        fprintf(stderr, "Morph target geometry not found.\n");
 
707
                                }
 
708
                        }
 
709
                }
 
710
                else {
 
711
                        fprintf(stderr, "Morph target object not found.\n");
 
712
                }
 
713
        }
 
714
}
 
715
 
749
716
 
750
717
COLLADAFW::UniqueId *ArmatureImporter::get_geometry_uid(const COLLADAFW::UniqueId& controller_uid)
751
718
{
762
729
                SkinInfo& skin = it->second;
763
730
 
764
731
                if (skin.uses_joint_or_descendant(node))
765
 
                        return skin.get_armature();
 
732
                        return skin.BKE_armature_from_object();
766
733
        }
767
734
 
768
 
        std::map<COLLADAFW::UniqueId, Object*>::iterator arm;
 
735
        std::map<COLLADAFW::UniqueId, Object *>::iterator arm;
769
736
        for (arm = unskinned_armature_map.begin(); arm != unskinned_armature_map.end(); arm++) {
770
737
                if (arm->first == node->getUniqueId() )
771
738
                        return arm->second;
784
751
}
785
752
 
786
753
// gives a world-space mat
787
 
bool ArmatureImporter::get_joint_bind_mat(float m[][4], COLLADAFW::Node *joint)
 
754
bool ArmatureImporter::get_joint_bind_mat(float m[4][4], COLLADAFW::Node *joint)
788
755
{
789
756
        std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
790
757
        bool found = false;
798
765
 
799
766
        return found;
800
767
}
801
 
 
802
 
 
803