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)
|
|
|
|
|
|
|
|
from ..settings import load_settings
|
2024-05-12 09:05:11 +00:00
|
|
|
|
|
|
|
class OT_add_bevy_asset(Operator):
|
|
|
|
"""Add asset"""
|
|
|
|
bl_idname = "bevyassets.add"
|
|
|
|
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
|
|
|
assets = []
|
|
|
|
blueprint_assets = self.target_type == 'BLUEPRINT'
|
|
|
|
print("FOOO", self.target_name, self.target_type)
|
|
|
|
if blueprint_assets:
|
|
|
|
assets = json.loads(bpy.data.collections[self.target_name].get('assets')) if 'assets' in bpy.data.collections[self.target_name] else []
|
2024-05-12 13:10:35 +00:00
|
|
|
else:
|
2024-05-13 21:36:13 +00:00
|
|
|
assets = json.loads(bpy.data.scenes[self.target_name].get('assets')) if 'assets' in bpy.data.scenes[self.target_name] else []
|
2024-05-12 13:10:35 +00:00
|
|
|
|
2024-05-13 21:36:13 +00:00
|
|
|
in_list = [asset for asset in assets if (asset["path"] == self.asset_path)]
|
2024-05-12 13:10:35 +00:00
|
|
|
in_list = len(in_list) > 0
|
|
|
|
if not in_list:
|
2024-05-13 21:36:13 +00:00
|
|
|
assets.append({"name": self.asset_name, "type": self.asset_type, "path": self.asset_path, "internal": False})
|
2024-05-12 13:10:35 +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-13 21:36:13 +00:00
|
|
|
bpy.data.scenes[self.target_name]["assets"] = json.dumps(assets)
|
2024-05-12 13:10:35 +00:00
|
|
|
#context.window_manager.assets_registry.add_asset(self.asset_name, self.asset_type, self.asset_path, False)
|
2024-05-12 09:05:11 +00:00
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
|
|
class OT_remove_bevy_asset(Operator):
|
|
|
|
"""Remove asset"""
|
|
|
|
bl_idname = "bevyassets.remove"
|
|
|
|
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-05-13 21:36:13 +00:00
|
|
|
assets = []
|
|
|
|
blueprint_assets = self.target_type == 'BLUEPRINT'
|
|
|
|
if blueprint_assets:
|
|
|
|
assets = json.loads(bpy.data.collections[self.target_name].get('assets')) if 'assets' in bpy.data.collections[self.target_name] else []
|
2024-05-12 13:10:35 +00:00
|
|
|
else:
|
2024-05-13 21:36:13 +00:00
|
|
|
assets = json.loads(bpy.data.scenes[self.target_name].get('assets')) if 'assets' in bpy.data.scenes[self.target_name] else []
|
2024-05-12 13:10:35 +00:00
|
|
|
|
2024-05-13 21:36:13 +00:00
|
|
|
assets = [asset for asset in assets if (asset["path"] != self.asset_path)]
|
2024-05-12 13:10:35 +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-13 21:36:13 +00:00
|
|
|
bpy.data.scenes[self.target_name]["assets"] = json.dumps(assets)
|
2024-05-12 13:10:35 +00:00
|
|
|
#context.window_manager.assets_registry.remove_asset(self.asset_path)
|
2024-05-13 21:36:13 +00:00
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
from bpy_extras.io_utils import ImportHelper
|
|
|
|
|
|
|
|
class OT_Add_asset_filebrowser(Operator, ImportHelper):
|
|
|
|
"""Browse for asset files"""
|
|
|
|
bl_idname = "asset.open_filebrowser"
|
|
|
|
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
|
|
|
|
filter_glob: StringProperty(options={'HIDDEN'}, default='*.jpg;*.jpeg;*.png;*.bmp') # type: ignore
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
current_auto_settings = load_settings(".gltf_auto_export_settings")
|
|
|
|
export_root_folder = current_auto_settings.get("export_root_folder")
|
|
|
|
asset_path = os.path.relpath(self.filepath, export_root_folder)
|
|
|
|
|
|
|
|
assets_registry = context.window_manager.assets_registry
|
|
|
|
assets_registry.asset_path_selector = asset_path
|
|
|
|
|
|
|
|
print("SELECTED ASSET PATH", asset_path)
|
|
|
|
|
2024-05-12 09:05:11 +00:00
|
|
|
return {'FINISHED'}
|