2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
21 * ***** END GPL LICENSE BLOCK *****
24 /** \file blender/collada/ArmatureExporter.cpp
29 #include "COLLADASWBaseInputElement.h"
30 #include "COLLADASWInstanceController.h"
31 #include "COLLADASWPrimitves.h"
32 #include "COLLADASWSource.h"
34 #include "DNA_action_types.h"
35 #include "DNA_meshdata_types.h"
36 #include "DNA_modifier_types.h"
38 #include "BKE_action.h"
39 #include "BKE_armature.h"
44 #include "BKE_global.h"
45 #include "BKE_library.h"
48 #include "ED_armature.h"
50 #include "BLI_listbase.h"
52 #include "GeometryExporter.h"
53 #include "ArmatureExporter.h"
54 #include "SceneExporter.h"
56 #include "collada_utils.h"
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) {
65 void ArmatureExporter::add_armature_bones(Object *ob_arm, Scene *sce,
67 std::list<Object *>& child_objects)
70 bArmature *arm = (bArmature *)ob_arm->data;
71 for (Bone *bone = (Bone *)arm->bonebase.first; bone; bone = bone->next) {
72 // start from root bones
74 add_bone_node(bone, ob_arm, sce, se, child_objects);
78 void ArmatureExporter::write_bone_URLs(COLLADASW::InstanceController &ins, Object *ob_arm, Bone *bone)
80 if (bc_is_root_bone(bone, this->export_settings->deform_bones_only))
81 ins.addSkeleton(COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_joint_id(bone, ob_arm)));
83 for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
84 write_bone_URLs(ins, ob_arm, child);
89 bool ArmatureExporter::add_instance_controller(Object *ob)
91 Object *ob_arm = bc_get_assigned_armature(ob);
92 bArmature *arm = (bArmature *)ob_arm->data;
94 const std::string& controller_id = get_controller_id(ob_arm, ob);
96 COLLADASW::InstanceController ins(mSW);
97 ins.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, controller_id));
99 Mesh *me = (Mesh *)ob->data;
100 if (!me->dvert) return false;
102 // write root bone URLs
104 for (bone = (Bone *)arm->bonebase.first; bone; bone = bone->next) {
105 write_bone_URLs(ins, ob_arm, bone);
108 InstanceWriter::add_material_bindings(ins.getBindMaterial(), ob, this->export_settings->active_uv_only);
115 void ArmatureExporter::operator()(Object *ob)
117 Object *ob_arm = bc_get_assigned_armature(ob);
121 bool ArmatureExporter::already_written(Object *ob_arm)
123 return std::find(written_armatures.begin(), written_armatures.end(), ob_arm) != written_armatures.end();
126 void ArmatureExporter::wrote(Object *ob_arm)
128 written_armatures.push_back(ob_arm);
131 void ArmatureExporter::find_objects_using_armature(Object *ob_arm, std::vector<Object *>& objects, Scene *sce)
135 Base *base = (Base *) sce->base.first;
137 Object *ob = base->object;
139 if (ob->type == OB_MESH && get_assigned_armature(ob) == ob_arm) {
140 objects.push_back(ob);
148 std::string ArmatureExporter::get_joint_sid(Bone *bone, Object *ob_arm)
150 return get_joint_id(bone, ob_arm);
153 // parent_mat is armature-space
154 void ArmatureExporter::add_bone_node(Bone *bone, Object *ob_arm, Scene *sce,
156 std::list<Object *>& child_objects)
158 if (!(this->export_settings->deform_bones_only && bone->flag & BONE_NO_DEFORM)) {
159 std::string node_id = get_joint_id(bone, ob_arm);
160 std::string node_name = std::string(bone->name);
161 std::string node_sid = get_joint_sid(bone, ob_arm);
163 COLLADASW::Node node(mSW);
165 node.setType(COLLADASW::Node::JOINT);
166 node.setNodeId(node_id);
167 node.setNodeName(node_name);
168 node.setNodeSid(node_sid);
171 if (bone->childbase.first == NULL || BLI_countlist(&(bone->childbase)) >= 2) {
172 add_blender_leaf_bone( bone, ob_arm, node);
178 add_bone_transform(ob_arm, bone, node);
180 // Write nodes of childobjects, remove written objects from list
181 std::list<Object *>::iterator i = child_objects.begin();
183 while (i != child_objects.end()) {
184 if ((*i)->partype == PARBONE && (0 == strcmp((*i)->parsubstr, bone->name))) {
185 float backup_parinv[4][4];
186 copy_m4_m4(backup_parinv, (*i)->parentinv);
188 // crude, temporary change to parentinv
189 // so transform gets exported correctly.
191 // Add bone tail- translation... don't know why
192 // bone parenting is against the tail of a bone
193 // and not it's head, seems arbitrary.
194 (*i)->parentinv[3][1] += bone->length;
196 // SECOND_LIFE_COMPATIBILITY
197 // TODO: when such objects are animated as
198 // single matrix the tweak must be applied
200 if (export_settings->second_life) {
201 // tweak objects parentinverse to match compatibility
204 copy_m4_m4(temp, bone->arm_mat);
205 temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
207 mult_m4_m4m4((*i)->parentinv, temp, (*i)->parentinv);
210 se->writeNodes(*i, sce);
212 copy_m4_m4((*i)->parentinv, backup_parinv);
213 child_objects.erase(i++);
218 for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
219 add_bone_node(child, ob_arm, sce, se, child_objects);
224 for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
225 add_bone_node(child, ob_arm, sce, se, child_objects);
231 void ArmatureExporter::add_blender_leaf_bone(Bone *bone, Object *ob_arm, COLLADASW::Node& node)
235 add_bone_transform(ob_arm, bone, node);
237 node.addExtraTechniqueParameter("blender", "tip_x", bone->tail[0]);
238 node.addExtraTechniqueParameter("blender", "tip_y", bone->tail[1]);
239 node.addExtraTechniqueParameter("blender", "tip_z", bone->tail[2]);
241 /*for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
242 add_bone_node(child, ob_arm, sce, se, child_objects);
249 void ArmatureExporter::add_bone_transform(Object *ob_arm, Bone *bone, COLLADASW::Node& node)
251 //bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, bone->name);
256 // get bone-space matrix from parent pose
257 /*bPoseChannel *parchan = BKE_pose_channel_find_name(ob_arm->pose, bone->parent->name);
259 invert_m4_m4(invpar, parchan->pose_mat);
260 mult_m4_m4m4(mat, invpar, pchan->pose_mat);*/
263 invert_m4_m4(invpar, bone->parent->arm_mat);
264 mult_m4_m4m4(mat, invpar, bone->arm_mat);
269 //copy_m4_m4(mat, pchan->pose_mat);
270 //pose mat is object space
271 //New change: export bone->arm_mat
272 copy_m4_m4(mat, bone->arm_mat);
275 // SECOND_LIFE_COMPATIBILITY
276 if (export_settings->second_life) {
277 // Remove rotations vs armature from transform
278 // parent_rest_rot * mat * irest_rot
280 copy_m4_m4(temp, bone->arm_mat);
281 temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
284 mult_m4_m4m4(mat, mat, temp);
287 copy_m4_m4(temp, bone->parent->arm_mat);
288 temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
290 mult_m4_m4m4(mat, temp, mat);
294 TransformWriter::add_node_transform(node, mat, NULL);
297 std::string ArmatureExporter::get_controller_id(Object *ob_arm, Object *ob)
299 return translate_id(id_name(ob_arm)) + "_" + translate_id(id_name(ob)) + SKIN_CONTROLLER_ID_SUFFIX;