Skip to content

Commit

Permalink
Merge pull request #687 from AmbientRun/assets-import-helper
Browse files Browse the repository at this point in the history
add anim import helper and avoid repeat import
  • Loading branch information
chaosprint authored Aug 14, 2023
2 parents 1e2057a + c97d408 commit 5ed10d6
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 18 deletions.
8 changes: 7 additions & 1 deletion app/src/cli/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct ImportOptions {
pub collider_from_model: bool,
}

pub async fn handle(command: &AssetCommand) -> anyhow::Result<()> {
pub async fn handle(command: &AssetCommand, assets: &crate::AssetCache) -> anyhow::Result<()> {
match command {
AssetCommand::MigratePipelinesToml(opt) => {
let path = ProjectPath::new_local(opt.path.clone())?;
Expand All @@ -59,6 +59,12 @@ pub async fn handle(command: &AssetCommand) -> anyhow::Result<()> {
} else {
anyhow::bail!("Unsupported file type");
}
ambient_build::build_assets(
&assets,
&PathBuf::from("assets"),
&PathBuf::from("build"),
)
.await?;
}
None => anyhow::bail!("Unknown file type"),
},
Expand Down
8 changes: 5 additions & 3 deletions app/src/cli/new_project_template/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/target
/build
/tmp
**/target
**/build
**/tmp
**/.DS_Store
**/data
2 changes: 1 addition & 1 deletion app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn main() -> anyhow::Result<()> {
}

if let Commands::Assets { command } = &cli.command {
return rt.block_on(cli::assets::handle(command));
return rt.block_on(cli::assets::handle(command, &assets));
}

// Build the project if required. Note that this only runs if the project is local,
Expand Down
29 changes: 27 additions & 2 deletions crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn get_asset_files(assets_path: &Path) -> impl Iterator<Item = PathBuf> {
.map(|x| x.into_path())
}

async fn build_assets(
pub async fn build_assets(
assets: &AssetCache,
assets_path: &Path,
build_path: &Path,
Expand All @@ -209,6 +209,9 @@ async fn build_assets(

let has_errored = Arc::new(AtomicBool::new(false));

let anim_files = Arc::new(parking_lot::Mutex::new(vec![]));
let anim_files_clone = anim_files.clone();

let ctx = ProcessCtx {
assets: assets.clone(),
files: FileCollection(Arc::new(files)),
Expand All @@ -220,6 +223,15 @@ async fn build_assets(
let build_path = build_path.to_owned();
move |path, contents| {
let path = build_path.join("assets").join(path);
if let Some(ext) = path.extension() {
if ext == "anim" {
if !anim_files_clone.lock().contains(&path) {
anim_files_clone.lock().push(path.clone());
} else {
println!("๐Ÿค” repeated importing; please check the pipeline.toml");
}
}
}
async move {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
tokio::fs::write(&path, contents).await.unwrap();
Expand Down Expand Up @@ -247,7 +259,20 @@ async fn build_assets(
pipelines::process_pipelines(&ctx)
.await
.with_context(|| format!("Failed to process pipelines for {assets_path:?}"))?;

if !anim_files.lock().is_empty() {
println!("๐Ÿ† Available animation files: {:?}", anim_files.lock());
println!("๐Ÿง‚ You can use the animation files like this:");
println!("```rust");
for path in anim_files.lock().iter() {
println!(
"PlayClipFromUrlNode::new(assets::url({}));",
format!("{:?}", path).replace("build/assets/", "")
);
}
println!("```");
println!("๐Ÿ“˜ Learn more about animations:");
println!("๐Ÿ”— https://ambientrun.github.io/Ambient/reference/animations.html");
}
if has_errored.load(Ordering::SeqCst) {
anyhow::bail!("Failed to build assets");
}
Expand Down
22 changes: 16 additions & 6 deletions crates/build/src/pipelines/importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ pub fn import_audio(path: PathBuf, convert: bool) -> anyhow::Result<()> {
let current_dir = std::env::current_dir().context("Error getting current directory")?;
let asset_folder_path = current_dir.join("assets");
let tomlpath = current_dir.join("assets/pipeline.toml");
println!("Importing audio...");
println!("Read more about audio usage here: https://ambientrun.github.io/Ambient/reference/audio.html");

if !Path::new(&asset_folder_path).exists() {
std::fs::create_dir_all(&asset_folder_path)?;
}
Expand Down Expand Up @@ -47,7 +46,13 @@ pub fn import_audio(path: PathBuf, convert: bool) -> anyhow::Result<()> {
String::from("sources"),
Value::Array(vec![Value::String(filename_with_ext)]),
);

if pipelines.contains(&Value::Table(new_pipeline.clone())) {
println!("\n๐Ÿšจ This audio file is already imported\n");
return Ok(());
}
println!("\n๐Ÿ‘‰ Importing audio...");
println!("๐Ÿ“˜ Read more about audio import here:");
println!("๐Ÿ”— https://ambientrun.github.io/Ambient/reference/audio.html\n");
pipelines.push(Value::Table(new_pipeline));
} else {
panic!("Expected table at the root of the TOML document");
Expand All @@ -69,8 +74,7 @@ pub fn import_model(path: PathBuf, collider_from_model: bool) -> anyhow::Result<
let current_dir = std::env::current_dir().context("Error getting current directory")?;
let asset_folder_path = current_dir.join("assets");
let tomlpath = current_dir.join("assets/pipeline.toml");
println!("Importing model...");
println!("Read more about model import here: https://ambientrun.github.io/Ambient/reference/asset_pipeline.html");

if !Path::new(&asset_folder_path).exists() {
std::fs::create_dir_all(&asset_folder_path)?;
}
Expand Down Expand Up @@ -119,7 +123,13 @@ pub fn import_model(path: PathBuf, collider_from_model: bool) -> anyhow::Result<
String::from("sources"),
Value::Array(vec![Value::String(filename_with_ext)]),
);

if pipelines.contains(&Value::Table(new_pipeline.clone())) {
println!("\n๐Ÿšจ This model file is already imported\n");
return Ok(());
}
println!("\n๐Ÿ‘‰ Importing model...");
println!("๐Ÿ“˜ Read more about model import here:");
println!("๐Ÿ”— https://ambientrun.github.io/Ambient/reference/asset_pipeline.html\n");
pipelines.push(Value::Table(new_pipeline));
}

Expand Down
62 changes: 57 additions & 5 deletions web/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5ed10d6

Please sign in to comment.