Merge branch 'master' into blender2.8
[blender.git] / source / blender / collada / ControllerExporter.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, Sukhitha Jayathilake
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/collada/ControllerExporter.cpp
25  *  \ingroup collada
26  */
27
28 #include "COLLADASWBaseInputElement.h"
29 #include "COLLADASWInstanceController.h"
30 #include "COLLADASWPrimitves.h"
31 #include "COLLADASWSource.h"
32
33 #include "DNA_action_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_modifier_types.h"
36
37 #include "BKE_action.h"
38 #include "BKE_armature.h"
39
40 extern "C" {
41 #include "BKE_main.h"
42 #include "BKE_mesh.h"
43 #include "BKE_global.h"
44 #include "BKE_library.h"
45 #include "BKE_idprop.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 "ControllerExporter.h"
55 #include "SceneExporter.h"
56
57 #include "collada_utils.h"
58
59 // XXX exporter writes wrong data for shared armatures.  A separate
60 // controller should be written for each armature-mesh binding how do
61 // we make controller ids then?
62 ControllerExporter::ControllerExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryControllers(sw), export_settings(export_settings) {
63 }
64
65 bool ControllerExporter::is_skinned_mesh(Object *ob)
66 {
67         return bc_get_assigned_armature(ob) != NULL;
68 }
69
70
71 void ControllerExporter::write_bone_URLs(COLLADASW::InstanceController &ins, Object *ob_arm, Bone *bone)
72 {
73         if (bc_is_root_bone(bone, this->export_settings->deform_bones_only))
74                 ins.addSkeleton(COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_joint_id(ob_arm, bone)));
75         else {
76                 for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
77                         write_bone_URLs(ins, ob_arm, child);
78                 }
79         }
80 }
81
82 bool ControllerExporter::add_instance_controller(Object *ob)
83 {
84         Object *ob_arm = bc_get_assigned_armature(ob);
85         bArmature *arm = (bArmature *)ob_arm->data;
86
87         const std::string& controller_id = get_controller_id(ob_arm, ob);
88
89         COLLADASW::InstanceController ins(mSW);
90         ins.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, controller_id));
91
92         Mesh *me = (Mesh *)ob->data;
93         if (!me->dvert) return false;
94
95         // write root bone URLs
96         Bone *bone;
97         for (bone = (Bone *)arm->bonebase.first; bone; bone = bone->next) {
98                 write_bone_URLs(ins, ob_arm, bone);
99         }
100
101         InstanceWriter::add_material_bindings(ins.getBindMaterial(), ob, this->export_settings->active_uv_only);
102
103         ins.add();
104         return true;
105 }
106
107 void ControllerExporter::export_controllers(Main *bmain, Depsgraph *depsgraph, Scene *sce)
108 {
109         this->depsgraph = depsgraph;
110         m_bmain = bmain;
111         scene = sce;
112
113         openLibrary();
114
115         GeometryFunctor gf;
116         gf.forEachMeshObjectInExportSet<ControllerExporter>(sce, *this, this->export_settings->export_set);
117
118         closeLibrary();
119 }
120
121 void ControllerExporter::operator()(Object *ob)
122 {
123         Object *ob_arm = bc_get_assigned_armature(ob);
124         Key *key = BKE_key_from_object(ob);
125
126         if (ob_arm) {
127                 export_skin_controller(ob, ob_arm);
128         }
129         if (key && this->export_settings->include_shapekeys) {
130                 export_morph_controller(ob, key);
131         }
132 }
133 #if 0
134
135 bool ArmatureExporter::already_written(Object *ob_arm)
136 {
137         return std::find(written_armatures.begin(), written_armatures.end(), ob_arm) != written_armatures.end();
138 }
139
140 void ArmatureExporter::wrote(Object *ob_arm)
141 {
142         written_armatures.push_back(ob_arm);
143 }
144
145 void ArmatureExporter::find_objects_using_armature(Object *ob_arm, std::vector<Object *>& objects, Scene *sce)
146 {
147         objects.clear();
148
149         Base *base = (Base *) sce->base.first;
150         while (base) {
151                 Object *ob = base->object;
152
153                 if (ob->type == OB_MESH && get_assigned_armature(ob) == ob_arm) {
154                         objects.push_back(ob);
155                 }
156
157                 base = base->next;
158         }
159 }
160 #endif
161
162 std::string ControllerExporter::get_controller_id(Object *ob_arm, Object *ob)
163 {
164         return translate_id(id_name(ob_arm)) + "_" + translate_id(id_name(ob)) + SKIN_CONTROLLER_ID_SUFFIX;
165 }
166
167 std::string ControllerExporter::get_controller_id(Key *key, Object *ob)
168 {
169         return translate_id(id_name(ob)) + MORPH_CONTROLLER_ID_SUFFIX;
170 }
171
172 // ob should be of type OB_MESH
173 // both args are required
174 void ControllerExporter::export_skin_controller(Object *ob, Object *ob_arm)
175 {
176         // joint names
177         // joint inverse bind matrices
178         // vertex weights
179
180         // input:
181         // joint names: ob -> vertex group names
182         // vertex group weights: me->dvert -> groups -> index, weight
183
184 #if 0
185         me->dvert :
186
187         typedef struct MDeformVert {
188                 struct MDeformWeight *dw;
189                 int totweight;
190                 int flag;   // flag only in use for weightpaint now
191         } MDeformVert;
192
193         typedef struct MDeformWeight {
194                 int def_nr;
195                 float weight;
196         } MDeformWeight;
197 #endif
198
199         bool use_instantiation = this->export_settings->use_object_instantiation;
200         Mesh *me;
201
202         me = bc_get_mesh_copy(
203                                 m_bmain,
204                                 depsgraph,
205                                 scene,
206                                 ob,
207                                 this->export_settings->export_mesh_type,
208                                 this->export_settings->apply_modifiers,
209                                 this->export_settings->triangulate);
210
211         if (!me->dvert) return;
212
213         std::string controller_name = id_name(ob_arm);
214         std::string controller_id = get_controller_id(ob_arm, ob);
215
216         openSkin(controller_id, controller_name,
217                  COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob, use_instantiation)));
218
219         add_bind_shape_mat(ob);
220
221         std::string joints_source_id = add_joints_source(ob_arm, &ob->defbase, controller_id);
222         std::string inv_bind_mat_source_id = add_inv_bind_mats_source(ob_arm, &ob->defbase, controller_id);
223
224         std::list<int> vcounts;
225         std::list<int> joints;
226         std::list<float> weights;
227
228         {
229                 int i, j;
230
231                 // def group index -> joint index
232                 std::vector<int> joint_index_by_def_index;
233                 bDeformGroup *def;
234
235                 for (def = (bDeformGroup *)ob->defbase.first, i = 0, j = 0; def; def = def->next, i++) {
236                         if (is_bone_defgroup(ob_arm, def))
237                                 joint_index_by_def_index.push_back(j++);
238                         else
239                                 joint_index_by_def_index.push_back(-1);
240                 }
241
242                 int oob_counter = 0;
243                 for (i = 0; i < me->totvert; i++) {
244                         MDeformVert *vert = &me->dvert[i];
245                         std::map<int, float> jw;
246
247                         // We're normalizing the weights later
248                         float sumw = 0.0f;
249
250                         for (j = 0; j < vert->totweight; j++) {
251                                 int idx = vert->dw[j].def_nr;
252                                 if (idx >= joint_index_by_def_index.size()) {
253                                         // XXX: Maybe better find out where and
254                                         //      why the Out Of Bound indexes get created ?
255                                         oob_counter += 1;
256                                 }
257                                 else {
258                                         if (idx >= 0) {
259                                                 int joint_index = joint_index_by_def_index[idx];
260                                                 if (joint_index != -1 && vert->dw[j].weight > 0.0f) {
261                                                         jw[joint_index] += vert->dw[j].weight;
262                                                         sumw += vert->dw[j].weight;
263                                                 }
264                                         }
265                                 }
266                         }
267
268                         if (sumw > 0.0f) {
269                                 float invsumw = 1.0f / sumw;
270                                 vcounts.push_back(jw.size());
271                                 for (std::map<int, float>::iterator m = jw.begin(); m != jw.end(); ++m) {
272                                         joints.push_back((*m).first);
273                                         weights.push_back(invsumw * (*m).second);
274                                 }
275                         }
276                         else {
277                                 vcounts.push_back(0);
278 #if 0
279                                 vcounts.push_back(1);
280                                 joints.push_back(-1);
281                                 weights.push_back(1.0f);
282 #endif
283                         }
284                 }
285
286                 if (oob_counter > 0) {
287                         fprintf(stderr, "Ignored %d Vertex weights which use index to non existing VGroup %zu.\n",
288                                 oob_counter, joint_index_by_def_index.size());
289                 }
290         }
291
292         std::string weights_source_id = add_weights_source(me, controller_id, weights);
293         add_joints_element(&ob->defbase, joints_source_id, inv_bind_mat_source_id);
294         add_vertex_weights_element(weights_source_id, joints_source_id, vcounts, joints);
295
296         BKE_libblock_free_us(m_bmain, me);
297
298         closeSkin();
299         closeController();
300 }
301
302 void ControllerExporter::export_morph_controller(Object *ob, Key *key)
303 {
304         bool use_instantiation = this->export_settings->use_object_instantiation;
305         Mesh *me;
306
307         me = bc_get_mesh_copy(
308                                 m_bmain,
309                                 depsgraph,
310                                 scene,
311                                 ob,
312                                 this->export_settings->export_mesh_type,
313                                 this->export_settings->apply_modifiers,
314                                 this->export_settings->triangulate);
315
316         std::string controller_name = id_name(ob) + "-morph";
317         std::string controller_id = get_controller_id(key, ob);
318
319         openMorph(controller_id, controller_name,
320                  COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob, use_instantiation)));
321
322         std::string targets_id = add_morph_targets(key, ob);
323         std::string morph_weights_id = add_morph_weights(key, ob);
324
325         COLLADASW::TargetsElement targets(mSW);
326
327         COLLADASW::InputList &input = targets.getInputList();
328
329         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::MORPH_TARGET, // constant declared in COLLADASWInputList.h
330                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, targets_id)));
331         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::MORPH_WEIGHT,
332                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, morph_weights_id)));
333         targets.add();
334
335         BKE_libblock_free_us(m_bmain, me);
336
337
338         //support for animations
339         //can also try the base element and param alternative
340         add_weight_extras(key);
341         closeMorph();
342         closeController();
343 }
344
345 std::string ControllerExporter::add_morph_targets(Key *key, Object *ob)
346 {
347         std::string source_id = translate_id(id_name(ob)) + TARGETS_SOURCE_ID_SUFFIX;
348
349         COLLADASW::IdRefSource source(mSW);
350         source.setId(source_id);
351         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
352         source.setAccessorCount(key->totkey - 1);
353         source.setAccessorStride(1);
354
355         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
356         param.push_back("IDREF");
357
358         source.prepareToAppendValues();
359
360         KeyBlock *kb = (KeyBlock *)key->block.first;
361         //skip the basis
362         kb = kb->next;
363         for (; kb; kb = kb->next) {
364                 std::string geom_id = get_geometry_id(ob, false) + "_morph_" + translate_id(kb->name);
365                 source.appendValues(geom_id);
366
367         }
368
369         source.finish();
370
371         return source_id;
372 }
373
374 std::string ControllerExporter::add_morph_weights(Key *key, Object *ob)
375 {
376         std::string source_id = translate_id(id_name(ob)) + WEIGHTS_SOURCE_ID_SUFFIX;
377
378         COLLADASW::FloatSourceF source(mSW);
379         source.setId(source_id);
380         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
381         source.setAccessorCount(key->totkey - 1);
382         source.setAccessorStride(1);
383
384         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
385         param.push_back("MORPH_WEIGHT");
386
387         source.prepareToAppendValues();
388
389         KeyBlock *kb = (KeyBlock *)key->block.first;
390         //skip the basis
391         kb = kb->next;
392         for (; kb; kb = kb->next) {
393                 float weight = kb->curval;
394                 source.appendValues(weight);
395         }
396         source.finish();
397
398         return source_id;
399 }
400
401 //Added to implemente support for animations.
402 void ControllerExporter::add_weight_extras(Key *key)
403 {
404         // can also try the base element and param alternative
405         COLLADASW::BaseExtraTechnique extra;
406
407         KeyBlock * kb = (KeyBlock *)key->block.first;
408         //skip the basis
409         kb = kb->next;
410         for (; kb; kb = kb->next) {
411                 // XXX why is the weight not used here and set to 0.0?
412                 // float weight = kb->curval;
413                 extra.addExtraTechniqueParameter ("KHR", "morph_weights" , 0.000, "MORPH_WEIGHT_TO_TARGET");
414         }
415 }
416
417
418
419 void ControllerExporter::add_joints_element(ListBase *defbase,
420                                           const std::string& joints_source_id, const std::string& inv_bind_mat_source_id)
421 {
422         COLLADASW::JointsElement joints(mSW);
423         COLLADASW::InputList &input = joints.getInputList();
424
425         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::JOINT, // constant declared in COLLADASWInputList.h
426                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, joints_source_id)));
427         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::BINDMATRIX,
428                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, inv_bind_mat_source_id)));
429         joints.add();
430 }
431
432 void ControllerExporter::add_bind_shape_mat(Object *ob)
433 {
434         double bind_mat[4][4];
435
436         converter.mat4_to_dae_double(bind_mat, ob->obmat);
437
438         addBindShapeTransform(bind_mat);
439 }
440
441 std::string ControllerExporter::add_joints_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
442 {
443         std::string source_id = controller_id + JOINTS_SOURCE_ID_SUFFIX;
444
445         int totjoint = 0;
446         bDeformGroup *def;
447         for (def = (bDeformGroup *)defbase->first; def; def = def->next) {
448                 if (is_bone_defgroup(ob_arm, def))
449                         totjoint++;
450         }
451
452         COLLADASW::NameSource source(mSW);
453         source.setId(source_id);
454         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
455         source.setAccessorCount(totjoint);
456         source.setAccessorStride(1);
457
458         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
459         param.push_back("JOINT");
460
461         source.prepareToAppendValues();
462
463         for (def = (bDeformGroup *)defbase->first; def; def = def->next) {
464                 Bone *bone = get_bone_from_defgroup(ob_arm, def);
465                 if (bone)
466                         source.appendValues(get_joint_sid(bone));
467         }
468
469         source.finish();
470
471         return source_id;
472 }
473
474
475 std::string ControllerExporter::add_inv_bind_mats_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
476 {
477         std::string source_id = controller_id + BIND_POSES_SOURCE_ID_SUFFIX;
478
479         int totjoint = 0;
480         for (bDeformGroup *def = (bDeformGroup *)defbase->first; def; def = def->next) {
481                 if (is_bone_defgroup(ob_arm, def))
482                         totjoint++;
483         }
484
485         COLLADASW::FloatSourceF source(mSW);
486         source.setId(source_id);
487         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
488         source.setAccessorCount(totjoint); //BLI_listbase_count(defbase));
489         source.setAccessorStride(16);
490
491         source.setParameterTypeName(&COLLADASW::CSWC::CSW_VALUE_TYPE_FLOAT4x4);
492         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
493         param.push_back("TRANSFORM");
494
495         source.prepareToAppendValues();
496
497         bPose *pose = ob_arm->pose;
498         bArmature *arm = (bArmature *)ob_arm->data;
499
500         int flag = arm->flag;
501
502         // put armature in rest position
503         if (!(arm->flag & ARM_RESTPOS)) {
504                 arm->flag |= ARM_RESTPOS;
505                 BKE_pose_where_is(depsgraph, scene, ob_arm);
506         }
507
508         for (bDeformGroup *def = (bDeformGroup *)defbase->first; def; def = def->next) {
509                 if (is_bone_defgroup(ob_arm, def)) {
510                         bPoseChannel *pchan = BKE_pose_channel_find_name(pose, def->name);
511
512                         float mat[4][4];
513                         float world[4][4];
514                         float inv_bind_mat[4][4];
515
516                         float bind_mat[4][4]; /* derived from bone->arm_mat */
517
518                         bool has_bindmat = bc_get_property_matrix(pchan->bone, "bind_mat", bind_mat);
519
520                         if (!has_bindmat) {
521
522                                 /* Have no bind matrix stored, try old style <= Blender 2.78 */
523
524                                 bc_create_restpose_mat(this->export_settings, pchan->bone, bind_mat, pchan->bone->arm_mat, true);
525
526                                 // SL/OPEN_SIM COMPATIBILITY
527                                 if (export_settings->open_sim) {
528
529                                         float loc[3];
530                                         float rot[3] = { 0, 0, 0 };
531                                         float scale[3];
532                                         bc_decompose(bind_mat, loc, NULL, NULL, scale);
533
534                                         // Only translations, no rotation vs armature
535                                         loc_eulO_size_to_mat4(bind_mat, loc, rot, scale, 6);
536                                 }
537                         }
538
539                         // make world-space matrix (bind_mat is armature-space)
540                         mul_m4_m4m4(world, ob_arm->obmat, bind_mat);
541
542                         invert_m4_m4(mat, world);
543                         converter.mat4_to_dae(inv_bind_mat, mat);
544                         if (this->export_settings->limit_precision)
545                                 bc_sanitize_mat(inv_bind_mat, 6);
546                         source.appendValues(inv_bind_mat);
547                 }
548         }
549
550         // back from rest positon
551         if (!(flag & ARM_RESTPOS)) {
552                 arm->flag = flag;
553                 BKE_pose_where_is(depsgraph, scene, ob_arm);
554         }
555
556         source.finish();
557
558         return source_id;
559 }
560
561 Bone *ControllerExporter::get_bone_from_defgroup(Object *ob_arm, bDeformGroup *def)
562 {
563         bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, def->name);
564         return pchan ? pchan->bone : NULL;
565 }
566
567 bool ControllerExporter::is_bone_defgroup(Object *ob_arm, bDeformGroup *def)
568 {
569         return get_bone_from_defgroup(ob_arm, def) != NULL;
570 }
571
572 std::string ControllerExporter::add_weights_source(Mesh *me, const std::string& controller_id, const std::list<float>& weights)
573 {
574         std::string source_id = controller_id + WEIGHTS_SOURCE_ID_SUFFIX;
575
576         COLLADASW::FloatSourceF source(mSW);
577         source.setId(source_id);
578         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
579         source.setAccessorCount(weights.size());
580         source.setAccessorStride(1);
581
582         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
583         param.push_back("WEIGHT");
584
585         source.prepareToAppendValues();
586
587         for (std::list<float>::const_iterator i = weights.begin(); i != weights.end(); ++i) {
588                 source.appendValues(*i);
589         }
590
591         source.finish();
592
593         return source_id;
594 }
595
596 void ControllerExporter::add_vertex_weights_element(const std::string& weights_source_id, const std::string& joints_source_id,
597                                                   const std::list<int>& vcounts,
598                                                   const std::list<int>& joints)
599 {
600         COLLADASW::VertexWeightsElement weightselem(mSW);
601         COLLADASW::InputList &input = weightselem.getInputList();
602
603         int offset = 0;
604         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::JOINT, // constant declared in COLLADASWInputList.h
605                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, joints_source_id), offset++));
606         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::WEIGHT,
607                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, weights_source_id), offset++));
608
609         weightselem.setCount(vcounts.size());
610
611         // write number of deformers per vertex
612         COLLADASW::PrimitivesBase::VCountList vcountlist;
613
614         vcountlist.resize(vcounts.size());
615         std::copy(vcounts.begin(), vcounts.end(), vcountlist.begin());
616
617         weightselem.prepareToAppendVCountValues();
618         weightselem.appendVertexCount(vcountlist);
619
620         weightselem.CloseVCountAndOpenVElement();
621
622         // write deformer index - weight index pairs
623         int weight_index = 0;
624         for (std::list<int>::const_iterator i = joints.begin(); i != joints.end(); ++i) {
625                 weightselem.appendValues(*i, weight_index++);
626         }
627
628         weightselem.finish();
629 }