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