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