19#ifdef MOMENTUM_IO_WITH_USD
35[[nodiscard]]
CharacterFormat parseCharacterFormat(
const filesystem::path& filepath)
noexcept {
36 std::string ext = filepath.extension().string();
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")) {
53[[nodiscard]] std::optional<Character> loadCharacterByFormat(
55 const filesystem::path& filepath,
60 character.blendShape.reset();
61 character.faceExpressionBlendShape.reset();
67#ifdef MOMENTUM_IO_WITH_USD
70 MT_THROW(
"USD format is not supported in this build");
77[[nodiscard]] std::optional<Character> loadCharacterByFormatFromBuffer(
79 const std::span<const std::byte> fileBuffer,
84 character.blendShape.reset();
85 character.faceExpressionBlendShape.reset();
91#ifdef MOMENTUM_IO_WITH_USD
94 MT_THROW(
"USD format is not supported in this build");
104 const std::string& characterPath,
105 const std::string& parametersPath,
106 const std::string& locatorsPath,
109 const auto format = parseCharacterFormat(characterPath);
114 auto character = loadCharacterByFormat(format, characterPath, loadBlendShapes);
115 MT_THROW_IF(!character,
"Failed to load buffered character");
118 if (!parametersPath.empty()) {
124 if (!locatorsPath.empty()) {
125 (*character).locators =
126 loadLocators(locatorsPath, (*character).skeleton, (*character).parameterTransform);
129 return character.value();
134 const std::span<const std::byte> characterBuffer,
135 const std::span<const std::byte> paramBuffer,
136 const std::span<const std::byte> locBuffer,
139 auto character = loadCharacterByFormatFromBuffer(format, characterBuffer, loadBlendShapes);
140 MT_THROW_IF(!character,
"Failed to load buffered character");
143 if (!paramBuffer.empty()) {
149 if (!locBuffer.empty()) {
150 (*character).locators =
154 return character.value();
158 const filesystem::path& filename,
162 std::span<
const std::vector<Marker>> markerSequence,
165 const auto format = parseCharacterFormat(filename);
168 "Unknown character format for path: {}. Supported formats: .fbx, .glb, .gltf, .usd, .usda, .usdc",
183 filename, character, motion,
VectorXf(),
static_cast<double>(fps), markerSequence, options);
185#ifdef MOMENTUM_IO_WITH_USD
195 MT_THROW(
"USD format is not supported in this build");
199 "{} is not a supported format. Supported formats: .fbx, .glb, .gltf, .usd, .usda, .usdc",
205 const filesystem::path& filename,
208 std::span<const SkeletonState> skeletonStates,
209 std::span<
const std::vector<Marker>> markerSequence,
212 const auto format = parseCharacterFormat(filename);
215 "Unknown character format for path: {}. Supported formats: .fbx, .glb, .gltf, .usd, .usda, .usdc",
219 saveGltfCharacter(filename, character, fps, skeletonStates, markerSequence, options);
223 filename, character, skeletonStates,
static_cast<double>(fps), markerSequence, options);
225#ifdef MOMENTUM_IO_WITH_USD
226 saveUsdCharacter(filename, character, fps, skeletonStates, markerSequence, options);
228 MT_THROW(
"USD format is not supported in this build");
232 "{} is not a supported format. Supported formats: .fbx, .glb, .gltf, .usd, .usda, .usdc",
#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.
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.
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.
CharacterFormat
Represents the supported character formats.
@ Unknown
Unknown or unsupported file format.
Character loadFullCharacter(const std::string &characterPath, const std::string ¶metersPath, 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.
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 loadFbxCharacter(const filesystem::path &inputPath, KeepLocators keepLocators, Permissive permissive, LoadBlendShapes loadBlendShapes, bool stripNamespaces)
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.
Character loadUsdCharacter(const filesystem::path &inputPath)
Load a USD character from a local file path.
LocatorList loadLocators(const filesystem::path &filename, const Skeleton &skeleton, const ParameterTransform ¶meterTransform)
LocatorList loadLocatorsFromBuffer(std::span< const std::byte > rawData, const Skeleton &skeleton, const ParameterTransform ¶meterTransform)
Character loadGltfCharacter(const fx::gltf::Document &model)
Load a glTF character from a document.
VectorX< float > VectorXf
void loadParameters(std::unordered_map< std::string, std::string > ¶m, Character &character)
MatrixX< float > MatrixXf
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.
ParameterTransform parameterTransform
Maps model parameters to joint parameters.
Unified options for saving files in both FBX and GLTF formats.