Asset Pipeline v2 #145
@ -1,8 +1,8 @@
|
||||
import bpy
|
||||
|
||||
from . import asset_suffix, transferable_data, util
|
||||
from . import asset_suffix, transferable_data, id_remap
|
||||
from pathlib import Path
|
||||
from .constants import TASK_LAYER_ITEMS
|
||||
|
||||
|
||||
# TODO refactor merge functions into a class based on AssetBuilder class of Asset Pipeline 1
|
||||
|
||||
@ -67,7 +67,7 @@ def merge_task_layer(
|
||||
|
||||
transferable_data.apply_transfer_data(context, transfer_data, target_col)
|
||||
|
||||
remap_external_datablocks(context)
|
||||
id_remap.remap_external_datablocks(context)
|
||||
|
||||
bpy.ops.outliner.orphans_purge(
|
||||
do_local_ids=True, do_linked_ids=False, do_recursive=True
|
||||
@ -127,39 +127,3 @@ def import_data_from_lib(
|
||||
)
|
||||
|
||||
return eval(f"bpy.data.{data_category}['{data_name}']")
|
||||
|
||||
|
||||
### REMAPPING IDS
|
||||
def remap_get_data_blocks(context, suffix):
|
||||
retun_datablocks = []
|
||||
for datablock in bpy.data.user_map():
|
||||
has_type = hasattr(datablock, 'type')
|
||||
# TODO IMPROVE FILTERING?
|
||||
if not (
|
||||
has_type
|
||||
and datablock.type == 'OBJECT'
|
||||
and datablock.name not in context.scene.objects
|
||||
):
|
||||
if datablock.name.endswith(suffix):
|
||||
retun_datablocks.append(datablock)
|
||||
return retun_datablocks
|
||||
|
||||
|
||||
def remap_user(datablock, target_name):
|
||||
storage = util.get_storage_of_id(datablock)
|
||||
remap_datablock = storage.get(target_name)
|
||||
if remap_datablock:
|
||||
remap_datablock.user_remap(datablock)
|
||||
remap_datablock.name += "_Users_Remapped"
|
||||
|
||||
|
||||
def remap_external_datablocks(context):
|
||||
# TODO STANDARDIZE IF ASSET SHOULD HAVE . IN SUFFIX OR NOT,
|
||||
# ADD A CORE FUNCTION FOR APPENDING SUFFIX TO SINGLE ITEM
|
||||
external_suffix = ".EXTERNAL"
|
||||
target_suffix = ".LOCAL"
|
||||
|
||||
datablocks = remap_get_data_blocks(context, external_suffix)
|
||||
for datablock in datablocks:
|
||||
target_name = datablock.name.replace(external_suffix, target_suffix)
|
||||
remap_user(datablock, target_name)
|
||||
|
70
scripts-blender/addons/asset_pipeline_2/id_remap.py
Normal file
70
scripts-blender/addons/asset_pipeline_2/id_remap.py
Normal file
@ -0,0 +1,70 @@
|
||||
import bpy
|
||||
|
||||
from . import util
|
||||
from bpy.types import bpy_prop_collection
|
||||
|
||||
# TODO CHECK OTHER DATA TYPES OUTSIDE OF OBJECTS
|
||||
|
||||
|
||||
### REMAPPING IDS
|
||||
def get_users(col, ID):
|
||||
ret = tuple(repr(o) for o in col if o.user_of_id(ID))
|
||||
return [eval(item) for item in ret] if ret else None
|
||||
|
||||
|
||||
def get_users_of_id(ID):
|
||||
users_of_id = []
|
||||
for p in dir(bpy.data):
|
||||
if isinstance(getattr(bpy.data, p, None), bpy_prop_collection):
|
||||
users = get_users(getattr(bpy.data, p), ID)
|
||||
if users:
|
||||
for user in users:
|
||||
users_of_id.append(user)
|
||||
return users_of_id
|
||||
|
||||
|
||||
def get_users_in_scene(context, ID):
|
||||
users_in_scene = []
|
||||
users = get_users_of_id(ID)
|
||||
for user in users:
|
||||
has_type = hasattr(user, 'type')
|
||||
if has_type and user in list(context.scene.objects):
|
||||
users_in_scene.append(user)
|
||||
return users_in_scene
|
||||
|
||||
|
||||
def remap_get_data_blocks(context):
|
||||
retun_datablocks = []
|
||||
for obj in bpy.data.objects:
|
||||
references_in_scene = get_users_in_scene(context, obj)
|
||||
if references_in_scene and obj not in list(context.scene.objects):
|
||||
retun_datablocks.append(obj)
|
||||
return retun_datablocks
|
||||
|
||||
|
||||
def remap_user(datablock, target_name):
|
||||
storage = util.get_storage_of_id(datablock)
|
||||
remap_datablock = storage.get(target_name)
|
||||
if remap_datablock:
|
||||
datablock.user_remap(remap_datablock)
|
||||
datablock.name += "_Users_Remapped"
|
||||
|
||||
|
||||
def get_opposite_suffix(suffix):
|
||||
# TODO Creating a map would be easier that doing this on the fly
|
||||
if suffix.endswith("EXTERNAL"):
|
||||
return ".LOCAL"
|
||||
if suffix.endswith("LOCAL"):
|
||||
return ".EXTERNAL"
|
||||
|
||||
|
||||
def remap_external_datablocks(context):
|
||||
# TODO STANDARDIZE IF ASSET SHOULD HAVE . IN SUFFIX OR NOT,
|
||||
# ADD A CORE FUNCTION FOR APPENDING SUFFIX TO SINGLE ITEM
|
||||
datablocks = remap_get_data_blocks(context)
|
||||
for datablock in datablocks:
|
||||
current_suffix = "." + datablock.name.split(".")[-1]
|
||||
target_suffix = get_opposite_suffix(datablock.name)
|
||||
target_name = datablock.name.replace(current_suffix, target_suffix)
|
||||
print(f"Will remap {datablock.name} to {target_name}")
|
||||
remap_user(datablock, target_name)
|
Loading…
Reference in New Issue
Block a user