Skip to content

Instantly share code, notes, and snippets.

@CGArtPython
Created June 19, 2024 08:29
Show Gist options
  • Save CGArtPython/1d72f786b924bd517a9a4b92c1a004e0 to your computer and use it in GitHub Desktop.
Save CGArtPython/1d72f786b924bd517a9a4b92c1a004e0 to your computer and use it in GitHub Desktop.
This script demonstrates how to align nodes in the Shader Editor using the built-in 'node_arrange' add-on in Blender.
"""
This script demonstrates how to align nodes in the Shader Editor using the built-in 'node_arrange' add-on in Blender.
Note: This is not the most optimal method as it relies on a bpy.ops call, but it is functional.
The script performs the following steps:
1. Checks if the 'node_arrange' add-on is enabled and enables it if it is not.
2. Defines a context manager to switch the area type to the Shader Editor.
3. Aligns the nodes of a specified material using the 'node_arrange' add-on.
Functions:
enable_node_arrange(): Checks and enables the 'node_arrange' add-on.
shader_editor(): Context manager to switch the area to Shader Editor.
align_nodes(mat): Aligns the nodes of the given material using the 'node_arrange' add-on.
main(): Main function to enable the add-on and align nodes for a specified material.
Usage:
Run the script in Blender's scripting editor with a material named 'material' in your project.
Make sure the Shader Node editor is open, the script will not work if you don't have it open.
"""
import contextlib
import bpy
import addon_utils
def enable_node_arrange():
loaded_default, loaded_state = addon_utils.check("node_arrange")
if not loaded_state:
addon_utils.enable("node_arrange")
@contextlib.contextmanager
def shader_editor():
old_area = bpy.context.area.type
bpy.context.area.type = "NODE_EDITOR"
bpy.context.area.ui_type = "ShaderNodeTree"
try:
yield
finally:
bpy.context.area.type = old_area
def align_nodes(mat):
with shader_editor():
bpy.context.space_data.node_tree = mat.node_tree
# requires enable_node_arrange()
# uses built in node_arrange add-on
bpy.ops.node.button()
def main():
enable_node_arrange()
mat = bpy.data.materials['material']
align_nodes(mat)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment