Skip to content

Commit

Permalink
Add command line option for importing character anims from gltf
Browse files Browse the repository at this point in the history
  • Loading branch information
PikminGuts92 committed Feb 8, 2025
1 parent c322d97 commit ced0814
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
34 changes: 34 additions & 0 deletions apps/cli/mesh_tool/src/apps/anim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::apps::SubApp;
use clap::Parser;

use std::error::Error;

use grim::{Platform, SystemInfo};
use grim::model::*;
use grim::io::*;

#[derive(Parser, Debug)]
pub struct AnimApp {
#[arg(help = "Path to input animation file (.gltf)", required = true)]
pub anim_path: String,
#[arg(help = "Path to output directory", required = true)]
pub output_path: String,
}

// TODO: Get from args
const SYSTEM_INFO: SystemInfo = SystemInfo {
version: 25,
platform: Platform::X360,
endian: IOEndian::Little,
};

impl SubApp for AnimApp {
fn process(&mut self) -> Result<(), Box<dyn Error>> {
let importer = GltfImporter2::new(&self.anim_path)?;
let assets = importer.process();

assets.dump_to_directory(&self.output_path, &SYSTEM_INFO)?;

Ok(())
}
}
5 changes: 5 additions & 0 deletions apps/cli/mesh_tool/src/apps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::error::Error;

use grim::SystemInfo;

mod anim;
mod milo2gltf;
mod model2group;
use self::anim::*;
use self::model2group::*;
use self::milo2gltf::*;

Expand All @@ -25,6 +27,8 @@ struct Options {

#[derive(Subcommand, Debug)]
enum SubCommand {
#[command(name = "anim", about = "Import character anims from gltf")]
Anim(AnimApp),
#[command(name = "milo2gltf", about = "Convert milo to gltf")]
Milo2Gltf(Milo2GltfApp),
#[command(name = "model2group", about = "Convert model to milo group")]
Expand All @@ -45,6 +49,7 @@ impl MeshTool {

pub fn run(&mut self) -> Result<(), Box<dyn Error>> {
match &mut self.options.commands {
SubCommand::Anim(app) => app.process(),
SubCommand::Milo2Gltf(app) => app.process(),
SubCommand::Model2Group(app) => app.process()
}
Expand Down
18 changes: 18 additions & 0 deletions core/grim/src/model/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use gltf::mesh::util::*;
use gltf::json::extensions::scene::*;
use gltf::json::extensions::mesh::*;
use gltf::scene::Node;
use crate::SystemInfo;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

Expand All @@ -30,6 +31,23 @@ pub struct MiloAssets {
char_clip_samples: Vec<CharClipSamples>,
}

impl MiloAssets {
pub fn dump_to_directory<T>(&self, out_dir: T, info: &SystemInfo) -> Result<(), Box<dyn std::error::Error>> where T: AsRef<Path> {
// Create output dir
super::create_dir_if_not_exists(&out_dir)?;

for char_clip in self.char_clip_samples.iter() {
let char_clip_dir = out_dir.as_ref().join("CharClipSamples");
super::create_dir_if_not_exists(&char_clip_dir)?;

let char_clip_path = char_clip_dir.join(&char_clip.name);
save_to_file(char_clip, &char_clip_path, info)?;
}

Ok(())
}
}

impl GltfImporter2 {
pub fn new<T>(source_path: T) -> Result<Self, GltfError> where T: AsRef<Path> {
let (document, buffers, images) = gltf::import(&source_path)?;
Expand Down

0 comments on commit ced0814

Please sign in to comment.