Blender  V2.93
ArmatureImporter.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 /* COLLADABU_ASSERT, may be able to remove later */
22 #include "COLLADABUPlatform.h"
23 
24 #include <algorithm>
25 
26 #include "COLLADAFWUniqueId.h"
27 
28 #include "BKE_action.h"
29 #include "BKE_armature.h"
30 #include "BKE_object.h"
31 #include "BLI_listbase.h"
32 #include "BLI_string.h"
33 #include "ED_armature.h"
34 
35 #include "DEG_depsgraph.h"
36 
37 #include "ArmatureImporter.h"
38 #include "collada_utils.h"
39 
40 /* use node name, or fall back to original id if not present (name is optional) */
41 template<class T> static const char *bc_get_joint_name(T *node)
42 {
43  const std::string &id = node->getName();
44  return id.empty() ? node->getOriginalId().c_str() : id.c_str();
45 }
46 
49  Main *bmain,
50  Scene *sce,
51  ViewLayer *view_layer,
52  const ImportSettings *import_settings)
53  : TransformReader(conv),
54  m_bmain(bmain),
55  scene(sce),
56  view_layer(view_layer),
57  unit_converter(conv),
58  import_settings(import_settings),
59  empty(nullptr),
60  mesh_importer(mesh)
61 {
62 }
63 
65 {
66  /* free skin controller data if we forget to do this earlier */
67  std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
68  for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
69  it->second.free();
70  }
71 }
72 
73 #if 0
74 JointData *ArmatureImporter::get_joint_data(COLLADAFW::Node *node);
75 {
76  const COLLADAFW::UniqueId &joint_id = node->getUniqueId();
77 
78  if (joint_id_to_joint_index_map.find(joint_id) == joint_id_to_joint_index_map.end()) {
79  fprintf(
80  stderr, "Cannot find a joint index by joint id for %s.\n", node->getOriginalId().c_str());
81  return NULL;
82  }
83 
84  int joint_index = joint_id_to_joint_index_map[joint_id];
85 
86  return &joint_index_to_joint_info_map[joint_index];
87 }
88 #endif
89 
90 int ArmatureImporter::create_bone(SkinInfo *skin,
91  COLLADAFW::Node *node,
92  EditBone *parent,
93  int totchild,
94  float parent_mat[4][4],
95  bArmature *arm,
96  std::vector<std::string> &layer_labels)
97 {
98  float mat[4][4];
99  float joint_inv_bind_mat[4][4];
100  float joint_bind_mat[4][4];
101  int chain_length = 0;
102 
103  /* Checking if bone is already made. */
104  std::vector<COLLADAFW::Node *>::iterator it;
105  it = std::find(finished_joints.begin(), finished_joints.end(), node);
106  if (it != finished_joints.end()) {
107  return chain_length;
108  }
109 
111  totbone++;
112 
113  /*
114  * We use the inv_bind_shape matrix to apply the armature bind pose as its rest pose.
115  */
116 
117  std::map<COLLADAFW::UniqueId, SkinInfo>::iterator skin_it;
118  bool bone_is_skinned = false;
119  for (skin_it = skin_by_data_uid.begin(); skin_it != skin_by_data_uid.end(); skin_it++) {
120 
121  SkinInfo *b = &skin_it->second;
122  if (b->get_joint_inv_bind_matrix(joint_inv_bind_mat, node)) {
123 
124  /* get original world-space matrix */
125  invert_m4_m4(mat, joint_inv_bind_mat);
126  copy_m4_m4(joint_bind_mat, mat);
127  /* And make local to armature */
128  Object *ob_arm = skin->BKE_armature_from_object();
129  if (ob_arm) {
130  float invmat[4][4];
131  invert_m4_m4(invmat, ob_arm->obmat);
132  mul_m4_m4m4(mat, invmat, mat);
133  }
134 
135  bone_is_skinned = true;
136  break;
137  }
138  }
139 
140  /* create a bone even if there's no joint data for it (i.e. it has no influence) */
141  if (!bone_is_skinned) {
142  get_node_mat(mat, node, nullptr, nullptr, parent_mat);
143  }
144 
145  if (parent) {
146  bone->parent = parent;
147  }
148 
149  float loc[3], size[3], rot[3][3];
150  BoneExtensionMap &extended_bones = bone_extension_manager.getExtensionMap(arm);
151  BoneExtended &be = add_bone_extended(bone, node, totchild, layer_labels, extended_bones);
152  int layer = be.get_bone_layers();
153  if (layer) {
154  bone->layer = layer;
155  }
156  arm->layer |= layer; /* ensure that all populated bone layers are visible after import */
157 
158  float *tail = be.get_tail();
159  int use_connect = be.get_use_connect();
160 
161  switch (use_connect) {
162  case 1:
163  bone->flag |= BONE_CONNECTED;
164  break;
165  case -1: /* Connect type not specified */
166  case 0:
167  bone->flag &= ~BONE_CONNECTED;
168  break;
169  }
170 
171  if (be.has_roll()) {
172  bone->roll = be.get_roll();
173  }
174  else {
175  float angle;
176  mat4_to_loc_rot_size(loc, rot, size, mat);
177  mat3_to_vec_roll(rot, nullptr, &angle);
178  bone->roll = angle;
179  }
180  copy_v3_v3(bone->head, mat[3]);
181 
182  if (bone_is_skinned && this->import_settings->keep_bind_info) {
183  float rest_mat[4][4];
184  get_node_mat(rest_mat, node, nullptr, nullptr, nullptr);
185  bc_set_IDPropertyMatrix(bone, "bind_mat", joint_bind_mat);
186  bc_set_IDPropertyMatrix(bone, "rest_mat", rest_mat);
187  }
188 
189  add_v3_v3v3(bone->tail, bone->head, tail); /* tail must be non zero */
190 
191  /* find smallest bone length in armature (used later for leaf bone length) */
192  if (parent) {
193 
194  if (use_connect == 1) {
195  copy_v3_v3(parent->tail, bone->head);
196  }
197 
198  /* guess reasonable leaf bone length */
199  float length = len_v3v3(parent->head, bone->head);
200  if ((length < leaf_bone_length || totbone == 0) && length > MINIMUM_BONE_LENGTH) {
201  leaf_bone_length = length;
202  }
203  }
204 
205  COLLADAFW::NodePointerArray &children = node->getChildNodes();
206 
207  for (unsigned int i = 0; i < children.getCount(); i++) {
208  int cl = create_bone(skin, children[i], bone, children.getCount(), mat, arm, layer_labels);
209  if (cl > chain_length) {
210  chain_length = cl;
211  }
212  }
213 
214  bone->length = len_v3v3(bone->head, bone->tail);
215  joint_by_uid[node->getUniqueId()] = node;
216  finished_joints.push_back(node);
217 
218  be.set_chain_length(chain_length + 1);
219 
220  return chain_length + 1;
221 }
222 
229 void ArmatureImporter::fix_leaf_bone_hierarchy(bArmature *armature,
230  Bone *bone,
231  bool fix_orientation)
232 {
233  if (bone == nullptr) {
234  return;
235  }
236 
237  if (bc_is_leaf_bone(bone)) {
238  BoneExtensionMap &extended_bones = bone_extension_manager.getExtensionMap(armature);
239  BoneExtended *be = extended_bones[bone->name];
240  EditBone *ebone = bc_get_edit_bone(armature, bone->name);
241  fix_leaf_bone(armature, ebone, be, fix_orientation);
242  }
243 
244  for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
245  fix_leaf_bone_hierarchy(armature, child, fix_orientation);
246  }
247 }
248 
249 void ArmatureImporter::fix_leaf_bone(bArmature *armature,
250  EditBone *ebone,
251  BoneExtended *be,
252  bool fix_orientation)
253 {
254  if (be == nullptr || !be->has_tail()) {
255 
256  /* Collada only knows Joints, Here we guess a reasonable leaf bone length */
257  float leaf_length = (leaf_bone_length == FLT_MAX) ? 1.0 : leaf_bone_length;
258 
259  float vec[3];
260 
261  if (fix_orientation && ebone->parent != nullptr) {
262  EditBone *parent = ebone->parent;
263  sub_v3_v3v3(vec, ebone->head, parent->head);
264  if (len_squared_v3(vec) < MINIMUM_BONE_LENGTH) {
265  sub_v3_v3v3(vec, parent->tail, parent->head);
266  }
267  }
268  else {
269  vec[2] = 0.1f;
270  sub_v3_v3v3(vec, ebone->tail, ebone->head);
271  }
272 
273  normalize_v3_v3(vec, vec);
274  mul_v3_fl(vec, leaf_length);
275  add_v3_v3v3(ebone->tail, ebone->head, vec);
276  }
277 }
278 
279 void ArmatureImporter::fix_parent_connect(bArmature *armature, Bone *bone)
280 {
281  /* armature has no bones */
282  if (bone == nullptr) {
283  return;
284  }
285 
286  if (bone->parent && bone->flag & BONE_CONNECTED) {
287  copy_v3_v3(bone->parent->tail, bone->head);
288  }
289 
290  for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
291  fix_parent_connect(armature, child);
292  }
293 }
294 
295 void ArmatureImporter::connect_bone_chains(bArmature *armature,
296  Bone *parentbone,
297  int max_chain_length)
298 {
299  BoneExtensionMap &extended_bones = bone_extension_manager.getExtensionMap(armature);
300  BoneExtended *dominant_child = nullptr;
301  int maxlen = 0;
302 
303  if (parentbone == nullptr) {
304  return;
305  }
306 
307  Bone *child = (Bone *)parentbone->childbase.first;
308  if (child && (import_settings->find_chains || child->next == nullptr)) {
309  for (; child; child = child->next) {
310  BoneExtended *be = extended_bones[child->name];
311  if (be != nullptr) {
312  int chain_len = be->get_chain_length();
313  if (chain_len <= max_chain_length) {
314  if (chain_len > maxlen) {
315  dominant_child = be;
316  maxlen = chain_len;
317  }
318  else if (chain_len == maxlen) {
319  dominant_child = nullptr;
320  }
321  }
322  }
323  }
324  }
325 
326  BoneExtended *pbe = extended_bones[parentbone->name];
327  if (dominant_child != nullptr) {
328  /* Found a valid chain. Now connect current bone with that chain.*/
329  EditBone *pebone = bc_get_edit_bone(armature, parentbone->name);
330  EditBone *cebone = bc_get_edit_bone(armature, dominant_child->get_name());
331  if (pebone && !(cebone->flag & BONE_CONNECTED)) {
332  float vec[3];
333  sub_v3_v3v3(vec, cebone->head, pebone->head);
334 
335  /*
336  * It is possible that the child's head is located on the parents head.
337  * When this happens, then moving the parent's tail to the child's head
338  * would result in a zero sized bone and Blender would silently remove the bone.
339  * So we move the tail only when the resulting bone has a minimum length:
340  */
341 
342  if (len_squared_v3(vec) > MINIMUM_BONE_LENGTH) {
343  copy_v3_v3(pebone->tail, cebone->head);
344  pbe->set_tail(pebone->tail); /* to make fix_leafbone happy ...*/
345  if (pbe && pbe->get_chain_length() >= this->import_settings->min_chain_length) {
346 
347  BoneExtended *cbe = extended_bones[cebone->name];
348  cbe->set_use_connect(true);
349 
350  cebone->flag |= BONE_CONNECTED;
351  pbe->set_leaf_bone(false);
352  printf("Connect Bone chain: parent (%s --> %s) child)\n", pebone->name, cebone->name);
353  }
354  }
355  }
356  for (Bone *ch = (Bone *)parentbone->childbase.first; ch; ch = ch->next) {
357  ArmatureImporter::connect_bone_chains(armature, ch, UNLIMITED_CHAIN_MAX);
358  }
359  }
360  else if (maxlen > 1 && maxlen > this->import_settings->min_chain_length) {
361  /* Try again with smaller chain length */
362  ArmatureImporter::connect_bone_chains(armature, parentbone, maxlen - 1);
363  }
364  else {
365  /* can't connect this Bone. Proceed with children ... */
366  if (pbe) {
367  pbe->set_leaf_bone(true);
368  }
369  for (Bone *ch = (Bone *)parentbone->childbase.first; ch; ch = ch->next) {
370  ArmatureImporter::connect_bone_chains(armature, ch, UNLIMITED_CHAIN_MAX);
371  }
372  }
373 }
374 
375 #if 0
376 void ArmatureImporter::set_leaf_bone_shapes(Object *ob_arm)
377 {
378  bPose *pose = ob_arm->pose;
379 
380  std::vector<LeafBone>::iterator it;
381  for (it = leaf_bones.begin(); it != leaf_bones.end(); it++) {
382  LeafBone &leaf = *it;
383 
384  bPoseChannel *pchan = BKE_pose_channel_find_name(pose, leaf.name);
385  if (pchan) {
386  pchan->custom = get_empty_for_leaves();
387  }
388  else {
389  fprintf(stderr, "Cannot find a pose channel for leaf bone %s\n", leaf.name);
390  }
391  }
392 }
393 
394 void ArmatureImporter::set_euler_rotmode()
395 {
396  /* just set rotmode = ROT_MODE_EUL on pose channel for each joint */
397 
398  std::map<COLLADAFW::UniqueId, COLLADAFW::Node *>::iterator it;
399 
400  for (it = joint_by_uid.begin(); it != joint_by_uid.end(); it++) {
401 
402  COLLADAFW::Node *joint = it->second;
403 
404  std::map<COLLADAFW::UniqueId, SkinInfo>::iterator sit;
405 
406  for (sit = skin_by_data_uid.begin(); sit != skin_by_data_uid.end(); sit++) {
407  SkinInfo &skin = sit->second;
408 
409  if (skin.uses_joint_or_descendant(joint)) {
410  bPoseChannel *pchan = skin.get_pose_channel_from_node(joint);
411 
412  if (pchan) {
413  pchan->rotmode = ROT_MODE_EUL;
414  }
415  else {
416  fprintf(stderr, "Cannot find pose channel for %s.\n", get_joint_name(joint));
417  }
418 
419  break;
420  }
421  }
422  }
423 }
424 #endif
425 
426 Object *ArmatureImporter::get_empty_for_leaves()
427 {
428  if (empty) {
429  return empty;
430  }
431 
432  empty = bc_add_object(m_bmain, scene, view_layer, OB_EMPTY, nullptr);
434 
435  return empty;
436 }
437 
438 #if 0
439 Object *ArmatureImporter::find_armature(COLLADAFW::Node *node)
440 {
441  JointData *jd = get_joint_data(node);
442  if (jd) {
443  return jd->ob_arm;
444  }
445 
446  COLLADAFW::NodePointerArray &children = node->getChildNodes();
447  for (int i = 0; i < children.getCount(); i++) {
448  Object *ob_arm = find_armature(children[i]);
449  if (ob_arm) {
450  return ob_arm;
451  }
452  }
453 
454  return NULL;
455 }
456 
457 ArmatureJoints &ArmatureImporter::get_armature_joints(Object *ob_arm)
458 {
459  /* try finding it */
460  std::vector<ArmatureJoints>::iterator it;
461  for (it = armature_joints.begin(); it != armature_joints.end(); it++) {
462  if ((*it).ob_arm == ob_arm) {
463  return *it;
464  }
465  }
466 
467  /* not found, create one */
468  ArmatureJoints aj;
469  aj.ob_arm = ob_arm;
470  armature_joints.push_back(aj);
471 
472  return armature_joints.back();
473 }
474 #endif
475 void ArmatureImporter::create_armature_bones(Main *bmain, std::vector<Object *> &arm_objs)
476 {
477  std::vector<COLLADAFW::Node *>::iterator ri;
478  std::vector<std::string> layer_labels;
479 
480  /* if there is an armature created for root_joint next root_joint */
481  for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
482  COLLADAFW::Node *node = *ri;
483  if (get_armature_for_joint(node) != nullptr) {
484  continue;
485  }
486 
487  Object *ob_arm = joint_parent_map[node->getUniqueId()];
488  if (!ob_arm) {
489  continue;
490  }
491 
492  bArmature *armature = (bArmature *)ob_arm->data;
493  if (!armature) {
494  continue;
495  }
496 
497  char *bone_name = (char *)bc_get_joint_name(node);
498  Bone *bone = BKE_armature_find_bone_name(armature, bone_name);
499  if (bone) {
500  fprintf(stderr,
501  "Reuse of child bone [%s] as root bone in same Armature is not supported.\n",
502  bone_name);
503  continue;
504  }
505 
506  ED_armature_to_edit(armature);
507  armature->layer = 0; /* layer is set according to imported bone set in create_bone() */
508 
509  create_bone(
510  nullptr, node, nullptr, node->getChildNodes().getCount(), nullptr, armature, layer_labels);
511  if (this->import_settings->find_chains) {
512  connect_bone_chains(armature, (Bone *)armature->bonebase.first, UNLIMITED_CHAIN_MAX);
513  }
514 
515  /* exit armature edit mode to populate the Armature object */
516  ED_armature_from_edit(bmain, armature);
517  ED_armature_edit_free(armature);
518  ED_armature_to_edit(armature);
519 
520  fix_leaf_bone_hierarchy(
521  armature, (Bone *)armature->bonebase.first, this->import_settings->fix_orientation);
522  unskinned_armature_map[node->getUniqueId()] = ob_arm;
523 
524  ED_armature_from_edit(bmain, armature);
525  ED_armature_edit_free(armature);
526 
527  set_bone_transformation_type(node, ob_arm);
528 
529  int index = std::find(arm_objs.begin(), arm_objs.end(), ob_arm) - arm_objs.begin();
530  if (index == 0) {
531  arm_objs.push_back(ob_arm);
532  }
533 
535  }
536 }
537 
538 Object *ArmatureImporter::create_armature_bones(Main *bmain, SkinInfo &skin)
539 {
540  /* just do like so:
541  * - get armature
542  * - enter editmode
543  * - add edit bones and head/tail properties using matrices and parent-child info
544  * - exit edit mode
545  * - set a sphere shape to leaf bones */
546 
547  Object *ob_arm = nullptr;
548 
549  /*
550  * find if there's another skin sharing at least one bone with this skin
551  * if so, use that skin's armature
552  */
553 
582  SkinInfo *a = &skin;
583  Object *shared = nullptr;
584  std::vector<COLLADAFW::Node *> skin_root_joints;
585  std::vector<std::string> layer_labels;
586 
587  std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
588  for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
589  SkinInfo *b = &it->second;
590  if (b == a || b->BKE_armature_from_object() == nullptr) {
591  continue;
592  }
593 
594  skin_root_joints.clear();
595 
596  b->find_root_joints(root_joints, joint_by_uid, skin_root_joints);
597 
598  std::vector<COLLADAFW::Node *>::iterator ri;
599  for (ri = skin_root_joints.begin(); ri != skin_root_joints.end(); ri++) {
600  COLLADAFW::Node *node = *ri;
601  if (a->uses_joint_or_descendant(node)) {
602  shared = b->BKE_armature_from_object();
603  break;
604  }
605  }
606 
607  if (shared != nullptr) {
608  break;
609  }
610  }
611 
612  if (!shared && !this->joint_parent_map.empty()) {
613  /* All armatures have been created while creating the Node tree.
614  * The Collada exporter currently does not create a
615  * strict relationship between geometries and armatures
616  * So when we reimport a Blender collada file, then we have
617  * to guess what is meant.
618  * XXX This is not safe when we have more than one armatures
619  * in the import. */
620  shared = this->joint_parent_map.begin()->second;
621  }
622 
623  if (shared) {
624  ob_arm = skin.set_armature(shared);
625  }
626  else {
627  ob_arm = skin.create_armature(m_bmain, scene, view_layer); /* once for every armature */
628  }
629 
630  /* enter armature edit mode */
631  bArmature *armature = (bArmature *)ob_arm->data;
632  ED_armature_to_edit(armature);
633 
634  totbone = 0;
635  // bone_direction_row = 1; /* TODO: don't default to Y but use asset and based on it decide on */
636  /* default row */
637 
638  /* create bones */
639  /* TODO:
640  * check if bones have already been created for a given joint */
641 
642  std::vector<COLLADAFW::Node *>::iterator ri;
643  for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
644  COLLADAFW::Node *node = *ri;
645  /* for shared armature check if bone tree is already created */
646  if (shared && std::find(skin_root_joints.begin(), skin_root_joints.end(), node) !=
647  skin_root_joints.end()) {
648  continue;
649  }
650 
651  /* since root_joints may contain joints for multiple controllers, we need to filter */
652  if (skin.uses_joint_or_descendant(node)) {
653 
654  create_bone(
655  &skin, node, nullptr, node->getChildNodes().getCount(), nullptr, armature, layer_labels);
656 
657  if (joint_parent_map.find(node->getUniqueId()) != joint_parent_map.end() &&
658  !skin.get_parent()) {
659  skin.set_parent(joint_parent_map[node->getUniqueId()]);
660  }
661  }
662  }
663 
664  /* exit armature edit mode to populate the Armature object */
665  ED_armature_from_edit(bmain, armature);
666  ED_armature_edit_free(armature);
667 
668  for (ri = root_joints.begin(); ri != root_joints.end(); ri++) {
669  COLLADAFW::Node *node = *ri;
670  set_bone_transformation_type(node, ob_arm);
671  }
672 
673  ED_armature_to_edit(armature);
674  if (this->import_settings->find_chains) {
675  connect_bone_chains(armature, (Bone *)armature->bonebase.first, UNLIMITED_CHAIN_MAX);
676  }
677  fix_leaf_bone_hierarchy(
678  armature, (Bone *)armature->bonebase.first, this->import_settings->fix_orientation);
679  ED_armature_from_edit(bmain, armature);
680  ED_armature_edit_free(armature);
681 
683 
684  return ob_arm;
685 }
686 
687 void ArmatureImporter::set_bone_transformation_type(const COLLADAFW::Node *node, Object *ob_arm)
688 {
690  if (pchan) {
691  pchan->rotmode = (node_is_decomposed(node)) ? ROT_MODE_EUL : ROT_MODE_QUAT;
692  }
693 
694  COLLADAFW::NodePointerArray childnodes = node->getChildNodes();
695  for (int index = 0; index < childnodes.getCount(); index++) {
696  node = childnodes[index];
697  set_bone_transformation_type(node, ob_arm);
698  }
699 }
700 
701 void ArmatureImporter::set_pose(Object *ob_arm,
702  COLLADAFW::Node *root_node,
703  const char *parentname,
704  float parent_mat[4][4])
705 {
706  const char *bone_name = bc_get_joint_name(root_node);
707  float mat[4][4];
708  float obmat[4][4];
709 
710  /* object-space */
711  get_node_mat(obmat, root_node, nullptr, nullptr);
712  bool is_decomposed = node_is_decomposed(root_node);
713 
714  // if (*edbone)
715  bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, bone_name);
716  pchan->rotmode = (is_decomposed) ? ROT_MODE_EUL : ROT_MODE_QUAT;
717 
718  // else fprintf ( "",
719 
720  /* get world-space */
721  if (parentname) {
722  mul_m4_m4m4(mat, parent_mat, obmat);
723  bPoseChannel *parchan = BKE_pose_channel_find_name(ob_arm->pose, parentname);
724 
725  mul_m4_m4m4(pchan->pose_mat, parchan->pose_mat, mat);
726  }
727  else {
728 
729  copy_m4_m4(mat, obmat);
730  float invObmat[4][4];
731  invert_m4_m4(invObmat, ob_arm->obmat);
732  mul_m4_m4m4(pchan->pose_mat, invObmat, mat);
733  }
734 
735 #if 0
736  float angle = 0.0f;
737  mat4_to_axis_angle(ax, &angle, mat);
738  pchan->bone->roll = angle;
739 #endif
740 
741  COLLADAFW::NodePointerArray &children = root_node->getChildNodes();
742  for (unsigned int i = 0; i < children.getCount(); i++) {
743  set_pose(ob_arm, children[i], bone_name, mat);
744  }
745 }
746 
747 bool ArmatureImporter::node_is_decomposed(const COLLADAFW::Node *node)
748 {
749  const COLLADAFW::TransformationPointerArray &nodeTransforms = node->getTransformations();
750  for (unsigned int i = 0; i < nodeTransforms.getCount(); i++) {
751  COLLADAFW::Transformation *transform = nodeTransforms[i];
752  COLLADAFW::Transformation::TransformationType tm_type = transform->getTransformationType();
753  if (tm_type == COLLADAFW::Transformation::MATRIX) {
754  return false;
755  }
756  }
757  return true;
758 }
759 
765 void ArmatureImporter::add_root_joint(COLLADAFW::Node *node, Object *parent)
766 {
767  root_joints.push_back(node);
768  if (parent) {
769  joint_parent_map[node->getUniqueId()] = parent;
770  }
771 }
772 
773 #if 0
774 void ArmatureImporter::add_root_joint(COLLADAFW::Node *node)
775 {
776  // root_joints.push_back(node);
777  Object *ob_arm = find_armature(node);
778  if (ob_arm) {
779  get_armature_joints(ob_arm).root_joints.push_back(node);
780  }
781 # ifdef COLLADA_DEBUG
782  else {
783  fprintf(stderr, "%s cannot be added to armature.\n", get_joint_name(node));
784  }
785 # endif
786 }
787 #endif
788 
789 /* here we add bones to armatures, having armatures previously created in write_controller */
790 void ArmatureImporter::make_armatures(bContext *C, std::vector<Object *> &objects_to_scale)
791 {
792  Main *bmain = CTX_data_main(C);
793  std::vector<Object *> arm_objs;
794  std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
795 
796  /* TODO: Make this work for more than one armature in the import file. */
797  leaf_bone_length = FLT_MAX;
798 
799  for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
800 
801  SkinInfo &skin = it->second;
802 
803  Object *ob_arm = create_armature_bones(bmain, skin);
804 
805  /* link armature with a mesh object */
806  const COLLADAFW::UniqueId &uid = skin.get_controller_uid();
807  const COLLADAFW::UniqueId *guid = get_geometry_uid(uid);
808  if (guid != nullptr) {
809  Object *ob = mesh_importer->get_object_by_geom_uid(*guid);
810  if (ob) {
811  skin.link_armature(C, ob, joint_by_uid, this);
812 
813  std::vector<Object *>::iterator ob_it = std::find(
814  objects_to_scale.begin(), objects_to_scale.end(), ob);
815 
816  if (ob_it != objects_to_scale.end()) {
817  int index = ob_it - objects_to_scale.begin();
818  objects_to_scale.erase(objects_to_scale.begin() + index);
819  }
820 
821  if (std::find(objects_to_scale.begin(), objects_to_scale.end(), ob_arm) ==
822  objects_to_scale.end()) {
823  objects_to_scale.push_back(ob_arm);
824  }
825 
826  if (std::find(arm_objs.begin(), arm_objs.end(), ob_arm) == arm_objs.end()) {
827  arm_objs.push_back(ob_arm);
828  }
829  }
830  else {
831  fprintf(stderr, "Cannot find object to link armature with.\n");
832  }
833  }
834  else {
835  fprintf(stderr, "Cannot find geometry to link armature with.\n");
836  }
837 
838  /* set armature parent if any */
839  Object *par = skin.get_parent();
840  if (par) {
841  bc_set_parent(skin.BKE_armature_from_object(), par, C, false);
842  }
843 
844  /* free memory stolen from SkinControllerData */
845  skin.free();
846  }
847 
848  /* for bones without skins */
849  create_armature_bones(bmain, arm_objs);
850 
851  /* Fix bone relations */
852  std::vector<Object *>::iterator ob_arm_it;
853  for (ob_arm_it = arm_objs.begin(); ob_arm_it != arm_objs.end(); ob_arm_it++) {
854 
855  Object *ob_arm = *ob_arm_it;
856  bArmature *armature = (bArmature *)ob_arm->data;
857 
858  /* and step back to edit mode to fix the leaf nodes */
859  ED_armature_to_edit(armature);
860 
861  fix_parent_connect(armature, (Bone *)armature->bonebase.first);
862 
863  ED_armature_from_edit(bmain, armature);
864  ED_armature_edit_free(armature);
865  }
866 }
867 
868 #if 0
869 /* link with meshes, create vertex groups, assign weights */
870 void ArmatureImporter::link_armature(Object *ob_arm,
871  const COLLADAFW::UniqueId &geom_id,
872  const COLLADAFW::UniqueId &controller_data_id)
873 {
874  Object *ob = mesh_importer->get_object_by_geom_uid(geom_id);
875 
876  if (!ob) {
877  fprintf(stderr, "Cannot find object by geometry UID.\n");
878  return;
879  }
880 
881  if (skin_by_data_uid.find(controller_data_id) == skin_by_data_uid.end()) {
882  fprintf(stderr, "Cannot find skin info by controller data UID.\n");
883  return;
884  }
885 
886  SkinInfo &skin = skin_by_data_uid[conroller_data_id];
887 
888  /* create vertex groups */
889 }
890 #endif
891 
892 bool ArmatureImporter::write_skin_controller_data(const COLLADAFW::SkinControllerData *data)
893 {
894  /* at this stage we get vertex influence info that should go into me->verts and ob->defbase
895  * there's no info to which object this should be long so we associate it with
896  * skin controller data UID. */
897 
898  /* don't forget to call BKE_object_defgroup_unique_name before we copy */
899 
900  /* controller data uid -> [armature] -> joint data,
901  * [mesh object] */
902 
903  SkinInfo skin(unit_converter);
905 
906  /* store join inv bind matrix to use it later in armature construction */
907  const COLLADAFW::Matrix4Array &inv_bind_mats = data->getInverseBindMatrices();
908  for (unsigned int i = 0; i < data->getJointsCount(); i++) {
909  skin.add_joint(inv_bind_mats[i]);
910  }
911 
912  skin_by_data_uid[data->getUniqueId()] = skin;
913 
914  return true;
915 }
916 
917 bool ArmatureImporter::write_controller(const COLLADAFW::Controller *controller)
918 {
919  /* - create and store armature object */
920  const COLLADAFW::UniqueId &con_id = controller->getUniqueId();
921 
922  if (controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_SKIN) {
923  COLLADAFW::SkinController *co = (COLLADAFW::SkinController *)controller;
924  /* to be able to find geom id by controller id */
925  geom_uid_by_controller_uid[con_id] = co->getSource();
926 
927  const COLLADAFW::UniqueId &data_uid = co->getSkinControllerData();
928  if (skin_by_data_uid.find(data_uid) == skin_by_data_uid.end()) {
929  fprintf(stderr, "Cannot find skin by controller data UID.\n");
930  return true;
931  }
932 
933  skin_by_data_uid[data_uid].set_controller(co);
934  }
935  /* morph controller */
936  else if (controller->getControllerType() == COLLADAFW::Controller::CONTROLLER_TYPE_MORPH) {
937  COLLADAFW::MorphController *co = (COLLADAFW::MorphController *)controller;
938  /* to be able to find geom id by controller id */
939  geom_uid_by_controller_uid[con_id] = co->getSource();
940  /* Shape keys are applied in DocumentImporter->finish() */
941  morph_controllers.push_back(co);
942  }
943 
944  return true;
945 }
946 
948 {
949  Main *bmain = CTX_data_main(C);
950  std::vector<COLLADAFW::MorphController *>::iterator mc;
951  float weight;
952 
953  for (mc = morph_controllers.begin(); mc != morph_controllers.end(); mc++) {
954  /* Controller data */
955  COLLADAFW::UniqueIdArray &morphTargetIds = (*mc)->getMorphTargets();
956  COLLADAFW::FloatOrDoubleArray &morphWeights = (*mc)->getMorphWeights();
957 
958  /* Prereq: all the geometries must be imported and mesh objects must be made */
959  Object *source_ob = this->mesh_importer->get_object_by_geom_uid((*mc)->getSource());
960 
961  if (source_ob) {
962 
963  Mesh *source_me = (Mesh *)source_ob->data;
964  /* insert key to source mesh */
965  Key *key = source_me->key = BKE_key_add(bmain, (ID *)source_me);
966  key->type = KEY_RELATIVE;
967  KeyBlock *kb;
968 
969  /* insert basis key */
970  kb = BKE_keyblock_add_ctime(key, "Basis", false);
971  BKE_keyblock_convert_from_mesh(source_me, key, kb);
972 
973  /* insert other shape keys */
974  for (int i = 0; i < morphTargetIds.getCount(); i++) {
975  /* Better to have a separate map of morph objects,
976  * This will do for now since only mesh morphing is imported. */
977 
978  Mesh *me = this->mesh_importer->get_mesh_by_geom_uid(morphTargetIds[i]);
979 
980  if (me) {
981  me->key = key;
982  std::string morph_name = *this->mesh_importer->get_geometry_name(me->id.name);
983 
984  kb = BKE_keyblock_add_ctime(key, morph_name.c_str(), false);
985  BKE_keyblock_convert_from_mesh(me, key, kb);
986 
987  /* apply weights */
988  weight = morphWeights.getFloatValues()->getData()[i];
989  kb->curval = weight;
990  }
991  else {
992  fprintf(stderr, "Morph target geometry not found.\n");
993  }
994  }
995  }
996  else {
997  fprintf(stderr, "Morph target object not found.\n");
998  }
999  }
1000 }
1001 
1002 COLLADAFW::UniqueId *ArmatureImporter::get_geometry_uid(const COLLADAFW::UniqueId &controller_uid)
1003 {
1004  if (geom_uid_by_controller_uid.find(controller_uid) == geom_uid_by_controller_uid.end()) {
1005  return nullptr;
1006  }
1007 
1008  return &geom_uid_by_controller_uid[controller_uid];
1009 }
1010 
1012 {
1013  std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
1014  for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
1015  SkinInfo &skin = it->second;
1016 
1017  if (skin.uses_joint_or_descendant(node)) {
1018  return skin.BKE_armature_from_object();
1019  }
1020  }
1021 
1022  std::map<COLLADAFW::UniqueId, Object *>::iterator arm;
1023  for (arm = unskinned_armature_map.begin(); arm != unskinned_armature_map.end(); arm++) {
1024  if (arm->first == node->getUniqueId()) {
1025  return arm->second;
1026  }
1027  }
1028  return nullptr;
1029 }
1030 
1031 void ArmatureImporter::set_tags_map(TagsMap &tags_map)
1032 {
1033  this->uid_tags_map = tags_map;
1034 }
1035 
1037  char *joint_path,
1038  size_t count)
1039 {
1040  char bone_name_esc[sizeof(((Bone *)nullptr)->name) * 2];
1041  BLI_str_escape(bone_name_esc, bc_get_joint_name(node), sizeof(bone_name_esc));
1042  BLI_snprintf(joint_path, count, "pose.bones[\"%s\"]", bone_name_esc);
1043 }
1044 
1045 /* gives a world-space mat */
1046 bool ArmatureImporter::get_joint_bind_mat(float m[4][4], COLLADAFW::Node *joint)
1047 {
1048  std::map<COLLADAFW::UniqueId, SkinInfo>::iterator it;
1049  bool found = false;
1050  for (it = skin_by_data_uid.begin(); it != skin_by_data_uid.end(); it++) {
1051  SkinInfo &skin = it->second;
1052  if ((found = skin.get_joint_inv_bind_matrix(m, joint))) {
1053  invert_m4(m);
1054  break;
1055  }
1056  }
1057 
1058  return found;
1059 }
1060 
1061 BoneExtended &ArmatureImporter::add_bone_extended(EditBone *bone,
1062  COLLADAFW::Node *node,
1063  int sibcount,
1064  std::vector<std::string> &layer_labels,
1065  BoneExtensionMap &extended_bones)
1066 {
1067  BoneExtended *be = new BoneExtended(bone);
1068  extended_bones[bone->name] = be;
1069 
1070  TagsMap::iterator etit;
1071  ExtraTags *et = nullptr;
1072  etit = uid_tags_map.find(node->getUniqueId().toAscii());
1073 
1074  bool has_connect = false;
1075  int connect_type = -1;
1076 
1077  if (etit != uid_tags_map.end()) {
1078 
1079  float tail[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
1080  float roll = 0;
1081  std::string layers;
1082 
1083  et = etit->second;
1084 
1085  bool has_tail = false;
1086  has_tail |= et->setData("tip_x", &tail[0]);
1087  has_tail |= et->setData("tip_y", &tail[1]);
1088  has_tail |= et->setData("tip_z", &tail[2]);
1089 
1090  has_connect = et->setData("connect", &connect_type);
1091  bool has_roll = et->setData("roll", &roll);
1092 
1093  layers = et->setData("layer", layers);
1094 
1095  if (has_tail && !has_connect) {
1096  /* got a bone tail definition but no connect info -> bone is not connected */
1097  has_connect = true;
1098  connect_type = 0;
1099  }
1100 
1101  be->set_bone_layers(layers, layer_labels);
1102  if (has_tail) {
1103  be->set_tail(tail);
1104  }
1105  if (has_roll) {
1106  be->set_roll(roll);
1107  }
1108  }
1109 
1110  if (!has_connect && this->import_settings->auto_connect) {
1111  /* Auto connect only when parent has exactly one child. */
1112  connect_type = sibcount == 1;
1113  }
1114 
1115  be->set_use_connect(connect_type);
1116  be->set_leaf_bone(true);
1117 
1118  return *be;
1119 }
static const char * bc_get_joint_name(T *node)
#define UNLIMITED_CHAIN_MAX
#define MINIMUM_BONE_LENGTH
Blender kernel action and pose functionality.
struct bPoseChannel * BKE_pose_channel_find_name(const struct bPose *pose, const char *name)
struct Bone * BKE_armature_find_bone_name(struct bArmature *arm, const char *name)
Definition: armature.c:609
void mat3_to_vec_roll(const float mat[3][3], float r_vec[3], float *r_roll)
Definition: armature.c:2059
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
void BKE_keyblock_convert_from_mesh(struct Mesh *me, struct Key *key, struct KeyBlock *kb)
Definition: key.c:2208
struct KeyBlock * BKE_keyblock_add_ctime(struct Key *key, const char *name, const bool do_force)
Definition: key.c:1873
struct Key * BKE_key_add(struct Main *bmain, struct ID *id)
Definition: key.c:259
General operations, lookup, etc. for blender objects.
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:262
bool invert_m4(float R[4][4])
Definition: math_matrix.c:1187
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1278
void mat4_to_loc_rot_size(float loc[3], float rot[3][3], float size[3], const float wmat[4][4])
Definition: math_matrix.c:2236
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:95
void mat4_to_axis_angle(float axis[3], float *angle, const float M[4][4])
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
size_t size_t char size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, const size_t dst_maxncpy) ATTR_NONNULL()
Definition: string.c:333
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_TRANSFORM
Definition: DNA_ID.h:599
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:611
@ ROT_MODE_QUAT
@ ROT_MODE_EUL
@ BONE_CONNECTED
@ KEY_RELATIVE
@ OB_EMPTY_SPHERE
@ OB_EMPTY
static Controller * controller
#define C
Definition: RandGen.cpp:39
EditBone * ED_armature_ebone_add(bArmature *arm, const char *name)
Definition: armature_add.c:69
void ED_armature_edit_free(struct bArmature *arm)
void ED_armature_from_edit(Main *bmain, bArmature *arm)
void ED_armature_to_edit(bArmature *arm)
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:895
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
void make_shape_keys(bContext *C)
void get_rna_path_for_joint(COLLADAFW::Node *node, char *joint_path, size_t count)
void make_armatures(bContext *C, std::vector< Object * > &objects_to_scale)
bool write_skin_controller_data(const COLLADAFW::SkinControllerData *data)
bool write_controller(const COLLADAFW::Controller *controller)
void set_tags_map(TagsMap &tags_map)
ArmatureImporter(UnitConverter *conv, MeshImporterBase *mesh, Main *bmain, Scene *sce, ViewLayer *view_layer, const ImportSettings *import_settings)
Object * get_armature_for_joint(COLLADAFW::Node *node)
void add_root_joint(COLLADAFW::Node *node, Object *parent)
bool get_joint_bind_mat(float m[4][4], COLLADAFW::Node *joint)
COLLADAFW::UniqueId * get_geometry_uid(const COLLADAFW::UniqueId &controller_uid)
void set_use_connect(int use_connect)
int get_chain_length()
void set_roll(float roll)
char * get_name()
void set_tail(const float vec[])
void set_leaf_bone(bool state)
void set_chain_length(const int aLength)
void set_bone_layers(std::string layers, std::vector< std::string > &layer_labels)
float * get_tail()
BoneExtensionMap & getExtensionMap(bArmature *armature)
Class for saving <extra> tags for a specific UniqueId.
Definition: ExtraTags.h:29
bool setData(std::string tag, short *data)
Definition: ExtraTags.cpp:79
virtual Object * get_object_by_geom_uid(const COLLADAFW::UniqueId &geom_uid)=0
virtual std::string * get_geometry_name(const std::string &mesh_name)=0
virtual Mesh * get_mesh_by_geom_uid(const COLLADAFW::UniqueId &mesh_uid)=0
void find_root_joints(const std::vector< COLLADAFW::Node * > &root_joints, std::map< COLLADAFW::UniqueId, COLLADAFW::Node * > &joint_by_uid, std::vector< COLLADAFW::Node * > &result)
Definition: SkinInfo.cpp:315
const COLLADAFW::UniqueId & get_controller_uid()
Definition: SkinInfo.cpp:190
void link_armature(bContext *C, Object *ob, std::map< COLLADAFW::UniqueId, COLLADAFW::Node * > &joint_by_uid, TransformReader *tm)
Definition: SkinInfo.cpp:220
Object * set_armature(Object *ob_arm)
Definition: SkinInfo.cpp:161
void borrow_skin_controller_data(const COLLADAFW::SkinControllerData *skin)
Definition: SkinInfo.cpp:102
bool uses_joint_or_descendant(COLLADAFW::Node *node)
Definition: SkinInfo.cpp:200
Object * BKE_armature_from_object()
Definition: SkinInfo.cpp:185
bool get_joint_inv_bind_matrix(float inv_bind_mat[4][4], COLLADAFW::Node *node)
Definition: SkinInfo.cpp:171
void add_joint(const COLLADABU::Math::Matrix4 &matrix)
Definition: SkinInfo.cpp:129
void set_parent(Object *_parent)
Definition: SkinInfo.cpp:305
Object * get_parent()
Definition: SkinInfo.cpp:310
bPoseChannel * get_pose_channel_from_node(COLLADAFW::Node *node)
Definition: SkinInfo.cpp:300
Object * create_armature(Main *bmain, Scene *scene, ViewLayer *view_layer)
Definition: SkinInfo.cpp:155
void free()
Definition: SkinInfo.cpp:118
void get_node_mat(float mat[4][4], COLLADAFW::Node *node, std::map< COLLADAFW::UniqueId, Animation > *animation_map, Object *ob)
bool bc_set_parent(Object *ob, Object *par, bContext *C, bool is_parent_space)
EditBone * bc_get_edit_bone(bArmature *armature, char *name)
void bc_set_IDPropertyMatrix(EditBone *ebone, const char *key, float mat[4][4])
Object * bc_add_object(Main *bmain, Scene *scene, ViewLayer *view_layer, int type, const char *name)
bool bc_is_leaf_bone(Bone *bone)
std::map< std::string, BoneExtended * > BoneExtensionMap
OperationNode * node
Scene scene
#define rot(x, k)
int count
#define T
static unsigned a[3]
Definition: RandGen.cpp:92
float roll
struct Bone * parent
char name[64]
float tail[3]
float head[3]
struct Bone * next
ListBase childbase
char name[64]
Definition: BKE_armature.h:57
float tail[3]
Definition: BKE_armature.h:66
float roll
Definition: BKE_armature.h:62
float length
Definition: BKE_armature.h:77
struct EditBone * parent
Definition: BKE_armature.h:55
float head[3]
Definition: BKE_armature.h:65
Definition: DNA_ID.h:273
char name[66]
Definition: DNA_ID.h:283
float curval
Definition: DNA_key_types.h:50
char type
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
struct Key * key
struct bPose * pose
char empty_drawtype
float obmat[4][4]
void * data
ListBase bonebase
unsigned int layer
struct Bone * bone
struct Object * custom
float pose_mat[4][4]