metaforce/DataSpec/DNAMP1/CINF.hpp

182 lines
5.3 KiB
C++
Raw Normal View History

2015-08-11 23:32:02 +00:00
#ifndef _DNAMP1_CINF_HPP_
#define _DNAMP1_CINF_HPP_
2015-08-13 07:29:00 +00:00
#include "BlenderConnection.hpp"
2015-08-11 23:32:02 +00:00
#include "../DNACommon/DNACommon.hpp"
2016-04-07 03:40:25 +00:00
#include "../DNACommon/RigInverter.hpp"
2015-08-11 23:32:02 +00:00
2016-02-13 09:02:47 +00:00
namespace DataSpec
2015-08-11 23:32:02 +00:00
{
namespace DNAMP1
{
struct CINF : BigDNA
{
DECL_DNA
Value<atUint32> boneCount;
struct Bone : BigDNA
{
DECL_DNA
Value<atUint32> id;
Value<atUint32> parentId;
Value<atVec3f> origin;
Value<atUint32> linkedCount;
Vector<atUint32, DNA_COUNT(linkedCount)> linked;
};
Vector<Bone, DNA_COUNT(boneCount)> bones;
Value<atUint32> boneIdCount;
Vector<atUint32, DNA_COUNT(boneIdCount)> boneIds;
Value<atUint32> nameCount;
struct Name : BigDNA
{
DECL_DNA
String<-1> name;
Value<atUint32> boneId;
};
Vector<Name, DNA_COUNT(nameCount)> names;
2015-08-13 07:29:00 +00:00
2016-04-07 03:40:25 +00:00
atUint32 getInternalBoneIdxFromId(atUint32 id) const
{
atUint32 idx = 0;
for (const Bone& b : bones)
{
if (b.id == id)
return idx;
++idx;
}
return -1;
}
2015-08-13 07:29:00 +00:00
atUint32 getBoneIdxFromId(atUint32 id) const
{
atUint32 idx = 0;
for (atUint32 bid : boneIds)
{
if (bid == id)
return idx;
++idx;
}
return 0;
}
const std::string* getBoneNameFromId(atUint32 id) const
{
for (const Name& name : names)
if (id == name.boneId)
return &name.name;
return nullptr;
}
2016-03-04 23:04:53 +00:00
void sendVertexGroupsToBlender(hecl::BlenderConnection::PyOutStream& os) const
2015-08-13 07:29:00 +00:00
{
for (atUint32 bid : boneIds)
{
for (const Name& name : names)
{
if (name.boneId == bid)
{
os.format("obj.vertex_groups.new('%s')\n", name.name.c_str());
break;
}
}
}
}
2016-03-04 23:04:53 +00:00
void sendCINFToBlender(hecl::BlenderConnection::PyOutStream& os, const UniqueID32& cinfId) const
2015-08-13 07:29:00 +00:00
{
2016-04-07 03:40:25 +00:00
DNAANIM::RigInverter<CINF> inverter(*this);
2016-04-08 03:37:14 +00:00
2015-08-13 07:29:00 +00:00
os.format("arm = bpy.data.armatures.new('CINF_%08X')\n"
"arm_obj = bpy.data.objects.new(arm.name, arm)\n"
"bpy.context.scene.objects.link(arm_obj)\n"
"bpy.context.scene.objects.active = arm_obj\n"
"bpy.ops.object.mode_set(mode='EDIT')\n"
"arm_bone_table = {}\n",
cinfId.toUint32());
2016-04-07 03:40:25 +00:00
for (const DNAANIM::RigInverter<CINF>::Bone& bone : inverter.getBones())
2015-08-13 07:29:00 +00:00
os.format("bone = arm.edit_bones.new('%s')\n"
"bone.head = (%f,%f,%f)\n"
2016-04-07 03:40:25 +00:00
"bone.tail = (%f,%f,%f)\n"
2015-09-28 00:42:47 +00:00
"bone.use_inherit_scale = False\n"
2016-04-07 03:40:25 +00:00
"arm_bone_table[%u] = bone\n", getBoneNameFromId(bone.m_origBone.id)->c_str(),
bone.m_origBone.origin.vec[0], bone.m_origBone.origin.vec[1], bone.m_origBone.origin.vec[2],
bone.m_tail[0], bone.m_tail[1], bone.m_tail[2],
bone.m_origBone.id);
2015-08-13 07:29:00 +00:00
for (const Bone& bone : bones)
if (bone.parentId != 2)
os.format("arm_bone_table[%u].parent = arm_bone_table[%u]\n", bone.id, bone.parentId);
os << "bpy.ops.object.mode_set(mode='OBJECT')\n";
}
2016-04-07 03:40:25 +00:00
2016-04-08 03:37:14 +00:00
static std::string GetCINFArmatureName(const UniqueID32& cinfId)
{
return hecl::Format("CINF_%08X", cinfId.toUint32());
}
2016-04-07 03:40:25 +00:00
CINF() = default;
using Armature = hecl::BlenderConnection::DataStream::Actor::Armature;
2016-04-09 23:19:17 +00:00
int RecursiveAddArmatureBone(const Armature& armature, const Armature::Bone* bone, int parent, int& curId,
std::unordered_map<std::string, atInt32>& idMap)
2016-04-07 03:40:25 +00:00
{
2016-04-09 23:19:17 +00:00
int selId;
auto search = idMap.find(bone->name);
if (search == idMap.end())
{
selId = curId++;
idMap.emplace(std::make_pair(bone->name, selId));
}
else
selId = search->second;
2016-04-07 03:40:25 +00:00
bones.emplace_back();
names.emplace_back();
Bone& boneOut = bones.back();
Name& nameOut = names.back();
nameOut.name = bone->name;
2016-04-09 23:19:17 +00:00
nameOut.boneId = selId;
boneOut.id = selId;
2016-04-07 03:40:25 +00:00
boneOut.parentId = parent;
boneOut.origin = bone->origin;
2016-04-09 23:19:17 +00:00
boneOut.linkedCount = bone->children.size() + 1;
2016-04-07 03:40:25 +00:00
boneOut.linked.reserve(boneOut.linkedCount);
const Armature::Bone* child;
2016-04-09 23:19:17 +00:00
boneOut.linked.push_back(parent);
2016-04-07 03:40:25 +00:00
for (size_t i=0 ; (child = armature.getChild(bone, i)) ; ++i)
2016-04-09 23:19:17 +00:00
boneOut.linked.push_back(RecursiveAddArmatureBone(armature, child, boneOut.id, selId, idMap));
2016-04-07 03:40:25 +00:00
return boneOut.id;
}
2016-04-09 23:19:17 +00:00
CINF(const Armature& armature, std::unordered_map<std::string, atInt32>& idMap)
2016-04-07 03:40:25 +00:00
{
2016-04-09 23:19:17 +00:00
idMap.reserve(armature.bones.size());
2016-04-07 03:40:25 +00:00
bones.reserve(armature.bones.size());
names.reserve(armature.bones.size());
const Armature::Bone* bone = armature.getRoot();
int curId = 3;
if (bone)
2016-04-09 23:19:17 +00:00
RecursiveAddArmatureBone(armature, bone, 2, curId, idMap);
2016-04-07 03:40:25 +00:00
boneCount = bones.size();
nameCount = names.size();
boneIdCount = boneCount;
boneIds.reserve(boneIdCount);
for (auto it=bones.crbegin() ; it != bones.crend() ; ++it)
boneIds.push_back(it->id);
}
2015-08-11 23:32:02 +00:00
};
}
}
#endif // _DNAMP1_CINF_HPP_