2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2010-10-06 11:02:44 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
|
|
|
|
* Nathan Letwory
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2011-02-27 20:30:35 +00:00
|
|
|
/** \file blender/collada/ArmatureExporter.cpp
|
|
|
|
* \ingroup collada
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-10-06 11:02:44 +00:00
|
|
|
#include "COLLADASWBaseInputElement.h"
|
|
|
|
#include "COLLADASWInstanceController.h"
|
|
|
|
#include "COLLADASWPrimitves.h"
|
|
|
|
#include "COLLADASWSource.h"
|
|
|
|
|
|
|
|
#include "DNA_action_types.h"
|
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
#include "DNA_modifier_types.h"
|
|
|
|
|
|
|
|
#include "BKE_action.h"
|
|
|
|
#include "BKE_armature.h"
|
2012-06-07 17:55:26 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "BKE_main.h"
|
|
|
|
#include "BKE_mesh.h"
|
|
|
|
#include "BKE_global.h"
|
|
|
|
#include "BKE_library.h"
|
|
|
|
}
|
|
|
|
|
2011-08-20 16:48:53 +00:00
|
|
|
#include "ED_armature.h"
|
2010-10-06 11:02:44 +00:00
|
|
|
|
2010-10-06 12:04:56 +00:00
|
|
|
#include "BLI_listbase.h"
|
2010-10-06 11:02:44 +00:00
|
|
|
|
|
|
|
#include "GeometryExporter.h"
|
|
|
|
#include "ArmatureExporter.h"
|
2012-02-18 16:55:41 +00:00
|
|
|
#include "SceneExporter.h"
|
2010-10-06 11:02:44 +00:00
|
|
|
|
2012-06-07 17:55:26 +00:00
|
|
|
#include "collada_utils.h"
|
|
|
|
|
2010-10-06 11:02:44 +00:00
|
|
|
// XXX exporter writes wrong data for shared armatures. A separate
|
|
|
|
// controller should be written for each armature-mesh binding how do
|
|
|
|
// we make controller ids then?
|
2012-06-12 22:05:33 +00:00
|
|
|
ArmatureExporter::ArmatureExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryControllers(sw), export_settings(export_settings) {
|
|
|
|
}
|
2010-10-06 11:02:44 +00:00
|
|
|
|
|
|
|
// write bone nodes
|
2012-06-12 22:05:33 +00:00
|
|
|
void ArmatureExporter::add_armature_bones(Object *ob_arm, Scene *sce,
|
|
|
|
SceneExporter *se,
|
|
|
|
std::list<Object *>& child_objects)
|
2010-10-06 11:02:44 +00:00
|
|
|
{
|
|
|
|
// write bone nodes
|
2012-06-12 22:05:33 +00:00
|
|
|
bArmature *arm = (bArmature *)ob_arm->data;
|
|
|
|
for (Bone *bone = (Bone *)arm->bonebase.first; bone; bone = bone->next) {
|
2010-10-06 11:02:44 +00:00
|
|
|
// start from root bones
|
|
|
|
if (!bone->parent)
|
2012-02-18 16:55:41 +00:00
|
|
|
add_bone_node(bone, ob_arm, sce, se, child_objects);
|
2010-10-06 11:02:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-15 22:00:25 +00:00
|
|
|
void ArmatureExporter::write_bone_URLs(COLLADASW::InstanceController &ins, Object *ob_arm, Bone *bone)
|
|
|
|
{
|
2012-06-16 09:16:24 +00:00
|
|
|
if (bc_is_root_bone(bone, this->export_settings->deform_bones_only))
|
2012-06-15 22:00:25 +00:00
|
|
|
ins.addSkeleton(COLLADABU::URI(COLLADABU::Utils::EMPTY_STRING, get_joint_id(bone, ob_arm)));
|
|
|
|
else {
|
|
|
|
for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
|
|
|
|
write_bone_URLs(ins, ob_arm, child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 17:55:26 +00:00
|
|
|
bool ArmatureExporter::add_instance_controller(Object *ob)
|
2010-10-06 11:02:44 +00:00
|
|
|
{
|
2012-06-07 17:55:26 +00:00
|
|
|
Object *ob_arm = bc_get_assigned_armature(ob);
|
2012-06-12 22:05:33 +00:00
|
|
|
bArmature *arm = (bArmature *)ob_arm->data;
|
2010-10-06 11:02:44 +00:00
|
|
|
|
|
|
|
const std::string& controller_id = get_controller_id(ob_arm, ob);
|
|
|
|
|
|
|
|
COLLADASW::InstanceController ins(mSW);
|
|
|
|
ins.setUrl(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, controller_id));
|
|
|
|
|
2012-06-07 17:55:26 +00:00
|
|
|
Mesh *me = (Mesh *)ob->data;
|
|
|
|
if (!me->dvert) return false;
|
|
|
|
|
2010-10-06 11:02:44 +00:00
|
|
|
// write root bone URLs
|
|
|
|
Bone *bone;
|
2012-06-12 22:05:33 +00:00
|
|
|
for (bone = (Bone *)arm->bonebase.first; bone; bone = bone->next) {
|
2012-06-15 22:00:25 +00:00
|
|
|
write_bone_URLs(ins, ob_arm, bone);
|
2010-10-06 11:02:44 +00:00
|
|
|
}
|
2013-02-04 00:18:09 +00:00
|
|
|
|
2012-06-22 16:16:58 +00:00
|
|
|
InstanceWriter::add_material_bindings(ins.getBindMaterial(), ob, this->export_settings->active_uv_only);
|
2010-10-06 11:02:44 +00:00
|
|
|
|
|
|
|
ins.add();
|
2012-06-07 17:55:26 +00:00
|
|
|
return true;
|
2010-10-06 11:02:44 +00:00
|
|
|
}
|
|
|
|
|
2013-02-01 15:17:51 +00:00
|
|
|
#if 0
|
2010-10-06 11:02:44 +00:00
|
|
|
void ArmatureExporter::operator()(Object *ob)
|
|
|
|
{
|
2012-06-07 17:55:26 +00:00
|
|
|
Object *ob_arm = bc_get_assigned_armature(ob);
|
2010-10-06 11:02:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArmatureExporter::already_written(Object *ob_arm)
|
|
|
|
{
|
|
|
|
return std::find(written_armatures.begin(), written_armatures.end(), ob_arm) != written_armatures.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArmatureExporter::wrote(Object *ob_arm)
|
|
|
|
{
|
|
|
|
written_armatures.push_back(ob_arm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArmatureExporter::find_objects_using_armature(Object *ob_arm, std::vector<Object *>& objects, Scene *sce)
|
|
|
|
{
|
|
|
|
objects.clear();
|
|
|
|
|
2012-06-12 22:05:33 +00:00
|
|
|
Base *base = (Base *) sce->base.first;
|
2012-04-28 06:31:57 +00:00
|
|
|
while (base) {
|
2010-10-06 11:02:44 +00:00
|
|
|
Object *ob = base->object;
|
|
|
|
|
|
|
|
if (ob->type == OB_MESH && get_assigned_armature(ob) == ob_arm) {
|
|
|
|
objects.push_back(ob);
|
|
|
|
}
|
|
|
|
|
2012-06-12 22:05:33 +00:00
|
|
|
base = base->next;
|
2010-10-06 11:02:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string ArmatureExporter::get_joint_sid(Bone *bone, Object *ob_arm)
|
|
|
|
{
|
|
|
|
return get_joint_id(bone, ob_arm);
|
|
|
|
}
|
|
|
|
|
|
|
|
// parent_mat is armature-space
|
2012-06-12 22:05:33 +00:00
|
|
|
void ArmatureExporter::add_bone_node(Bone *bone, Object *ob_arm, Scene *sce,
|
|
|
|
SceneExporter *se,
|
|
|
|
std::list<Object *>& child_objects)
|
2010-10-06 11:02:44 +00:00
|
|
|
{
|
2012-06-15 22:00:25 +00:00
|
|
|
if (!(this->export_settings->deform_bones_only && bone->flag & BONE_NO_DEFORM)) {
|
|
|
|
std::string node_id = get_joint_id(bone, ob_arm);
|
|
|
|
std::string node_name = std::string(bone->name);
|
|
|
|
std::string node_sid = get_joint_sid(bone, ob_arm);
|
2010-10-06 11:02:44 +00:00
|
|
|
|
2012-06-15 22:00:25 +00:00
|
|
|
COLLADASW::Node node(mSW);
|
2010-10-06 11:02:44 +00:00
|
|
|
|
2012-06-15 22:00:25 +00:00
|
|
|
node.setType(COLLADASW::Node::JOINT);
|
|
|
|
node.setNodeId(node_id);
|
|
|
|
node.setNodeName(node_name);
|
|
|
|
node.setNodeSid(node_sid);
|
2010-10-06 11:02:44 +00:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
#if 0
|
2012-06-16 09:16:24 +00:00
|
|
|
if (bone->childbase.first == NULL || BLI_countlist(&(bone->childbase)) >= 2) {
|
2012-08-11 22:12:32 +00:00
|
|
|
add_blender_leaf_bone( bone, ob_arm, node);
|
2012-06-16 09:16:24 +00:00
|
|
|
}
|
2013-03-24 01:19:55 +00:00
|
|
|
else {
|
2012-06-16 09:16:24 +00:00
|
|
|
#endif
|
2013-01-21 13:45:49 +00:00
|
|
|
node.start();
|
2010-10-06 11:02:44 +00:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
add_bone_transform(ob_arm, bone, node);
|
2010-10-06 11:02:44 +00:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
// Write nodes of childobjects, remove written objects from list
|
|
|
|
std::list<Object *>::iterator i = child_objects.begin();
|
2012-02-18 16:55:41 +00:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
while (i != child_objects.end()) {
|
|
|
|
if ((*i)->partype == PARBONE && (0 == strcmp((*i)->parsubstr, bone->name))) {
|
|
|
|
float backup_parinv[4][4];
|
|
|
|
copy_m4_m4(backup_parinv, (*i)->parentinv);
|
2012-02-18 16:55:41 +00:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
// crude, temporary change to parentinv
|
|
|
|
// so transform gets exported correctly.
|
2012-02-24 21:45:59 +00:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
// Add bone tail- translation... don't know why
|
|
|
|
// bone parenting is against the tail of a bone
|
|
|
|
// and not it's head, seems arbitrary.
|
|
|
|
(*i)->parentinv[3][1] += bone->length;
|
2012-02-24 21:45:59 +00:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
// SECOND_LIFE_COMPATIBILITY
|
|
|
|
// TODO: when such objects are animated as
|
|
|
|
// single matrix the tweak must be applied
|
|
|
|
// to the result.
|
|
|
|
if (export_settings->second_life) {
|
|
|
|
// tweak objects parentinverse to match compatibility
|
|
|
|
float temp[4][4];
|
2012-02-18 16:55:41 +00:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
copy_m4_m4(temp, bone->arm_mat);
|
|
|
|
temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
|
2012-02-18 16:55:41 +00:00
|
|
|
|
2013-05-26 18:36:25 +00:00
|
|
|
mul_m4_m4m4((*i)->parentinv, temp, (*i)->parentinv);
|
2013-01-21 13:45:49 +00:00
|
|
|
}
|
2012-02-18 16:55:41 +00:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
se->writeNodes(*i, sce);
|
2012-02-18 16:55:41 +00:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
copy_m4_m4((*i)->parentinv, backup_parinv);
|
|
|
|
child_objects.erase(i++);
|
|
|
|
}
|
|
|
|
else i++;
|
2012-06-15 22:00:25 +00:00
|
|
|
}
|
2012-02-18 16:55:41 +00:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
|
|
|
|
add_bone_node(child, ob_arm, sce, se, child_objects);
|
|
|
|
}
|
|
|
|
node.end();
|
2012-06-15 22:00:25 +00:00
|
|
|
}
|
2013-01-21 13:45:49 +00:00
|
|
|
else {
|
|
|
|
for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
|
|
|
|
add_bone_node(child, ob_arm, sce, se, child_objects);
|
|
|
|
}
|
2012-06-15 22:00:25 +00:00
|
|
|
}
|
2010-10-06 11:02:44 +00:00
|
|
|
}
|
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
//#if 1
|
2012-06-12 22:05:33 +00:00
|
|
|
void ArmatureExporter::add_blender_leaf_bone(Bone *bone, Object *ob_arm, COLLADASW::Node& node)
|
2011-07-24 20:27:27 +00:00
|
|
|
{
|
|
|
|
node.start();
|
2011-08-20 16:48:53 +00:00
|
|
|
|
2011-07-24 20:27:27 +00:00
|
|
|
add_bone_transform(ob_arm, bone, node);
|
2011-08-20 16:48:53 +00:00
|
|
|
|
2012-06-12 22:05:33 +00:00
|
|
|
node.addExtraTechniqueParameter("blender", "tip_x", bone->tail[0]);
|
|
|
|
node.addExtraTechniqueParameter("blender", "tip_y", bone->tail[1]);
|
|
|
|
node.addExtraTechniqueParameter("blender", "tip_z", bone->tail[2]);
|
2011-07-24 20:27:27 +00:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
/*for (Bone *child = (Bone *)bone->childbase.first; child; child = child->next) {
|
2012-02-18 16:55:41 +00:00
|
|
|
add_bone_node(child, ob_arm, sce, se, child_objects);
|
2013-01-21 13:45:49 +00:00
|
|
|
}*/
|
2011-07-24 20:27:27 +00:00
|
|
|
node.end();
|
|
|
|
|
2012-06-12 22:05:33 +00:00
|
|
|
}
|
2013-01-21 13:45:49 +00:00
|
|
|
//#endif
|
2012-06-12 22:05:33 +00:00
|
|
|
|
2010-10-06 11:02:44 +00:00
|
|
|
void ArmatureExporter::add_bone_transform(Object *ob_arm, Bone *bone, COLLADASW::Node& node)
|
|
|
|
{
|
2013-02-01 15:17:51 +00:00
|
|
|
//bPoseChannel *pchan = BKE_pose_channel_find_name(ob_arm->pose, bone->name);
|
2010-10-06 11:02:44 +00:00
|
|
|
|
|
|
|
float mat[4][4];
|
|
|
|
|
|
|
|
if (bone->parent) {
|
2013-01-21 13:45:49 +00:00
|
|
|
// get bone-space matrix from parent pose
|
|
|
|
/*bPoseChannel *parchan = BKE_pose_channel_find_name(ob_arm->pose, bone->parent->name);
|
2010-10-06 11:02:44 +00:00
|
|
|
float invpar[4][4];
|
|
|
|
invert_m4_m4(invpar, parchan->pose_mat);
|
2013-05-26 18:36:25 +00:00
|
|
|
mul_m4_m4m4(mat, invpar, pchan->pose_mat);*/
|
2013-01-21 13:45:49 +00:00
|
|
|
|
|
|
|
float invpar[4][4];
|
|
|
|
invert_m4_m4(invpar, bone->parent->arm_mat);
|
2013-05-26 18:36:25 +00:00
|
|
|
mul_m4_m4m4(mat, invpar, bone->arm_mat);
|
2013-01-21 13:45:49 +00:00
|
|
|
|
2010-10-06 11:02:44 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-01-21 13:45:49 +00:00
|
|
|
|
|
|
|
//copy_m4_m4(mat, pchan->pose_mat);
|
|
|
|
//pose mat is object space
|
|
|
|
//New change: export bone->arm_mat
|
|
|
|
copy_m4_m4(mat, bone->arm_mat);
|
2012-02-05 16:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SECOND_LIFE_COMPATIBILITY
|
2013-02-10 17:06:05 +00:00
|
|
|
if (export_settings->second_life) {
|
2012-02-05 16:19:28 +00:00
|
|
|
// Remove rotations vs armature from transform
|
|
|
|
// parent_rest_rot * mat * irest_rot
|
|
|
|
float temp[4][4];
|
|
|
|
copy_m4_m4(temp, bone->arm_mat);
|
|
|
|
temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
|
|
|
|
invert_m4(temp);
|
|
|
|
|
2013-05-26 18:36:25 +00:00
|
|
|
mul_m4_m4m4(mat, mat, temp);
|
2012-02-05 16:19:28 +00:00
|
|
|
|
2012-04-28 06:31:57 +00:00
|
|
|
if (bone->parent) {
|
2012-02-05 16:19:28 +00:00
|
|
|
copy_m4_m4(temp, bone->parent->arm_mat);
|
|
|
|
temp[3][0] = temp[3][1] = temp[3][2] = 0.0f;
|
|
|
|
|
2013-05-26 18:36:25 +00:00
|
|
|
mul_m4_m4m4(mat, temp, mat);
|
2012-02-05 16:19:28 +00:00
|
|
|
}
|
2010-10-06 11:02:44 +00:00
|
|
|
}
|
|
|
|
|
2012-04-29 15:47:02 +00:00
|
|
|
TransformWriter::add_node_transform(node, mat, NULL);
|
2010-10-06 11:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ArmatureExporter::get_controller_id(Object *ob_arm, Object *ob)
|
|
|
|
{
|
|
|
|
return translate_id(id_name(ob_arm)) + "_" + translate_id(id_name(ob)) + SKIN_CONTROLLER_ID_SUFFIX;
|
|
|
|
}
|