From 86023834451f1794845a1f453af031be58772594 Mon Sep 17 00:00:00 2001 From: "kaosat.dev" Date: Wed, 10 Jul 2024 09:01:45 +0200 Subject: [PATCH] feat(Blenvy:Bevy): * fixed huge logical error in component processing (require Name components) that was breaking scene level extras/components * added a convience from_path function to BlueprintInfos to generate name from path * minor tweaks --- .../src/blueprints/spawn_from_blueprints.rs | 26 ++++++++++++--- .../components/blender_settings/lighting.rs | 1 + crates/blenvy/src/components/process_gltfs.rs | 32 +++++++++++-------- testing/bevy_example/src/game/in_game.rs | 20 ++---------- testing/bevy_example/src/hierarchy_debug.rs | 21 +++++++----- tools/blenvy/TODO.md | 6 ++-- 6 files changed, 60 insertions(+), 46 deletions(-) diff --git a/crates/blenvy/src/blueprints/spawn_from_blueprints.rs b/crates/blenvy/src/blueprints/spawn_from_blueprints.rs index c209e1e..771611a 100644 --- a/crates/blenvy/src/blueprints/spawn_from_blueprints.rs +++ b/crates/blenvy/src/blueprints/spawn_from_blueprints.rs @@ -22,6 +22,17 @@ pub struct BlueprintInfo { pub name: String, pub path: String, } +use std::path::Path; + +impl BlueprintInfo { + pub fn from_path(path: &str) -> BlueprintInfo { + let p = Path::new(&path); + return BlueprintInfo { + name: p.file_stem().unwrap().to_os_string().into_string().unwrap(), // seriously ? , also unwraps !! + path: path.into(), + }; + } +} /// flag component needed to signify the intent to spawn a Blueprint #[derive(Component, Reflect, Default, Debug)] @@ -118,11 +129,14 @@ Overview of the Blueprint Spawning process */ pub(crate) fn blueprints_prepare_spawn( - blueprint_instances_to_spawn: Query<(Entity, &BlueprintInfo), Added>, + blueprint_instances_to_spawn: Query< + (Entity, &BlueprintInfo, Option<&Name>), + Added, + >, mut commands: Commands, asset_server: Res, ) { - for (entity, blueprint_info) in blueprint_instances_to_spawn.iter() { + for (entity, blueprint_info, entity_name) in blueprint_instances_to_spawn.iter() { info!( "BLUEPRINT: to spawn detected: {:?} path:{:?}", blueprint_info.name, blueprint_info.path @@ -152,7 +166,6 @@ pub(crate) fn blueprints_prepare_spawn( for scene in gltf.scenes() { let scene_extras = scene.extras().clone().unwrap(); let lookup: HashMap = serde_json::from_str(&scene_extras.get()).unwrap(); - if lookup.contains_key("BlueprintAssets") { let assets_raw = &lookup["BlueprintAssets"]; //println!("ASSETS RAW {}", assets_raw); @@ -191,8 +204,13 @@ pub(crate) fn blueprints_prepare_spawn( } else { commands.entity(entity).insert(BlueprintAssetsLoaded); } + + // if the entity has no name, add one based on the blueprint's + commands + .entity(entity) + .insert(bevy::prelude::Name::from(blueprint_info.name.clone())); // add the blueprint spawning marker - commands.entity(entity).insert(BlueprintSpawning); + commands.entity(entity).insert((BlueprintSpawning)); } } diff --git a/crates/blenvy/src/components/blender_settings/lighting.rs b/crates/blenvy/src/components/blender_settings/lighting.rs index c776212..4f3bc7f 100644 --- a/crates/blenvy/src/components/blender_settings/lighting.rs +++ b/crates/blenvy/src/components/blender_settings/lighting.rs @@ -155,6 +155,7 @@ fn process_colorgrading( ) { for entity in cameras.iter() { for (scene_id, blender_colorgrading) in blender_colorgradings.iter() { + info!("COLOR GRADING"); commands.entity(entity).insert(ColorGrading { global: ColorGradingGlobal { exposure: blender_colorgrading.exposure, diff --git a/crates/blenvy/src/components/process_gltfs.rs b/crates/blenvy/src/components/process_gltfs.rs index 2493780..4e5b176 100644 --- a/crates/blenvy/src/components/process_gltfs.rs +++ b/crates/blenvy/src/components/process_gltfs.rs @@ -18,7 +18,7 @@ use crate::{ronstring_to_reflect_component, GltfProcessed}; // , mut entity_components: HashMap, TypeRegistration)>> fn find_entity_components( entity: Entity, - name: &Name, + name: Option<&Name>, parent: Option<&Parent>, reflect_components: Vec<(Box, TypeRegistration)>, entity_components: &HashMap, TypeRegistration)>>, @@ -27,10 +27,13 @@ fn find_entity_components( let mut target_entity = entity; // if the node contains "components" or ends with "_pa" (ie add to parent), the components will not be added to the entity itself but to its parent // this is mostly used for Blender collections - if parent.is_some() && (name.as_str().contains("components") || name.as_str().ends_with("_pa")) - { - debug!("adding components to parent"); - target_entity = parent.expect("the target entity had a parent ").get(); + if parent.is_some() { + if let Some(name) = name { + if name.as_str().contains("components") || name.as_str().ends_with("_pa") { + debug!("adding components to parent"); + target_entity = parent.expect("the target entity had a parent ").get(); + } + } } debug!("adding to {:?}", target_entity); @@ -54,10 +57,10 @@ fn find_entity_components( /// main function: injects components into each entity in gltf files that have `gltf_extras`, using reflection pub fn add_components_from_gltf_extras(world: &mut World) { - let mut extras = world.query_filtered::<(Entity, &Name, &GltfExtras, Option<&Parent>), (Added, Without)>(); - let mut scene_extras = world.query_filtered::<(Entity, &Name, &GltfSceneExtras, Option<&Parent>), (Added, Without)>(); - let mut mesh_extras = world.query_filtered::<(Entity, &Name, &GltfMeshExtras, Option<&Parent>), (Added, Without)>(); - let mut material_extras = world.query_filtered::<(Entity, &Name, &GltfMaterialExtras, Option<&Parent>), (Added, Without)>(); + let mut extras = world.query_filtered::<(Entity, Option<&Name>, &GltfExtras, Option<&Parent>), (Added, Without)>(); + let mut scene_extras = world.query_filtered::<(Entity, Option<&Name>, &GltfSceneExtras, Option<&Parent>), (Added, Without)>(); + let mut mesh_extras = world.query_filtered::<(Entity, Option<&Name>, &GltfMeshExtras, Option<&Parent>), (Added, Without)>(); + let mut material_extras = world.query_filtered::<(Entity, Option<&Name>, &GltfMaterialExtras, Option<&Parent>), (Added, Without)>(); let mut entity_components: HashMap, TypeRegistration)>> = HashMap::new(); @@ -66,13 +69,14 @@ pub fn add_components_from_gltf_extras(world: &mut World) { for (entity, name, extra, parent) in extras.iter(world) { debug!( - "Name: {}, entity {:?}, parent: {:?}, extras {:?}", + "Gltf Extra: Name: {:?}, entity {:?}, parent: {:?}, extras {:?}", name, entity, parent, extra ); let type_registry: &AppTypeRegistry = world.resource(); let type_registry = type_registry.read(); let reflect_components = ronstring_to_reflect_component(&extra.value, &type_registry); + // let name = name.unwrap_or(&Name::new("")); let (target_entity, updated_components) = find_entity_components(entity, name, parent, reflect_components, &entity_components); @@ -81,7 +85,7 @@ pub fn add_components_from_gltf_extras(world: &mut World) { for (entity, name, extra, parent) in scene_extras.iter(world) { debug!( - "Name: {}, entity {:?}, parent: {:?}, scene_extras {:?}", + "Gltf Scene Extra: Name: {:?}, entity {:?}, parent: {:?}, scene_extras {:?}", name, entity, parent, extra ); @@ -96,7 +100,7 @@ pub fn add_components_from_gltf_extras(world: &mut World) { for (entity, name, extra, parent) in mesh_extras.iter(world) { debug!( - "Name: {}, entity {:?}, parent: {:?}, mesh_extras {:?}", + "Gltf Mesh Extra: Name: {:?}, entity {:?}, parent: {:?}, mesh_extras {:?}", name, entity, parent, extra ); @@ -111,7 +115,7 @@ pub fn add_components_from_gltf_extras(world: &mut World) { for (entity, name, extra, parent) in material_extras.iter(world) { debug!( - "Name: {}, entity {:?}, parent: {:?}, material_extras {:?}", + "Name: {:?}, entity {:?}, parent: {:?}, material_extras {:?}", name, entity, parent, extra ); @@ -146,7 +150,7 @@ pub fn add_components_from_gltf_extras(world: &mut World) { .expect("Unable to reflect component") .insert(&mut entity_mut, &*component, &type_registry); - entity_mut.insert(GltfProcessed); // this is how can we insert any additional components + entity_mut.insert(GltfProcessed); // } } } diff --git a/testing/bevy_example/src/game/in_game.rs b/testing/bevy_example/src/game/in_game.rs index 8ee07b5..ebf572b 100644 --- a/testing/bevy_example/src/game/in_game.rs +++ b/testing/bevy_example/src/game/in_game.rs @@ -14,23 +14,9 @@ pub fn setup_game( mut next_game_state: ResMut>, ) { // here we actually spawn our game world/level - /*commands.spawn(( - SceneBundle { - scene: asset_server.load("levels/World.glb#Scene0"), - ..default() - }, - bevy::prelude::Name::from("world"), - GameWorldTag, - InAppRunning, - ));*/ - commands.spawn(( - BlueprintInfo { - name: "World".into(), - path: "levels/World.glb".into(), - }, + BlueprintInfo::from_path("levels/World.gltf"), HideUntilReady, - bevy::prelude::Name::from("world"), //FIXME: not really needed ? could be infered from blueprint's name/ path SpawnBlueprint, GameWorldTag, InAppRunning, @@ -70,8 +56,8 @@ pub fn spawn_test( .spawn(( BluePrintBundle { blueprint: BlueprintInfo { - name: "Blueprint8_animated_no_bones".into(), - path: "blueprints/Blueprint6_animated.glb".into(), + name: "spawned".into(), + path: "blueprints/Blueprint 3.gltf".into(), }, // FIXME ..Default::default() }, diff --git a/testing/bevy_example/src/hierarchy_debug.rs b/testing/bevy_example/src/hierarchy_debug.rs index 0bddd16..7699c26 100644 --- a/testing/bevy_example/src/hierarchy_debug.rs +++ b/testing/bevy_example/src/hierarchy_debug.rs @@ -4,7 +4,7 @@ use bevy::{ }; use blenvy::{BlueprintAssets, BlueprintInstanceReady}; -use crate::{BasicTest, EnumComplex, RedirectPropHitImpulse}; +use crate::{BasicTest, EnumComplex, EnumTest, RedirectPropHitImpulse}; #[derive(Component)] pub struct HiearchyDebugTag; @@ -43,7 +43,7 @@ pub fn get_descendants( all_transforms: &Query<&Transform>, all_global_transforms: &Query<&GlobalTransform>, nesting: usize, - to_check: &Query<&BasicTest>, //&Query<(&BlueprintInstanceReady, &BlueprintAssets)>, + to_check: &Query<&EnumTest>, //&Query<(&BlueprintInstanceReady, &BlueprintAssets)>, ) -> String { let mut hierarchy_display: Vec = vec![]; let root_name = all_names.get(*root); @@ -54,15 +54,20 @@ pub fn get_descendants( name = "no_name".to_string() } + let mut component_display: String = "".into(); let components_to_check = to_check.get(*root); + if let Ok(compo) = to_check.get(*root) { + component_display = format!("{:?}", compo).clone(); + } + hierarchy_display.push(format!( - "{}{} ({:?}) ({:?})", + "{}{} ====> {} ", " ".repeat(nesting), name, - all_transforms.get(*root), - all_global_transforms.get(*root) - )); //components_to_check ({:?}) + component_display // all_transforms.get(*root), + //all_global_transforms.get(*root) + )); // ({:?}) // ({:?}) ({:?}) if let Ok(children) = all_children.get(*root) { for child in children.iter() { @@ -88,7 +93,7 @@ pub fn draw_hierarchy_debug( all_transforms: Query<&Transform>, all_global_transforms: Query<&GlobalTransform>, - to_check: Query<&BasicTest>, //Query<(&BlueprintInstanceReady, &BlueprintAssets)>, + to_check: Query<&EnumTest>, //Query<(&BlueprintInstanceReady, &BlueprintAssets)>, mut display: Query<&mut Text, With>, ) { let mut hierarchy_display: Vec = vec![]; @@ -183,7 +188,7 @@ impl Plugin for HiearchyDebugPlugin { app .add_systems(Startup, setup_hierarchy_debug) //.add_systems(Update, check_for_component) - //.add_systems(Update, draw_hierarchy_debug) + .add_systems(Update, draw_hierarchy_debug) //.add_systems(Update, check_for_gltf_extras) ; } diff --git a/tools/blenvy/TODO.md b/tools/blenvy/TODO.md index 873fbad..e010e7d 100644 --- a/tools/blenvy/TODO.md +++ b/tools/blenvy/TODO.md @@ -233,9 +233,9 @@ Bevy Side: - [x] fix/upgrade scene level animations - [ ] rename SceneAnimations to LevelAnimations (more coherent with the rest) - [x] move sub blueprint handling to blueprints_finalize_instances -- [ ] look into component overriding , it seems broken: - - [ ] blueprint level/ collection level components are now visible in instances in Blender - - [ ] they do not seem to be transfered to the (instance) entity above: +- [x] look into component overriding , it seems broken: + - [x] blueprint level/ collection level components are now visible in instances in Blender + - [x] they do not seem to be transfered to the (instance) entity above: could they be on the "empty node" ? - [ ] simplify testing example: