|
| 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 | + |
| 8 | +/** |
| 9 | + * Export OBJs Example |
| 10 | + * |
| 11 | + * This example demonstrates how to export animation data from GLB/GLTF or FBX files |
| 12 | + * as per-frame OBJ mesh files. It loads a character with animation and exports each |
| 13 | + * frame as a separate OBJ file containing the deformed mesh geometry. |
| 14 | + * |
| 15 | + * Supported formats: |
| 16 | + * - GLB/GLTF: Loaded using loadCharacterWithMotion() |
| 17 | + * - FBX: Loaded using loadFbxCharacterWithMotion() |
| 18 | + * |
| 19 | + * Usage: |
| 20 | + * export_objs -i <input_file> -o <output_folder> [options] |
| 21 | + * |
| 22 | + * See README.md for detailed documentation. |
| 23 | + */ |
| 24 | + |
| 25 | +#include <momentum/character/character.h> |
| 26 | +#include <momentum/character/character_state.h> |
| 27 | +#include <momentum/common/log.h> |
| 28 | +#include <momentum/io/fbx/fbx_io.h> |
| 29 | +#include <momentum/io/gltf/gltf_io.h> |
| 30 | +#include <momentum/math/mesh.h> |
| 31 | + |
| 32 | +#include <CLI/CLI.hpp> |
| 33 | +#include <fmt/format.h> |
| 34 | + |
| 35 | +#include <filesystem> |
| 36 | + |
| 37 | +using namespace momentum; |
| 38 | + |
| 39 | +namespace { |
| 40 | + |
| 41 | +struct Options { |
| 42 | + std::string inputFile; |
| 43 | + std::string outputFolder; |
| 44 | + size_t firstFrame = 0; |
| 45 | + int lastFrame = -1; |
| 46 | + size_t stride = 1; |
| 47 | +}; |
| 48 | + |
| 49 | +std::shared_ptr<Options> setupOptions(CLI::App& app) { |
| 50 | + auto opt = std::make_shared<Options>(); |
| 51 | + app.add_option("-i,--input", opt->inputFile, "Path to the input animation file (.fbx/.glb).") |
| 52 | + ->required() |
| 53 | + ->check(CLI::ExistingFile); |
| 54 | + app.add_option("-o,--output", opt->outputFolder, "Path to the output folder.")->required(); |
| 55 | + app.add_option("--first", opt->firstFrame, "First frame in the motion to start obj export.") |
| 56 | + ->default_val(opt->firstFrame) |
| 57 | + ->check(CLI::NonNegativeNumber); |
| 58 | + app.add_option( |
| 59 | + "--last", |
| 60 | + opt->lastFrame, |
| 61 | + "Last frame in the motion to export (inclusive). -1 to indicate the last frame in the motion.") |
| 62 | + ->default_val(opt->lastFrame); |
| 63 | + app.add_option("--stride", opt->stride, "Frame stride when exporting data.") |
| 64 | + ->default_val(opt->stride) |
| 65 | + ->check(CLI::PositiveNumber); |
| 66 | + |
| 67 | + return opt; |
| 68 | +} |
| 69 | + |
| 70 | +// A simple obj export function as an example to avoid external deps. |
| 71 | +int saveObj(const std::string& filename, const Mesh* mesh) { |
| 72 | + std::ofstream file(filename); |
| 73 | + if (!file.is_open()) { |
| 74 | + MT_LOGE("Failed to open {} for writing", filename); |
| 75 | + return -1; |
| 76 | + } |
| 77 | + |
| 78 | + // Simple info |
| 79 | + file << "# " << mesh->vertices.size() << " vertices; " << mesh->faces.size() << " faces" |
| 80 | + << std::endl; |
| 81 | + |
| 82 | + // Vertex positions |
| 83 | + for (const auto& v : mesh->vertices) { |
| 84 | + file << "v " << v(0) << " " << v(1) << " " << v(2) << std::endl; |
| 85 | + } |
| 86 | + file << std::endl; |
| 87 | + |
| 88 | + // Faces -- our mesh is triangle only. |
| 89 | + // NOTE: obj vertex indices is 1-based not 0-based. |
| 90 | + for (const auto& f : mesh->faces) { |
| 91 | + file << "f " << 1 + f(0) << " " << 1 + f(1) << " " << 1 + f(2) << std::endl; |
| 92 | + } |
| 93 | + file.close(); |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace |
| 98 | + |
| 99 | +int main(int argc, char* argv[]) try { |
| 100 | + CLI::App app("Export objs app"); |
| 101 | + auto opts = setupOptions(app); |
| 102 | + CLI11_PARSE(app, argc, argv); |
| 103 | + |
| 104 | + // Determine file type by extension |
| 105 | + const std::string extension = std::filesystem::path(opts->inputFile).extension().string(); |
| 106 | + const bool isGlb = (extension == ".glb" || extension == ".gltf"); |
| 107 | + const bool isFbx = (extension == ".fbx"); |
| 108 | + |
| 109 | + if (!isGlb && !isFbx) { |
| 110 | + MT_LOGE("Unsupported file format: {}. Only .glb, .gltf, and .fbx are supported.", extension); |
| 111 | + return EXIT_FAILURE; |
| 112 | + } |
| 113 | + |
| 114 | + Character character; |
| 115 | + MatrixXf motion; // For GLB: single motion matrix |
| 116 | + std::vector<MatrixXf> motions; // For FBX: vector of motion matrices |
| 117 | + VectorXf id; // Identity parameters (GLB only) |
| 118 | + float fps = 0.0f; |
| 119 | + |
| 120 | + // Load character and motion based on file type |
| 121 | + if (isGlb) { |
| 122 | + MT_LOGI("Loading GLB/GLTF file: {}", opts->inputFile); |
| 123 | + std::tie(character, motion, id, fps) = loadCharacterWithMotion(opts->inputFile); |
| 124 | + } else { |
| 125 | + MT_LOGI("Loading FBX file: {}", opts->inputFile); |
| 126 | + std::tie(character, motions, fps) = loadFbxCharacterWithMotion(opts->inputFile); |
| 127 | + // Convert vector of motions to single motion matrix if needed |
| 128 | + if (!motions.empty() && motions[0].cols() > 0) { |
| 129 | + motion = motions[0]; // Use first motion |
| 130 | + if (motions.size() > 1) { |
| 131 | + MT_LOGW("FBX file contains {} motions. Using only the first one.", motions.size()); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if (character.mesh == nullptr) { |
| 137 | + MT_LOGW("No mesh found in the input; exit without saving."); |
| 138 | + return EXIT_SUCCESS; |
| 139 | + } |
| 140 | + |
| 141 | + // Check and create the output folder if needed |
| 142 | + if (!std::filesystem::is_directory(opts->outputFolder)) { |
| 143 | + MT_LOGI("Create output folder {}", opts->outputFolder); |
| 144 | + std::filesystem::create_directories(opts->outputFolder); |
| 145 | + } |
| 146 | + |
| 147 | + const size_t numFrames = motion.cols(); |
| 148 | + |
| 149 | + if (numFrames == 0) { |
| 150 | + // Export the template mesh (no animation) |
| 151 | + MT_LOGI("No animation data found. Exporting template mesh."); |
| 152 | + const std::string outFile = fmt::format( |
| 153 | + "{}/{}.obj", |
| 154 | + opts->outputFolder, |
| 155 | + std::filesystem::path(opts->inputFile).filename().stem().string()); |
| 156 | + if (saveObj(outFile, character.mesh.get()) == 0) { |
| 157 | + return EXIT_SUCCESS; |
| 158 | + } else { |
| 159 | + return EXIT_FAILURE; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // Export animation sequence |
| 164 | + MT_LOGI("Exporting animation with {} frames at {} fps", numFrames, fps); |
| 165 | + |
| 166 | + // Apply frame range options |
| 167 | + const size_t startFrame = opts->firstFrame; |
| 168 | + const size_t endFrame = (opts->lastFrame < 0) |
| 169 | + ? numFrames |
| 170 | + : std::min(static_cast<size_t>(opts->lastFrame + 1), numFrames); |
| 171 | + |
| 172 | + if (startFrame >= numFrames) { |
| 173 | + MT_LOGE("First frame ({}) is out of range (total frames: {})", startFrame, numFrames); |
| 174 | + return EXIT_FAILURE; |
| 175 | + } |
| 176 | + |
| 177 | + CharacterState state(character); |
| 178 | + CharacterParameters params; |
| 179 | + if (isGlb && id.size() > 0) { |
| 180 | + // id is in JointParameters. It is constant for the character and not time-varying. |
| 181 | + params.offsets = id; |
| 182 | + } else if (isFbx) { |
| 183 | + params.pose.v.setZero(character.parameterTransform.numAllModelParameters()); // should be zero |
| 184 | + } |
| 185 | + |
| 186 | + size_t exportCount = 0; |
| 187 | + for (size_t iFrame = startFrame; iFrame < endFrame; iFrame += opts->stride) { |
| 188 | + // Here is the tricky part: for glb files, we store model parameters in a custom plugin and |
| 189 | + // read it back in. So the motion matrix we read from glb is of ModelParameters type. For fbx |
| 190 | + // files, we do not store custom information, so we can only read back joint parameters. The |
| 191 | + // motion matrix we read from fbx is of JointParameters type. We will need to handle them |
| 192 | + // differently. |
| 193 | + if (isGlb) { |
| 194 | + params.pose = motion.col(iFrame); |
| 195 | + } else { |
| 196 | + params.offsets = motion.col(iFrame); |
| 197 | + } |
| 198 | + state.set(params, character, true, false, false); |
| 199 | + const std::string outFile = fmt::format("{}/{:05}.obj", opts->outputFolder, exportCount); |
| 200 | + if (saveObj(outFile, state.meshState.get()) == 0) { |
| 201 | + exportCount++; |
| 202 | + } |
| 203 | + } |
| 204 | + MT_LOGI("Exported {} frames to {}", exportCount, opts->outputFolder); |
| 205 | + return EXIT_SUCCESS; |
| 206 | +} catch (std::exception& e) { |
| 207 | + MT_LOGE("Exception thrown {}", e.what()); |
| 208 | + return EXIT_FAILURE; |
| 209 | +} catch (...) { |
| 210 | + MT_LOGE("Unknown exception."); |
| 211 | + return EXIT_FAILURE; |
| 212 | +} |
0 commit comments