cleanup: style
[blender-staging.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(bone, ob_arm)));
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(Scene *sce)
108 {
109         scene = sce;
110
111         openLibrary();
112
113         GeometryFunctor gf;
114         gf.forEachMeshObjectInExportSet<ControllerExporter>(sce, *this, this->export_settings->export_set);
115
116         closeLibrary();
117 }
118
119 void ControllerExporter::operator()(Object *ob)
120 {
121         Object *ob_arm = bc_get_assigned_armature(ob);
122         Key *key = BKE_key_from_object(ob);
123
124         if (ob_arm) {
125                 export_skin_controller(ob, ob_arm);
126         }
127         if (key && this->export_settings->include_shapekeys) {
128                 export_morph_controller(ob, key);
129         }
130 }
131 #if 0
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 std::string ControllerExporter::get_joint_sid(Bone *bone, Object *ob_arm)
161 {
162         return get_joint_id(bone, ob_arm);
163 }
164
165 std::string ControllerExporter::get_controller_id(Object *ob_arm, Object *ob)
166 {
167         return translate_id(id_name(ob_arm)) + "_" + translate_id(id_name(ob)) + SKIN_CONTROLLER_ID_SUFFIX;
168 }
169
170 std::string ControllerExporter::get_controller_id(Key *key, Object *ob)
171 {
172         return translate_id(id_name(ob)) + MORPH_CONTROLLER_ID_SUFFIX;
173 }
174
175 // ob should be of type OB_MESH
176 // both args are required
177 void ControllerExporter::export_skin_controller(Object *ob, Object *ob_arm)
178 {
179         // joint names
180         // joint inverse bind matrices
181         // vertex weights
182
183         // input:
184         // joint names: ob -> vertex group names
185         // vertex group weights: me->dvert -> groups -> index, weight
186
187 #if 0
188         me->dvert :
189
190         typedef struct MDeformVert {
191                 struct MDeformWeight *dw;
192                 int totweight;
193                 int flag;   // flag only in use for weightpaint now
194         } MDeformVert;
195
196         typedef struct MDeformWeight {
197                 int def_nr;
198                 float weight;
199         } MDeformWeight;
200 #endif
201
202         bool use_instantiation = this->export_settings->use_object_instantiation;
203         Mesh *me;
204
205         me = bc_get_mesh_copy(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 %ld.\n", oob_counter, joint_index_by_def_index.size());
288                 }
289         }
290
291         std::string weights_source_id = add_weights_source(me, controller_id, weights);
292         add_joints_element(&ob->defbase, joints_source_id, inv_bind_mat_source_id);
293         add_vertex_weights_element(weights_source_id, joints_source_id, vcounts, joints);
294
295         BKE_libblock_free_us(G.main, me);
296
297         closeSkin();
298         closeController();
299 }
300
301 void ControllerExporter::export_morph_controller(Object *ob, Key *key)
302 {
303         bool use_instantiation = this->export_settings->use_object_instantiation;
304         Mesh *me;
305
306         me = bc_get_mesh_copy(scene,
307                                 ob,
308                                 this->export_settings->export_mesh_type,
309                                 this->export_settings->apply_modifiers,
310                                 this->export_settings->triangulate);
311
312         std::string controller_name = id_name(ob) + "-morph";
313         std::string controller_id = get_controller_id(key, ob);
314
315         openMorph(controller_id, controller_name,
316                  COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_geometry_id(ob, use_instantiation)));
317
318         std::string targets_id = add_morph_targets(key, ob);
319         std::string morph_weights_id = add_morph_weights(key, ob);
320         
321         COLLADASW::TargetsElement targets(mSW);
322
323         COLLADASW::InputList &input = targets.getInputList();
324
325         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::MORPH_TARGET, // constant declared in COLLADASWInputList.h
326                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, targets_id)));
327         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::MORPH_WEIGHT,
328                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, morph_weights_id)));
329         targets.add();
330
331         BKE_libblock_free_us(G.main, me);
332
333
334         //support for animations
335         //can also try the base element and param alternative
336         add_weight_extras(key);
337         closeMorph();
338         closeController();
339 }
340
341 std::string ControllerExporter::add_morph_targets(Key *key, Object *ob)
342 {
343         std::string source_id = translate_id(id_name(ob)) + TARGETS_SOURCE_ID_SUFFIX;
344
345         COLLADASW::IdRefSource source(mSW);
346         source.setId(source_id);
347         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
348         source.setAccessorCount(key->totkey - 1);
349         source.setAccessorStride(1);
350
351         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
352         param.push_back("IDREF");
353
354         source.prepareToAppendValues();
355
356         KeyBlock *kb = (KeyBlock *)key->block.first;
357         //skip the basis
358         kb = kb->next;
359         for (; kb; kb = kb->next) {
360                 std::string geom_id = get_geometry_id(ob, false) + "_morph_" + translate_id(kb->name);
361                 source.appendValues(geom_id);
362
363         }
364
365         source.finish();
366
367         return source_id;
368 }
369
370 std::string ControllerExporter::add_morph_weights(Key *key, Object *ob)
371 {
372         std::string source_id = translate_id(id_name(ob)) + WEIGHTS_SOURCE_ID_SUFFIX;
373
374         COLLADASW::FloatSourceF source(mSW);
375         source.setId(source_id);
376         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
377         source.setAccessorCount(key->totkey - 1);
378         source.setAccessorStride(1);
379
380         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
381         param.push_back("MORPH_WEIGHT");
382         
383         source.prepareToAppendValues();
384
385         KeyBlock *kb = (KeyBlock *)key->block.first;
386         //skip the basis
387         kb = kb->next;
388         for (; kb; kb = kb->next) {
389                 float weight = kb->curval;
390                 source.appendValues(weight);
391         }
392         source.finish();
393
394         return source_id;
395 }
396
397 //Added to implemente support for animations.
398 void ControllerExporter::add_weight_extras(Key *key)
399 {
400         // can also try the base element and param alternative
401         COLLADASW::BaseExtraTechnique extra;
402         
403         KeyBlock * kb = (KeyBlock *)key->block.first;
404         //skip the basis
405         kb = kb->next;
406         for (; kb; kb = kb->next) {
407                 // XXX why is the weight not used here and set to 0.0?
408                 // float weight = kb->curval;
409                 extra.addExtraTechniqueParameter ("KHR", "morph_weights" , 0.000, "MORPH_WEIGHT_TO_TARGET");
410         }
411 }
412
413
414
415 void ControllerExporter::add_joints_element(ListBase *defbase,
416                                           const std::string& joints_source_id, const std::string& inv_bind_mat_source_id)
417 {
418         COLLADASW::JointsElement joints(mSW);
419         COLLADASW::InputList &input = joints.getInputList();
420
421         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::JOINT, // constant declared in COLLADASWInputList.h
422                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, joints_source_id)));
423         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::BINDMATRIX,
424                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, inv_bind_mat_source_id)));
425         joints.add();
426 }
427
428 void ControllerExporter::add_bind_shape_mat(Object *ob)
429 {
430         double bind_mat[4][4];
431
432         converter.mat4_to_dae_double(bind_mat, ob->obmat);
433
434         addBindShapeTransform(bind_mat);
435 }
436
437 std::string ControllerExporter::add_joints_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
438 {
439         std::string source_id = controller_id + JOINTS_SOURCE_ID_SUFFIX;
440
441         int totjoint = 0;
442         bDeformGroup *def;
443         for (def = (bDeformGroup *)defbase->first; def; def = def->next) {
444                 if (is_bone_defgroup(ob_arm, def))
445                         totjoint++;
446         }
447
448         COLLADASW::NameSource source(mSW);
449         source.setId(source_id);
450         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
451         source.setAccessorCount(totjoint);
452         source.setAccessorStride(1);
453         
454         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
455         param.push_back("JOINT");
456
457         source.prepareToAppendValues();
458
459         for (def = (bDeformGroup *)defbase->first; def; def = def->next) {
460                 Bone *bone = get_bone_from_defgroup(ob_arm, def);
461                 if (bone)
462                         source.appendValues(get_joint_sid(bone, ob_arm));
463         }
464
465         source.finish();
466
467         return source_id;
468 }
469
470 static float get_property(Bone *bone, const char *key, float def)
471 {
472         float result = def;
473         if (bone->prop) {
474                 IDProperty *property = IDP_GetPropertyFromGroup(bone->prop, key);
475                 if (property) {
476                         switch (property->type) {
477                                 case IDP_INT:
478                                         result = (float)(IDP_Int(property));
479                                         break;
480                                 case IDP_FLOAT:
481                                         result = (float)(IDP_Float(property));
482                                         break;
483                                 case IDP_DOUBLE:
484                                         result = (float)(IDP_Double(property));
485                                         break;
486                                 default:
487                                         result = def;
488                         }
489                 }
490         }
491         return result;
492 }
493
494 /**
495  * This function creates an arbitrary rest pose matrix from
496  * data provided as custom properties. This is a workaround
497  * for support of maya's restpose matrix which can be arbitrary
498  * in opposition to Blender where the Rest pose Matrix is always
499  * the Identity matrix.
500  *
501  * The custom properties are:
502  *
503  * restpose_scale_x
504  * restpose_scale_y
505  * restpose_scale_z
506  *
507  * restpose_rot_x
508  * restpose_rot_y
509  * restpose_rot_z
510  *
511  * restpose_loc_x
512  * restpose_loc_y
513  * restpose_loc_z
514  *
515  * The matrix is only setup if the scale AND the rot properties are defined.
516  * The presence of the loc properties is optional.
517  *
518  * This feature has been implemented to support Second Life "Fitted Mesh"
519  * TODO: Check if an arbitrary rest pose matrix makes sense within Blender.
520  * Eventually leverage the custom property data into an "official" 
521  * Edit_bone Property
522  */
523 static void create_restpose_mat(Bone *bone, float mat[4][4])
524 {
525         float loc[3] = {
526                 get_property(bone, "restpose_loc_x", 0.0),
527                 get_property(bone, "restpose_loc_y", 0.0),
528                 get_property(bone, "restpose_loc_z", 0.0)
529         };
530
531         float rot[3] = {
532                 DEG2RADF(get_property(bone, "restpose_rot_x", 0.0)),
533                 DEG2RADF(get_property(bone, "restpose_rot_y", 0.0)),
534                 DEG2RADF(get_property(bone, "restpose_rot_z", 0.0))
535         };
536
537         float scale[3] = {
538                 get_property(bone, "restpose_scale_x", 1.0),
539                 get_property(bone, "restpose_scale_y", 1.0),
540                 get_property(bone, "restpose_scale_z", 1.0)
541         };
542
543         loc_eulO_size_to_mat4(mat, loc, rot, scale, 6);
544 }
545
546 std::string ControllerExporter::add_inv_bind_mats_source(Object *ob_arm, ListBase *defbase, const std::string& controller_id)
547 {
548         std::string source_id = controller_id + BIND_POSES_SOURCE_ID_SUFFIX;
549
550         int totjoint = 0;
551         for (bDeformGroup *def = (bDeformGroup *)defbase->first; def; def = def->next) {
552                 if (is_bone_defgroup(ob_arm, def))
553                         totjoint++;
554         }
555
556         COLLADASW::FloatSourceF source(mSW);
557         source.setId(source_id);
558         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
559         source.setAccessorCount(totjoint); //BLI_listbase_count(defbase));
560         source.setAccessorStride(16);
561         
562         source.setParameterTypeName(&COLLADASW::CSWC::CSW_VALUE_TYPE_FLOAT4x4);
563         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
564         param.push_back("TRANSFORM");
565
566         source.prepareToAppendValues();
567
568         bPose *pose = ob_arm->pose;
569         bArmature *arm = (bArmature *)ob_arm->data;
570
571         int flag = arm->flag;
572
573         // put armature in rest position
574         if (!(arm->flag & ARM_RESTPOS)) {
575                 arm->flag |= ARM_RESTPOS;
576                 BKE_pose_where_is(scene, ob_arm);
577         }
578
579         for (bDeformGroup *def = (bDeformGroup *)defbase->first; def; def = def->next) {
580                 if (is_bone_defgroup(ob_arm, def)) {
581                         bPoseChannel *pchan = BKE_pose_channel_find_name(pose, def->name);
582
583                         float mat[4][4];
584                         float world[4][4];
585                         float inv_bind_mat[4][4];
586
587                         
588                         // SL/OPEN_SIM COMPATIBILITY
589                         if (export_settings->open_sim) {
590                                 // Only translations, no rotation vs armature
591                                 float temp[4][4];
592                                 unit_m4(temp);
593                                 copy_v3_v3(temp[3], pchan->bone->arm_mat[3]);
594                                 mul_m4_m4m4(world, ob_arm->obmat, temp);
595
596                                 // Add Maya restpose matrix (if defined as properties)
597                                 float restpose_mat[4][4];
598                                 create_restpose_mat(pchan->bone, restpose_mat);
599                                 mul_m4_m4m4(world, world, restpose_mat);
600
601                         }
602                         else {
603                                 // make world-space matrix, arm_mat is armature-space
604                                 mul_m4_m4m4(world, ob_arm->obmat, pchan->bone->arm_mat);
605                         }
606
607
608                         invert_m4_m4(mat, world);
609                         converter.mat4_to_dae(inv_bind_mat, mat);
610
611                         source.appendValues(inv_bind_mat);
612                 }
613         }
614
615         // back from rest positon
616         if (!(flag & ARM_RESTPOS)) {
617                 arm->flag = flag;
618                 BKE_pose_where_is(scene, ob_arm);
619         }
620
621         source.finish();
622
623         return source_id;
624 }
625
626 Bone *ControllerExporter::get_bone_from_defgroup(Object *ob_arm, bDeformGroup *def)
627 {
628         bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, def->name);
629         return pchan ? pchan->bone : NULL;
630 }
631
632 bool ControllerExporter::is_bone_defgroup(Object *ob_arm, bDeformGroup *def)
633 {
634         return get_bone_from_defgroup(ob_arm, def) != NULL;
635 }
636
637 std::string ControllerExporter::add_weights_source(Mesh *me, const std::string& controller_id, const std::list<float>& weights)
638 {
639         std::string source_id = controller_id + WEIGHTS_SOURCE_ID_SUFFIX;
640
641         COLLADASW::FloatSourceF source(mSW);
642         source.setId(source_id);
643         source.setArrayId(source_id + ARRAY_ID_SUFFIX);
644         source.setAccessorCount(weights.size());
645         source.setAccessorStride(1);
646         
647         COLLADASW::SourceBase::ParameterNameList &param = source.getParameterNameList();
648         param.push_back("WEIGHT");
649
650         source.prepareToAppendValues();
651
652         for (std::list<float>::const_iterator i = weights.begin(); i != weights.end(); ++i) {
653                 source.appendValues(*i);
654         }
655
656         source.finish();
657
658         return source_id;
659 }
660
661 void ControllerExporter::add_vertex_weights_element(const std::string& weights_source_id, const std::string& joints_source_id,
662                                                   const std::list<int>& vcounts,
663                                                   const std::list<int>& joints)
664 {
665         COLLADASW::VertexWeightsElement weightselem(mSW);
666         COLLADASW::InputList &input = weightselem.getInputList();
667
668         int offset = 0;
669         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::JOINT, // constant declared in COLLADASWInputList.h
670                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, joints_source_id), offset++));
671         input.push_back(COLLADASW::Input(COLLADASW::InputSemantic::WEIGHT,
672                                          COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, weights_source_id), offset++));
673
674         weightselem.setCount(vcounts.size());
675
676         // write number of deformers per vertex
677         COLLADASW::PrimitivesBase::VCountList vcountlist;
678
679         vcountlist.resize(vcounts.size());
680         std::copy(vcounts.begin(), vcounts.end(), vcountlist.begin());
681
682         weightselem.prepareToAppendVCountValues();
683         weightselem.appendVertexCount(vcountlist);
684
685         weightselem.CloseVCountAndOpenVElement();
686
687         // write deformer index - weight index pairs
688         int weight_index = 0;
689         for (std::list<int>::const_iterator i = joints.begin(); i != joints.end(); ++i) {
690                 weightselem.appendValues(*i, weight_index++);
691         }
692
693         weightselem.finish();
694 }