Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Auto generate first workfile based on workfile template
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyprien CAILLOT committed Sep 11, 2024
1 parent d9731d0 commit 08570ac
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 4 deletions.
17 changes: 16 additions & 1 deletion openpype/hosts/maya/api/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
AVALON_CONTAINER_ID,
)
from openpype.pipeline.load import any_outdated_containers
from openpype.pipeline.workfile.workfile_template_builder import (
is_last_workfile_exists,
should_build_first_workfile
)
from openpype.pipeline.workfile.lock_workfile import (
create_workfile_lock,
remove_workfile_lock,
Expand All @@ -47,7 +51,10 @@
from openpype.hosts.maya.lib import create_workspace_mel

from . import menu, lib
from .workfile_template_builder import MayaPlaceholderLoadPlugin
from .workfile_template_builder import (
MayaPlaceholderLoadPlugin,
build_workfile_template
)
from .workio import (
open_file,
save_file,
Expand Down Expand Up @@ -580,11 +587,18 @@ def on_save():
lib.set_id(node, new_id, overwrite=False)


def _autobuild_first_workfile():
if not is_last_workfile_exists() and should_build_first_workfile():
build_workfile_template()


def on_open():
"""On scene open let's assume the containers have changed."""

from openpype.widgets import popup

utils.executeDeferred(_autobuild_first_workfile)

# Validate FPS after update_task_from_path to
# ensure it is using correct FPS for the asset
lib.validate_fps()
Expand Down Expand Up @@ -621,6 +635,7 @@ def on_new():
with lib.suspended_refresh():
lib.set_context_settings()

utils.executeDeferred(_autobuild_first_workfile)
_remove_workfile_lock()


Expand Down
85 changes: 82 additions & 3 deletions openpype/pipeline/workfile/workfile_template_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
get_linked_assets,
get_representations,
)
from openpype.client.entities import get_projects
from openpype.settings import (
get_project_settings,
get_system_settings,
Expand All @@ -49,6 +50,13 @@
CreateContext,
)

from openpype.pipeline.context_tools import (
get_current_asset_name,
get_current_project_name,
get_current_task_name,
get_current_host_name
)


class TemplateNotFound(Exception):
"""Exception raised when template does not exist."""
Expand Down Expand Up @@ -577,14 +585,14 @@ def create_first_workfile_version(self):
template_path (str): Fullpath for current task and
host's template file.
"""
last_workfile_path = os.environ.get("AVALON_LAST_WORKFILE")
last_workfile_path = get_last_workfile_path()
self.log.info("__ last_workfile_path: {}".format(last_workfile_path))
if os.path.exists(last_workfile_path):
if is_last_workfile_exists():
# ignore in case workfile existence
self.log.info("Workfile already exists, skipping creation.")
return False

# Create first version
# Create the first version
self.log.info("Creating first version of workfile.")
self.save_workfile(last_workfile_path)

Expand Down Expand Up @@ -2047,3 +2055,74 @@ def get_errors(self):

def create_failed(self, creator_data):
self._failed_created_publish_instances.append(creator_data)


def get_library_project_names():
libraries = list()

for project in get_projects(fields=["name", "data.library_project"]):
if project.get("data", {}).get("library_project", False):
libraries.append(project["name"])

return libraries


def should_build_first_workfile(
project_name=None,
project_settings=None,
asset_doc=None,
asset_name=None,
task_name=None,
host_name=None
):
"""Return whether first workfile should be created for given context"""

project_name = project_name or get_current_project_name()
if project_settings is None:
project_settings = get_project_settings(project_name)

host_name = host_name or get_current_host_name()
build_workfile_profiles = project_settings[host_name]["templated_workfile_build"] # noqa

if not build_workfile_profiles['profiles']:
return False

asset_name = asset_name or get_current_asset_name()
asset_doc = asset_doc or get_asset_by_name(project_name, asset_name)
task_name = task_name or get_current_task_name()
current_tasks = asset_doc.get("data").get("tasks")
task_type = current_tasks.get(task_name).get("type")

filtering_criteria = {
"task_names": task_name,
"task_types": task_type
}

profile = filter_profiles(
build_workfile_profiles["profiles"],
filtering_criteria
)

if not profile or not profile.get("create_first_version"):
return False

is_task_name = task_name in profile["task_names"]
is_task_type = task_type in profile["task_types"]

if not is_task_name and not is_task_type:
return False

return True


def get_last_workfile_path():
return os.environ.get("AVALON_LAST_WORKFILE")


def is_last_workfile_exists():
last_workfile_path = get_last_workfile_path()

if last_workfile_path and os.path.exists(last_workfile_path):
return True

return False

0 comments on commit 08570ac

Please sign in to comment.