14#ifdef MOMENTUM_WITH_FBX_SDK
30#ifdef MOMENTUM_WITH_FBX_SDK
37 const filesystem::path& filename,
40 const double framerate,
42 const bool skipActiveJointParamCheck,
45 std::span<
const std::vector<Marker>> markerSequence,
46 std::string_view fbxNamespace,
51 auto* manager = ::fbxsdk::FbxManager::Create();
56 const auto managerGuard = gsl::finally([manager]() { manager->Destroy(); });
57 auto* ios = ::fbxsdk::FbxIOSettings::Create(manager, IOSROOT);
58 manager->SetIOSettings(ios);
61 ::fbxsdk::FbxExporter* lExporter = ::fbxsdk::FbxExporter::Create(manager,
"");
68 std::string sFilename = filename.string();
69 const char* lFilename = sFilename.c_str();
72 bool lExportStatus = lExporter->Initialize(lFilename, -1, manager->GetIOSettings());
76 "Unable to initialize fbx exporter {}",
77 lExporter->GetStatus().GetErrorString());
80 std::string namespacePrefix(fbxNamespace);
81 if (!namespacePrefix.empty() && namespacePrefix.back() !=
':') {
82 namespacePrefix +=
":";
88 ::fbxsdk::FbxScene* scene = ::fbxsdk::FbxScene::Create(manager,
"momentum_scene");
89 ::fbxsdk::FbxNode* root = scene->GetRootNode();
90 MT_THROW_IF(root ==
nullptr,
"Unable to get root node from FBX scene");
93 ::fbxsdk::FbxAxisSystem axis =
toFbx(coordSystemInfo);
94 axis.ConvertScene(scene);
104 MeshBlendShapeResult meshResult;
105 if (saveMesh && character.mesh !=
nullptr) {
106 meshResult =
createMeshNode(character, scene, root, skeletonResult.jointToNodeMap, permissive);
110 if (!skeletonResult.nodes.empty()) {
111 root->AddChild(skeletonResult.rootNode);
117 if (jointValues.cols() != 0) {
118 if (jointValues.rows() == character.parameterTransform.numJointParameters()) {
122 skeletonResult.nodes,
125 skipActiveJointParamCheck);
128 "Rows of joint values {} do not match joint parameter dimension {} so not saving any motion.",
130 character.parameterTransform.numJointParameters());
137 if (!markerSequence.empty()) {
147 if (blendShapeWeights.cols() > 0 && !meshResult.blendShapeChannels.empty()) {
149 scene, meshResult.blendShapeChannels, blendShapeWeights, framerate);
152 if (faceExpressionWeights.cols() > 0 && !meshResult.faceExprChannels.empty()) {
154 scene, meshResult.faceExprChannels, faceExpressionWeights, framerate);
161 if (!namespacePrefix.empty()) {
171 lExporter->Export(scene);
179 const filesystem::path& inputPath,
183 bool stripNamespaces) {
185 inputPath, keepLocators, permissive, loadBlendShapes, stripNamespaces);
189 std::span<const std::byte> inputSpan,
193 bool stripNamespaces) {
195 inputSpan, keepLocators, permissive, loadBlendShapes, stripNamespaces);
199 const filesystem::path& inputPath,
203 bool stripNamespaces) {
205 inputPath, keepLocators, permissive, loadBlendShapes, stripNamespaces);
209 std::span<const std::byte> inputSpan,
213 bool stripNamespaces) {
215 inputSpan, keepLocators, permissive, loadBlendShapes, stripNamespaces);
226#ifdef MOMENTUM_WITH_FBX_SDK
229 const filesystem::path& filename,
230 const Character& character,
233 const double framerate,
234 std::span<
const std::vector<Marker>> markerSequence,
235 const FileSaveOptions& options) {
237 if (identity.size() == character.parameterTransform.numJointParameters()) {
238 params.offsets = identity;
240 params.offsets = character.parameterTransform.bindPose();
245 if (poses.cols() > 0) {
246 params.pose = poses.col(0);
247 state.set(params, character,
false,
false,
false);
249 jointValues.resize(state.skeletonState.jointParameters.v.size(), poses.cols());
251 jointValues.col(0) = state.skeletonState.jointParameters.v;
253 for (Eigen::Index f = 1; f < poses.cols(); f++) {
254 params.pose = poses.col(f);
255 state.set(params, character,
false,
false,
false);
256 jointValues.col(f) = state.skeletonState.jointParameters.v;
267 options.coordSystemInfo,
270 options.fbxNamespace,
275 const filesystem::path& filename,
278 const double framerate,
279 std::span<
const std::vector<Marker>> markerSequence,
288 options.coordSystemInfo,
291 options.fbxNamespace,
296 const filesystem::path& filename,
298 std::span<const SkeletonState> skeletonStates,
299 const double framerate,
300 std::span<
const std::vector<Marker>> markerSequence,
302 const size_t nFrames = skeletonStates.size();
303 MatrixXf jointParams(character.parameterTransform.zero().v.size(), nFrames);
304 for (
size_t iFrame = 0; iFrame < nFrames; ++iFrame) {
305 jointParams.col(iFrame) =
316 options.coordSystemInfo,
319 options.fbxNamespace,
324 const filesystem::path& filename,
332 auto* manager = ::fbxsdk::FbxManager::Create();
333 auto* ios = ::fbxsdk::FbxIOSettings::Create(manager, IOSROOT);
334 manager->SetIOSettings(ios);
337 ::fbxsdk::FbxScene* scene =
nullptr;
338 const auto cleanup = [&]() {
339 if (scene !=
nullptr) {
345 auto* importer = ::fbxsdk::FbxImporter::Create(manager,
"");
346 const std::string sFilename = filename.string();
347 if (!importer->Initialize(sFilename.c_str(), -1, manager->GetIOSettings())) {
352 scene = ::fbxsdk::FbxScene::Create(manager,
"scene");
353 importer->Import(scene);
357 if (scene->GetSrcObjectCount<::fbxsdk::FbxAnimStack>() == 0) {
361 auto* animStack = scene->GetSrcObject<::fbxsdk::FbxAnimStack>(0);
362 if (animStack->GetMemberCount<::fbxsdk::FbxAnimLayer>() == 0) {
366 auto* animLayer = animStack->GetMember<::fbxsdk::FbxAnimLayer>(0);
369 std::vector<::fbxsdk::FbxBlendShapeChannel*> allChannels;
370 for (
int iMesh = 0; iMesh < scene->GetSrcObjectCount<::fbxsdk::FbxMesh>(); ++iMesh) {
371 auto* mesh = scene->GetSrcObject<::fbxsdk::FbxMesh>(iMesh);
372 for (
int iDeformer = 0; iDeformer < mesh->GetDeformerCount(::fbxsdk::FbxDeformer::eBlendShape);
374 auto* blendShape =
static_cast<::fbxsdk::FbxBlendShape*
>(
375 mesh->GetDeformer(iDeformer, ::fbxsdk::FbxDeformer::eBlendShape));
376 for (
int iChannel = 0; iChannel < blendShape->GetBlendShapeChannelCount(); ++iChannel) {
377 allChannels.push_back(blendShape->GetBlendShapeChannel(iChannel));
382 if (allChannels.empty()) {
388 Eigen::Index numFrames = 0;
389 for (
auto* channel : allChannels) {
390 auto* curve = channel->DeformPercent.GetCurve(animLayer);
391 if (curve !=
nullptr) {
392 numFrames = std::max(numFrames,
static_cast<Eigen::Index
>(curve->KeyGetCount()));
396 if (numFrames == 0) {
402 const auto numChannels =
static_cast<Eigen::Index
>(allChannels.size());
403 MatrixXf weights = MatrixXf::Zero(numChannels, numFrames);
405 for (Eigen::Index i = 0; i < numChannels; i++) {
406 auto* curve = allChannels[i]->DeformPercent.GetCurve(animLayer);
407 if (curve ==
nullptr) {
411 const int keyCount = curve->KeyGetCount();
412 for (
int k = 0; k < keyCount && k < numFrames; k++) {
414 weights(i, k) = curve->KeyGetValue(k) / 100.0f;
425 const filesystem::path& ,
430 std::span<
const std::vector<Marker>> ,
433 "FBX saving is not supported in OpenFBX-only mode. FBX loading is available via OpenFBX, but saving requires the full Autodesk FBX SDK.");
437 const filesystem::path& ,
441 std::span<
const std::vector<Marker>> ,
444 "FBX saving is not supported in OpenFBX-only mode. FBX loading is available via OpenFBX, but saving requires the full Autodesk FBX SDK.");
448 const filesystem::path& ,
450 std::span<const SkeletonState> ,
452 std::span<
const std::vector<Marker>> ,
455 "FBX saving is not supported in OpenFBX-only mode. FBX loading is available via OpenFBX, but saving requires the full Autodesk FBX SDK.");
459 const filesystem::path& ,
463 "FBX saving is not supported in OpenFBX-only mode. FBX loading is available via OpenFBX, but saving requires the full Autodesk FBX SDK.");
467 MT_THROW(
"Loading blend shape weights requires the full Autodesk FBX SDK.");
#define MT_THROW(...)
Throws a std::runtime_error with a formatted message.
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
MatrixXf extractBlendShapeWeights(const Character &character, const MatrixXf &poses)
void addPhysicalProperties(const Character &character, const std::unordered_map< size_t, fbxsdk::FbxNode * > &jointToNodeMap)
void prependNamespaceToAllNodes(::fbxsdk::FbxNode *node, const std::string &namespacePrefix)
void createAnimationCurves(const Character &character, ::fbxsdk::FbxScene *scene, const std::vector<::fbxsdk::FbxNode * > &skeletonNodes, const MatrixXf &jointValues, const double framerate, const bool skipActiveJointParamCheck)
std::vector<::fbxsdk::FbxNode * > createMarkerNodes(::fbxsdk::FbxScene *scene, std::span< const std::vector< Marker > > markerSequence, const double framerate)
::fbxsdk::FbxAxisSystem::EUpVector toFbx(const FbxUpVector upVector)
void createBlendShapeAnimationCurves(::fbxsdk::FbxScene *scene, const std::vector<::fbxsdk::FbxBlendShapeChannel * > &channels, const MatrixXf &weights, const double framerate)
MatrixXf extractFaceExpressionWeights(const Character &character, const MatrixXf &poses)
void createLocatorNodes(const Character &character, ::fbxsdk::FbxScene *scene, const std::vector<::fbxsdk::FbxNode * > &skeletonNodes)
void createCollisionGeometryNodes(const Character &character, ::fbxsdk::FbxScene *scene, const std::vector<::fbxsdk::FbxNode * > &skeletonNodes)
SkeletonNodeResult createSkeletonNodes(const Character &character, ::fbxsdk::FbxScene *scene)
MeshBlendShapeResult createMeshNode(const Character &character, ::fbxsdk::FbxScene *scene, ::fbxsdk::FbxNode *parentNode, const std::unordered_map< size_t, fbxsdk::FbxNode * > &jointToNodeMap, Permissive permissive, const std::string &meshName="body_mesh")
void addMetaData(::fbxsdk::FbxNode *skeletonRootNode, const Character &character)
CharacterStateT< float > CharacterState
void saveFbxWithJointParams(const filesystem::path &, const Character &, const MatrixXf &, const double, std::span< const std::vector< Marker > >, const FileSaveOptions &)
Save a character with animation using joint parameters directly.
void saveFbxWithSkeletonStates(const filesystem::path &, const Character &, std::span< const SkeletonState >, const double, std::span< const std::vector< Marker > >, const FileSaveOptions &)
Save a character with animation using skeleton states directly.
std::tuple< Character, std::vector< MatrixXf >, float > loadOpenFbxCharacterWithMotion(std::span< const std::byte > inputData, KeepLocators keepLocators, Permissive permissive, LoadBlendShapes loadBlendShapes, bool stripNamespaces)
MarkerSequence loadOpenFbxMarkerSequence(std::span< const std::byte > inputData, bool stripNamespaces)
Load a MarkerSequence from an in-memory FBX byte buffer.
void saveFbx(const filesystem::path &, const Character &, const MatrixXf &, const VectorXf &, const double, std::span< const std::vector< Marker > >, const FileSaveOptions &)
Save a character with animation to an FBX file.
Character loadOpenFbxCharacter(const std::span< const std::byte > inputData, KeepLocators keepLocators, Permissive permissive, LoadBlendShapes loadBlendShapes, bool stripNamespaces)
Character loadFbxCharacter(const filesystem::path &inputPath, KeepLocators keepLocators, Permissive permissive, LoadBlendShapes loadBlendShapes, bool stripNamespaces)
std::tuple< Character, std::vector< MatrixXf >, float > loadFbxCharacterWithMotion(const filesystem::path &inputPath, KeepLocators keepLocators, Permissive permissive, LoadBlendShapes loadBlendShapes, bool stripNamespaces)
CharacterParametersT< float > CharacterParameters
JointParametersT< T > skeletonStateToJointParameters(const SkeletonStateT< T > &state, const Skeleton &skeleton)
The "RespectingActiveParameters" variants use rotationMatrixToOneAxisEuler or rotationMatrixToTwoAxis...
MarkerSequence loadFbxMarkerSequence(const filesystem::path &filename, bool stripNamespaces)
Loads a MarkerSequence from an FBX file.
VectorX< float > VectorXf
MatrixXf loadFbxBlendShapeWeights(const filesystem::path &)
Load per-frame blend shape animation weights from an FBX file.
void saveFbxModel(const filesystem::path &, const Character &, const FileSaveOptions &)
Save a character model (skeleton and mesh) without animation.
MatrixX< float > MatrixXf
A character model with skeletal structure, mesh, and optional components.
A struct containing the up, front vectors and coordinate system for FBX export.
Unified options for saving files in both FBX and GLTF formats.
All frames from a single motion capture sequence for one subject (actor).