0
$\begingroup$

I am trying to select one node in node editor, then select same type of nodes and change width of all selected nodes using python, this script select same type of nodes but it cannot change its height and width. What should I do?

for area in bpy.context.screen.areas:
    if area.type == 'NODE_EDITOR':
        an = bpy.context.active_node
        type = an.bl_static_type

        amn = bpy.context.active_object.active_material.name
        nodes = bpy.data.materials[amn].node_tree.nodes

        for n in nodes:
            if n.type == type:
                n.select = True
                n.height == 159
                n.width == 1000
$\endgroup$
4
  • $\begingroup$ hello ! please be advised overriding builtin keyword type is not recommended and can lead to funky bugs. Cheers $\endgroup$
    – Gorgious
    Apr 7, 2023 at 21:29
  • $\begingroup$ thank you but I can't understand what "overriding builtin keyword type " means. I want to avoid glitches. $\endgroup$
    – Yokomizo
    Apr 7, 2023 at 23:46
  • $\begingroup$ see stackoverflow.com/questions/10568087/… $\endgroup$
    – Gorgious
    Apr 8, 2023 at 8:06
  • $\begingroup$ I see. I will avoid "type" as a variable. thank you. $\endgroup$
    – Yokomizo
    Apr 8, 2023 at 13:51

1 Answer 1

2
$\begingroup$

Considering you can have only one active object and one active material, you can without going through areas:

# find active object
obj = bpy.context.object
# find active material
mat = obj.active_material

# find active node
nodes = mat.node_tree.nodes
active_node = nodes.active

# then loop over the nodes, checking for their types
for node in nodes:
    if node.type == active_node.type:
        # and change select state and width
        node.select = True
        node.width = 600
        # but you cannot change the height
        #node.height = 300  

Of course, you may add some checks in this code in case no active element exists at a step or another.

$\endgroup$
6
  • $\begingroup$ it works! thank you. only active node's width and height can be changed then.. $\endgroup$
    – Yokomizo
    Apr 7, 2023 at 16:33
  • $\begingroup$ you can change width of any node... what do you mean? $\endgroup$
    – lemon
    Apr 7, 2023 at 16:35
  • $\begingroup$ I mean, yours goes through active_material path but mine isn't. That's why I can't change node's width? amn = bpy.context.active_object.active_material.name nodes = bpy.data.materials[amn].node_tree.nodes If I touch nodes data from this path,node data can not be edited? $\endgroup$
    – Yokomizo
    Apr 7, 2023 at 16:44
  • $\begingroup$ never mind. it should be n.width = 1000,not n.width == 1000. thanx lol. $\endgroup$
    – Yokomizo
    Apr 7, 2023 at 16:52
  • $\begingroup$ I think an = bpy.context.active_node may throw an exception in your code. $\endgroup$
    – lemon
    Apr 7, 2023 at 16:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .