In Blender, when creating complex procedural materials or geometry nodes, the node tree often moves away from the starting point of the node editor area. Later, when working with materials, this causes some inconvenience – when switching from material to material, the node tree “runs away” and we have to scroll the screen each time to see the nodes. This problem can be solved using a simple script that will move the entire node tree to the origin of the node editor area.
To shift the entire node tree to the origin without distortion, we will start from the current active node. We will move it to the origin of the area, coordinates (0.0, 0.0). All other nodes will be shifted relative to it by exactly the same vector.
Shader Editor
Let’s take the active node
1 |
active_node = bpy.context.object.active_material.node_tree.nodes.active |
And get its coordinates
1 |
active_node_co = active_node.location.copy() |
This is the vector by which we need to shift all the nodes.
We use copy() to get a static copy of the values, otherwise when the active node moves to the origin, this values will be lost.
Now for each node, recalculate the coordinates, subtracting the shift vector from the current coordinates of the node.
1 2 |
for node in bpy.context.object.active_material.node_tree.nodes: node.location -= active_node_co |
Full script code:
1 2 3 4 5 |
active_node = bpy.context.object.active_material.node_tree.nodes.active active_node_co = active_node.location.copy() for node in bpy.context.object.active_material.node_tree.nodes: node.location -= active_node_co |
Let’s execute this script for each material and centralize the node editor area by pressing the dot key (Del) on the numeric part of the keyboard.
Now, when switching materials, we no longer need to scroll the window in search of nodes, it will always be centered on the starting point.
Geometry Nodes Editor
For the Geometry Nodes area, everything works exactly the same.
The active node:
1 |
active_node = bpy.context.object.modifiers['GeometryNodes'].node_group.nodes.active |
Static copy of its coordinates – our shift vector:
1 |
active_node_co = active_node.location.copy() |
Moving each node by this vector:
1 2 |
for node in bpy.context.object.modifiers['GeometryNodes'].node_group.nodes: node.location -= active_node_co |
Full script code:
1 2 3 4 5 |
active_node = bpy.context.object.modifiers['GeometryNodes'].node_group.nodes.active active_node_co = active_node.location.copy() for node in bpy.context.object.modifiers['GeometryNodes'].node_group.nodes: node.location -= active_node_co |