View3DShading popover: Naming + alignment
[blender.git] / source / blender / collada / ArmatureExporter.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
19  *                 Nathan Letwory
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/collada/ArmatureExporter.cpp
25  *  \ingroup collada
26  */
27
28
29 #include "COLLADASWBaseInputElement.h"
30 #include "COLLADASWInstanceController.h"
31 #include "COLLADASWPrimitves.h"
32 #include "COLLADASWSource.h"
33
34 #include "DNA_action_types.h"
35 #include "DNA_meshdata_types.h"
36 #include "DNA_modifier_types.h"
37
38 #include "BKE_action.h"
39 #include "BKE_armature.h"
40
41 extern "C" {
42 #include "BKE_main.h"
43 #include "BKE_mesh.h"
44 #include "BKE_global.h"
45 #include "BKE_library.h"
46 }
47
48 #include "ED_armature.h"
49
50 #include "BLI_listbase.h"
51
52 #include "GeometryExporter.h"
53 #include "ArmatureExporter.h"
54 #include "SceneExporter.h"
55
56 #include "collada_utils.h"
57
58 // XXX exporter writes wrong data for shared armatures.  A separate
59 // controller should be written for each armature-mesh binding how do
60 // we make controller ids then?
61 ArmatureExporter::ArmatureExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryControllers(sw), export_settings(export_settings) {
62 }
63
64 // write bone nodes
65 void ArmatureExporter::add_armature_bones(Depsgraph *depsgraph, Object *ob_arm,
66                                           Scene *sce, SceneExporter *se,
67                                           std::list<Object *>& child_objects)
68 {
69         // write bone nodes
70
71         bArmature * armature = (bArmature *)ob_arm->data;
72         bool is_edited = armature->edbo != NULL;
73
74         if (!is_edited)
75                 ED_armature_to_edit(armature);
76
77         for (Bone *bone = (Bone *)armature->bonebase.first; bone; bone = bone->next) {
78                 // start from root bones
79                 if (!bone->parent)
80                         add_bone_node(depsgraph, bone, ob_arm, sce, se, child_objects);
81         }
82
83         if (!is_edited) {
84                 ED_armature_from_edit(armature);
85                 ED_armature_edit_free(armature);
86         }
87 }
88
89 void ArmatureExporter::write_bone_URLs(COLLADASW::InstanceController &ins, Object *ob_arm, Bone *bone)
90 {
91         if (bc_is_root_bone(bone, this->export_settings->deform_bones_only))
92                 ins.addSkeleton(COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_joint_id(ob_arm, bone)));
93         else {
94                 for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
95                         write_bone_URLs(ins, ob_arm, child);
96                 }
97         }
98 }
99
100 bool ArmatureExporter::add_instance_controller(Object *ob)
101 {
102         Object *ob_arm = bc_get_assigned_armature(ob);
103         bArmature *arm = (bArmature *)ob_arm->data;
104
105         const std::string& controller_id = get_controller_id(ob_arm, ob);
106
107         COLLADASW::InstanceController ins(mSW);
108         ins.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, controller_id));
109
110         Mesh *me = (Mesh *)ob->data;
111         if (!me->dvert) return false;
112
113         // write root bone URLs
114         Bone *bone;
115         for (bone = (Bone *)arm->bonebase.first; bone; bone = bone->next) {
116                 write_bone_URLs(ins, ob_arm, bone);
117         }
118
119         InstanceWriter::add_material_bindings(ins.getBindMaterial(), ob, this->export_settings->active_uv_only);
120                 
121         ins.add();
122         return true;
123 }
124
125 #if 0
126 void ArmatureExporter::operator()(Object *ob)
127 {
128         Object *ob_arm = bc_get_assigned_armature(ob);
129
130 }
131
132 bool ArmatureExporter::already_written(Object *ob_arm)
133 {
134         return std::find(written_armatures.begin(), written_armatures.end(), ob_arm) != written_armatures.end();
135 }
136
137 void ArmatureExporter::wrote(Object *ob_arm)
138 {
139         written_armatures.push_back(ob_arm);
140 }
141
142 void ArmatureExporter::find_objects_using_armature(Object *ob_arm, std::vector<Object *>& objects, Scene *sce)
143 {
144         objects.clear();
145
146         Base *base = (Base *) sce->base.first;
147         while (base) {
148                 Object *ob = base->object;
149                 
150                 if (ob->type == OB_MESH && get_assigned_armature(ob) == ob_arm) {
151                         objects.push_back(ob);
152                 }
153
154                 base = base->next;
155         }
156 }
157 #endif
158
159 // parent_mat is armature-space
160 void ArmatureExporter::add_bone_node(Depsgraph *depsgraph, Bone *bone, Object *ob_arm, Scene *sce,
161                                      SceneExporter *se,
162                                      std::list<Object *>& child_objects)
163 {
164         if (!(this->export_settings->deform_bones_only && bone->flag & BONE_NO_DEFORM)) {
165                 std::string node_id = get_joint_id(ob_arm, bone);
166                 std::string node_name = std::string(bone->name);
167                 std::string node_sid = get_joint_sid(bone);
168
169                 COLLADASW::Node node(mSW);
170
171                 node.setType(COLLADASW::Node::JOINT);
172                 node.setNodeId(node_id);
173                 node.setNodeName(node_name);
174                 node.setNodeSid(node_sid);
175
176                 if (this->export_settings->use_blender_profile)
177                 {
178                         if (bone->parent) {
179                                 if (bone->flag & BONE_CONNECTED) {
180                                         node.addExtraTechniqueParameter("blender", "connect", true);
181                                 }
182                         }
183                         std::string layers = BoneExtended::get_bone_layers(bone->layer);
184                         node.addExtraTechniqueParameter("blender", "layer", layers);
185
186                         bArmature *armature = (bArmature *)ob_arm->data;
187                         EditBone *ebone = bc_get_edit_bone(armature, bone->name);
188                         if (ebone && ebone->roll != 0)
189                         {
190                                 node.addExtraTechniqueParameter("blender", "roll", ebone->roll);
191                         }
192                         if (bc_is_leaf_bone(bone))
193                         {
194                                 node.addExtraTechniqueParameter("blender", "tip_x", bone->arm_tail[0] - bone->arm_head[0]);
195                                 node.addExtraTechniqueParameter("blender", "tip_y", bone->arm_tail[1] - bone->arm_head[1]);
196                                 node.addExtraTechniqueParameter("blender", "tip_z", bone->arm_tail[2] - bone->arm_head[2]);
197                         }
198                 }
199
200                         node.start();
201
202                         add_bone_transform(ob_arm, bone, node);
203
204                         // Write nodes of childobjects, remove written objects from list
205                         std::list<Object *>::iterator i = child_objects.begin();
206
207                         while (i != child_objects.end()) {
208                                 if ((*i)->partype == PARBONE && STREQ((*i)->parsubstr, bone->name)) {
209                                         float backup_parinv[4][4];
210                                         copy_m4_m4(backup_parinv, (*i)->parentinv);
211
212                                         // crude, temporary change to parentinv
213                                         // so transform gets exported correctly.
214
215                                         // Add bone tail- translation... don't know why
216                                         // bone parenting is against the tail of a bone
217                                         // and not it's head, seems arbitrary.
218                                         (*i)->parentinv[3][1] += bone->length;
219
220                                         // OPEN_SIM_COMPATIBILITY
221                                         // TODO: when such objects are animated as
222                                         // single matrix the tweak must be applied
223                                         // to the result.
224                                         if (export_settings->open_sim) {
225                                                 // tweak objects parentinverse to match compatibility
226                                                 float temp[4][4];
227
228                                                 copy_m4_m4(temp, bone->arm_mat);
229                                                 temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
230
231                                                 mul_m4_m4m4((*i)->parentinv, temp, (*i)->parentinv);
232                                         }
233
234                                         se->writeNodes(depsgraph, *i, sce);
235
236                                         copy_m4_m4((*i)->parentinv, backup_parinv);
237                                         child_objects.erase(i++);
238                                 }
239                                 else i++;
240                         }
241
242                         for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
243                                 add_bone_node(depsgraph, child, ob_arm, sce, se, child_objects);
244                         }
245                         node.end();
246                 }
247                 else {
248                         for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
249                                 add_bone_node(depsgraph, child, ob_arm, sce, se, child_objects);
250                         }
251                 }
252 }
253
254 void ArmatureExporter::add_bone_transform(Object *ob_arm, Bone *bone, COLLADASW::Node& node)
255 {
256         //bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, bone->name);
257
258         float mat[4][4];
259         float bone_rest_mat[4][4]; /* derived from bone->arm_mat */
260         float parent_rest_mat[4][4]; /* derived from bone->parent->arm_mat */
261
262         bool has_restmat = bc_get_property_matrix(bone, "rest_mat", mat);
263
264         if (!has_restmat) {
265
266                 /* Have no restpose matrix stored, try old style <= Blender 2.78 */
267                 
268                 bc_create_restpose_mat(this->export_settings, bone, bone_rest_mat, bone->arm_mat, true);
269
270                 if (bone->parent) {
271                         // get bone-space matrix from parent pose
272                         /*bPoseChannel *parchan = BKE_pose_channel_find_name(ob_arm->pose, bone->parent->name);
273                         float invpar[4][4];
274                         invert_m4_m4(invpar, parchan->pose_mat);
275                         mul_m4_m4m4(mat, invpar, pchan->pose_mat);*/
276                         float invpar[4][4];
277                         bc_create_restpose_mat(this->export_settings, bone->parent, parent_rest_mat, bone->parent->arm_mat, true);
278
279                         invert_m4_m4(invpar, parent_rest_mat);
280                         mul_m4_m4m4(mat, invpar, bone_rest_mat);
281
282                 }
283                 else {
284                         copy_m4_m4(mat, bone_rest_mat);
285                 }
286
287                 // OPEN_SIM_COMPATIBILITY
288                 if (export_settings->open_sim) {
289                         // Remove rotations vs armature from transform
290                         // parent_rest_rot * mat * irest_rot
291                         float temp[4][4];
292                         copy_m4_m4(temp, bone_rest_mat);
293                         temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
294                         invert_m4(temp);
295
296                         mul_m4_m4m4(mat, mat, temp);
297
298                         if (bone->parent) {
299                                 copy_m4_m4(temp, parent_rest_mat);
300                                 temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
301
302                                 mul_m4_m4m4(mat, temp, mat);
303                         }
304                 }
305         }
306
307         if (this->export_settings->limit_precision)
308                 bc_sanitize_mat(mat, 6);
309
310         TransformWriter::add_node_transform(node, mat, NULL);
311
312 }
313
314 std::string ArmatureExporter::get_controller_id(Object *ob_arm, Object *ob)
315 {
316         return translate_id(id_name(ob_arm)) + "_" + translate_id(id_name(ob)) + SKIN_CONTROLLER_ID_SUFFIX;
317 }