flesh out scene loader

This commit is contained in:
yuni 2024-06-11 05:04:55 +02:00
parent 0047c4eda4
commit 2c1dacbf03
3 changed files with 28 additions and 11 deletions

View file

@ -12,7 +12,7 @@ use blend::Blend;
use std::fs::File;
use std::io::Write;
fn main() {
fn main() -> std::io::Result<()> {
let target = std::env::var("TARGET").unwrap();
if target.contains("windows") {
println!("cargo:warning=Embedding Windows Icon");
@ -21,14 +21,16 @@ fn main() {
let file = File::create("src/data/scenes.in");
if let Ok(mut file) = file {
generate_scenes(&mut file, "src/blender/suit_v2.blend");
generate_scenes(&mut file, "src/blender/cruiser.blend");
write!(&file, "[\n")?;
extract_scene(&mut file, "test", "src/blender/scene_test.blend")?;
write!(&file, "]\n")?;
}
Ok(())
}
fn generate_scenes(stream: &mut File, source_blend_file: &str) {
let blend = Blend::from_path(source_blend_file).expect("error loading blend file");
for obj in blend.root_instances() {
fn extract_scene(file: &mut File, scene_name: &str, blend_file: &str) -> std::io::Result<()> {
let blend = Blend::from_path(blend_file).expect("error loading blend file");
for obj in blend.instances_with_code(*b"OB") {
let loc: Vec<f32> = if obj.is_valid("loc") {
obj.get_f32_vec("loc")
} else {
@ -40,11 +42,20 @@ fn generate_scenes(stream: &mut File, source_blend_file: &str) {
vec![0.0, 0.0, 0.0]
};
let name = obj.get("id").get_string("name");
let result = write!(stream, "(\"{}\", {:?}, {:?}),\n", name, loc, rot);
if result.is_err() {
println!("{:?}", result);
break;
if let Some(name) = get_scene_object_name(name.as_str()) {
write!(file, "({scene_name:?}, {name:?}, {loc:?}, {rot:?}),\n")?;
}
}
Ok(())
}
fn get_scene_object_name(full_id: &str) -> Option<&str> {
let prefix = "OBLOAD=";
if full_id.starts_with(prefix) {
let remainder: &str = &full_id[prefix.len()..];
let parts: Vec<&str> = remainder.split('.').collect();
let name = parts[0];
return Some(name);
}
return None;
}

Binary file not shown.

6
src/data/scenes.in Normal file
View file

@ -0,0 +1,6 @@
[
("test", "cruiser", [0.0, 10.971298, 1.222765], [-0.27833763, 0.74558806, 0.31813696]),
("test", "cruiser", [-0.46667862, -1.2666901, -7.938822], [1.7932074, 0.10752687, 0.15762906]),
("test", "cruiser", [-7.409776, -1.4187636, 11.451159], [-0.27833763, 0.74558806, 0.31813696]),
("test", "cruiser", [6.8125467, -11.003204, 0.5323599], [-0.019556522, -1.0606266, -3.0019674]),
]