Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
character_io.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
19#ifdef MOMENTUM_IO_WITH_USD
21#endif
22#include "momentum/math/fwd.h"
23
24#include <algorithm>
25#include <optional>
26
27namespace momentum {
28
29namespace {
30
35[[nodiscard]] CharacterFormat parseCharacterFormat(const filesystem::path& filepath) noexcept {
36 std::string ext = filepath.extension().string();
37 if (ext.empty()) {
39 }
40
41 std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
42 if ((ext == ".glb") || (ext == ".gltf")) {
44 } else if (ext == ".fbx") {
46 } else if ((ext == ".usd") || (ext == ".usda") || (ext == ".usdc")) {
48 } else {
50 }
51}
52
53[[nodiscard]] std::optional<Character> loadCharacterByFormat(
54 const CharacterFormat format,
55 const filesystem::path& filepath,
56 LoadBlendShapes loadBlendShapes = LoadBlendShapes::No) {
57 if (format == CharacterFormat::Gltf) {
58 auto character = loadGltfCharacter(filepath);
59 if (loadBlendShapes == LoadBlendShapes::No) {
60 character.blendShape.reset();
61 character.faceExpressionBlendShape.reset();
62 }
63 return character;
64 } else if (format == CharacterFormat::Fbx) {
65 return loadFbxCharacter(filepath, KeepLocators::Yes, Permissive::No, loadBlendShapes);
66 } else if (format == CharacterFormat::Usd) {
67#ifdef MOMENTUM_IO_WITH_USD
68 return loadUsdCharacter(filepath);
69#else
70 MT_THROW("USD format is not supported in this build");
71#endif
72 } else {
73 return {};
74 }
75}
76
77[[nodiscard]] std::optional<Character> loadCharacterByFormatFromBuffer(
78 const CharacterFormat format,
79 const std::span<const std::byte> fileBuffer,
80 LoadBlendShapes loadBlendShapes = LoadBlendShapes::No) {
81 if (format == CharacterFormat::Gltf) {
82 auto character = loadGltfCharacter(fileBuffer);
83 if (loadBlendShapes == LoadBlendShapes::No) {
84 character.blendShape.reset();
85 character.faceExpressionBlendShape.reset();
86 }
87 return character;
88 } else if (format == CharacterFormat::Fbx) {
89 return loadFbxCharacter(fileBuffer, KeepLocators::Yes, Permissive::No, loadBlendShapes);
90 } else if (format == CharacterFormat::Usd) {
91#ifdef MOMENTUM_IO_WITH_USD
92 return loadUsdCharacter(fileBuffer);
93#else
94 MT_THROW("USD format is not supported in this build");
95#endif
96 } else {
97 return {};
98 }
99}
100
101} // namespace
102
104 const std::string& characterPath,
105 const std::string& parametersPath,
106 const std::string& locatorsPath,
107 LoadBlendShapes loadBlendShapes) {
108 // Parse format
109 const auto format = parseCharacterFormat(characterPath);
111 format == CharacterFormat::Unknown, "Unknown character format for path: {}", characterPath);
112
113 // Load character
114 auto character = loadCharacterByFormat(format, characterPath, loadBlendShapes);
115 MT_THROW_IF(!character, "Failed to load buffered character");
116
117 // load parameter transform
118 if (!parametersPath.empty()) {
119 auto def = loadMomentumModel(parametersPath);
120 loadParameters(def, *character);
121 }
122
123 // Load locators
124 if (!locatorsPath.empty()) {
125 (*character).locators =
126 loadLocators(locatorsPath, (*character).skeleton, (*character).parameterTransform);
127 }
128
129 return character.value();
130}
131
133 CharacterFormat format,
134 const std::span<const std::byte> characterBuffer,
135 const std::span<const std::byte> paramBuffer,
136 const std::span<const std::byte> locBuffer,
137 LoadBlendShapes loadBlendShapes) {
138 // Load character
139 auto character = loadCharacterByFormatFromBuffer(format, characterBuffer, loadBlendShapes);
140 MT_THROW_IF(!character, "Failed to load buffered character");
141
142 // load parameter transform
143 if (!paramBuffer.empty()) {
144 auto def = loadMomentumModelFromBuffer(paramBuffer);
145 loadParameters(def, *character);
146 }
147
148 // Load locators
149 if (!locBuffer.empty()) {
150 (*character).locators =
151 loadLocatorsFromBuffer(locBuffer, (*character).skeleton, (*character).parameterTransform);
152 }
153
154 return character.value();
155}
156
158 const filesystem::path& filename,
159 const Character& character,
160 const float fps,
161 const MatrixXf& motion,
162 std::span<const std::vector<Marker>> markerSequence,
163 const FileSaveOptions& options) {
164 // Parse format from file extension
165 const auto format = parseCharacterFormat(filename);
167 format == CharacterFormat::Unknown,
168 "Unknown character format for path: {}. Supported formats: .fbx, .glb, .gltf, .usd, .usda, .usdc",
169 filename.string());
170
171 if (format == CharacterFormat::Gltf) {
173 filename,
174 character,
175 fps,
176 {character.parameterTransform.name, motion},
177 {},
178 markerSequence,
179 options);
180 } else if (format == CharacterFormat::Fbx) {
181 // Save as FBX
182 saveFbx(
183 filename, character, motion, VectorXf(), static_cast<double>(fps), markerSequence, options);
184 } else if (format == CharacterFormat::Usd) {
185#ifdef MOMENTUM_IO_WITH_USD
187 filename,
188 character,
189 fps,
190 {character.parameterTransform.name, motion},
191 {},
192 markerSequence,
193 options);
194#else
195 MT_THROW("USD format is not supported in this build");
196#endif
197 } else {
198 MT_THROW(
199 "{} is not a supported format. Supported formats: .fbx, .glb, .gltf, .usd, .usda, .usdc",
200 filename.string());
201 }
202}
203
205 const filesystem::path& filename,
206 const Character& character,
207 const float fps,
208 std::span<const SkeletonState> skeletonStates,
209 std::span<const std::vector<Marker>> markerSequence,
210 const FileSaveOptions& options) {
211 // Parse format from file extension
212 const auto format = parseCharacterFormat(filename);
214 format == CharacterFormat::Unknown,
215 "Unknown character format for path: {}. Supported formats: .fbx, .glb, .gltf, .usd, .usda, .usdc",
216 filename.string());
217
218 if (format == CharacterFormat::Gltf) {
219 saveGltfCharacter(filename, character, fps, skeletonStates, markerSequence, options);
220 } else if (format == CharacterFormat::Fbx) {
221 // Save as FBX
223 filename, character, skeletonStates, static_cast<double>(fps), markerSequence, options);
224 } else if (format == CharacterFormat::Usd) {
225#ifdef MOMENTUM_IO_WITH_USD
226 saveUsdCharacter(filename, character, fps, skeletonStates, markerSequence, options);
227#else
228 MT_THROW("USD format is not supported in this build");
229#endif
230 } else {
231 MT_THROW(
232 "{} is not a supported format. Supported formats: .fbx, .glb, .gltf, .usd, .usda, .usdc",
233 filename.string());
234 }
235}
236
237} // namespace momentum
#define MT_THROW(...)
Throws a std::runtime_error with a formatted message.
Definition exception.h:31
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
Definition exception.h:55
void saveCharacter(const filesystem::path &filename, const Character &character, const float fps, const MatrixXf &motion, std::span< const std::vector< Marker > > markerSequence, const FileSaveOptions &options)
High level function to save a character with motion and markers to any supported format.
void saveUsdCharacter(const filesystem::path &filename, const Character &character, float fps, std::span< const SkeletonState > skeletonStates, std::span< const std::vector< Marker > > markerSequence, const FileSaveOptions &options)
Save a character with skeleton state animation to a USD file.
Definition usd_io.cpp:651
std::unordered_map< std::string, std::string > loadMomentumModel(const filesystem::path &filename)
void saveGltfCharacter(const filesystem::path &filename, const Character &character, const float fps, const MotionParameters &motion, const IdentityParameters &offsets, std::span< const std::vector< Marker > > markerSequence, const FileSaveOptions &options, std::span< const int64_t > timestamps)
Save a character with motion to a glTF file.
Definition gltf_io.cpp:493
CharacterFormat
Represents the supported character formats.
@ Fbx
FBX file format.
@ Unknown
Unknown or unsupported file format.
@ Usd
USD file format.
@ Gltf
glTF file format.
Character loadFullCharacter(const std::string &characterPath, const std::string &parametersPath, const std::string &locatorsPath, LoadBlendShapes loadBlendShapes)
High level function to load a character of any type, with a local path.
std::unordered_map< std::string, std::string > loadMomentumModelFromBuffer(std::span< const std::byte > buffer)
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.
Definition fbx_io.cpp:447
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.
Definition fbx_io.cpp:424
Character loadFbxCharacter(const filesystem::path &inputPath, KeepLocators keepLocators, Permissive permissive, LoadBlendShapes loadBlendShapes, bool stripNamespaces)
Definition fbx_io.cpp:178
void saveUsdCharacterWithMotion(const filesystem::path &filename, const Character &character, float fps, const MotionParameters &motion, const IdentityParameters &offsets, std::span< const std::vector< Marker > > markerSequence, const FileSaveOptions &options)
Save a character with model-parameter motion to a USD file.
Definition usd_io.cpp:674
Character loadUsdCharacter(const filesystem::path &inputPath)
Load a USD character from a local file path.
Definition usd_io.cpp:595
LocatorList loadLocators(const filesystem::path &filename, const Skeleton &skeleton, const ParameterTransform &parameterTransform)
LocatorList loadLocatorsFromBuffer(std::span< const std::byte > rawData, const Skeleton &skeleton, const ParameterTransform &parameterTransform)
LoadBlendShapes
Definition fbx_io.h:26
Character loadGltfCharacter(const fx::gltf::Document &model)
Load a glTF character from a document.
Definition gltf_io.cpp:256
VectorX< float > VectorXf
Definition types.h:209
void loadParameters(std::unordered_map< std::string, std::string > &param, Character &character)
MatrixX< float > MatrixXf
Definition types.h:82
Character loadFullCharacterFromBuffer(CharacterFormat format, const std::span< const std::byte > characterBuffer, const std::span< const std::byte > paramBuffer, const std::span< const std::byte > locBuffer, LoadBlendShapes loadBlendShapes)
Buffer version of loadFullCharacter function, supports .glb and .fbx file formats.
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
ParameterTransform parameterTransform
Maps model parameters to joint parameters.
Definition character.h:39
Unified options for saving files in both FBX and GLTF formats.
std::vector< std::string > name
The list of model parameter names.