From 55b984dc66c4948b793033afe27e111047b62ae1 Mon Sep 17 00:00:00 2001 From: Kayla Man Date: Tue, 21 Mar 2023 17:45:35 +0800 Subject: [PATCH 1/7] scene length setting for 3dsmax --- openpype/hosts/max/api/lib.py | 127 +++++++++++++++++++++++++++++ openpype/hosts/max/api/menu.py | 21 ++++- openpype/hosts/max/api/pipeline.py | 5 ++ 3 files changed, 152 insertions(+), 1 deletion(-) diff --git a/openpype/hosts/max/api/lib.py b/openpype/hosts/max/api/lib.py index 4fb750d91b..f1d1f91dd1 100644 --- a/openpype/hosts/max/api/lib.py +++ b/openpype/hosts/max/api/lib.py @@ -6,6 +6,14 @@ from pymxs import runtime as rt from typing import Union import contextlib +from openpype.client import ( + get_project, + get_asset_by_name +) +from openpype.pipeline import legacy_io + +from openpype.pipeline.context_tools import get_current_project_asset + JSON_PREFIX = "JSON::" @@ -157,6 +165,125 @@ def get_multipass_setting(project_setting=None): ["multipass"]) +def set_scene_resolution(width, height): + """Set the render resolution + + Args: + width(int): value of the width + height(int): value of the height + + Returns: + None + + """ + rt.renderWidth = width + rt.renderHeight = height + + +def reset_scene_resolution(): + """Apply the scene resolution from the project definition + + scene resolution can be overwritten by an asset if the asset.data contains + any information regarding scene resolution . + + Returns: + None + """ + project_name = legacy_io.active_project() + project_doc = get_project(project_name) + project_data = project_doc["data"] + asset_data = get_current_project_asset()["data"] + + # Set project resolution + width_key = "resolutionWidth" + height_key = "resolutionHeight" + proj_width_key = project_data.get(width_key, 1920) + proj_height_key = project_data.get(height_key, 1080) + + width = asset_data.get(width_key, proj_width_key) + height = asset_data.get(height_key, proj_height_key) + + set_scene_resolution(width, height) + + +def get_frame_range(): + """Get the current assets frame range and handles.""" + # Set frame start/end + project_name = legacy_io.active_project() + asset_name = legacy_io.Session["AVALON_ASSET"] + asset = get_asset_by_name(project_name, asset_name) + + frame_start = asset["data"].get("frameStart") + frame_end = asset["data"].get("frameEnd") + # Backwards compatibility + if frame_start is None or frame_end is None: + frame_start = asset["data"].get("edit_in") + frame_end = asset["data"].get("edit_out") + + if frame_start is None or frame_end is None: + return + + handles = asset["data"].get("handles") or 0 + handle_start = asset["data"].get("handleStart") + if handle_start is None: + handle_start = handles + + handle_end = asset["data"].get("handleEnd") + if handle_end is None: + handle_end = handles + + return { + "frameStart": frame_start, + "frameEnd": frame_end, + "handleStart": handle_start, + "handleEnd": handle_end + } + + +def reset_frame_range(fps=True): + """Set frame range to current asset + + Args: + animationRange: A System Global variable which lets you get and + set an Interval value that defines the start and end frames + of the Active Time Segment. + frameRate: A System Global variable which lets you get + and set an Integer value that defines the current + scene frame rate in frames-per-second. + """ + if fps: + fps_number = float(legacy_io.Session.get("AVALON_FPS", + 25)) + rt.frameRate = fps_number + + frame_range = get_frame_range() + + frame_start = frame_range["frameStart"] - int(frame_range["handleStart"]) + frame_end = frame_range["frameEnd"] + int(frame_range["handleEnd"]) + + frange_cmd = f"animationRange = interval {frame_start} {frame_end}" + + rt.execute(frange_cmd) + + +def set_context_setting(): + """Apply the project settings from the project definition + + Settings can be overwritten by an asset if the asset.data contains + any information regarding those settings. + + Examples of settings: + frame range + resolution + + Returns: + None + """ + reset_scene_resolution() + + reset_frame_range() + + def get_max_version(): """ Args: diff --git a/openpype/hosts/max/api/menu.py b/openpype/hosts/max/api/menu.py index 5c273b49b4..fdac5eba09 100644 --- a/openpype/hosts/max/api/menu.py +++ b/openpype/hosts/max/api/menu.py @@ -4,7 +4,7 @@ from qtpy import QtWidgets, QtCore from pymxs import runtime as rt from openpype.tools.utils import host_tools - +from openpype.hosts.max.api import lib class OpenPypeMenu(object): """Object representing OpenPype menu. @@ -107,6 +107,17 @@ class OpenPypeMenu(object): workfiles_action = QtWidgets.QAction("Work Files...", openpype_menu) workfiles_action.triggered.connect(self.workfiles_callback) openpype_menu.addAction(workfiles_action) + + openpype_menu.addSeparator() + + res_action = QtWidgets.QAction("Set Resolution", openpype_menu) + res_action.triggered.connect(self.resolution_callback) + openpype_menu.addAction(res_action) + + frame_action = QtWidgets.QAction("Set Frame Range", openpype_menu) + frame_action.triggered.connect(self.frame_range_callback) + openpype_menu.addAction(frame_action) + return openpype_menu def load_callback(self): @@ -128,3 +139,11 @@ class OpenPypeMenu(object): def workfiles_callback(self): """Callback to show Workfiles tool.""" host_tools.show_workfiles(parent=self.main_widget) + + def resolution_callback(self): + """Callback to reset scene resolution""" + return lib.reset_scene_resolution() + + def frame_range_callback(self): + """Callback to reset frame range""" + return lib.reset_frame_range() diff --git a/openpype/hosts/max/api/pipeline.py b/openpype/hosts/max/api/pipeline.py index f8a7b8ea5c..dacc402318 100644 --- a/openpype/hosts/max/api/pipeline.py +++ b/openpype/hosts/max/api/pipeline.py @@ -50,6 +50,11 @@ class MaxHost(HostBase, IWorkfileHost, ILoadHost, INewPublisher): self._has_been_setup = True + def context_setting(): + return lib.set_context_setting() + rt.callbacks.addScript(rt.Name('systemPostNew'), + context_setting) + def has_unsaved_changes(self): # TODO: how to get it from 3dsmax? return True From d861e556319bcc4f34fba481d6ec1e0aef9fbc84 Mon Sep 17 00:00:00 2001 From: Kayla Man Date: Wed, 22 Mar 2023 21:49:00 +0800 Subject: [PATCH 2/7] update the frame range settings --- openpype/hosts/max/api/lib.py | 72 ++++++++++++++--------------------- 1 file changed, 28 insertions(+), 44 deletions(-) diff --git a/openpype/hosts/max/api/lib.py b/openpype/hosts/max/api/lib.py index f1d1f91dd1..ac8f6aedfe 100644 --- a/openpype/hosts/max/api/lib.py +++ b/openpype/hosts/max/api/lib.py @@ -6,13 +6,10 @@ from pymxs import runtime as rt from typing import Union import contextlib -from openpype.client import ( - get_project, - get_asset_by_name +from openpype.pipeline.context_tools import ( + get_current_project_asset, + get_current_project ) -from openpype.pipeline import legacy_io - -from openpype.pipeline.context_tools import get_current_project_asset JSON_PREFIX = "JSON::" @@ -165,7 +162,7 @@ def get_multipass_setting(project_setting=None): ["multipass"]) -def set_scene_resolution(width, height): +def set_scene_resolution(width: int, height: int): """Set the render resolution Args: @@ -189,49 +186,43 @@ def reset_scene_resolution(): Returns: None """ - project_name = legacy_io.active_project() - project_doc = get_project(project_name) - project_data = project_doc["data"] - asset_data = get_current_project_asset()["data"] - + data = ["data.resolutionWidth", "data.resolutionHeight"] + project_resolution = get_current_project(fields=data)["data"] + project_resolution_data = project_resolution["data"] + asset_resolution = get_current_project_asset(fields=data)["data"] + asset_resolution_data = asset_resolution["data"] # Set project resolution - width_key = "resolutionWidth" - height_key = "resolutionHeight" - proj_width_key = project_data.get(width_key, 1920) - proj_height_key = project_data.get(height_key, 1080) - - width = asset_data.get(width_key, proj_width_key) - height = asset_data.get(height_key, proj_height_key) + project_width = int(project_resolution_data.get("resolutionWidth", 1920)) + project_height = int(project_resolution_data.get("resolutionHeight", 1080)) + width = int(asset_resolution_data.get("resolutionWidth", project_width)) + height = int(asset_resolution_data.get("resolutionHeight", project_height)) set_scene_resolution(width, height) -def get_frame_range(): - """Get the current assets frame range and handles.""" - # Set frame start/end - project_name = legacy_io.active_project() - asset_name = legacy_io.Session["AVALON_ASSET"] - asset = get_asset_by_name(project_name, asset_name) +def get_frame_range() -> dict: + """Get the current assets frame range and handles. + Returns: + dict: with frame start, frame end, handle start, handle end. + """ + # Set frame start/end + asset = get_current_project_asset() frame_start = asset["data"].get("frameStart") frame_end = asset["data"].get("frameEnd") # Backwards compatibility if frame_start is None or frame_end is None: frame_start = asset["data"].get("edit_in") frame_end = asset["data"].get("edit_out") - if frame_start is None or frame_end is None: return - handles = asset["data"].get("handles") or 0 handle_start = asset["data"].get("handleStart") if handle_start is None: handle_start = handles - handle_end = asset["data"].get("handleEnd") if handle_end is None: handle_end = handles - return { "frameStart": frame_start, "frameEnd": frame_end, @@ -240,29 +231,24 @@ def get_frame_range(): } -def reset_frame_range(fps=True): - """Set frame range to current asset +def reset_frame_range(fps: bool=True): + """Set frame range to current asset. - Args: + This is part of 3dsmax documentation: animationRange: A System Global variable which lets you get and - set an Interval value that defines the start and end frames - of the Active Time Segment. + set an Interval value that defines the start and end frames + of the Active Time Segment. frameRate: A System Global variable which lets you get - and set an Integer value that defines the current - scene frame rate in frames-per-second. + and set an Integer value that defines the current + scene frame rate in frames-per-second. """ if fps: - fps_number = float(legacy_io.Session.get("AVALON_FPS", - 25)) + fps_number = float(get_current_project(fields=["data.fps"])["data"]["fps"]) rt.frameRate = fps_number - frame_range = get_frame_range() - frame_start = frame_range["frameStart"] - int(frame_range["handleStart"]) frame_end = frame_range["frameEnd"] + int(frame_range["handleEnd"]) - frange_cmd = f"animationRange = interval {frame_start} {frame_end}" - rt.execute(frange_cmd) @@ -281,8 +267,6 @@ def set_context_setting(): """ reset_scene_resolution() - reset_frame_range() - def get_max_version(): """ From 4e8354eec810023d83a23616c1af0d76035932a6 Mon Sep 17 00:00:00 2001 From: Kayla Man Date: Wed, 22 Mar 2023 21:54:43 +0800 Subject: [PATCH 3/7] hound fix --- openpype/hosts/max/api/lib.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/openpype/hosts/max/api/lib.py b/openpype/hosts/max/api/lib.py index ac8f6aedfe..09edaaa2ef 100644 --- a/openpype/hosts/max/api/lib.py +++ b/openpype/hosts/max/api/lib.py @@ -182,7 +182,6 @@ def reset_scene_resolution(): scene resolution can be overwritten by an asset if the asset.data contains any information regarding scene resolution . - Returns: None """ @@ -204,7 +203,7 @@ def get_frame_range() -> dict: """Get the current assets frame range and handles. Returns: - dict: with frame start, frame end, handle start, handle end. + dict: with frame start, frame end, handle start, handle end. """ # Set frame start/end asset = get_current_project_asset() @@ -233,17 +232,18 @@ def get_frame_range() -> dict: def reset_frame_range(fps: bool=True): """Set frame range to current asset. + This is part of 3dsmax documentation: - This is part of 3dsmax documentation: - animationRange: A System Global variable which lets you get and - set an Interval value that defines the start and end frames - of the Active Time Segment. - frameRate: A System Global variable which lets you get - and set an Integer value that defines the current - scene frame rate in frames-per-second. + animationRange: A System Global variable which lets you get and + set an Interval value that defines the start and end frames + of the Active Time Segment. + frameRate: A System Global variable which lets you get + and set an Integer value that defines the current + scene frame rate in frames-per-second. """ if fps: - fps_number = float(get_current_project(fields=["data.fps"])["data"]["fps"]) + data_fps = get_current_project(fields=["data.fps"]) + fps_number = float(data_fps["data"]["fps"]) rt.frameRate = fps_number frame_range = get_frame_range() frame_start = frame_range["frameStart"] - int(frame_range["handleStart"]) From e373ccf4e3a747d3db3266f56478cdacfeb0aa3c Mon Sep 17 00:00:00 2001 From: Kayla Man Date: Wed, 22 Mar 2023 21:57:19 +0800 Subject: [PATCH 4/7] hound fix --- openpype/hosts/max/api/lib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openpype/hosts/max/api/lib.py b/openpype/hosts/max/api/lib.py index 09edaaa2ef..c398d7cf48 100644 --- a/openpype/hosts/max/api/lib.py +++ b/openpype/hosts/max/api/lib.py @@ -230,7 +230,7 @@ def get_frame_range() -> dict: } -def reset_frame_range(fps: bool=True): +def reset_frame_range(fps:bool=True): """Set frame range to current asset. This is part of 3dsmax documentation: From 57308ff488e0a2f931231a455cad13ca23eaaa7e Mon Sep 17 00:00:00 2001 From: Kayla Man Date: Wed, 22 Mar 2023 21:58:29 +0800 Subject: [PATCH 5/7] hound fix --- openpype/hosts/max/api/lib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openpype/hosts/max/api/lib.py b/openpype/hosts/max/api/lib.py index c398d7cf48..aa7f72b26c 100644 --- a/openpype/hosts/max/api/lib.py +++ b/openpype/hosts/max/api/lib.py @@ -230,7 +230,7 @@ def get_frame_range() -> dict: } -def reset_frame_range(fps:bool=True): +def reset_frame_range(fps: bool = True): """Set frame range to current asset. This is part of 3dsmax documentation: From 62ced23858534c081c4aee84714b236507e64439 Mon Sep 17 00:00:00 2001 From: Kayla Man Date: Thu, 23 Mar 2023 00:45:58 +0800 Subject: [PATCH 6/7] remove irrelevant attrs of data --- openpype/hosts/max/api/lib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openpype/hosts/max/api/lib.py b/openpype/hosts/max/api/lib.py index aa7f72b26c..ac7d75db08 100644 --- a/openpype/hosts/max/api/lib.py +++ b/openpype/hosts/max/api/lib.py @@ -186,9 +186,9 @@ def reset_scene_resolution(): None """ data = ["data.resolutionWidth", "data.resolutionHeight"] - project_resolution = get_current_project(fields=data)["data"] + project_resolution = get_current_project(fields=data) project_resolution_data = project_resolution["data"] - asset_resolution = get_current_project_asset(fields=data)["data"] + asset_resolution = get_current_project_asset(fields=data) asset_resolution_data = asset_resolution["data"] # Set project resolution project_width = int(project_resolution_data.get("resolutionWidth", 1920)) From a178ca1569a43c1becfba3db0209ba3f982d4959 Mon Sep 17 00:00:00 2001 From: Kayla Man Date: Mon, 27 Mar 2023 17:35:45 +0800 Subject: [PATCH 7/7] remove duplicated imported function --- openpype/hosts/max/api/menu.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openpype/hosts/max/api/menu.py b/openpype/hosts/max/api/menu.py index b52acc3c38..066cc90039 100644 --- a/openpype/hosts/max/api/menu.py +++ b/openpype/hosts/max/api/menu.py @@ -5,7 +5,7 @@ from pymxs import runtime as rt from openpype.tools.utils import host_tools from openpype.hosts.max.api import lib -from openpype.hosts.max.api import lib + class OpenPypeMenu(object): """Object representing OpenPype menu.