Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions client/ayon_nuke/api/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
env_value_to_bool,
Logger,
StringTemplate,
BoolDef,
UILabelDef,
)

from ayon_core.tools.attribute_defs.dialog import (
AttributeDefinitionsDialog
)

from ayon_core.settings import (
Expand All @@ -44,6 +50,7 @@
AVALON_INSTANCE_ID,
get_current_context,
)
from ayon_core.pipeline.create import CreateContext
from ayon_core.pipeline.load import filter_containers
from ayon_core.pipeline.colorspace import (
get_current_context_imageio_config_preset
Expand Down Expand Up @@ -2568,6 +2575,113 @@ def _launch_workfile_app():
host_tools.show_workfiles(parent=None, on_top=True)


def update_content_on_context_change():
"""Update creator instances when the current folder/task changes."""
host = registered_host()
create_context = CreateContext(host, discover_publish_plugins=False)

project_entity = create_context.get_current_project_entity()
folder_entity = create_context.get_current_folder_entity()
task_entity = create_context.get_current_task_entity()

current_folder_path = folder_entity["path"]
current_task = task_entity["name"]

project_name = project_entity["name"]
host_name = create_context.host_name

_changed = False

for instance in create_context.instances:
if instance.creator_identifier == "workfile":
continue

if (
instance.get("folderPath") == current_folder_path
and instance.get("task") == current_task
):
continue

creator = create_context.creators[instance.creator_identifier]

product_name = creator.get_product_name(
project_name=project_name,
project_entity=project_entity,
folder_entity=folder_entity,
task_entity=task_entity,
variant=instance.get("variant"),
host_name=host_name,
)

instance["folderPath"] = current_folder_path
instance["task"] = current_task
instance["productName"] = product_name

_changed = True

if _changed:
create_context.save_changes()


def prompt_reset_context():
"""Prompt the user what context settings to reset.
This prompt is used on saving to a different task to allow the scene to
get matched to the new context.
"""
definitions = [
UILabelDef(
label=(
"You are saving your workfile into a different folder or task."
"\n\n"
"Would you like to update some settings to the new context?\n"
)
),
BoolDef(
"resolution",
label="Resolution",
tooltip="Reset workfile resolution",
default=True
),
BoolDef(
"frame_range_fps",
label="Frame Range / FPS",
tooltip="Reset workfile start/end frame ranges and fps",
default=True
),
BoolDef(
"colorspace",
label="Colorspace",
tooltip="Reset workfile colorspace",
default=True
),
BoolDef(
"instances",
label="Publish instances",
tooltip="Update all publish instance's folder and task to match "
"the new folder and task",
default=True
),
]

dialog = AttributeDefinitionsDialog(definitions)
dialog.setWindowTitle("Saving to different context.")
if not dialog.exec_():
return None

options = dialog.get_values()

if options["resolution"]:
WorkfileSettings().reset_resolution()
if options["frame_range_fps"]:
WorkfileSettings().reset_frame_range_handles()
if options["colorspace"]:
WorkfileSettings().set_colorspace()
if options["instances"]:
update_content_on_context_change()

dialog.deleteLater()


def start_workfile_template_builder():
from .workfile_template_builder import (
build_workfile_template
Expand Down
37 changes: 36 additions & 1 deletion client/ayon_nuke/api/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ayon_core.host import (
ApplicationInformation,
ContextChangeData,
HostBase,
IWorkfileHost,
ILoadHost,
Expand Down Expand Up @@ -55,6 +56,7 @@
set_avalon_knob_data,
read_avalon_data,
on_script_load,
prompt_reset_context,
dirmap_file_name_filter,
add_scripts_menu,
add_scripts_gizmo,
Expand Down Expand Up @@ -92,6 +94,9 @@
if os.getenv("PYBLISH_GUI", None):
pyblish.api.register_gui(os.getenv("PYBLISH_GUI", None))

# Track whether the workfile tool is about to save
_about_to_save = False


class NukeHost(
HostBase, IWorkfileHost, ILoadHost, IPublishHost
Expand Down Expand Up @@ -138,8 +143,9 @@ def install(self):
register_workfile_build_plugin_path(WORKFILE_BUILD_PATH)

# Register AYON event for workfiles loading.
register_event_callback("workfile.save.before", before_workfile_save)
register_event_callback("workfile.save.after", after_workfile_save)
register_event_callback("workio.open_file", check_inventory_versions)
register_event_callback("taskChanged", change_context_label)

def setup_ui_callbacks_and_menu(self):
"""Setup AYON menus."""
Expand All @@ -162,6 +168,25 @@ def update_context_data(self, data, changes):
root_node = nuke.root()
set_node_data(root_node, ROOT_DATA_KNOB, data)

def _after_context_change(self, context_change_data: ContextChangeData):
"""After context is changed.

This method is called after the context is changed.

Args:
context_change_data (ContextChangeData): Object with information
about context change.

"""
if not nuke.GUI:
return

change_context_label()

if _about_to_save:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also pointing out that there is also _before_workfile_save and _after_workfile_save on IWorkfileHost so you can store the _about_to_save on the host itself too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sebastianbrandhuber I assume this is a change still to do?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will implement this next week when back at work 👍

# Let's prompt the user to update the context settings or not
prompt_reset_context()


def add_nuke_callbacks(project_settings: dict = None):
"""Adding all available nuke callbacks"""
Expand Down Expand Up @@ -405,6 +430,16 @@ def change_context_label():
old_label, new_label))


def after_workfile_save():
global _about_to_save
_about_to_save = False


def before_workfile_save(event):
global _about_to_save
_about_to_save = True


def add_shortcuts_from_presets(project_settings: dict):
menubar = nuke.menu("Nuke")
nuke_presets = project_settings["nuke"]["general"]
Expand Down
Loading