Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created December 16, 2022 19:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigRoy/092016f45d27e9acc41a753b084da0de to your computer and use it in GitHub Desktop.
Save BigRoy/092016f45d27e9acc41a753b084da0de to your computer and use it in GitHub Desktop.
Example of how to frame the FlowView to all tools, selected tools or specified tools using Python (tested in Fusion 18.1.1 Build 7)
def get_correct_position(tool):
"""Get position of a tool that is usable for FlowView positions"""
result = tool.SaveSettings()
for tool in result["Tools"].values():
pos = tool["ViewInfo"]["Pos"]
return pos[1], pos[2]
def set_flow_position(flow, position, scale=None):
"""Set FlowView offset position and scaling factor"""
if scale is None:
# Preserve scaling
scale = flow.GetScale()
# Use a temporary bookmark to set flow position
NAME = "__TMP__"
flow.InsertBookmark(NAME)
bookmarks = flow.GetBookmarkList()
# Get index by name
index = next(key for key, value in bookmarks.items() if value["Name"] == NAME)
bookmark = bookmarks[index]
bookmark["Offset"][1] = position[0]
bookmark["Offset"][2] = position[1]
bookmark["Scale"] = scale
flow.SetBookmarkList(bookmarks)
flow.GoToBookmark(NAME)
# flow.DeleteBookmark doesn't work so we set the
# BookmarkList ourselves minus the temp bookmark
bookmarks.pop(index)
flow.SetBookmarkList(bookmarks)
def frame_tools(flow, tools):
"""Frame tools in the FlowView"""
# TODO: Take into account size of a Sticky Note or Backdrop
# TODO: Take into the visual size of a Tool (e.g. with thumbnail)
positions = [get_correct_position(tool) for tool in tools]
if len(tools) > 1:
# Get bounding box of the tools
bb_min_x = min(p[0] for p in positions)
bb_min_y = min(p[1] for p in positions)
bb_max_x = max(p[0] for p in positions)
bb_max_y = max(p[1] for p in positions)
center = [(bb_min_x + bb_max_x) / 2.0, (bb_min_y + bb_max_y) / 2.0]
size = [(bb_max_x - bb_min_x), (bb_max_y - bb_min_y)]
# Get flow size
flow_attrs = flow.GetAttrs()
flow_size = [flow_attrs["VIEWN_Right"] - flow_attrs["VIEWN_Left"], flow_attrs["VIEWN_Bottom"] - flow_attrs["VIEWN_Top"]]
# Get relative scale of tool distance compared to the flow screen space
relative_size = [a / b for a, b in zip(flow_size, size)]
# Take the smallest scale factor of the two axes
scale = min(relative_size) / 2.0
else:
center = positions[0]
scale = None
set_flow_position(flow, position=center, scale=scale)
def frame_selected(flow):
"""Frame selected tools in the flow view"""
comp = flow.Composition
tools = comp.GetToolList(True).values()
frame_tools(flow, tools)
def frame_all(flow):
"""Frame all tools in the flow view"""
comp = flow.Composition
tools = comp.GetToolList(False).values()
frame_tools(flow, tools)
# Example usage
flow = comp.CurrentFrame.FlowView
frame_selected(flow)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment