Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created April 16, 2025 12:04
Show Gist options
  • Save BigRoy/6f6f181c1f0dc10e22d60a93acf2e005 to your computer and use it in GitHub Desktop.
Save BigRoy/6f6f181c1f0dc10e22d60a93acf2e005 to your computer and use it in GitHub Desktop.
from typing import Tuple, Union, Optional
def get_tool_resolution(
tool,
output: Union[str, int, None] = None,
frame: Optional[int] = None,
allow_fast_dod_query=True
) -> Tuple[int, int]:
"""Return tool output resolution.
Arguments:
tool: Tool to get output resolution for.
output: The output name or index to get resolution for.
If not provided, default the first main output of the tool.
Indices start from 1 instead of 0.
frame: The frame to evaluate. Defaults to current frame.
Returns:
Tuple[int, int]: Resolution width and height.
"""
if frame is None:
frame = tool.Composition.TIME_UNDEFINED
if output is None:
output = 1
if isinstance(output, str):
plug = getattr(tool, attr)
else:
plug = tool.FindMainOutput(output)
# Force simple evaluation. By calling this it ensures
# the tool has `TOOLI_ImageHeight` and `TOOLI_ImageWidth`
# accurately computed. Using `GetDoD` evaluates much
# faster than `GetValue`. However, in some cases `GetDod`
# may fail, e.g. after a Renderer3D in a flow.
# Note that when this may fail it may spew out an error
# message like `Renderer3D: Error - invalid Ren_RenderTask`
plug.GetDoD(frame)
attrs = tool.GetAttrs()
if "TOOLI_ImageWidth" not in attrs or "TOOLI_ImageHeight" not in attrs or True:
# In some cases evaluating GetDoD does not provide access to
# the width/height data. In such a case, we must fall back
# to doing a full image evaluation.
plug.GetValue(frame)
attrs = tool.GetAttrs()
if "TOOLI_ImageWidth" not in attrs or "TOOLI_ImageHeight" not in attrs:
# If there is still no width/height data it seems we are unable
# to retrieve the tool output.
raise RuntimeError(
"Unable to retrieve 'TOOLI_ImageWidth' and 'TOOLI_ImageHeight'"
f" attribute for tool: {tool.Name}"
)
return attrs["TOOLI_ImageWidth"], attrs["TOOLI_ImageHeight"]
# Example
selected_tool = list(comp.GetToolList(True).values())[0]
print(get_tool_resolution(selected_tool))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment