2024-05-12 09:05:11 +00:00
|
|
|
import os
|
2024-05-12 13:10:35 +00:00
|
|
|
import json
|
2024-05-12 09:05:11 +00:00
|
|
|
import bpy
|
|
|
|
from bpy_types import (Operator)
|
2024-05-13 21:36:13 +00:00
|
|
|
from bpy.props import (BoolProperty, StringProperty, EnumProperty)
|
|
|
|
|
2024-05-16 12:09:40 +00:00
|
|
|
from .asset_helpers import does_asset_exist, get_user_assets, remove_asset, upsert_asset
|
2024-05-15 21:32:01 +00:00
|
|
|
from .assets_scan import get_main_scene_assets_tree
|
2024-05-15 11:25:30 +00:00
|
|
|
from ..core.path_helpers import absolute_path_from_blend_file
|
2024-05-12 09:05:11 +00:00
|
|
|
|
2024-06-12 10:41:50 +00:00
|
|
|
class BLENVY_OT_assets_add(Operator):
|
2024-05-12 09:05:11 +00:00
|
|
|
"""Add asset"""
|
2024-06-12 10:41:50 +00:00
|
|
|
bl_idname = "blenvy.assets_add"
|
2024-05-12 09:05:11 +00:00
|
|
|
bl_label = "Add bevy asset"
|
|
|
|
bl_options = {"UNDO"}
|
|
|
|
|
|
|
|
asset_name: StringProperty(
|
|
|
|
name="asset name",
|
|
|
|
description="name of asset to add",
|
|
|
|
) # type: ignore
|
|
|
|
|
2024-05-13 08:28:44 +00:00
|
|
|
asset_type: EnumProperty(
|
2024-05-12 09:05:11 +00:00
|
|
|
items=(
|
|
|
|
('MODEL', "Model", ""),
|
|
|
|
('AUDIO', "Audio", ""),
|
|
|
|
('IMAGE', "Image", ""),
|
2024-05-13 21:36:13 +00:00
|
|
|
('TEXT', "Text", ""),
|
2024-05-12 09:05:11 +00:00
|
|
|
)
|
|
|
|
) # type: ignore
|
|
|
|
|
|
|
|
asset_path: StringProperty(
|
|
|
|
name="asset path",
|
|
|
|
description="path of asset to add",
|
|
|
|
subtype='FILE_PATH'
|
|
|
|
) # type: ignore
|
|
|
|
|
2024-05-13 21:36:13 +00:00
|
|
|
# what are we targetting
|
|
|
|
target_type: EnumProperty(
|
|
|
|
name="target type",
|
|
|
|
description="type of the target: scene or blueprint to add an asset to",
|
|
|
|
items=(
|
|
|
|
('SCENE', "Scene", ""),
|
|
|
|
('BLUEPRINT', "Blueprint", ""),
|
|
|
|
),
|
|
|
|
) # type: ignore
|
|
|
|
|
|
|
|
target_name: StringProperty(
|
|
|
|
name="target name",
|
|
|
|
description="name of the target blueprint or scene to add asset to"
|
|
|
|
) # type: ignore
|
|
|
|
|
2024-05-12 09:05:11 +00:00
|
|
|
def execute(self, context):
|
2024-05-13 21:36:13 +00:00
|
|
|
blueprint_assets = self.target_type == 'BLUEPRINT'
|
2024-05-16 12:09:40 +00:00
|
|
|
target = None
|
2024-05-13 21:36:13 +00:00
|
|
|
if blueprint_assets:
|
2024-05-16 12:09:40 +00:00
|
|
|
target = bpy.data.collections[self.target_name]
|
2024-05-12 13:10:35 +00:00
|
|
|
else:
|
2024-05-16 12:09:40 +00:00
|
|
|
target = bpy.data.scenes[self.target_name]
|
|
|
|
assets = get_user_assets(target)
|
|
|
|
asset = {"name": self.asset_name, "type": self.asset_type, "path": self.asset_path}
|
2024-05-25 08:46:14 +00:00
|
|
|
print('assets', assets, target)
|
2024-05-16 12:09:40 +00:00
|
|
|
if not does_asset_exist(target, asset):
|
2024-05-25 08:46:14 +00:00
|
|
|
print("add asset", target, asset)
|
2024-05-16 12:09:40 +00:00
|
|
|
upsert_asset(target, asset)
|
2024-05-12 13:10:35 +00:00
|
|
|
|
2024-05-16 12:09:40 +00:00
|
|
|
#assets.append({"name": self.asset_name, "type": self.asset_type, "path": self.asset_path, "internal": False})
|
2024-05-14 21:49:45 +00:00
|
|
|
# reset controls
|
|
|
|
context.window_manager.assets_registry.asset_name_selector = ""
|
|
|
|
context.window_manager.assets_registry.asset_type_selector = "MODEL"
|
|
|
|
context.window_manager.assets_registry.asset_path_selector = ""
|
2024-05-12 13:10:35 +00:00
|
|
|
|
2024-05-16 12:09:40 +00:00
|
|
|
"""if blueprint_assets:
|
2024-05-13 21:36:13 +00:00
|
|
|
bpy.data.collections[self.target_name]["assets"] = json.dumps(assets)
|
2024-05-12 13:10:35 +00:00
|
|
|
else:
|
2024-05-16 12:09:40 +00:00
|
|
|
bpy.data.scenes[self.target_name]["assets"] = json.dumps(assets)"""
|
2024-05-14 21:49:45 +00:00
|
|
|
|
2024-05-12 09:05:11 +00:00
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
2024-06-12 10:41:50 +00:00
|
|
|
class BLENVY_OT_assets_remove(Operator):
|
2024-05-12 09:05:11 +00:00
|
|
|
"""Remove asset"""
|
2024-06-12 10:41:50 +00:00
|
|
|
bl_idname = "blenvy.assets_remove"
|
2024-05-12 09:05:11 +00:00
|
|
|
bl_label = "remove bevy asset"
|
|
|
|
bl_options = {"UNDO"}
|
|
|
|
|
|
|
|
asset_path: StringProperty(
|
|
|
|
name="asset path",
|
|
|
|
description="path of asset to add",
|
|
|
|
subtype='FILE_PATH'
|
|
|
|
) # type: ignore
|
|
|
|
|
2024-05-13 21:36:13 +00:00
|
|
|
|
|
|
|
clear_all: BoolProperty (
|
|
|
|
name="clear all assets",
|
|
|
|
description="clear all assets",
|
|
|
|
default=False
|
|
|
|
) # type: ignore
|
|
|
|
|
|
|
|
# what are we targetting
|
|
|
|
target_type: EnumProperty(
|
|
|
|
name="target type",
|
|
|
|
description="type of the target: scene or blueprint to add an asset to",
|
|
|
|
items=(
|
|
|
|
('SCENE', "Scene", ""),
|
|
|
|
('BLUEPRINT', "Blueprint", ""),
|
|
|
|
),
|
|
|
|
) # type: ignore
|
|
|
|
|
|
|
|
target_name: StringProperty(
|
|
|
|
name="target name",
|
|
|
|
description="name of the target blueprint or scene to add asset to"
|
|
|
|
) # type: ignore
|
|
|
|
|
|
|
|
|
2024-05-12 09:05:11 +00:00
|
|
|
def execute(self, context):
|
2024-06-05 09:17:49 +00:00
|
|
|
print("REMOVE ASSET", self.target_name, self.target_type, self.asset_path)
|
2024-05-13 21:36:13 +00:00
|
|
|
assets = []
|
|
|
|
blueprint_assets = self.target_type == 'BLUEPRINT'
|
|
|
|
if blueprint_assets:
|
2024-05-16 12:09:40 +00:00
|
|
|
target = bpy.data.collections[self.target_name]
|
2024-05-12 13:10:35 +00:00
|
|
|
else:
|
2024-05-16 12:09:40 +00:00
|
|
|
target = bpy.data.scenes[self.target_name]
|
2024-06-05 09:17:49 +00:00
|
|
|
print("removing this", target)
|
2024-05-16 12:09:40 +00:00
|
|
|
remove_asset(target, {"path": self.asset_path})
|
|
|
|
|
2024-05-13 21:36:13 +00:00
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
from bpy_extras.io_utils import ImportHelper
|
2024-06-05 09:17:49 +00:00
|
|
|
from pathlib import Path
|
2024-05-13 21:36:13 +00:00
|
|
|
|
2024-06-12 10:41:50 +00:00
|
|
|
class BLENVY_OT_assets_browse(Operator, ImportHelper):
|
2024-05-13 21:36:13 +00:00
|
|
|
"""Browse for asset files"""
|
2024-06-12 10:41:50 +00:00
|
|
|
bl_idname = "blenvy.assets_open_filebrowser"
|
2024-05-13 21:36:13 +00:00
|
|
|
bl_label = "Select asset file"
|
|
|
|
|
|
|
|
# Define this to tell 'fileselect_add' that we want a directoy
|
|
|
|
filepath: bpy.props.StringProperty(
|
|
|
|
name="asset Path",
|
|
|
|
description="selected file",
|
|
|
|
subtype='FILE_PATH',
|
|
|
|
) # type: ignore
|
|
|
|
|
|
|
|
# Filters files
|
2024-06-05 09:17:49 +00:00
|
|
|
filter_glob: StringProperty(options={'HIDDEN'}, default='*.*') # type: ignore
|
2024-05-13 21:36:13 +00:00
|
|
|
|
2024-06-04 21:16:16 +00:00
|
|
|
def execute(self, context):
|
|
|
|
blenvy = context.window_manager.blenvy
|
|
|
|
project_root_path = blenvy.project_root_path
|
|
|
|
assets_path = blenvy.assets_path
|
2024-05-14 21:49:45 +00:00
|
|
|
# FIXME: not sure
|
2024-05-17 12:30:15 +00:00
|
|
|
print("project_root_path", project_root_path, "assets_path", assets_path)
|
|
|
|
export_assets_path_absolute = absolute_path_from_blend_file(os.path.join(project_root_path, assets_path))
|
2024-05-14 21:49:45 +00:00
|
|
|
|
2024-05-15 11:25:30 +00:00
|
|
|
asset_path = os.path.relpath(self.filepath, export_assets_path_absolute)
|
|
|
|
print("asset path", asset_path)
|
2024-05-13 21:36:13 +00:00
|
|
|
|
|
|
|
assets_registry = context.window_manager.assets_registry
|
|
|
|
assets_registry.asset_path_selector = asset_path
|
2024-06-05 09:17:49 +00:00
|
|
|
if assets_registry.asset_name_selector == "":
|
|
|
|
assets_registry.asset_name_selector = Path(os.path.basename(asset_path)).stem
|
2024-05-13 21:36:13 +00:00
|
|
|
|
|
|
|
print("SELECTED ASSET PATH", asset_path)
|
2024-06-05 09:17:49 +00:00
|
|
|
|
|
|
|
|
2024-05-13 21:36:13 +00:00
|
|
|
|
2024-05-15 21:32:01 +00:00
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from types import SimpleNamespace
|
|
|
|
|
2024-05-16 12:09:40 +00:00
|
|
|
|
2024-05-19 09:27:25 +00:00
|
|
|
def write_ron_assets_file(level_name, assets_hierarchy, internal_only=False, levels_path_full="."):
|
2024-05-16 12:09:40 +00:00
|
|
|
# just for testing, this uses the format of bevy_asset_loader's asset files
|
|
|
|
'''
|
|
|
|
({
|
|
|
|
"world":File (path: "models/StartLevel.glb"),
|
|
|
|
"level1":File (path: "models/Level1.glb"),
|
|
|
|
"level2":File (path: "models/Level2.glb"),
|
|
|
|
|
|
|
|
"models": Folder (
|
|
|
|
path: "models/library",
|
|
|
|
),
|
|
|
|
"materials": Folder (
|
|
|
|
path: "materials",
|
|
|
|
),
|
|
|
|
})
|
|
|
|
'''
|
|
|
|
formated_assets = []
|
|
|
|
for asset in assets_hierarchy:
|
|
|
|
if asset["internal"] or not internal_only:
|
|
|
|
bla = f'\n "{asset["name"]}": File ( path: "{asset["path"]}" ),'
|
|
|
|
formated_assets.append(bla)
|
2024-05-19 09:27:25 +00:00
|
|
|
with open(f"{levels_path_full}/{level_name}.assets.ron", "w") as assets_file:
|
2024-05-16 12:09:40 +00:00
|
|
|
assets_file.write("({")
|
|
|
|
assets_file.writelines(formated_assets)
|
|
|
|
assets_file.write("\n})")
|
|
|
|
|
2024-06-12 10:41:50 +00:00
|
|
|
class BLENVY_OT_assets_generate_files(Operator):
|
2024-05-15 21:32:01 +00:00
|
|
|
"""Test assets"""
|
2024-06-12 10:41:50 +00:00
|
|
|
bl_idname = "blenvy.assets_generate_files"
|
2024-05-15 21:32:01 +00:00
|
|
|
bl_label = "test bevy assets"
|
|
|
|
bl_options = {"UNDO"}
|
|
|
|
|
|
|
|
def execute(self, context):
|
2024-05-19 09:22:34 +00:00
|
|
|
blenvy = context.window_manager.blenvy
|
2024-05-20 00:06:26 +00:00
|
|
|
settings = blenvy
|
2024-05-15 21:32:01 +00:00
|
|
|
blueprints_registry = context.window_manager.blueprints_registry
|
2024-05-25 08:46:14 +00:00
|
|
|
blueprints_registry.refresh_blueprints()
|
2024-05-15 21:32:01 +00:00
|
|
|
blueprints_data = blueprints_registry.blueprints_data
|
|
|
|
|
2024-05-19 09:22:34 +00:00
|
|
|
for scene in blenvy.main_scenes:
|
|
|
|
assets_hierarchy = get_main_scene_assets_tree(scene, blueprints_data, settings)
|
|
|
|
scene["assets"] = json.dumps(assets_hierarchy)
|
2024-05-19 09:27:25 +00:00
|
|
|
write_ron_assets_file(scene.name, assets_hierarchy, internal_only = False, levels_path_full = blenvy.levels_path_full)
|
2024-05-16 12:09:40 +00:00
|
|
|
|
2024-05-15 21:32:01 +00:00
|
|
|
return {'FINISHED'}
|