Blender  V3.3
ArmatureExporter.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "COLLADASWBaseInputElement.h"
8 #include "COLLADASWInstanceController.h"
9 #include "COLLADASWPrimitves.h"
10 #include "COLLADASWSource.h"
11 
12 #include "DNA_action_types.h"
13 #include "DNA_meshdata_types.h"
14 #include "DNA_modifier_types.h"
15 
16 #include "BKE_action.h"
17 #include "BKE_armature.h"
18 #include "BKE_global.h"
19 #include "BKE_mesh.h"
20 
21 #include "ED_armature.h"
22 
23 #include "BLI_listbase.h"
24 
25 #include "ArmatureExporter.h"
26 #include "GeometryExporter.h"
27 #include "SceneExporter.h"
28 
30  ViewLayer *view_layer,
31  SceneExporter *se,
32  std::vector<Object *> &child_objects)
33 
34 {
35  /* write bone nodes */
36 
37  bArmature *armature = (bArmature *)ob_arm->data;
38  bool is_edited = armature->edbo != nullptr;
39 
40  if (!is_edited) {
41  ED_armature_to_edit(armature);
42  }
43 
44  for (Bone *bone = (Bone *)armature->bonebase.first; bone; bone = bone->next) {
45  add_bone_node(bone, ob_arm, se, child_objects);
46  }
47 
48  if (!is_edited) {
49  ED_armature_edit_free(armature);
50  }
51 }
52 
53 void ArmatureExporter::write_bone_URLs(COLLADASW::InstanceController &ins,
54  Object *ob_arm,
55  Bone *bone)
56 {
57  if (bc_is_root_bone(bone, this->export_settings.get_deform_bones_only())) {
58  std::string joint_id = translate_id(id_name(ob_arm) + "_" + bone->name);
59  ins.addSkeleton(COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, joint_id));
60  }
61  else {
62  for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
63  write_bone_URLs(ins, ob_arm, child);
64  }
65  }
66 }
67 
69 {
70  Object *ob_arm = bc_get_assigned_armature(ob);
71  bArmature *arm = (bArmature *)ob_arm->data;
72 
73  const std::string &controller_id = get_controller_id(ob_arm, ob);
74 
75  COLLADASW::InstanceController ins(mSW);
76  ins.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, controller_id));
77 
78  Mesh *me = (Mesh *)ob->data;
79  if (!me->dvert) {
80  return false;
81  }
82 
83  /* write root bone URLs */
84  Bone *bone;
85  for (bone = (Bone *)arm->bonebase.first; bone; bone = bone->next) {
86  write_bone_URLs(ins, ob_arm, bone);
87  }
88 
90  ins.getBindMaterial(), ob, this->export_settings.get_active_uv_only());
91 
92  ins.add();
93  return true;
94 }
95 
96 #if 0
98 {
99  Object *ob_arm = bc_get_assigned_armature(ob);
100 }
101 
102 bool ArmatureExporter::already_written(Object *ob_arm)
103 {
104  return std::find(written_armatures.begin(), written_armatures.end(), ob_arm) !=
105  written_armatures.end();
106 }
107 
108 void ArmatureExporter::wrote(Object *ob_arm)
109 {
110  written_armatures.push_back(ob_arm);
111 }
112 
113 void ArmatureExporter::find_objects_using_armature(Object *ob_arm,
114  std::vector<Object *> &objects,
115  Scene *sce)
116 {
117  objects.clear();
118 
119  Base *base = (Base *)sce->base.first;
120  while (base) {
121  Object *ob = base->object;
122 
123  if (ob->type == OB_MESH && get_assigned_armature(ob) == ob_arm) {
124  objects.push_back(ob);
125  }
126 
127  base = base->next;
128  }
129 }
130 #endif
131 
132 void ArmatureExporter::add_bone_node(Bone *bone,
133  Object *ob_arm,
134  SceneExporter *se,
135  std::vector<Object *> &child_objects)
136 {
137  if (can_export(bone)) {
138  std::string node_id = translate_id(id_name(ob_arm) + "_" + bone->name);
139  std::string node_name = std::string(bone->name);
140  std::string node_sid = get_joint_sid(bone);
141 
142  COLLADASW::Node node(mSW);
143 
144  node.setType(COLLADASW::Node::JOINT);
145  node.setNodeId(node_id);
146  node.setNodeName(node_name);
147  node.setNodeSid(node_sid);
148 
149  if (this->export_settings.get_use_blender_profile()) {
150  if (!is_export_root(bone)) {
151  if (bone->flag & BONE_CONNECTED) {
152  node.addExtraTechniqueParameter("blender", "connect", true);
153  }
154  }
155  std::string layers = BoneExtended::get_bone_layers(bone->layer);
156  node.addExtraTechniqueParameter("blender", "layer", layers);
157 
158  bArmature *armature = (bArmature *)ob_arm->data;
159  EditBone *ebone = bc_get_edit_bone(armature, bone->name);
160  if (ebone && ebone->roll != 0) {
161  node.addExtraTechniqueParameter("blender", "roll", ebone->roll);
162  }
163  if (bc_is_leaf_bone(bone)) {
164  Vector head, tail;
165  const BCMatrix &global_transform = this->export_settings.get_global_transform();
166  if (this->export_settings.get_apply_global_orientation()) {
167  bc_add_global_transform(head, bone->arm_head, global_transform);
168  bc_add_global_transform(tail, bone->arm_tail, global_transform);
169  }
170  else {
171  copy_v3_v3(head, bone->arm_head);
172  copy_v3_v3(tail, bone->arm_tail);
173  }
174  node.addExtraTechniqueParameter("blender", "tip_x", tail[0] - head[0]);
175  node.addExtraTechniqueParameter("blender", "tip_y", tail[1] - head[1]);
176  node.addExtraTechniqueParameter("blender", "tip_z", tail[2] - head[2]);
177  }
178  }
179 
180  node.start();
181 
182  add_bone_transform(ob_arm, bone, node);
183 
184  /* Write nodes of child-objects, remove written objects from list. */
185  std::vector<Object *>::iterator iter = child_objects.begin();
186 
187  while (iter != child_objects.end()) {
188  Object *ob = *iter;
189  if (ob->partype == PARBONE && STREQ(ob->parsubstr, bone->name)) {
190  float backup_parinv[4][4];
191  copy_m4_m4(backup_parinv, ob->parentinv);
192 
193  /* Crude, temporary change to parentinv
194  * so transform gets exported correctly. */
195 
196  /* Add bone tail- translation... don't know why
197  * bone parenting is against the tail of a bone
198  * and not its head, seems arbitrary. */
199  ob->parentinv[3][1] += bone->length;
200 
201  /* OPEN_SIM_COMPATIBILITY
202  * TODO: when such objects are animated as
203  * single matrix the tweak must be applied
204  * to the result. */
205  if (export_settings.get_open_sim()) {
206  /* Tweak objects parent-inverse to match compatibility. */
207  float temp[4][4];
208 
209  copy_m4_m4(temp, bone->arm_mat);
210  temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
211 
212  mul_m4_m4m4(ob->parentinv, temp, ob->parentinv);
213  }
214 
215  se->writeNode(ob);
216  copy_m4_m4(ob->parentinv, backup_parinv);
217  iter = child_objects.erase(iter);
218  }
219  else {
220  iter++;
221  }
222  }
223 
224  for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
225  add_bone_node(child, ob_arm, se, child_objects);
226  }
227  node.end();
228  }
229  else {
230  for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
231  add_bone_node(child, ob_arm, se, child_objects);
232  }
233  }
234 }
235 
236 bool ArmatureExporter::is_export_root(Bone *bone)
237 {
238  Bone *entry = bone->parent;
239  while (entry) {
240  if (can_export(entry)) {
241  return false;
242  }
243  entry = entry->parent;
244  }
245  return can_export(bone);
246 }
247 
248 void ArmatureExporter::add_bone_transform(Object *ob_arm, Bone *bone, COLLADASW::Node &node)
249 {
250  // bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, bone->name);
251 
252  float mat[4][4];
253  float bone_rest_mat[4][4]; /* derived from bone->arm_mat */
254  float parent_rest_mat[4][4]; /* derived from bone->parent->arm_mat */
255 
256  bool has_restmat = bc_get_property_matrix(bone, "rest_mat", mat);
257 
258  if (!has_restmat) {
259 
260  /* Have no restpose matrix stored, try old style <= Blender 2.78 */
261 
262  bc_create_restpose_mat(this->export_settings, bone, bone_rest_mat, bone->arm_mat, true);
263 
264  if (is_export_root(bone)) {
265  copy_m4_m4(mat, bone_rest_mat);
266  }
267  else {
268  Matrix parent_inverse;
270  this->export_settings, bone->parent, parent_rest_mat, bone->parent->arm_mat, true);
271 
272  invert_m4_m4(parent_inverse, parent_rest_mat);
273  mul_m4_m4m4(mat, parent_inverse, bone_rest_mat);
274  }
275 
276  /* OPEN_SIM_COMPATIBILITY */
277 
278  if (export_settings.get_open_sim()) {
279  /* Remove rotations vs armature from transform
280  * parent_rest_rot * mat * irest_rot */
281  Matrix workmat;
282  copy_m4_m4(workmat, bone_rest_mat);
283 
284  workmat[3][0] = workmat[3][1] = workmat[3][2] = 0.0f;
285  invert_m4(workmat);
286 
287  mul_m4_m4m4(mat, mat, workmat);
288 
289  if (!is_export_root(bone)) {
290  copy_m4_m4(workmat, parent_rest_mat);
291  workmat[3][0] = workmat[3][1] = workmat[3][2] = 0.0f;
292 
293  mul_m4_m4m4(mat, workmat, mat);
294  }
295  }
296  }
297 
298  if (this->export_settings.get_limit_precision()) {
300  }
301 
302  TransformWriter::add_joint_transform(node, mat, nullptr, this->export_settings, has_restmat);
303 }
304 
305 std::string ArmatureExporter::get_controller_id(Object *ob_arm, Object *ob)
306 {
307  return translate_id(id_name(ob_arm)) + "_" + translate_id(id_name(ob)) +
308  SKIN_CONTROLLER_ID_SUFFIX;
309 }
std::string EMPTY_STRING
Blender kernel action and pose functionality.
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 copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:77
MINLINE void copy_v3_v3(float r[3], const float a[3])
#define STREQ(a, b)
@ BONE_CONNECTED
@ OB_MESH
@ PARBONE
void ED_armature_edit_free(struct bArmature *arm)
void ED_armature_to_edit(bArmature *arm)
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition: btTransform.h:90
bool add_instance_controller(Object *ob)
void add_armature_bones(Object *ob_arm, ViewLayer *view_layer, SceneExporter *se, std::vector< Object * > &child_objects)
static void sanitize(Matrix &matrix, int precision)
Definition: BCMath.cpp:139
void add_material_bindings(COLLADASW::BindMaterial &bind_material, Object *ob, bool active_uv_only)
void add_joint_transform(COLLADASW::Node &node, float mat[4][4], float parent_mat[4][4], BCExportSettings &export_settings, bool has_restmat)
std::string get_joint_sid(Bone *bone)
std::string translate_id(const char *idString)
std::string id_name(void *id)
bool bc_is_root_bone(Bone *aBone, bool deform_bones_only)
void bc_add_global_transform(Matrix &to_mat, const Matrix &from_mat, const BCMatrix &global_transform, const bool invert)
EditBone * bc_get_edit_bone(bArmature *armature, char *name)
void bc_create_restpose_mat(BCExportSettings &export_settings, Bone *bone, float to_mat[4][4], float from_mat[4][4], bool use_local_space)
bool bc_get_property_matrix(Bone *bone, std::string key, float mat[4][4])
Object * bc_get_assigned_armature(Object *ob)
bool bc_is_leaf_bone(Bone *bone)
constexpr int LIMITTED_PRECISION
Definition: collada_utils.h:53
OperationNode * node
@ Vector
Vector data type.
struct Base * next
struct Object * object
struct Bone * parent
float arm_head[3]
char name[64]
float arm_tail[3]
float length
float arm_mat[4][4]
struct Bone * next
ListBase childbase
void * first
Definition: DNA_listBase.h:31
struct MDeformVert * dvert
short partype
float parentinv[4][4]
void * data
char parsubstr[64]
ListBase bonebase
ListBase * edbo