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

Commit

Permalink
Merge pull request #1571 from pypeclub/feature/895-add-option-to-defi…
Browse files Browse the repository at this point in the history
…ne-paht-to-workfile-template
  • Loading branch information
mkolar authored Jun 1, 2021
2 parents 47ed425 + d96354a commit 17d8409
Show file tree
Hide file tree
Showing 32 changed files with 753 additions and 104 deletions.
15 changes: 13 additions & 2 deletions openpype/hooks/pre_add_last_workfile_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@ class AddLastWorkfileToLaunchArgs(PreLaunchHook):
This is not possible to do for all applications the same way.
"""

order = 0
app_groups = ["maya", "nuke", "nukex", "hiero", "nukestudio"]
# Execute after workfile template copy
order = 10
app_groups = [
"maya",
"nuke",
"nukex",
"hiero",
"nukestudio",
"blender",
"photoshop",
"tvpaint",
"afftereffects"
]

def execute(self):
if not self.data.get("start_last_workfile"):
Expand Down
127 changes: 127 additions & 0 deletions openpype/hooks/pre_copy_template_workfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import os
import shutil
from openpype.lib import (
PreLaunchHook,
get_custom_workfile_template_by_context,
get_custom_workfile_template_by_string_context
)
from openpype.settings import get_project_settings


class CopyTemplateWorkfile(PreLaunchHook):
"""Copy workfile template.
This is not possible to do for all applications the same way.
Prelaunch hook works only if last workfile leads to not existing file.
- That is possible only if it's first version.
"""

# Before `AddLastWorkfileToLaunchArgs`
order = 0
app_groups = ["blender", "photoshop", "tvpaint", "afftereffects"]

def execute(self):
"""Check if can copy template for context and do it if possible.
First check if host for current project should create first workfile.
Second check is if template is reachable and can be copied.
Args:
last_workfile(str): Path where template will be copied.
Returns:
None: This is a void method.
"""

last_workfile = self.data.get("last_workfile_path")
if not last_workfile:
self.log.warning((
"Last workfile was not collected."
" Can't add it to launch arguments or determine if should"
" copy template."
))
return

if os.path.exists(last_workfile):
self.log.debug("Last workfile exits. Skipping {} process.".format(
self.__class__.__name__
))
return

self.log.info("Last workfile does not exits.")

project_name = self.data["project_name"]
asset_name = self.data["asset_name"]
task_name = self.data["task_name"]

project_settings = get_project_settings(project_name)
host_settings = project_settings[self.application.host_name]

workfile_builder_settings = host_settings.get("workfile_builder")
if not workfile_builder_settings:
# TODO remove warning when deprecated
self.log.warning((
"Seems like old version of settings is used."
" Can't access custom templates in host \"{}\"."
).format(self.application.full_label))
return

if not workfile_builder_settings["create_first_version"]:
self.log.info((
"Project \"{}\" has turned off to create first workfile for"
" application \"{}\""
).format(project_name, self.application.full_label))
return

# Backwards compatibility
template_profiles = workfile_builder_settings.get("custom_templates")
if not template_profiles:
self.log.info(
"Custom templates are not filled. Skipping template copy."
)
return

project_doc = self.data.get("project_doc")
asset_doc = self.data.get("asset_doc")
anatomy = self.data.get("anatomy")
if project_doc and asset_doc:
self.log.debug("Started filtering of custom template paths.")
template_path = get_custom_workfile_template_by_context(
template_profiles, project_doc, asset_doc, task_name, anatomy
)

else:
self.log.warning((
"Global data collection probably did not execute."
" Using backup solution."
))
dbcon = self.data.get("dbcon")
template_path = get_custom_workfile_template_by_string_context(
template_profiles, project_name, asset_name, task_name,
dbcon, anatomy
)

if not template_path:
self.log.info(
"Registered custom templates didn't match current context."
)
return

if not os.path.exists(template_path):
self.log.warning(
"Couldn't find workfile template file \"{}\"".format(
template_path
)
)
return

self.log.info(
f"Creating workfile from template: \"{template_path}\""
)

# Copy template workfile to new destinantion
shutil.copy2(
os.path.normpath(template_path),
os.path.normpath(last_workfile)
)
2 changes: 1 addition & 1 deletion openpype/hosts/nuke/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def install():
# Set context settings.
nuke.addOnCreate(workfile_settings.set_context_settings, nodeClass="Root")
nuke.addOnCreate(workfile_settings.set_favorites, nodeClass="Root")
nuke.addOnCreate(lib.open_last_workfile, nodeClass="Root")
nuke.addOnCreate(lib.process_workfile_builder, nodeClass="Root")
nuke.addOnCreate(lib.launch_workfiles_app, nodeClass="Root")
menu.install()

Expand Down
73 changes: 60 additions & 13 deletions openpype/hosts/nuke/api/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from openpype.api import (
Logger,
Anatomy,
BuildWorkfile,
get_version_from_path,
get_anatomy_settings,
get_hierarchy,
Expand Down Expand Up @@ -1641,23 +1642,69 @@ def launch_workfiles_app():
workfiles.show(os.environ["AVALON_WORKDIR"])


def open_last_workfile():
def process_workfile_builder():
from openpype.lib import (
env_value_to_bool,
get_custom_workfile_template
)

# get state from settings
open_last_version = get_current_project_settings()["nuke"].get(
"general", {}).get("create_initial_workfile")
workfile_builder = get_current_project_settings()["nuke"].get(
"workfile_builder", {})

# get all imortant settings
openlv_on = env_value_to_bool(
env_key="AVALON_OPEN_LAST_WORKFILE",
default=None)

# get settings
createfv_on = workfile_builder.get("create_first_version") or None
custom_templates = workfile_builder.get("custom_templates") or None
builder_on = workfile_builder.get("builder_on_start") or None

log.info("Opening last workfile...")
last_workfile_path = os.environ.get("AVALON_LAST_WORKFILE")

if not os.path.exists(last_workfile_path):
# return if none is defined
if not open_last_version:
return
# generate first version in file not existing and feature is enabled
if createfv_on and not os.path.exists(last_workfile_path):
# get custom template path if any
custom_template_path = get_custom_workfile_template(
custom_templates
)

# if custom template is defined
if custom_template_path:
log.info("Adding nodes from `{}`...".format(
custom_template_path
))
try:
# import nodes into current script
nuke.nodePaste(custom_template_path)
except RuntimeError:
raise RuntimeError((
"Template defined for project: {} is not working. "
"Talk to your manager for an advise").format(
custom_template_path))

# if builder at start is defined
if builder_on:
log.info("Building nodes from presets...")
# build nodes by defined presets
BuildWorkfile().process()

log.info("Saving script as version `{}`...".format(
last_workfile_path
))
# safe file as version
save_file(last_workfile_path)
else:
# to avoid looping of the callback, remove it!
nuke.removeOnCreate(open_last_workfile, nodeClass="Root")
return

# open workfile
open_file(last_workfile_path)
# skip opening of last version if it is not enabled
if not openlv_on or not os.path.exists(last_workfile_path):
return

# to avoid looping of the callback, remove it!
nuke.removeOnCreate(process_workfile_builder, nodeClass="Root")

log.info("Opening last workfile...")
# open workfile
open_file(last_workfile_path)
50 changes: 1 addition & 49 deletions openpype/hosts/tvpaint/hooks/pre_launch_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@ def execute(self):
"run", self.launch_script_path(), executable_path
)

# Add workfile to launch arguments
workfile_path = self.workfile_path()
if workfile_path:
new_launch_args.append(workfile_path)

# How to create new command line
# if platform.system().lower() == "windows":
# new_launch_args = [
# "cmd.exe",
# "/c",
# "Call cmd.exe /k",
# *new_launch_args
# ]

# Append as whole list as these areguments should not be separated
self.launch_context.launch_args.append(new_launch_args)

Expand All @@ -64,38 +50,4 @@ def launch_script_path(self):
"tvpaint",
"launch_script.py"
)
return script_path

def workfile_path(self):
workfile_path = self.data["last_workfile_path"]

# copy workfile from template if doesnt exist any on path
if not os.path.exists(workfile_path):
# TODO add ability to set different template workfile path via
# settings
pype_dir = os.path.dirname(os.path.abspath(tvpaint.__file__))
template_path = os.path.join(
pype_dir, "resources", "template.tvpp"
)

if not os.path.exists(template_path):
self.log.warning(
"Couldn't find workfile template file in {}".format(
template_path
)
)
return

self.log.info(
f"Creating workfile from template: \"{template_path}\""
)

# Copy template workfile to new destinantion
shutil.copy2(
os.path.normpath(template_path),
os.path.normpath(workfile_path)
)

self.log.info(f"Workfile to open: \"{workfile_path}\"")

return workfile_path
return script_path
12 changes: 11 additions & 1 deletion openpype/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@

get_creator_by_name,

change_timer_to_current_context
get_custom_workfile_template,

change_timer_to_current_context,

get_custom_workfile_template_by_context,
get_custom_workfile_template_by_string_context,
get_custom_workfile_template
)

from .local_settings import (
Expand Down Expand Up @@ -192,6 +198,10 @@

"change_timer_to_current_context",

"get_custom_workfile_template_by_context",
"get_custom_workfile_template_by_string_context",
"get_custom_workfile_template",

"IniSettingRegistry",
"JSONSettingRegistry",
"OpenPypeSecureRegistry",
Expand Down
Loading

0 comments on commit 17d8409

Please sign in to comment.