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

Merge feature from #4793 and #4996 to set frame range on instances #5201

Closed
Show file tree
Hide file tree
Changes from 12 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
88 changes: 64 additions & 24 deletions openpype/hosts/maya/api/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2261,7 +2261,7 @@ def get_frame_range(include_animation_range=False):
return frame_range


def reset_frame_range(playback=True, render=True, fps=True):
def reset_frame_range(playback=True, render=True, fps=True, instances=True):
"""Set frame range to current asset

Args:
Expand All @@ -2270,6 +2270,8 @@ def reset_frame_range(playback=True, render=True, fps=True):
render (bool, Optional): Whether to set the maya render frame range.
Defaults to True.
fps (bool, Optional): Whether to set scene FPS. Defaults to True.
instances (bool, Optional): Whether to update publishable instances.
Defaults to True.
"""
if fps:
fps = convert_to_maya_fps(
Expand Down Expand Up @@ -2300,6 +2302,12 @@ def reset_frame_range(playback=True, render=True, fps=True):
cmds.setAttr("defaultRenderGlobals.startFrame", animation_start)
cmds.setAttr("defaultRenderGlobals.endFrame", animation_end)

if instances:
project_name = get_current_project_name()
settings = get_project_settings(project_name)
if settings["maya"]["update_publishable_frame_range"]["enabled"]:
update_instances_frame_range()


def reset_scene_resolution():
"""Apply the scene resolution from the project definition
Expand Down Expand Up @@ -3129,31 +3137,63 @@ def remove_render_layer_observer():
pass


def update_content_on_context_change():
def iter_publish_instances():
"""Iterate over publishable instances (their objectSets).
"""
This will update scene content to match new asset on context change
for node in cmds.ls(
"*.id",
long=True,
type="objectSet",
recursive=True,
objectsOnly=True
):
if cmds.getAttr("{}.id".format(node)) != "pyblish.avalon.instance":
continue
yield node


def update_instances_asset_attribute():
"""Update 'asset' attribute of publishable instances (their objectSets)
that got one.
"""
scene_sets = cmds.listSets(allSets=True)
asset_doc = get_current_project_asset()
new_asset = asset_doc["name"]
new_data = asset_doc["data"]
for s in scene_sets:
try:
if cmds.getAttr("{}.id".format(s)) == "pyblish.avalon.instance":
attr = cmds.listAttr(s)
print(s)
if "asset" in attr:
print(" - setting asset to: [ {} ]".format(new_asset))
cmds.setAttr("{}.asset".format(s),
new_asset, type="string")
if "frameStart" in attr:
cmds.setAttr("{}.frameStart".format(s),
new_data["frameStart"])
if "frameEnd" in attr:
cmds.setAttr("{}.frameEnd".format(s),
new_data["frameEnd"],)
except ValueError:
pass

for instance in iter_publish_instances():
if not cmds.attributeQuery("asset", node=instance, exists=True):
continue
attr = "{}.asset".format(instance)
cmds.setAttr(attr, get_current_project_asset()['name'], type="string")
ClementHector marked this conversation as resolved.
Show resolved Hide resolved


def update_instances_frame_range():
"""Update 'frameStart', 'frameEnd', 'handleStart', 'handleEnd' and 'fps'
attributes of publishable instances (their objectSets) that got one.
"""

attributes = ["frameStart", "frameEnd", "handleStart", "handleEnd", "fps"]

attrs_per_instance = {}
for instance in iter_publish_instances():
instance_attrs = [
attr for attr in attributes
if cmds.attributeQuery(attr, node=instance, exists=True)
]

if instance_attrs :
ClementHector marked this conversation as resolved.
Show resolved Hide resolved
attrs_per_instance [instance] = instance_attrs
ClementHector marked this conversation as resolved.
Show resolved Hide resolved

if not attrs_per_instance:
# no instances with any frame related attributes
return

fields = ["data.{}".format(key) for key in attributes]
asset_doc = get_current_project_asset(fields=fields)
asset_data = asset_doc["data"]

for node, attrs in attrs_per_instance.items():
for attr in attrs:
plug = "{}.{}".format(node, attr)
value = asset_data[attr]
cmds.setAttr(plug, value)


def show_message(title, msg):
Expand Down
7 changes: 6 additions & 1 deletion openpype/hosts/maya/api/lib_rendersettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ def _set_arnold_settings(self, width, height):
cmds.setAttr(
"defaultArnoldDriver.mergeAOVs", multi_exr)
self._additional_attribs_setter(additional_options)
reset_frame_range(playback=False, fps=False, render=True)
reset_frame_range(
playback=False,
fps=False,
render=True,
instances=False
)

def _set_redshift_settings(self, width, height):
"""Sets settings for Redshift."""
Expand Down
2 changes: 1 addition & 1 deletion openpype/hosts/maya/api/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ def on_task_changed():

with lib.suspended_refresh():
lib.set_context_settings()
lib.update_content_on_context_change()
lib.update_instances_frame_range()
ClementHector marked this conversation as resolved.
Show resolved Hide resolved

msg = " project: {}\n asset: {}\n task:{}".format(
legacy_io.active_project(),
Expand Down
10 changes: 9 additions & 1 deletion openpype/hosts/maya/api/workfile_template_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
WorkfileBuildPlaceholderDialog,
)

from .lib import read, imprint, get_main_window
from .lib import (
read,
imprint,
get_main_window,
update_instances_frame_range,
update_instances_asset_attribute,
)

PLACEHOLDER_SET = "PLACEHOLDERS_SET"

Expand Down Expand Up @@ -251,6 +257,8 @@ def cleanup_placeholder(self, placeholder, failed):
cmds.sets(node, addElement=PLACEHOLDER_SET)
cmds.hide(node)
cmds.setAttr(node + ".hiddenInOutliner", True)
update_instances_frame_range()
update_instances_asset_attribute()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are we sure this is in the right place. cleanup_placeholder sounds like it runs per placeholder instead of at the very end of workfile build. We only want to run this once, right? Not every single time.

I feel like this is probably in the wrong place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well seen, it is true that it is executed at each placeholder. I think we should move this to here with a post_populate_scene_placeholders method but we don't can't call maya commands here, maybe we should implement instance-related commands in host class? maybe it doesn't make sense with the new publisher (which I don't know)?
Tell me what would make more sense.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like the abstract workfile builder is missing some methods that trigger before the build and after the build. We could expose a after_build_process() and pre_build_process() so that each DCC could implement its own callbacks that run before or after ALL placeholders. Not sure if the best approach and whether it's really missing - but couldn't quickly find a feature that does that.

Another way could be for the workfile building to emit events emit_event("workfile.before.build") and emit_event("workfile.after.build") so that any module could attach callbacks to the events using register_event_callback - which the host itself could do e.g. on install() etc.

Not sure which would be the best approach. @iLLiCiTiT @tokejepsen @antirotor thoughts?


def load_succeed(self, placeholder, container):
self._parent_in_hierarchy(placeholder, container)
Expand Down
3 changes: 3 additions & 0 deletions openpype/settings/defaults/project_settings/maya.json
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@
"include_handles_default": false,
"per_task_type": []
},
"update_publishable_frame_range": {
"enabled": true
},
"scriptsmenu": {
"name": "OpenPype Tools",
"definition": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,24 @@
}
]
},
{
"type": "dict",
"collapsible": true,
"key": "update_publishable_frame_range",
"label": "Update publishable instances on Reset Frame Range",
"checkbox_key": "enabled",
"children": [
{
"type": "label",
"label": "If enabled, the frame range and the handles of all the publishable instances will be updated when using the 'Reset Frame Range' functionality"
},
{
"type": "boolean",
"key": "enabled",
"label": "Enabled"
}
]
},
{
"type": "dict",
"key": "include_handles",
Expand Down