Asset Pipeline v2 #145
@ -29,6 +29,7 @@ class ASSETPIPE_OT_create_new_asset(bpy.types.Operator):
|
||||
_name = ""
|
||||
_prefix = ""
|
||||
_json_path = None
|
||||
_asset_pipe = None
|
||||
|
||||
create_files: bpy.props.BoolProperty(
|
||||
name="Create Files for Unselected Task Layers", default=True
|
||||
@ -49,12 +50,12 @@ class ASSETPIPE_OT_create_new_asset(bpy.types.Operator):
|
||||
|
||||
def invoke(self, context: bpy.types.Context, event):
|
||||
# Dynamically Create Task Layer Bools
|
||||
asset_pipe = context.scene.asset_pipeline
|
||||
self._asset_pipe = context.scene.asset_pipeline
|
||||
|
||||
# TODO Check if this fails
|
||||
config.verify_json_data(Path(asset_pipe.task_layer_config_type))
|
||||
config.verify_json_data(Path(self._asset_pipe.task_layer_config_type))
|
||||
|
||||
all_task_layers = asset_pipe.all_task_layers
|
||||
all_task_layers = self._asset_pipe.all_task_layers
|
||||
all_task_layers.clear()
|
||||
|
||||
for task_layer_key in config.TASK_LAYER_TYPES:
|
||||
@ -66,34 +67,47 @@ class ASSETPIPE_OT_create_new_asset(bpy.types.Operator):
|
||||
|
||||
def draw(self, context: bpy.types.Context):
|
||||
box = self.layout.box()
|
||||
all_task_layers = context.scene.asset_pipeline.all_task_layers
|
||||
all_task_layers = self._asset_pipe.all_task_layers
|
||||
|
||||
box.label(text="Choose Which Task Layers will be local the current file")
|
||||
for task_layer_bool in all_task_layers:
|
||||
box.prop(task_layer_bool, "is_local", text=task_layer_bool.name)
|
||||
self.layout.prop(self, "create_files")
|
||||
|
||||
def execute(self, context: bpy.types.Context):
|
||||
asset_pipe = context.scene.asset_pipeline
|
||||
if asset_pipe.new_file_mode == "KEEP":
|
||||
# New File is Createed so Props need to be saved
|
||||
asset_col = asset_pipe.asset_collection
|
||||
self._name == "FOO"
|
||||
self._name = (
|
||||
def _asset_name_set(self, context) -> None:
|
||||
if self._asset_pipe.new_file_mode == "KEEP":
|
||||
asset_col = self._asset_pipe.asset_collection
|
||||
name = (
|
||||
asset_col.name
|
||||
if "-" not in asset_col.name
|
||||
else asset_col.name.split("-", 1)[1]
|
||||
)
|
||||
asset_path = Path(bpy.data.filepath).parent.__str__()
|
||||
prefix = (
|
||||
"" if "-" not in asset_col.name else asset_col.name.split("-", 1)[0]
|
||||
)
|
||||
|
||||
else:
|
||||
self._name = asset_pipe.name
|
||||
user_dir = bpy.path.abspath(asset_pipe.dir)
|
||||
self._prefix = asset_pipe.prefix
|
||||
# Create Asset Folder at Directory
|
||||
asset_path = os.path.join(user_dir, self._name)
|
||||
name = self._asset_pipe.name
|
||||
prefix = self._asset_pipe.prefix
|
||||
|
||||
all_task_layers = context.scene.asset_pipeline.all_task_layers
|
||||
# Set to easily access these properties
|
||||
self._name = name
|
||||
self._prefix = prefix
|
||||
|
||||
# Store these in the asset pipeline props group
|
||||
self._asset_pipe.name = name
|
||||
self._asset_pipe.prefix = prefix
|
||||
|
||||
def _asset_dir_get(self, context) -> str:
|
||||
if self._asset_pipe.new_file_mode == "KEEP":
|
||||
return Path(bpy.data.filepath).parent.__str__()
|
||||
|
||||
else:
|
||||
user_dir = bpy.path.abspath(self._asset_pipe.dir)
|
||||
return os.path.join(user_dir, self._name)
|
||||
|
||||
def _load_task_layers(self, context):
|
||||
all_task_layers = self._asset_pipe.all_task_layers
|
||||
local_tls = []
|
||||
for task_layer_bool in all_task_layers:
|
||||
if task_layer_bool.is_local:
|
||||
@ -105,12 +119,11 @@ class ASSETPIPE_OT_create_new_asset(bpy.types.Operator):
|
||||
"Please select at least one task layer to be local to the current file",
|
||||
)
|
||||
return {'CANCELLED'}
|
||||
return local_tls
|
||||
|
||||
if not os.path.exists(asset_path):
|
||||
os.mkdir(asset_path)
|
||||
|
||||
def _create_publish_directories(self, context, asset_directory):
|
||||
for publish_type in constants.PUBLISH_KEYS:
|
||||
new_dir_path = os.path.join(asset_path, publish_type)
|
||||
new_dir_path = os.path.join(asset_directory, publish_type)
|
||||
if os.path.exists(new_dir_path):
|
||||
self.report(
|
||||
{'ERROR'},
|
||||
@ -119,56 +132,22 @@ class ASSETPIPE_OT_create_new_asset(bpy.types.Operator):
|
||||
return {'CANCELLED'}
|
||||
os.mkdir(new_dir_path)
|
||||
|
||||
# TODO Save Task Layer Config File
|
||||
config.write_json_file(
|
||||
asset_path=Path(asset_path),
|
||||
source_file_path=Path(asset_pipe.task_layer_config_type),
|
||||
)
|
||||
|
||||
# Setup Base File
|
||||
asset_pipe = context.scene.asset_pipeline
|
||||
asset_pipe.is_asset_pipeline_file = True
|
||||
|
||||
if asset_pipe.new_file_mode == "KEEP":
|
||||
asset_col = asset_pipe.asset_collection
|
||||
def _asset_collection_get(self, context, local_tls):
|
||||
if self._asset_pipe.new_file_mode == "KEEP":
|
||||
asset_col = self._asset_pipe.asset_collection
|
||||
for col in asset_col.children:
|
||||
col.asset_id_owner = local_tls[0]
|
||||
else:
|
||||
bpy.data.collections.new(self._name)
|
||||
asset_col = bpy.data.collections.get(self._name)
|
||||
context.scene.collection.children.link(asset_col)
|
||||
asset_pipe.asset_collection = asset_col
|
||||
self._asset_pipe.asset_collection = asset_col
|
||||
return asset_col
|
||||
|
||||
asset_pipe.name = self._name
|
||||
asset_pipe.prefix = self._prefix
|
||||
|
||||
for task_layer_key in config.TASK_LAYER_TYPES:
|
||||
bpy.data.collections.new(task_layer_key)
|
||||
task_layer_col = bpy.data.collections.get(task_layer_key)
|
||||
task_layer_col.asset_id_owner = task_layer_key
|
||||
asset_col.children.link(task_layer_col)
|
||||
|
||||
starting_file = ""
|
||||
if bpy.data.filepath != "":
|
||||
first_file_name = Path(bpy.data.filepath).name
|
||||
else:
|
||||
first_file_name = self._name + "." + local_tls[0].lower() + ".blend"
|
||||
first_file = os.path.join(asset_path, first_file_name)
|
||||
|
||||
set_local_task_layers(local_tls)
|
||||
|
||||
bpy.ops.wm.save_as_mainfile(filepath=first_file, copy=True)
|
||||
starting_file = first_file
|
||||
|
||||
# Create the file for each task layer.
|
||||
for task_layer_key in config.TASK_LAYER_TYPES:
|
||||
if task_layer_key == "NONE" or task_layer_key in local_tls:
|
||||
continue
|
||||
name = self._name + "." + task_layer_key.lower() + ".blend"
|
||||
|
||||
# Remove Data From Newly Created Files that aren't the user's main file
|
||||
def _remove_collections(self, context):
|
||||
# Remove Data From task layer Files except for asset_collection
|
||||
for col in bpy.data.collections:
|
||||
if not col == asset_col:
|
||||
if not col == self._asset_pipe.asset_collection:
|
||||
bpy.data.collections.remove(col)
|
||||
for obj in bpy.data.objects:
|
||||
bpy.data.objects.remove(obj)
|
||||
@ -177,17 +156,73 @@ class ASSETPIPE_OT_create_new_asset(bpy.types.Operator):
|
||||
do_local_ids=True, do_linked_ids=False, do_recursive=True
|
||||
)
|
||||
|
||||
def _task_layer_collections_set(self, context, asset_col):
|
||||
for task_layer_key in config.TASK_LAYER_TYPES:
|
||||
bpy.data.collections.new(task_layer_key)
|
||||
task_layer_col = bpy.data.collections.get(task_layer_key)
|
||||
task_layer_col.asset_id_owner = task_layer_key
|
||||
asset_col.children.link(task_layer_col)
|
||||
|
||||
def _first_file_create(self, context, local_tls, asset_directory) -> str:
|
||||
self._asset_pipe.is_asset_pipeline_file = True
|
||||
|
||||
asset_col = self._asset_collection_get(context, local_tls)
|
||||
self._task_layer_collections_set(context, asset_col)
|
||||
|
||||
if bpy.data.filepath != "":
|
||||
first_file_name = Path(bpy.data.filepath).name
|
||||
else:
|
||||
first_file_name = self._name + "." + local_tls[0].lower() + ".blend"
|
||||
|
||||
first_file = os.path.join(asset_directory, first_file_name)
|
||||
|
||||
set_local_task_layers(local_tls)
|
||||
|
||||
bpy.ops.wm.save_as_mainfile(filepath=first_file, copy=True)
|
||||
return first_file
|
||||
|
||||
def _task_layer_file_create(self, context, task_layer_key, asset_directory):
|
||||
name = self._name + "." + task_layer_key.lower() + ".blend"
|
||||
|
||||
set_local_task_layers([task_layer_key])
|
||||
|
||||
task_layer_file = os.path.join(asset_path, name)
|
||||
task_layer_file = os.path.join(asset_directory, name)
|
||||
bpy.ops.wm.save_as_mainfile(filepath=task_layer_file, copy=True)
|
||||
|
||||
# Create intial publish based on task layers.
|
||||
publish_path = os.path.join(asset_path, constants.ACTIVE_PUBLISH_KEY)
|
||||
def _publish_file_create(self, context, asset_directory):
|
||||
publish_path = os.path.join(asset_directory, constants.ACTIVE_PUBLISH_KEY)
|
||||
name = self._name + "." + "v001" + ".blend"
|
||||
asset_pipe.asset_collection.asset_mark()
|
||||
self._asset_pipe.asset_collection.asset_mark()
|
||||
publish_file = os.path.join(publish_path, name)
|
||||
bpy.ops.wm.save_as_mainfile(filepath=publish_file, copy=True)
|
||||
|
||||
def execute(self, context: bpy.types.Context):
|
||||
self._asset_name_set(context)
|
||||
asset_directory = self._asset_dir_get(context)
|
||||
local_tls = self._load_task_layers(context)
|
||||
|
||||
if not os.path.exists(asset_directory):
|
||||
os.mkdir(asset_directory)
|
||||
|
||||
self._create_publish_directories(context, asset_directory)
|
||||
|
||||
# Save Task Layer Config File
|
||||
config.write_json_file(
|
||||
asset_path=Path(asset_directory),
|
||||
source_file_path=Path(self._asset_pipe.task_layer_config_type),
|
||||
)
|
||||
|
||||
starting_file = self._first_file_create(context, local_tls, asset_directory)
|
||||
|
||||
for task_layer_key in config.TASK_LAYER_TYPES:
|
||||
if task_layer_key == "NONE" or task_layer_key in local_tls:
|
||||
continue
|
||||
self._remove_collections(context)
|
||||
self._task_layer_file_create(context, task_layer_key, asset_directory)
|
||||
|
||||
# Create intial publish based on task layers.
|
||||
self._remove_collections(context)
|
||||
self._publish_file_create(context, asset_directory)
|
||||
if starting_file:
|
||||
bpy.ops.wm.open_mainfile(filepath=starting_file)
|
||||
return {'FINISHED'}
|
||||
|
Loading…
Reference in New Issue
Block a user