• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

SD Graph current position in Substance Designer.

New Here ,
Jan 16, 2023 Jan 16, 2023

Copy link to clipboard

Copied

Is there a way to read current position in SD Graph when inserting new Node so it can be placed in the middle of the screen ?

TOPICS
How to , Scripting , Substance Graph

Views

215

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

Hello @edward_woopie,

 

The QtForPython API (PySide2) may be used to acquire the graph scene coordinates corresponding to the center of the Graph View viewport, although we do not recommend this approach.

 

Here is a sample script to create a node at the center of the current view:

 

import sd
from sd.api import sdbasetypes
from PySide2 import QtWidgets

app = sd.getContext().getSDApplication()
ui_mgr = app.getQtForPythonUIMgr()

graph = ui_mgr.getCurrentGraph()
nodes = graph.getNodes()

main_window = ui_mgr.getMainWindow()

# Get Graph View dock
areaB_0 = main_window.findChild(QtWidgets.QDockWidget, "areaB_0")

# Find graph scene in dock children
for item in areaB_0.findChildren(QtWidgets.QGraphicsView):
	if item.isVisible():
		graph_scene = item
		
# Transformation matrix for current view of graph scene in viewport
transform = graph_scene.viewportTransform()

# QRect object for viewport
rect = graph_scene.rect()

# The center of the graph view is the position of the graph scene at the top left 
# corner of the viewport, plus half the viewport's width and height, both adjusted
# to the current scale or zoom level
#
# m11 = graph scene scale, or zoom level
# m31 = x translation, or x position of graph scene at top left corner of viewport
# m32 = y translation, or y position of graph scene at top left corner of viewport

view_center_position_x = (-transform.m31() / transform.m11()) + (rect.width() / transform.m11() / 2)

view_center_position_y = (-transform.m32() / transform.m11()) + (rect.height() / transform.m11() / 2)

# Save the position as a float2 value
new_position = sdbasetypes.float2(view_center_position_x, view_center_position_y)

# Create new node
node = graph.newNode('sbs::compositing::blur')

# Place new node at center of graph view
node.setPosition(new_position)

 

 

IMPORTANT: Please note using the QtForPython API to work with objects deep in the software that are not exposed in our Python API is not supported nor recommended. Also, any change we make in Designer in the future can break this script.

 

Best regards.

Luca Giarrizzo | QA Analyst, 3D & Immersive | Adobe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

Hi Luca,

 

thank you very much. I've got as far as "areaB_0" but didn't have a clue about m31, m11 and all of that. Could you guys just put a simple GET method in graph so people can get it out easily. I'm sure this would be greatly appriciated by many people out there. Thanks again.

 

Edward

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

LATEST

Hello Edward,

 

Indeed, I understand the m## items and transformation matrix concepts may be complex to get one's head around – I would know! – but these are currently the hoops to jump through for working with nodes in the context of the Graph View viewport.

 

Think of the viewport as a window into the graph scene. The node have coordinates in the space of that scene, however we want to place nodes at a point on the window. Therefore we need to find what is the equivalent point in the graph scene.

 

The m## items are components of the transformation matrix that pans and scales the graph scene relatively to that window. These components provide the coordinates and scale needed to know where the centre of the window falls in the graph scene.

 

In any case, I have raised your feedback and request to the rest of the team. I agree we could expose methods in our Python API to make these operations simpler. I appreciate your patience in the meantime!

 

Best regards.

Luca Giarrizzo | QA Analyst, 3D & Immersive | Adobe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines