metaforce/DataSpec/DNACommon/ANCS.cpp

286 lines
13 KiB
C++
Raw Normal View History

#include "DataSpec/DNACommon/ANCS.hpp"
#include "DataSpec/DNACommon/CMDL.hpp"
#include "DataSpec/DNACommon/DNACommon.hpp"
#include "DataSpec/DNACommon/RigInverter.hpp"
2017-12-29 08:08:12 +00:00
#include "DataSpec/DNAMP1/DNAMP1.hpp"
#include "DataSpec/DNAMP1/ANCS.hpp"
#include "DataSpec/DNAMP2/DNAMP2.hpp"
#include "DataSpec/DNAMP2/ANCS.hpp"
#include "DataSpec/DNAMP3/DNAMP3.hpp"
#include "DataSpec/DNAMP3/CHAR.hpp"
#include <hecl/Blender/Connection.hpp>
2017-12-29 08:08:12 +00:00
2018-12-08 05:30:43 +00:00
namespace DataSpec::DNAANCS {
2017-12-29 08:08:12 +00:00
template <class PAKRouter, class ANCSDNA, class MaterialSet, class SurfaceHeader, atUint32 CMDLVersion>
2020-04-14 04:15:17 +00:00
bool ReadANCSToBlender(hecl::blender::Token& btok, const ANCSDNA& ancs, const hecl::ProjectPath& outPath,
2018-12-08 05:30:43 +00:00
PAKRouter& pakRouter, const typename PAKRouter::EntryType& entry, const SpecBase& dataspec,
std::function<void(const hecl::SystemChar*)> fileChanged, bool force) {
2020-04-14 04:15:17 +00:00
auto& conn = btok.getBlenderConnection();
/* Extract character CMDL/CSKR/CINF first */
2018-12-08 05:30:43 +00:00
std::vector<CharacterResInfo<typename PAKRouter::IDType>> chResInfo;
ancs.getCharacterResInfo(chResInfo);
for (const auto& info : chResInfo) {
const nod::Node* node;
2019-10-01 07:38:03 +00:00
if (const typename PAKRouter::EntryType* cmdlE = pakRouter.lookupEntry(info.cmdl, &node, true, false)) {
2018-12-08 05:30:43 +00:00
hecl::ProjectPath cmdlPath = pakRouter.getWorking(cmdlE);
if (force || cmdlPath.isNone()) {
cmdlPath.makeDirChain(false);
if (!conn.createBlend(cmdlPath, hecl::blender::BlendType::Mesh))
return false;
std::string bestName = pakRouter.getBestEntryName(*cmdlE);
hecl::SystemStringConv bestNameView(bestName);
fileChanged(bestNameView.c_str());
typename ANCSDNA::CSKRType cskr;
pakRouter.lookupAndReadDNA(info.cskr, cskr);
typename ANCSDNA::CINFType cinf;
pakRouter.lookupAndReadDNA(info.cinf, cinf);
2019-10-01 07:38:03 +00:00
using RigPair = std::pair<std::pair<typename PAKRouter::IDType, typename ANCSDNA::CSKRType*>,
std::pair<typename PAKRouter::IDType, typename ANCSDNA::CINFType*>>;
RigPair rigPair({info.cskr, &cskr}, {info.cinf, &cinf});
2018-12-08 05:30:43 +00:00
PAKEntryReadStream rs = cmdlE->beginReadStream(*node);
DNACMDL::ReadCMDLToBlender<PAKRouter, MaterialSet, RigPair, SurfaceHeader, CMDLVersion>(
conn, rs, pakRouter, *cmdlE, dataspec, rigPair);
conn.saveBlend();
}
2017-12-29 08:08:12 +00:00
}
2020-04-14 04:15:17 +00:00
if (const typename PAKRouter::EntryType* cinfE = pakRouter.lookupEntry(info.cinf, &node, true, false)) {
hecl::ProjectPath cinfPath = pakRouter.getWorking(cinfE);
if (cinfPath.getPathType() == hecl::ProjectPath::Type::None) {
PAKEntryReadStream rs = cinfE->beginReadStream(*node);
ANCSDNA::CINFType::Extract(dataspec, rs, cinfPath, pakRouter, *cinfE, false, btok, fileChanged);
}
}
2018-12-08 05:30:43 +00:00
}
2020-04-14 04:15:17 +00:00
/* Extract attachment CMDL/CSKR/CINFs first */
2018-12-08 05:30:43 +00:00
auto attRange = pakRouter.lookupCharacterAttachmentRigs(entry.id);
for (auto it = attRange.first; it != attRange.second; ++it) {
2020-04-14 04:15:17 +00:00
auto cinfid = it->second.first.cinf;
2019-10-01 07:38:03 +00:00
auto cmdlid = it->second.first.cmdl;
2018-12-08 05:30:43 +00:00
const nod::Node* node;
2019-10-01 07:38:03 +00:00
if (const typename PAKRouter::EntryType* cmdlE = pakRouter.lookupEntry(cmdlid, &node, true, false)) {
2018-12-08 05:30:43 +00:00
hecl::ProjectPath cmdlPath = pakRouter.getWorking(cmdlE);
if (force || cmdlPath.isNone()) {
cmdlPath.makeDirChain(false);
2020-04-14 04:15:17 +00:00
if (!conn.createBlend(cmdlPath, hecl::blender::BlendType::Mesh)) {
2018-12-08 05:30:43 +00:00
return false;
2020-04-14 04:15:17 +00:00
}
2018-12-08 05:30:43 +00:00
std::string bestName = pakRouter.getBestEntryName(*cmdlE);
hecl::SystemStringConv bestNameView(bestName);
fileChanged(bestNameView.c_str());
const auto* rp = pakRouter.lookupCMDLRigPair(cmdlid);
typename ANCSDNA::CSKRType cskr;
2019-10-01 07:38:03 +00:00
pakRouter.lookupAndReadDNA(rp->cskr, cskr);
2018-12-08 05:30:43 +00:00
typename ANCSDNA::CINFType cinf;
2019-10-01 07:38:03 +00:00
pakRouter.lookupAndReadDNA(rp->cinf, cinf);
using RigPair = std::pair<std::pair<typename PAKRouter::IDType, typename ANCSDNA::CSKRType*>,
std::pair<typename PAKRouter::IDType, typename ANCSDNA::CINFType*>>;
RigPair rigPair({rp->cskr, &cskr}, {rp->cinf, &cinf});
2018-12-08 05:30:43 +00:00
PAKEntryReadStream rs = cmdlE->beginReadStream(*node);
DNACMDL::ReadCMDLToBlender<PAKRouter, MaterialSet, RigPair, SurfaceHeader, CMDLVersion>(
conn, rs, pakRouter, *cmdlE, dataspec, rigPair);
conn.saveBlend();
}
}
2020-04-14 04:15:17 +00:00
if (cinfid.isValid()) {
if (const typename PAKRouter::EntryType* cinfE = pakRouter.lookupEntry(cinfid, &node, true, false)) {
hecl::ProjectPath cinfPath = pakRouter.getWorking(cinfE);
if (cinfPath.getPathType() == hecl::ProjectPath::Type::None) {
PAKEntryReadStream rs = cinfE->beginReadStream(*node);
ANCSDNA::CINFType::Extract(dataspec, rs, cinfPath, pakRouter, *cinfE, false, btok, fileChanged);
}
}
}
2018-12-08 05:30:43 +00:00
}
std::string bestName = pakRouter.getBestEntryName(entry);
hecl::SystemStringConv bestNameView(bestName);
fileChanged(bestNameView.c_str());
/* Establish ANCS blend */
if (!conn.createBlend(outPath, hecl::blender::BlendType::Actor))
return false;
std::string firstName;
typename ANCSDNA::CINFType firstCinf;
{
hecl::blender::PyOutStream os = conn.beginPythonOut(true);
2021-06-07 19:29:18 +00:00
os.format(FMT_STRING("import bpy\n"
"from mathutils import Vector\n"
"bpy.context.scene.name = '{}'\n"
"bpy.context.scene.hecl_mesh_obj = bpy.context.scene.name\n"
"\n"
"# Clear Scene\n"
"if len(bpy.data.collections):\n"
" bpy.data.collections.remove(bpy.data.collections[0])\n"
"\n"
"actor_data = bpy.context.scene.hecl_sact_data\n"
"arm_obj = None\n"),
pakRouter.getBestEntryName(entry));
2018-12-08 05:30:43 +00:00
std::unordered_set<typename PAKRouter::IDType> cinfsDone;
for (const auto& info : chResInfo) {
/* Provide data to add-on */
2021-06-07 19:29:18 +00:00
os.format(FMT_STRING("actor_subtype = actor_data.subtypes.add()\n"
"actor_subtype.name = '{}'\n\n"),
info.name);
2018-12-08 05:30:43 +00:00
/* Build CINF if needed */
if (cinfsDone.find(info.cinf) == cinfsDone.end()) {
2019-10-01 07:38:03 +00:00
if (const typename PAKRouter::EntryType* cinfE = pakRouter.lookupEntry(info.cinf, nullptr, true, false)) {
hecl::ProjectPath cinfPath = pakRouter.getWorking(cinfE);
2020-04-11 22:51:39 +00:00
os.linkArmature(cinfPath.getAbsolutePathUTF8(), fmt::format(FMT_STRING("CINF_{}"), info.cinf));
2019-10-01 07:38:03 +00:00
os << "if obj.name not in bpy.context.scene.objects:\n"
" bpy.context.scene.collection.objects.link(obj)\n";
}
2018-12-08 05:30:43 +00:00
if (cinfsDone.empty()) {
firstName = ANCSDNA::CINFType::GetCINFArmatureName(info.cinf);
2019-10-01 07:38:03 +00:00
pakRouter.lookupAndReadDNA(info.cinf, firstCinf);
2017-12-29 08:08:12 +00:00
}
2018-12-08 05:30:43 +00:00
cinfsDone.insert(info.cinf);
2019-10-01 07:38:03 +00:00
}
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING("arm_obj = bpy.data.objects['CINF_{}']\n"), info.cinf);
2018-12-08 05:30:43 +00:00
os << "actor_subtype.linked_armature = arm_obj.name\n";
/* Link CMDL */
2019-10-01 07:38:03 +00:00
if (const typename PAKRouter::EntryType* cmdlE = pakRouter.lookupEntry(info.cmdl, nullptr, true, false)) {
2018-12-08 05:30:43 +00:00
hecl::ProjectPath cmdlPath = pakRouter.getWorking(cmdlE);
2019-10-01 07:38:03 +00:00
os.linkMesh(cmdlPath.getAbsolutePathUTF8(), pakRouter.getBestEntryName(*cmdlE));
2018-12-08 05:30:43 +00:00
/* Attach CMDL to CINF */
os << "if obj.name not in bpy.context.scene.objects:\n"
" bpy.context.scene.collection.objects.link(obj)\n"
2018-12-08 05:30:43 +00:00
"obj.parent = arm_obj\n"
"obj.parent_type = 'ARMATURE'\n"
"actor_subtype.linked_mesh = obj.name\n\n";
}
/* Link overlays */
for (const auto& overlay : info.overlays) {
os << "overlay = actor_subtype.overlays.add()\n";
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING("overlay.name = '{}'\n"), overlay.first);
2018-12-08 05:30:43 +00:00
/* Link CMDL */
2019-10-01 07:38:03 +00:00
if (const typename PAKRouter::EntryType* cmdlE =
2021-06-07 19:29:18 +00:00
pakRouter.lookupEntry(overlay.second.first, nullptr, true, false)) {
2018-12-08 05:30:43 +00:00
hecl::ProjectPath cmdlPath = pakRouter.getWorking(cmdlE);
2019-10-01 07:38:03 +00:00
os.linkMesh(cmdlPath.getAbsolutePathUTF8(), pakRouter.getBestEntryName(*cmdlE));
2018-12-08 05:30:43 +00:00
/* Attach CMDL to CINF */
os << "if obj.name not in bpy.context.scene.objects:\n"
" bpy.context.scene.collection.objects.link(obj)\n"
2018-12-08 05:30:43 +00:00
"obj.parent = arm_obj\n"
"obj.parent_type = 'ARMATURE'\n"
"overlay.linked_mesh = obj.name\n\n";
}
2018-12-08 05:30:43 +00:00
}
2017-12-29 08:08:12 +00:00
}
2018-12-08 05:30:43 +00:00
/* Link attachments */
for (auto it = attRange.first; it != attRange.second; ++it) {
os << "attachment = actor_data.attachments.add()\n";
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING("attachment.name = '{}'\n"), it->second.second);
2018-12-08 05:30:43 +00:00
2019-10-01 07:38:03 +00:00
auto cinfid = it->second.first.cinf;
auto cmdlid = it->second.first.cmdl;
2018-12-08 05:30:43 +00:00
2019-07-20 04:27:21 +00:00
if (cinfid.isValid()) {
2018-12-08 05:30:43 +00:00
/* Build CINF if needed */
if (cinfsDone.find(cinfid) == cinfsDone.end()) {
2019-10-01 07:38:03 +00:00
if (const typename PAKRouter::EntryType* cinfE = pakRouter.lookupEntry(cinfid, nullptr, true, false)) {
hecl::ProjectPath cinfPath = pakRouter.getWorking(cinfE);
2020-04-11 22:51:39 +00:00
os.linkArmature(cinfPath.getAbsolutePathUTF8(), fmt::format(FMT_STRING("CINF_{}"), cinfid));
2019-10-01 07:38:03 +00:00
os << "if obj.name not in bpy.context.scene.objects:\n"
" bpy.context.scene.collection.objects.link(obj)\n";
}
2018-12-08 05:30:43 +00:00
if (cinfsDone.empty()) {
firstName = ANCSDNA::CINFType::GetCINFArmatureName(cinfid);
2019-10-01 07:38:03 +00:00
pakRouter.lookupAndReadDNA(cinfid, firstCinf);
2018-12-08 05:30:43 +00:00
}
cinfsDone.insert(cinfid);
2019-10-01 07:38:03 +00:00
}
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING("arm_obj = bpy.data.objects['CINF_{}']\n"), cinfid);
2018-12-08 05:30:43 +00:00
os << "attachment.linked_armature = arm_obj.name\n";
}
/* Link CMDL */
2019-10-01 07:38:03 +00:00
if (const typename PAKRouter::EntryType* cmdlE = pakRouter.lookupEntry(cmdlid, nullptr, true, false)) {
2018-12-08 05:30:43 +00:00
hecl::ProjectPath cmdlPath = pakRouter.getWorking(cmdlE);
2019-10-01 07:38:03 +00:00
os.linkMesh(cmdlPath.getAbsolutePathUTF8(), pakRouter.getBestEntryName(*cmdlE));
2018-12-08 05:30:43 +00:00
/* Attach CMDL to CINF */
os << "if obj.name not in bpy.context.scene.objects:\n"
" bpy.context.scene.collection.objects.link(obj)\n"
2018-12-08 05:30:43 +00:00
"obj.parent = arm_obj\n"
"obj.parent_type = 'ARMATURE'\n"
"attachment.linked_mesh = obj.name\n\n";
}
}
}
{
hecl::blender::DataStream ds = conn.beginData();
std::unordered_map<std::string, hecl::blender::Matrix3f> matrices = ds.getBoneMatrices(firstName);
ds.close();
DNAANIM::RigInverter<typename ANCSDNA::CINFType> inverter(firstCinf, matrices);
hecl::blender::PyOutStream os = conn.beginPythonOut(true);
os << "import bpy\n"
"actor_data = bpy.context.scene.hecl_sact_data\n";
/* Get animation primitives */
std::map<atUint32, AnimationResInfo<typename PAKRouter::IDType>> animResInfo;
ancs.getAnimationResInfo(&pakRouter, animResInfo);
for (const auto& id : animResInfo) {
typename ANCSDNA::ANIMType anim;
if (pakRouter.lookupAndReadDNA(id.second.animId, anim, true)) {
2021-06-07 19:29:18 +00:00
os.format(FMT_STRING("act = bpy.data.actions.new('{}')\n"
"act.use_fake_user = True\n"
"act.anim_id = '{}'\n"),
id.second.name, id.second.animId);
2018-12-08 05:30:43 +00:00
anim.sendANIMToBlender(os, inverter, id.second.additive);
}
2021-06-07 19:29:18 +00:00
os.format(FMT_STRING("actor_action = actor_data.actions.add()\n"
"actor_action.name = '{}'\n"),
id.second.name);
2018-12-08 05:30:43 +00:00
/* Extract EVNT if present */
anim.extractEVNT(id.second, outPath, pakRouter, force);
2017-12-29 08:08:12 +00:00
}
2018-12-08 05:30:43 +00:00
}
conn.saveBlend();
return true;
2017-12-29 08:08:12 +00:00
}
2018-12-08 05:30:43 +00:00
template bool
ReadANCSToBlender<PAKRouter<DNAMP1::PAKBridge>, DNAMP1::ANCS, DNAMP1::MaterialSet, DNACMDL::SurfaceHeader_1, 2>(
2020-04-14 04:15:17 +00:00
hecl::blender::Token& btok, const DNAMP1::ANCS& ancs, const hecl::ProjectPath& outPath,
2018-12-08 05:30:43 +00:00
PAKRouter<DNAMP1::PAKBridge>& pakRouter, const typename PAKRouter<DNAMP1::PAKBridge>::EntryType& entry,
const SpecBase& dataspec, std::function<void(const hecl::SystemChar*)> fileChanged, bool force);
template bool
ReadANCSToBlender<PAKRouter<DNAMP2::PAKBridge>, DNAMP2::ANCS, DNAMP2::MaterialSet, DNACMDL::SurfaceHeader_2, 4>(
2020-04-14 04:15:17 +00:00
hecl::blender::Token& btok, const DNAMP2::ANCS& ancs, const hecl::ProjectPath& outPath,
2018-12-08 05:30:43 +00:00
PAKRouter<DNAMP2::PAKBridge>& pakRouter, const typename PAKRouter<DNAMP2::PAKBridge>::EntryType& entry,
const SpecBase& dataspec, std::function<void(const hecl::SystemChar*)> fileChanged, bool force);
template bool
ReadANCSToBlender<PAKRouter<DNAMP3::PAKBridge>, DNAMP3::CHAR, DNAMP3::MaterialSet, DNACMDL::SurfaceHeader_3, 4>(
2020-04-14 04:15:17 +00:00
hecl::blender::Token& btok, const DNAMP3::CHAR& ancs, const hecl::ProjectPath& outPath,
2018-12-08 05:30:43 +00:00
PAKRouter<DNAMP3::PAKBridge>& pakRouter, const typename PAKRouter<DNAMP3::PAKBridge>::EntryType& entry,
const SpecBase& dataspec, std::function<void(const hecl::SystemChar*)> fileChanged, bool force);
} // namespace DataSpec::DNAANCS