Asset Pipeline: Store Asset Catalog in String #230

Merged
Nick Alberelli merged 6 commits from TinyNick/blender-studio-pipeline:fix/asset-catalog into main 2024-02-15 22:54:08 +01:00
Showing only changes of commit 467918c6ac - Show all commits

View File

@ -1,15 +1,22 @@
import os import os
from pathlib import Path from pathlib import Path
import bpy import bpy
from typing import List
asset_file_cache = None asset_file_cache = None
cat_data_cache = None cat_data_cache = None
asset_cat_dict = {}
# TODO add refresh operator def find_asset_cat_file(directory: str) -> str:
"""Find Asset Catalog file in directory or parent directories, recursively
Args:
directory (str): Directory to search for Asset Catalog file
def find_asset_cat_file(directory): Returns:
str: Path to Asset Catalog file or None if not found
"""
global asset_file_cache global asset_file_cache
if asset_file_cache is not None: if asset_file_cache is not None:
return asset_file_cache return asset_file_cache
@ -24,16 +31,36 @@ def find_asset_cat_file(directory):
return find_asset_cat_file(parent_dir) return find_asset_cat_file(parent_dir)
def get_asset_cat_enum_items(reload: bool = False): def get_asset_catalog_items(reload: bool = False) -> List[str]:
"""Generate List of Asset Catalog Items, and Dictionary of
Asset Catalog UUIDs with their matching names. When this function
is called the list and dict of asset catalog items will be updated.
The List is used in the UI to populate the Asset Catalog List, and the
Dictionary is used to look up the asset catalog UUID based on the name.
Args:
reload (bool, optional): Forces reload of list/dict if True. Defaults to False.
Returns:
List[str]: Returns list of strings representing Asset Catalog Names
"""
global cat_data_cache global cat_data_cache
if cat_data_cache is not None and not reload: global asset_cat_dict
return cat_data_cache asset_cat_list = []
items = []
items.append(('NONE', 'None', '')) # Return Empty List if File doesn't exist
asset_cat_file = find_asset_cat_file(Path(bpy.data.filepath).parent.__str__()) asset_cat_file = find_asset_cat_file(Path(bpy.data.filepath).parent.__str__())
if asset_cat_file is None: if asset_cat_file is None:
return items return asset_cat_list
# Return Cached List if exists and reload is False
if cat_data_cache is not None and not reload:
return cat_data_cache
asset_cat_dict.clear() # Reset dict so it is in sync with name list
# Loop over items in file to find asset catalog
with (Path(asset_cat_file)).open() as file: with (Path(asset_cat_file)).open() as file:
for line in file.readlines(): for line in file.readlines():
if line.startswith(("#", "VERSION", "\n")): if line.startswith(("#", "VERSION", "\n")):
@ -41,6 +68,23 @@ def get_asset_cat_enum_items(reload: bool = False):
# Each line contains : 'uuid:catalog_tree:catalog_name' + eol ('\n') # Each line contains : 'uuid:catalog_tree:catalog_name' + eol ('\n')
name = line.split(':', 1)[1].split(":")[-1].strip("\n") name = line.split(':', 1)[1].split(":")[-1].strip("\n")
uuid = line.split(':', 1)[0] uuid = line.split(':', 1)[0]
asset_cat_dict[uuid] = name # Populate dict of uuid:name
asset_cat_list.append(name) # Populate list of asset catalogue names
items.append((uuid, name, '')) cat_data_cache = asset_cat_list # Update Cache List
return items return asset_cat_list
def get_asset_id(name: str) -> str:
"""Get Asset Catalog UUID based on Asset Catalog Name
Args:
name (str): Asset Catalog Name
Returns:
str: Asset Catalog UUID or None if not found
"""
global asset_cat_dict
for key, value in asset_cat_dict.items():
if value == name:
return key