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