mirror of
https://github.com/kaosat-dev/Blender_bevy_components_workflow.git
synced 2024-11-26 21:37:01 +00:00
Merge 68eac8d320
into 1353e14802
This commit is contained in:
commit
c195d7edbc
@ -120,6 +120,9 @@ impl Plugin for BlueprintsPlugin {
|
||||
.register_type::<MaterialInfo>()
|
||||
.register_type::<SpawnHere>()
|
||||
.register_type::<Animations>()
|
||||
.register_type::<BlueprintsList>()
|
||||
.register_type::<Vec<String>>()
|
||||
.register_type::<HashMap<String, Vec<String>>>()
|
||||
.insert_resource(BluePrintsConfig {
|
||||
format: self.format,
|
||||
library_folder: self.library_folder.clone(),
|
||||
@ -140,11 +143,24 @@ impl Plugin for BlueprintsPlugin {
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
spawn_from_blueprints,
|
||||
compute_scene_aabbs.run_if(aabbs_enabled),
|
||||
apply_deferred.run_if(aabbs_enabled),
|
||||
(
|
||||
prepare_blueprints,
|
||||
check_for_loaded,
|
||||
spawn_from_blueprints,
|
||||
apply_deferred,
|
||||
)
|
||||
.chain(),
|
||||
(compute_scene_aabbs, apply_deferred)
|
||||
.chain()
|
||||
.run_if(aabbs_enabled),
|
||||
apply_deferred,
|
||||
materials_inject.run_if(materials_library_enabled),
|
||||
(
|
||||
materials_inject,
|
||||
check_for_material_loaded,
|
||||
materials_inject2,
|
||||
)
|
||||
.chain()
|
||||
.run_if(materials_library_enabled),
|
||||
)
|
||||
.chain()
|
||||
.in_set(GltfBlueprintsSet::Spawn),
|
||||
|
@ -4,6 +4,7 @@ use bevy::{
|
||||
asset::{AssetServer, Assets, Handle},
|
||||
ecs::{
|
||||
component::Component,
|
||||
entity::Entity,
|
||||
query::{Added, With},
|
||||
reflect::ReflectComponent,
|
||||
system::{Commands, Query, Res, ResMut},
|
||||
@ -16,7 +17,7 @@ use bevy::{
|
||||
render::mesh::Mesh,
|
||||
};
|
||||
|
||||
use crate::BluePrintsConfig;
|
||||
use crate::{AssetLoadTracker, AssetsToLoad, BluePrintsConfig};
|
||||
|
||||
#[derive(Component, Reflect, Default, Debug)]
|
||||
#[reflect(Component)]
|
||||
@ -26,10 +27,111 @@ pub struct MaterialInfo {
|
||||
pub source: String,
|
||||
}
|
||||
|
||||
/// flag component
|
||||
#[derive(Component)]
|
||||
pub(crate) struct BlueprintMaterialAssetsLoaded;
|
||||
/// flag component
|
||||
#[derive(Component)]
|
||||
pub(crate) struct BlueprintMaterialAssetsNotLoaded;
|
||||
|
||||
/// system that injects / replaces materials from material library
|
||||
pub(crate) fn materials_inject(
|
||||
blueprints_config: ResMut<BluePrintsConfig>,
|
||||
material_infos: Query<(Entity, &MaterialInfo), Added<MaterialInfo>>,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
for (entity, material_info) in material_infos.iter() {
|
||||
let model_file_name = format!(
|
||||
"{}_materials_library.{}",
|
||||
&material_info.source, &blueprints_config.format
|
||||
);
|
||||
let materials_path = Path::new(&blueprints_config.material_library_folder)
|
||||
.join(Path::new(model_file_name.as_str()));
|
||||
let material_name = &material_info.name;
|
||||
let material_full_path = materials_path.to_str().unwrap().to_string() + "#" + material_name; // TODO: yikes, cleanup
|
||||
|
||||
if blueprints_config
|
||||
.material_library_cache
|
||||
.contains_key(&material_full_path)
|
||||
{
|
||||
debug!("material is cached, retrieving");
|
||||
blueprints_config
|
||||
.material_library_cache
|
||||
.get(&material_full_path)
|
||||
.expect("we should have the material available");
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(BlueprintMaterialAssetsLoaded);
|
||||
} else {
|
||||
let material_file_handle: Handle<Gltf> = asset_server.load(materials_path.clone());
|
||||
let material_file_id = material_file_handle.id();
|
||||
let asset_infos: Vec<AssetLoadTracker<Gltf>> = vec![AssetLoadTracker {
|
||||
name: material_full_path,
|
||||
id: material_file_id,
|
||||
loaded: false,
|
||||
handle: material_file_handle.clone(),
|
||||
}];
|
||||
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(AssetsToLoad {
|
||||
all_loaded: false,
|
||||
asset_infos,
|
||||
..Default::default()
|
||||
})
|
||||
.insert(BlueprintMaterialAssetsNotLoaded);
|
||||
/**/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO, merge with check_for_loaded, make generic ?
|
||||
pub(crate) fn check_for_material_loaded(
|
||||
mut blueprint_assets_to_load: Query<
|
||||
(Entity, &mut AssetsToLoad<Gltf>),
|
||||
With<BlueprintMaterialAssetsNotLoaded>,
|
||||
>,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
for (entity, mut assets_to_load) in blueprint_assets_to_load.iter_mut() {
|
||||
let mut all_loaded = true;
|
||||
let mut loaded_amount = 0;
|
||||
let total = assets_to_load.asset_infos.len();
|
||||
for tracker in assets_to_load.asset_infos.iter_mut() {
|
||||
let asset_id = tracker.id;
|
||||
let loaded = asset_server.is_loaded_with_dependencies(asset_id);
|
||||
tracker.loaded = loaded;
|
||||
if loaded {
|
||||
loaded_amount += 1;
|
||||
} else {
|
||||
all_loaded = false;
|
||||
}
|
||||
}
|
||||
let progress: f32 = loaded_amount as f32 / total as f32;
|
||||
assets_to_load.progress = progress;
|
||||
|
||||
if all_loaded {
|
||||
assets_to_load.all_loaded = true;
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(BlueprintMaterialAssetsLoaded)
|
||||
.remove::<BlueprintMaterialAssetsNotLoaded>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// system that injects / replaces materials from material library
|
||||
pub(crate) fn materials_inject2(
|
||||
mut blueprints_config: ResMut<BluePrintsConfig>,
|
||||
material_infos: Query<(&MaterialInfo, &Children), Added<MaterialInfo>>,
|
||||
material_infos: Query<
|
||||
(&MaterialInfo, &Children),
|
||||
(
|
||||
Added<BlueprintMaterialAssetsLoaded>,
|
||||
With<BlueprintMaterialAssetsLoaded>,
|
||||
),
|
||||
>,
|
||||
with_materials_and_meshes: Query<
|
||||
(),
|
||||
(
|
||||
@ -38,9 +140,9 @@ pub(crate) fn materials_inject(
|
||||
With<Handle<Mesh>>,
|
||||
),
|
||||
>,
|
||||
models: Res<Assets<bevy::gltf::Gltf>>,
|
||||
|
||||
assets_gltf: Res<Assets<Gltf>>,
|
||||
asset_server: Res<AssetServer>,
|
||||
|
||||
mut commands: Commands,
|
||||
) {
|
||||
for (material_info, children) in material_infos.iter() {
|
||||
@ -66,9 +168,9 @@ pub(crate) fn materials_inject(
|
||||
.expect("we should have the material available");
|
||||
material_found = Some(material);
|
||||
} else {
|
||||
let my_gltf: Handle<Gltf> = asset_server.load(materials_path.clone());
|
||||
let mat_gltf = models
|
||||
.get(my_gltf.id())
|
||||
let model_handle: Handle<Gltf> = asset_server.load(materials_path.clone()); // FIXME: kinda weird now
|
||||
let mat_gltf = assets_gltf
|
||||
.get(model_handle.id())
|
||||
.expect("material should have been preloaded");
|
||||
if mat_gltf.named_materials.contains_key(material_name) {
|
||||
let material = mat_gltf
|
||||
|
@ -1,6 +1,6 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use bevy::{gltf::Gltf, prelude::*};
|
||||
use bevy::{gltf::Gltf, prelude::*, utils::HashMap};
|
||||
|
||||
use crate::{Animations, BluePrintsConfig};
|
||||
|
||||
@ -46,8 +46,152 @@ pub struct AddToGameWorld;
|
||||
/// helper component, just to transfer child data
|
||||
pub(crate) struct OriginalChildren(pub Vec<Entity>);
|
||||
|
||||
/// main spawning functions,
|
||||
/// helper component, is used to store the list of sub blueprints to enable automatic loading of dependend blueprints
|
||||
#[derive(Component, Reflect, Default, Debug)]
|
||||
#[reflect(Component)]
|
||||
pub struct BlueprintsList(pub HashMap<String, Vec<String>>);
|
||||
|
||||
/// helper component, for tracking loaded assets's loading state, id , handle etc
|
||||
#[derive(Default, Debug)]
|
||||
pub(crate) struct AssetLoadTracker<T: bevy::prelude::Asset> {
|
||||
#[allow(dead_code)]
|
||||
pub name: String,
|
||||
pub id: AssetId<T>,
|
||||
pub loaded: bool,
|
||||
#[allow(dead_code)]
|
||||
pub handle: Handle<T>,
|
||||
}
|
||||
|
||||
/// helper component, for tracking loaded assets
|
||||
#[derive(Component, Debug)]
|
||||
pub(crate) struct AssetsToLoad<T: bevy::prelude::Asset> {
|
||||
pub all_loaded: bool,
|
||||
pub asset_infos: Vec<AssetLoadTracker<T>>,
|
||||
pub progress: f32,
|
||||
}
|
||||
impl<T: bevy::prelude::Asset> Default for AssetsToLoad<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
all_loaded: Default::default(),
|
||||
asset_infos: Default::default(),
|
||||
progress: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// flag component, usually added when a blueprint is loaded
|
||||
#[derive(Component)]
|
||||
pub(crate) struct BlueprintAssetsLoaded;
|
||||
/// flag component
|
||||
#[derive(Component)]
|
||||
pub(crate) struct BlueprintAssetsNotLoaded;
|
||||
|
||||
/// spawning prepare function,
|
||||
/// * also takes into account the already exisiting "override" components, ie "override components" > components from blueprint
|
||||
pub(crate) fn prepare_blueprints(
|
||||
spawn_placeholders: Query<
|
||||
(
|
||||
Entity,
|
||||
&BlueprintName,
|
||||
Option<&Parent>,
|
||||
Option<&Library>,
|
||||
Option<&Name>,
|
||||
Option<&BlueprintsList>,
|
||||
),
|
||||
(Added<BlueprintName>, Added<SpawnHere>, Without<Spawned>),
|
||||
>,
|
||||
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
blueprints_config: Res<BluePrintsConfig>,
|
||||
) {
|
||||
for (entity, blupeprint_name, original_parent, library_override, name, blueprints_list) in
|
||||
spawn_placeholders.iter()
|
||||
{
|
||||
debug!(
|
||||
"requesting to spawn {:?} for entity {:?}, id: {:?}, parent:{:?}",
|
||||
blupeprint_name.0, name, entity, original_parent
|
||||
);
|
||||
|
||||
// println!("main model path {:?}", model_path);
|
||||
if blueprints_list.is_some() {
|
||||
let blueprints_list = blueprints_list.unwrap();
|
||||
// println!("blueprints list {:?}", blueprints_list.0.keys());
|
||||
let mut asset_infos: Vec<AssetLoadTracker<Gltf>> = vec![];
|
||||
let library_path =
|
||||
library_override.map_or_else(|| &blueprints_config.library_folder, |l| &l.0);
|
||||
for (blueprint_name, _) in blueprints_list.0.iter() {
|
||||
let model_file_name = format!("{}.{}", &blueprint_name, &blueprints_config.format);
|
||||
let model_path = Path::new(&library_path).join(Path::new(model_file_name.as_str()));
|
||||
|
||||
let model_handle: Handle<Gltf> = asset_server.load(model_path.clone());
|
||||
let model_id = model_handle.id();
|
||||
let loaded = asset_server.is_loaded_with_dependencies(model_id);
|
||||
if !loaded {
|
||||
asset_infos.push(AssetLoadTracker {
|
||||
name: model_path.to_string_lossy().into(),
|
||||
id: model_id,
|
||||
loaded: false,
|
||||
handle: model_handle.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
// if not all assets are already loaded, inject a component to signal that we need them to be loaded
|
||||
if !asset_infos.is_empty() {
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(AssetsToLoad {
|
||||
all_loaded: false,
|
||||
asset_infos,
|
||||
..Default::default()
|
||||
})
|
||||
.insert(BlueprintAssetsNotLoaded);
|
||||
} else {
|
||||
commands.entity(entity).insert(BlueprintAssetsLoaded);
|
||||
}
|
||||
} else {
|
||||
// in case there are no blueprintsList
|
||||
commands.entity(entity).insert(BlueprintAssetsLoaded);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn check_for_loaded(
|
||||
mut blueprint_assets_to_load: Query<
|
||||
(Entity, &mut AssetsToLoad<Gltf>),
|
||||
With<BlueprintAssetsNotLoaded>,
|
||||
>,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
for (entity, mut assets_to_load) in blueprint_assets_to_load.iter_mut() {
|
||||
let mut all_loaded = true;
|
||||
let mut loaded_amount = 0;
|
||||
let total = assets_to_load.asset_infos.len();
|
||||
for tracker in assets_to_load.asset_infos.iter_mut() {
|
||||
let asset_id = tracker.id;
|
||||
let loaded = asset_server.is_loaded_with_dependencies(asset_id);
|
||||
tracker.loaded = loaded;
|
||||
if loaded {
|
||||
loaded_amount += 1;
|
||||
} else {
|
||||
all_loaded = false;
|
||||
}
|
||||
}
|
||||
let progress: f32 = loaded_amount as f32 / total as f32;
|
||||
// println!("progress: {}",progress);
|
||||
assets_to_load.progress = progress;
|
||||
|
||||
if all_loaded {
|
||||
assets_to_load.all_loaded = true;
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(BlueprintAssetsLoaded)
|
||||
.remove::<BlueprintAssetsNotLoaded>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn spawn_from_blueprints(
|
||||
spawn_placeholders: Query<
|
||||
(
|
||||
@ -59,7 +203,11 @@ pub(crate) fn spawn_from_blueprints(
|
||||
Option<&AddToGameWorld>,
|
||||
Option<&Name>,
|
||||
),
|
||||
(Added<BlueprintName>, Added<SpawnHere>, Without<Spawned>),
|
||||
(
|
||||
With<BlueprintAssetsLoaded>,
|
||||
Added<BlueprintAssetsLoaded>,
|
||||
Without<BlueprintAssetsNotLoaded>,
|
||||
),
|
||||
>,
|
||||
|
||||
mut commands: Commands,
|
||||
@ -82,17 +230,10 @@ pub(crate) fn spawn_from_blueprints(
|
||||
) in spawn_placeholders.iter()
|
||||
{
|
||||
debug!(
|
||||
"need to spawn {:?} for entity {:?}, id: {:?}, parent:{:?}",
|
||||
"attempting to spawn {:?} for entity {:?}, id: {:?}, parent:{:?}",
|
||||
blupeprint_name.0, name, entity, original_parent
|
||||
);
|
||||
|
||||
let mut original_children: Vec<Entity> = vec![];
|
||||
if let Ok(c) = children.get(entity) {
|
||||
for child in c.iter() {
|
||||
original_children.push(*child);
|
||||
}
|
||||
}
|
||||
|
||||
let what = &blupeprint_name.0;
|
||||
let model_file_name = format!("{}.{}", &what, &blueprints_config.format);
|
||||
|
||||
@ -101,8 +242,8 @@ pub(crate) fn spawn_from_blueprints(
|
||||
library_override.map_or_else(|| &blueprints_config.library_folder, |l| &l.0);
|
||||
let model_path = Path::new(&library_path).join(Path::new(model_file_name.as_str()));
|
||||
|
||||
debug!("attempting to spawn {:?}", model_path);
|
||||
let model_handle: Handle<Gltf> = asset_server.load(model_path);
|
||||
// info!("attempting to spawn {:?}", model_path);
|
||||
let model_handle: Handle<Gltf> = asset_server.load(model_path); // FIXME: kinda weird now
|
||||
|
||||
let gltf = assets_gltf
|
||||
.get(&model_handle)
|
||||
@ -123,6 +264,12 @@ pub(crate) fn spawn_from_blueprints(
|
||||
transforms = *transform.unwrap();
|
||||
}
|
||||
|
||||
let mut original_children: Vec<Entity> = vec![];
|
||||
if let Ok(c) = children.get(entity) {
|
||||
for child in c.iter() {
|
||||
original_children.push(*child);
|
||||
}
|
||||
}
|
||||
commands.entity(entity).insert((
|
||||
SceneBundle {
|
||||
scene: scene.clone(),
|
||||
|
@ -1,12 +1,16 @@
|
||||
use std::any::TypeId;
|
||||
|
||||
use bevy::gltf::Gltf;
|
||||
use bevy::prelude::*;
|
||||
use bevy::scene::SceneInstance;
|
||||
// use bevy::utils::hashbrown::HashSet;
|
||||
|
||||
use super::{AnimationPlayerLink, Animations};
|
||||
use super::{SpawnHere, Spawned};
|
||||
use crate::{CopyComponents, InBlueprint, NoInBlueprint, OriginalChildren};
|
||||
use crate::{
|
||||
AssetsToLoad, BlueprintAssetsLoaded, CopyComponents, InBlueprint, NoInBlueprint,
|
||||
OriginalChildren,
|
||||
};
|
||||
|
||||
/// this system is in charge of doing any necessary post processing after a blueprint scene has been spawned
|
||||
/// - it removes one level of useless nesting
|
||||
@ -89,6 +93,8 @@ pub(crate) fn spawned_blueprint_post_process(
|
||||
commands.entity(original).remove::<SpawnHere>();
|
||||
commands.entity(original).remove::<Spawned>();
|
||||
commands.entity(original).remove::<Handle<Scene>>();
|
||||
commands.entity(original).remove::<AssetsToLoad<Gltf>>(); // also clear the sub assets tracker to free up handles, perhaps just freeing up the handles and leave the rest would be better ?
|
||||
commands.entity(original).remove::<BlueprintAssetsLoaded>();
|
||||
commands.entity(root_entity).despawn_recursive();
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ pub fn setup_game(
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.id())
|
||||
.get(game_assets.world.clone().unwrap().id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
|
@ -20,7 +20,7 @@ pub fn setup_game(
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.id())
|
||||
.get(game_assets.world.clone().unwrap().id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
|
@ -20,7 +20,7 @@ pub fn setup_game(
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.id())
|
||||
.get(game_assets.world.clone().unwrap().id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
|
@ -21,7 +21,7 @@ pub fn setup_game(
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.id())
|
||||
.get(game_assets.world.clone().unwrap().id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
|
@ -21,7 +21,7 @@ pub fn setup_game(
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.id())
|
||||
.get(game_assets.world.clone().unwrap().id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
|
@ -76,7 +76,7 @@ pub fn trigger_level_transition(
|
||||
} else if target_level == "Level2" {
|
||||
level = game_assets.level2.clone().unwrap();
|
||||
} else {
|
||||
level = game_assets.world.clone();
|
||||
level = game_assets.world.clone().unwrap();
|
||||
}
|
||||
info!("spawning new level");
|
||||
commands.spawn((
|
||||
|
@ -21,7 +21,7 @@ pub fn setup_game(
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.id())
|
||||
.get(game_assets.world.clone().unwrap().id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
|
@ -5,8 +5,8 @@ use bevy_asset_loader::prelude::*;
|
||||
|
||||
#[derive(AssetCollection, Resource)]
|
||||
pub struct GameAssets {
|
||||
#[asset(key = "world")]
|
||||
pub world: Handle<Gltf>,
|
||||
#[asset(key = "world", optional)]
|
||||
pub world: Option<Handle<Gltf>>,
|
||||
|
||||
#[asset(key = "world_dynamic", optional)]
|
||||
pub world_dynamic: Option<Handle<Gltf>>,
|
||||
@ -16,8 +16,8 @@ pub struct GameAssets {
|
||||
#[asset(key = "level2", optional)]
|
||||
pub level2: Option<Handle<Gltf>>,
|
||||
|
||||
#[asset(key = "models", collection(typed, mapped))]
|
||||
pub models: HashMap<String, Handle<Gltf>>,
|
||||
#[asset(key = "models", collection(typed, mapped), optional)]
|
||||
pub models: Option<HashMap<String, Handle<Gltf>>>,
|
||||
|
||||
#[asset(key = "materials", collection(typed, mapped), optional)]
|
||||
pub materials: Option<HashMap<String, Handle<Gltf>>>,
|
||||
|
@ -1,6 +1,6 @@
|
||||
({
|
||||
"world":File (path: "models/World.glb"),
|
||||
/*"world":File (path: "models/World.glb"),
|
||||
"models": Folder (
|
||||
path: "models/library",
|
||||
),
|
||||
),*/
|
||||
})
|
@ -3576,6 +3576,22 @@
|
||||
"type": "array",
|
||||
"typeInfo": "TupleStruct"
|
||||
},
|
||||
"bevy_gltf_blueprints::spawn_from_blueprints::BlueprintsList": {
|
||||
"isComponent": true,
|
||||
"isResource": false,
|
||||
"items": false,
|
||||
"prefixItems": [
|
||||
{
|
||||
"type": {
|
||||
"$ref": "#/$defs/bevy_utils::hashbrown::HashMap<alloc::string::String, alloc::vec::Vec<alloc::string::String>, bevy_utils::hashbrown::hash_map::DefaultHashBuilder>"
|
||||
}
|
||||
}
|
||||
],
|
||||
"short_name": "BlueprintsList",
|
||||
"title": "bevy_gltf_blueprints::spawn_from_blueprints::BlueprintsList",
|
||||
"type": "array",
|
||||
"typeInfo": "TupleStruct"
|
||||
},
|
||||
"bevy_gltf_blueprints::spawn_from_blueprints::SpawnHere": {
|
||||
"additionalProperties": false,
|
||||
"isComponent": true,
|
||||
@ -10834,6 +10850,19 @@
|
||||
"type": "object",
|
||||
"typeInfo": "Value"
|
||||
},
|
||||
"bevy_utils::hashbrown::HashMap<alloc::string::String, alloc::vec::Vec<alloc::string::String>, bevy_utils::hashbrown::hash_map::DefaultHashBuilder>": {
|
||||
"additionalProperties": {
|
||||
"type": {
|
||||
"$ref": "#/$defs/alloc::vec::Vec<alloc::string::String>"
|
||||
}
|
||||
},
|
||||
"isComponent": false,
|
||||
"isResource": false,
|
||||
"short_name": "HashMap<String, Vec<String>, DefaultHashBuilder>",
|
||||
"title": "bevy_utils::hashbrown::HashMap<alloc::string::String, alloc::vec::Vec<alloc::string::String>, bevy_utils::hashbrown::hash_map::DefaultHashBuilder>",
|
||||
"type": "object",
|
||||
"typeInfo": "Map"
|
||||
},
|
||||
"bevy_utils::smallvec::SmallVec<[bevy_ecs::entity::Entity; 8]>": {
|
||||
"isComponent": false,
|
||||
"isResource": false,
|
||||
|
@ -11,6 +11,7 @@ impl Plugin for CorePlugin {
|
||||
legacy_mode: false,
|
||||
library_folder: "models/library".into(),
|
||||
format: GltfFormat::GLB,
|
||||
material_library: true,
|
||||
aabbs: true,
|
||||
..Default::default()
|
||||
},
|
||||
|
@ -1,37 +1,25 @@
|
||||
use bevy::prelude::*;
|
||||
use bevy_gltf_blueprints::{BluePrintBundle, BlueprintName, GameWorldTag};
|
||||
use bevy_gltf_worlflow_examples_common_rapier::{assets::GameAssets, GameState, InAppRunning};
|
||||
use bevy_gltf_worlflow_examples_common_rapier::{GameState, InAppRunning};
|
||||
|
||||
use bevy_rapier3d::prelude::Velocity;
|
||||
use rand::Rng;
|
||||
|
||||
pub fn setup_game(
|
||||
mut commands: Commands,
|
||||
game_assets: Res<GameAssets>,
|
||||
models: Res<Assets<bevy::gltf::Gltf>>,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut next_game_state: ResMut<NextState<GameState>>,
|
||||
) {
|
||||
commands.insert_resource(AmbientLight {
|
||||
color: Color::WHITE,
|
||||
brightness: 0.2,
|
||||
});
|
||||
// here we actually spawn our game world/level
|
||||
|
||||
commands.spawn((
|
||||
SceneBundle {
|
||||
// note: because of this issue https://github.com/bevyengine/bevy/issues/10436, "world" is now a gltf file instead of a scene
|
||||
scene: models
|
||||
.get(game_assets.world.id())
|
||||
.expect("main level should have been loaded")
|
||||
.scenes[0]
|
||||
.clone(),
|
||||
scene: asset_server.load("models/World.glb#Scene0"),
|
||||
..default()
|
||||
},
|
||||
bevy::prelude::Name::from("world"),
|
||||
GameWorldTag,
|
||||
InAppRunning,
|
||||
));
|
||||
|
||||
next_game_state.set(GameState::InGame)
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ use std::{
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use bevy_gltf_blueprints::{AnimationPlayerLink, BlueprintName};
|
||||
use bevy_gltf_blueprints::{AnimationPlayerLink, BlueprintName, BlueprintsList};
|
||||
pub use in_game::*;
|
||||
|
||||
use bevy::{
|
||||
@ -22,7 +22,8 @@ fn start_game(mut next_app_state: ResMut<NextState<AppState>>) {
|
||||
// if the export from Blender worked correctly, we should have animations (simplified here by using AnimationPlayerLink)
|
||||
// if the export from Blender worked correctly, we should have an Entity called "Cylinder" that has two components: UnitTest, TupleTestF32
|
||||
// if the export from Blender worked correctly, we should have an Entity called "Blueprint4_nested" that has a child called "Blueprint3" that has a "BlueprintName" component with value Blueprint3
|
||||
|
||||
// if the export from Blender worked correctly, we should have a blueprints_list
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn validate_export(
|
||||
parents: Query<&Parent>,
|
||||
children: Query<&Children>,
|
||||
@ -31,6 +32,8 @@ fn validate_export(
|
||||
animation_player_links: Query<(Entity, &AnimationPlayerLink)>,
|
||||
exported_cylinder: Query<(Entity, &Name, &UnitTest, &TupleTestF32)>,
|
||||
empties_candidates: Query<(Entity, &Name, &GlobalTransform)>,
|
||||
|
||||
blueprints_list: Query<(Entity, &BlueprintsList)>,
|
||||
) {
|
||||
let animations_found = !animation_player_links.is_empty();
|
||||
|
||||
@ -69,11 +72,13 @@ fn validate_export(
|
||||
}
|
||||
}
|
||||
|
||||
let blueprints_list_found = !blueprints_list.is_empty();
|
||||
|
||||
fs::write(
|
||||
"bevy_diagnostics.json",
|
||||
format!(
|
||||
"{{ \"animations\": {}, \"cylinder_found\": {} , \"nested_blueprint_found\": {}, \"empty_found\": {} }}",
|
||||
animations_found, cylinder_found, nested_blueprint_found, empty_found
|
||||
"{{ \"animations\": {}, \"cylinder_found\": {} , \"nested_blueprint_found\": {}, \"empty_found\": {}, \"blueprints_list_found\": {} }}",
|
||||
animations_found, cylinder_found, nested_blueprint_found, empty_found, blueprints_list_found
|
||||
),
|
||||
)
|
||||
.expect("Unable to write file");
|
||||
@ -97,9 +102,9 @@ impl Plugin for GamePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, (spawn_test).run_if(in_state(GameState::InGame)))
|
||||
.add_systems(Update, validate_export)
|
||||
.add_systems(Update, generate_screenshot.run_if(on_timer(Duration::from_secs_f32(0.2)))) // TODO: run once
|
||||
.add_systems(OnEnter(AppState::MenuRunning), start_game)
|
||||
.add_systems(OnEnter(AppState::AppRunning), setup_game)
|
||||
.add_systems(Update, generate_screenshot.run_if(on_timer(Duration::from_secs_f32(0.2)))) // TODO: run once
|
||||
.add_systems(
|
||||
Update,
|
||||
exit_game.run_if(on_timer(Duration::from_secs_f32(0.5))),
|
||||
|
@ -4,7 +4,7 @@ import bpy
|
||||
from ..helpers.generate_and_export import generate_and_export
|
||||
from .export_gltf import (generate_gltf_export_preferences, export_gltf)
|
||||
from ..modules.bevy_dynamic import is_object_dynamic, is_object_static
|
||||
from ..helpers.helpers_scenes import clear_hollow_scene, copy_hollowed_collection_into
|
||||
from ..helpers.helpers_scenes import clear_hollow_scene, copy_hollowed_collection_into, inject_blueprints_list_into_main_scene, remove_blueprints_list_from_main_scene
|
||||
|
||||
|
||||
# export all main scenes
|
||||
@ -16,6 +16,7 @@ def export_main_scene(scene, folder_path, addon_prefs, library_collections):
|
||||
gltf_export_preferences = generate_gltf_export_preferences(addon_prefs)
|
||||
export_output_folder = getattr(addon_prefs,"export_output_folder")
|
||||
export_blueprints = getattr(addon_prefs,"export_blueprints")
|
||||
legacy_mode = getattr(addon_prefs, "export_legacy_mode")
|
||||
export_separate_dynamic_and_static_objects = getattr(addon_prefs, "export_separate_dynamic_and_static_objects")
|
||||
|
||||
gltf_output_path = os.path.join(folder_path, export_output_folder, scene.name)
|
||||
@ -29,6 +30,9 @@ def export_main_scene(scene, folder_path, addon_prefs, library_collections):
|
||||
}
|
||||
|
||||
if export_blueprints :
|
||||
if not legacy_mode:
|
||||
inject_blueprints_list_into_main_scene(scene)
|
||||
|
||||
if export_separate_dynamic_and_static_objects:
|
||||
#print("SPLIT STATIC AND DYNAMIC")
|
||||
# first export static objects
|
||||
@ -67,5 +71,8 @@ def export_main_scene(scene, folder_path, addon_prefs, library_collections):
|
||||
print(" exporting gltf to", gltf_output_path, ".gltf/glb")
|
||||
export_gltf(gltf_output_path, export_settings)
|
||||
|
||||
if not legacy_mode:
|
||||
remove_blueprints_list_from_main_scene(scene)
|
||||
|
||||
|
||||
|
||||
|
@ -36,16 +36,23 @@ def get_marked_collections(scene, addon_prefs):
|
||||
return (collection_names, marked_collections)
|
||||
|
||||
# gets all collections within collections that might also be relevant
|
||||
def get_sub_collections(collections, parent, children_per_collection):
|
||||
def get_sub_collections(collections, parent=None, children_per_collection=None):
|
||||
if parent == None:
|
||||
parent = CollectionNode()
|
||||
if children_per_collection == None:
|
||||
children_per_collection = {}
|
||||
|
||||
collection_names = set()
|
||||
used_collections = []
|
||||
|
||||
for root_collection in collections:
|
||||
node = Node(name=root_collection.name, parent=parent)
|
||||
#print("collections", collections)
|
||||
node = CollectionNode(name=root_collection.name, parent=parent)
|
||||
parent.children.append(node)
|
||||
|
||||
#print("root collection", root_collection.name)
|
||||
for collection in traverse_tree(root_collection): # TODO: filter out COLLECTIONS that have the flatten flag (unlike the flatten flag on colleciton instances themselves)
|
||||
#print("sub", collection)
|
||||
node_name = collection.name
|
||||
children_per_collection[node_name] = []
|
||||
#print(" scanning", collection.name)
|
||||
@ -53,12 +60,15 @@ def get_sub_collections(collections, parent, children_per_collection):
|
||||
#print("FLATTEN", object.name, 'Flatten' in object)
|
||||
if object.instance_type == 'COLLECTION' : # and not 'Flatten' in object:
|
||||
collection_name = object.instance_collection.name
|
||||
#print("sub obj", collection_name)
|
||||
# FIXME: not sure:
|
||||
children_per_collection[node_name].append(collection_name)
|
||||
|
||||
(sub_names, sub_collections) = get_sub_collections([object.instance_collection], node, children_per_collection)
|
||||
if len(list(sub_names)) > 0:
|
||||
children_per_collection[node_name] += (list(sub_names))
|
||||
#print(" found sub collection in use", object.name, object.instance_collection)
|
||||
|
||||
|
||||
if not collection_name in collection_names:
|
||||
collection_names.add(collection_name)
|
||||
used_collections.append(object.instance_collection)
|
||||
@ -77,7 +87,7 @@ def flatten_collection_tree(node, children_per_collection):
|
||||
children_per_collection[node.name] = list(set( children_per_collection[node.name]))
|
||||
|
||||
|
||||
class Node :
|
||||
class CollectionNode :
|
||||
def __init__(self, name="", parent=None):
|
||||
self.name = name
|
||||
self.children = []
|
||||
@ -93,7 +103,7 @@ def get_exportable_collections(main_scenes, library_scenes, addon_prefs):
|
||||
|
||||
all_collections = []
|
||||
all_collection_names = []
|
||||
root_node = Node()
|
||||
root_node = CollectionNode()
|
||||
root_node.name = "root"
|
||||
children_per_collection = {}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
import json
|
||||
import bpy
|
||||
from .helpers_collections import (set_active_collection)
|
||||
from .helpers_collections import (CollectionNode, get_sub_collections, get_used_collections, set_active_collection)
|
||||
from .object_makers import (make_empty)
|
||||
|
||||
|
||||
@ -73,6 +74,17 @@ def copy_hollowed_collection_into(source_collection, destination_collection, par
|
||||
empty_obj['BlueprintName'] = '"'+collection_name+'"' if legacy_mode else '("'+collection_name+'")'
|
||||
empty_obj['SpawnHere'] = '()'
|
||||
|
||||
# we also inject a list of all sub blueprints, so that the bevy side can preload them
|
||||
if not legacy_mode:
|
||||
root_node = CollectionNode()
|
||||
root_node.name = "root"
|
||||
children_per_collection = {}
|
||||
print("collection stuff", original_name)
|
||||
get_sub_collections([object.instance_collection], root_node, children_per_collection)
|
||||
empty_obj["BlueprintsList"] = f"({json.dumps(dict(children_per_collection))})"
|
||||
#empty_obj["Assets"] = {"Animations": [], "Materials": [], "Models":[], "Textures":[], "Audio":[], "Other":[]}
|
||||
|
||||
|
||||
# we copy custom properties over from our original object to our empty
|
||||
for component_name, component_value in object.items():
|
||||
if component_name not in custom_properties_to_filter_out and is_component_valid(object, component_name): #copy only valid properties
|
||||
@ -149,3 +161,45 @@ def get_scenes(addon_prefs):
|
||||
return [level_scene_names, level_scenes, library_scene_names, library_scenes]
|
||||
|
||||
|
||||
|
||||
|
||||
def inject_blueprints_list_into_main_scene(scene):
|
||||
print("injecting assets/blueprints data into scene")
|
||||
root_collection = scene.collection
|
||||
assets_list = None
|
||||
assets_list_name = f"assets_list_{scene.name}_components"
|
||||
for object in scene.objects:
|
||||
if object.name == assets_list_name:
|
||||
assets_list = object
|
||||
break
|
||||
|
||||
if assets_list is None:
|
||||
assets_list = make_empty(assets_list_name, [0,0,0], [0,0,0], [0,0,0], root_collection)
|
||||
|
||||
# find all blueprints used in a scene
|
||||
# TODO: export a tree rather than a flat list ? because you could have potential clashing items in flat lists (amongst other issues)
|
||||
(collection_names, collections) = get_used_collections(scene)
|
||||
root_node = CollectionNode()
|
||||
root_node.name = "root"
|
||||
children_per_collection = {}
|
||||
|
||||
#print("collection_names", collection_names, "collections", collections)
|
||||
get_sub_collections(collections, root_node, children_per_collection)
|
||||
# what about marked assets ?
|
||||
# what about audio assets ?
|
||||
# what about materials ?
|
||||
# object['MaterialInfo'] = '(name: "'+material.name+'", source: "'+current_project_name + '")'
|
||||
|
||||
#assets_list["blueprints_direct"] = list(collection_names)
|
||||
assets_list["BlueprintsList"] = f"({json.dumps(dict(children_per_collection))})"
|
||||
#assets_list["Materials"]= '()'
|
||||
|
||||
def remove_blueprints_list_from_main_scene(scene):
|
||||
assets_list = None
|
||||
assets_list_name = f"assets_list_{scene.name}_components"
|
||||
|
||||
for object in scene.objects:
|
||||
if object.name == assets_list_name:
|
||||
assets_list = object
|
||||
if assets_list is not None:
|
||||
bpy.data.objects.remove(assets_list, do_unlink=True)
|
||||
|
@ -19,6 +19,8 @@ def setup_data(request):
|
||||
|
||||
def finalizer():
|
||||
print("\nPerforming teardown...")
|
||||
get_orphan_data()
|
||||
|
||||
if os.path.exists(models_path):
|
||||
shutil.rmtree(models_path)
|
||||
|
||||
@ -33,6 +35,11 @@ def setup_data(request):
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def get_orphan_data():
|
||||
orphan_meshes = [m.name for m in bpy.data.meshes if m.users == 0]
|
||||
# print("orphan meshes before", orphan_meshes)
|
||||
|
||||
def test_export_do_not_export_blueprints(setup_data):
|
||||
auto_export_operator = bpy.ops.export_scenes.auto_gltf
|
||||
|
||||
@ -57,7 +64,6 @@ def test_export_do_not_export_blueprints(setup_data):
|
||||
|
||||
def test_export_custom_blueprints_path(setup_data):
|
||||
auto_export_operator = bpy.ops.export_scenes.auto_gltf
|
||||
|
||||
# first, configure things
|
||||
# we use the global settings for that
|
||||
export_props = {
|
||||
@ -210,3 +216,27 @@ def test_export_separate_dynamic_and_static_objects(setup_data):
|
||||
|
||||
assert os.path.exists(os.path.join(setup_data["models_path"], "World.glb")) == True
|
||||
assert os.path.exists(os.path.join(setup_data["models_path"], "World_dynamic.glb")) == True
|
||||
|
||||
|
||||
def test_export_should_not_generate_orphan_data(setup_data):
|
||||
auto_export_operator = bpy.ops.export_scenes.auto_gltf
|
||||
|
||||
# first, configure things
|
||||
# we use the global settings for that
|
||||
export_props = {
|
||||
"main_scene_names" : ['World'],
|
||||
"library_scene_names": ['Library']
|
||||
}
|
||||
stored_settings = bpy.data.texts[".gltf_auto_export_settings"] if ".gltf_auto_export_settings" in bpy.data.texts else bpy.data.texts.new(".gltf_auto_export_settings")
|
||||
stored_settings.clear()
|
||||
stored_settings.write(json.dumps(export_props))
|
||||
|
||||
auto_export_operator(
|
||||
direct_mode=True,
|
||||
export_output_folder="./models",
|
||||
export_scene_settings=True,
|
||||
export_blueprints=False,
|
||||
)
|
||||
assert os.path.exists(os.path.join(setup_data["models_path"], "World.glb")) == True
|
||||
assert os.path.exists(os.path.join(setup_data["models_path"], "library", "Blueprint1.glb")) == False
|
||||
|
||||
|
@ -16,18 +16,16 @@ def setup_data(request):
|
||||
root_path = "../../testing/bevy_example"
|
||||
assets_root_path = os.path.join(root_path, "assets")
|
||||
models_path = os.path.join(assets_root_path, "models")
|
||||
#materials_path = os.path.join("../../testing", "materials")
|
||||
materials_path = os.path.join(assets_root_path, "materials")
|
||||
#other_materials_path = os.path.join("../../testing", "other_materials")
|
||||
|
||||
print("\nPerforming teardown...")
|
||||
if os.path.exists(models_path):
|
||||
shutil.rmtree(models_path)
|
||||
|
||||
"""if os.path.exists(materials_path):
|
||||
if os.path.exists(materials_path):
|
||||
shutil.rmtree(materials_path)
|
||||
|
||||
if os.path.exists(other_materials_path):
|
||||
shutil.rmtree(other_materials_path)"""
|
||||
diagnostics_file_path = os.path.join(root_path, "bevy_diagnostics.json")
|
||||
if os.path.exists(diagnostics_file_path):
|
||||
os.remove(diagnostics_file_path)
|
||||
@ -75,7 +73,8 @@ def test_export_complex(setup_data):
|
||||
export_scene_settings=True,
|
||||
export_blueprints=True,
|
||||
export_legacy_mode=False,
|
||||
export_animations=True
|
||||
export_animations=True,
|
||||
export_materials_library=True
|
||||
)
|
||||
# blueprint1 => has an instance, got changed, should export
|
||||
# blueprint2 => has NO instance, but marked as asset, should export
|
||||
@ -84,7 +83,6 @@ def test_export_complex(setup_data):
|
||||
# blueprint5 => has NO instance, not marked as asset, should NOT export
|
||||
|
||||
assert os.path.exists(os.path.join(models_path, "World.glb")) == True
|
||||
|
||||
assert os.path.exists(os.path.join(models_path, "library", "Blueprint1.glb")) == True
|
||||
assert os.path.exists(os.path.join(models_path, "library", "Blueprint2.glb")) == True
|
||||
assert os.path.exists(os.path.join(models_path, "library", "Blueprint3.glb")) == True
|
||||
@ -93,13 +91,14 @@ def test_export_complex(setup_data):
|
||||
assert os.path.exists(os.path.join(models_path, "library", "Blueprint6_animated.glb")) == True
|
||||
assert os.path.exists(os.path.join(models_path, "library", "Blueprint7_hierarchy.glb")) == True
|
||||
|
||||
# 'assets_list_'+scene.name+"_components" should have been removed after the export
|
||||
assets_list_object_name = "assets_list_"+"World"+"_components"
|
||||
assets_list_object_present = assets_list_object_name in bpy.data.objects
|
||||
assert assets_list_object_present == False
|
||||
|
||||
# now run bevy
|
||||
command = "cargo run --features bevy/dynamic_linking"
|
||||
# assert getattr(propertyGroup, 'a') == 0.5714026093482971
|
||||
FNULL = open(os.devnull, 'w') #use this if you want to suppress output to stdout from the subprocess
|
||||
filename = "my_file.dat"
|
||||
args = command
|
||||
#subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False, cwd=bevy_run_exec_path)
|
||||
return_code = subprocess.call(["cargo", "run", "--features", "bevy/dynamic_linking"], cwd=root_path)
|
||||
print("RETURN CODE OF BEVY APP", return_code)
|
||||
assert return_code == 0
|
||||
@ -110,6 +109,7 @@ def test_export_complex(setup_data):
|
||||
assert diagnostics["animations"] == True
|
||||
assert diagnostics["cylinder_found"] == True
|
||||
assert diagnostics["empty_found"] == True
|
||||
assert diagnostics["blueprints_list_found"] == True
|
||||
|
||||
# last but not least, do a visual compare
|
||||
screenshot_expected_path = os.path.join(root_path, "expected_screenshot.png")
|
||||
|
Loading…
Reference in New Issue
Block a user