Merge branch 'master' into blender2.8
[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(bContext *C, Depsgraph *depsgraph, Object *ob_arm,
66                                           Scene *sce, SceneExporter *se,
67                                           std::list<Object *>& child_objects)
68 {
69         Main *bmain = CTX_data_main(C);
70         // write bone nodes
71
72         bArmature * armature = (bArmature *)ob_arm->data;
73         bool is_edited = armature->edbo != NULL;
74
75         if (!is_edited)
76                 ED_armature_to_edit(armature);
77
78         for (Bone *bone = (Bone *)armature->bonebase.first; bone; bone = bone->next) {
79                 // start from root bones
80                 if (!bone->parent)
81                         add_bone_node(C, depsgraph, bone, ob_arm, sce, se, child_objects);
82         }
83
84         if (!is_edited) {
85                 ED_armature_from_edit(bmain, armature);
86                 ED_armature_edit_free(armature);
87         }
88 }
89
90 void ArmatureExporter::write_bone_URLs(COLLADASW::InstanceController &ins, Object *ob_arm, Bone *bone)
91 {
92         if (bc_is_root_bone(bone, this->export_settings->deform_bones_only))
93                 ins.addSkeleton(COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_joint_id(ob_arm, bone)));
94         else {
95                 for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
96                         write_bone_URLs(ins, ob_arm, child);
97                 }
98         }
99 }
100
101 bool ArmatureExporter::add_instance_controller(Object *ob)
102 {
103         Object *ob_arm = bc_get_assigned_armature(ob);
104         bArmature *arm = (bArmature *)ob_arm->data;
105
106         const std::string& controller_id = get_controller_id(ob_arm, ob);
107
108         COLLADASW::InstanceController ins(mSW);
109         ins.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, controller_id));
110
111         Mesh *me = (Mesh *)ob->data;
112         if (!me->dvert) return false;
113
114         // write root bone URLs
115         Bone *bone;
116         for (bone = (Bone *)arm->bonebase.first; bone; bone = bone->next) {
117                 write_bone_URLs(ins, ob_arm, bone);
118         }
119
120         InstanceWriter::add_material_bindings(ins.getBindMaterial(), ob, this->export_settings->active_uv_only);
121                 
122         ins.add();
123         return true;
124 }
125
126 #if 0
127 void ArmatureExporter::operator()(Object *ob)
128 {
129         Object *ob_arm = bc_get_assigned_armature(ob);
130
131 }
132
133 bool ArmatureExporter::already_written(Object *ob_arm)
134 {
135         return std::find(written_armatures.begin(), written_armatures.end(), ob_arm) != written_armatures.end();
136 }
137
138 void ArmatureExporter::wrote(Object *ob_arm)
139 {
140         written_armatures.push_back(ob_arm);
141 }
142
143 void ArmatureExporter::find_objects_using_armature(Object *ob_arm, std::vector<Object *>& objects, Scene *sce)
144 {
145         objects.clear();
146
147         Base *base = (Base *) sce->base.first;
148         while (base) {
149                 Object *ob = base->object;
150                 
151                 if (ob->type == OB_MESH && get_assigned_armature(ob) == ob_arm) {
152                         objects.push_back(ob);
153                 }
154
155                 base = base->next;
156         }
157 }
158 #endif
159
160 // parent_mat is armature-space
161 void ArmatureExporter::add_bone_node(bContext *C, Depsgraph *depsgraph, Bone *bone, Object *ob_arm, Scene *sce,
162                                      SceneExporter *se,
163                                      std::list<Object *>& child_objects)
164 {
165         if (!(this->export_settings->deform_bones_only && bone->flag & BONE_NO_DEFORM)) {
166                 std::string node_id = get_joint_id(ob_arm, bone);
167                 std::string node_name = std::string(bone->name);
168                 std::string node_sid = get_joint_sid(bone);
169
170                 COLLADASW::Node node(mSW);
171
172                 node.setType(COLLADASW::Node::JOINT);
173                 node.setNodeId(node_id);
174                 node.setNodeName(node_name);
175                 node.setNodeSid(node_sid);
176
177                 if (this->export_settings->use_blender_profile)
178                 {
179                         if (bone->parent) {
180                                 if (bone->flag & BONE_CONNECTED) {
181                                         node.addExtraTechniqueParameter("blender", "connect", true);
182                                 }
183                         }
184                         std::string layers = BoneExtended::get_bone_layers(bone->layer);
185                         node.addExtraTechniqueParameter("blender", "layer", layers);
186
187                         bArmature *armature = (bArmature *)ob_arm->data;
188                         EditBone *ebone = bc_get_edit_bone(armature, bone->name);
189                         if (ebone && ebone->roll != 0)
190                         {
191                                 node.addExtraTechniqueParameter("blender", "roll", ebone->roll);
192                         }
193                         if (bc_is_leaf_bone(bone))
194                         {
195                                 node.addExtraTechniqueParameter("blender", "tip_x", bone->arm_tail[0] - bone->arm_head[0]);
196                                 node.addExtraTechniqueParameter("blender", "tip_y", bone->arm_tail[1] - bone->arm_head[1]);
197                                 node.addExtraTechniqueParameter("blender", "tip_z", bone->arm_tail[2] - bone->arm_head[2]);
198                         }
199                 }
200
201                         node.start();
202
203                         add_bone_transform(ob_arm, bone, node);
204
205                         // Write nodes of childobjects, remove written objects from list
206                         std::list<Object *>::iterator i = child_objects.begin();
207
208                         while (i != child_objects.end()) {
209                                 if ((*i)->partype == PARBONE && STREQ((*i)->parsubstr, bone->name)) {
210                                         float backup_parinv[4][4];
211                                         copy_m4_m4(backup_parinv, (*i)->parentinv);
212
213                                         // crude, temporary change to parentinv
214                                         // so transform gets exported correctly.
215
216                                         // Add bone tail- translation... don't know why
217                                         // bone parenting is against the tail of a bone
218                                         // and not it's head, seems arbitrary.
219                                         (*i)->parentinv[3][1] += bone->length;
220
221                                         // OPEN_SIM_COMPATIBILITY
222                                         // TODO: when such objects are animated as
223                                         // single matrix the tweak must be applied
224                                         // to the result.
225                                         if (export_settings->open_sim) {
226                                                 // tweak objects parentinverse to match compatibility
227                                                 float temp[4][4];
228
229                                                 copy_m4_m4(temp, bone->arm_mat);
230                                                 temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
231
232                                                 mul_m4_m4m4((*i)->parentinv, temp, (*i)->parentinv);
233                                         }
234
235                                         se->writeNodes(C, depsgraph, *i, sce);
236
237                                         copy_m4_m4((*i)->parentinv, backup_parinv);
238                                         child_objects.erase(i++);
239                                 }
240                                 else i++;
241                         }
242
243                         for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
244                                 add_bone_node(C, depsgraph, child, ob_arm, sce, se, child_objects);
245                         }
246                         node.end();
247                 }
248                 else {
249                         for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
250                                 add_bone_node(C, depsgraph, child, ob_arm, sce, se, child_objects);
251                         }
252                 }
253 }
254
255 void ArmatureExporter::add_bone_transform(Object *ob_arm, Bone *bone, COLLADASW::Node& node)
256 {
257         //bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, bone->name);
258
259         float mat[4][4];
260         float bone_rest_mat[4][4]; /* derived from bone->arm_mat */
261         float parent_rest_mat[4][4]; /* derived from bone->parent->arm_mat */
262
263         bool has_restmat = bc_get_property_matrix(bone, "rest_mat", mat);
264
265         if (!has_restmat) {
266
267                 /* Have no restpose matrix stored, try old style <= Blender 2.78 */
268                 
269                 bc_create_restpose_mat(this->export_settings, bone, bone_rest_mat, bone->arm_mat, true);
270
271                 if (bone->parent) {
272                         // get bone-space matrix from parent pose
273                         /*bPoseChannel *parchan = BKE_pose_channel_find_name(ob_arm->pose, bone->parent->name);
274                         float invpar[4][4];
275                         invert_m4_m4(invpar, parchan->pose_mat);
276                         mul_m4_m4m4(mat, invpar, pchan->pose_mat);*/
277                         float invpar[4][4];
278                         bc_create_restpose_mat(this->export_settings, bone->parent, parent_rest_mat, bone->parent->arm_mat, true);
279
280                         invert_m4_m4(invpar, parent_rest_mat);
281                         mul_m4_m4m4(mat, invpar, bone_rest_mat);
282
283                 }
284                 else {
285                         copy_m4_m4(mat, bone_rest_mat);
286                 }
287
288                 // OPEN_SIM_COMPATIBILITY
289                 if (export_settings->open_sim) {
290                         // Remove rotations vs armature from transform
291                         // parent_rest_rot * mat * irest_rot
292                         float temp[4][4];
293                         copy_m4_m4(temp, bone_rest_mat);
294                         temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
295                         invert_m4(temp);
296
297                         mul_m4_m4m4(mat, mat, temp);
298
299                         if (bone->parent) {
300                                 copy_m4_m4(temp, parent_rest_mat);
301                                 temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
302
303                                 mul_m4_m4m4(mat, temp, mat);
304                         }
305                 }
306         }
307
308         if (this->export_settings->limit_precision)
309                 bc_sanitize_mat(mat, 6);
310
311         TransformWriter::add_node_transform(node, mat, NULL);
312
313 }
314
315 std::string ArmatureExporter::get_controller_id(Object *ob_arm, Object *ob)
316 {
317         return translate_id(id_name(ob_arm)) + "_" + translate_id(id_name(ob)) + SKIN_CONTROLLER_ID_SUFFIX;
318 }