0
$\begingroup$

My question is as follows and with the example working code.

I'm trying to change the Menu Switch node lists label in my python code, seems couldn't find a way to do it. The way to do it on the interface is not what I want, because I need to set everything right when start to use in my addon, not let people do it manually every time. enter image description here

import bpy

def update_geoNode_tree(node_tree):
    
    group_output = node_tree.nodes["Group Output"]
    group_input = node_tree.nodes["Group Input"]
   
    #Socket Menu
    menu_socket = node_tree.interface.new_socket(name = "Menu", in_out='INPUT', socket_type = 'NodeSocketMenu')
    menu_socket.attribute_domain = 'POINT'
    
    #node Menu Switch
    menu_switch = node_tree.nodes.new("GeometryNodeMenuSwitch")
    menu_switch.name = "Menu Switch"
    #####################################
    ### for question to test
    menu_switch.inputs[1].name = "Cylinder"
    #####################################
    
    #node Cylinder
    cylinder = node_tree.nodes.new("GeometryNodeMeshCylinder")
    cylinder.name = "Cylinder"
    cylinder.fill_type = 'NGON'
    #Vertices
    cylinder.inputs[0].default_value = 32
    #Side Segments
    cylinder.inputs[1].default_value = 1
    #Fill Segments
    cylinder.inputs[2].default_value = 1
    #Radius
    cylinder.inputs[3].default_value = 1.0
    #Depth
    cylinder.inputs[4].default_value = 1.0
    
    #node Cube
    cube = node_tree.nodes.new("GeometryNodeMeshCube")
    cube.name = "Cube"
    #Size
    cube.inputs[0].default_value = (1.0, 1.0, 3.0)

    #Set locations
    group_input.location = (-340.9637451171875, 39.62562561035156)
    group_output.location = (326.51617431640625, 65.04036712646484)
    menu_switch.location = (55.20476531982422, 51.1171760559082)
    cylinder.location = (-343.1625671386719, -62.844444274902344)
    cube.location = (-169.79373168945312, -105.80216217041016)
    
    #Set dimensions
    group_input.width, group_input.height = 140.0, 100.0
    group_output.width, group_output.height = 140.0, 100.0
    menu_switch.width, menu_switch.height = 140.0, 100.0
    cylinder.width, cylinder.height = 140.0, 100.0
    cube.width, cube.height = 140.0, 100.0
    
    #initialize node_tree links
    #menu_switch.Output -> group_output.Geometry
    node_tree.links.new(menu_switch.outputs[0], group_output.inputs[0])
    #cylinder.Mesh -> menu_switch.A
    node_tree.links.new(cylinder.outputs[0], menu_switch.inputs[1])
    #cube.Mesh -> menu_switch.B
    node_tree.links.new(cube.outputs[0], menu_switch.inputs[2])
    #group_input.Menu -> menu_switch.Menu
    
    return node_tree

def create_centerpiece():
    
    bpy.ops.mesh.primitive_circle_add()
    bpy.context.active_object.name = 'testGeoTree'

    bpy.ops.node.new_geometry_nodes_modifier()
    node_tree = bpy.data.node_groups["Geometry Nodes"]
    node_tree.name = "testGeoTree"

    update_geoNode_tree(node_tree)
    #node_tree.nodes['Style Switch'].inputs[0].name = 'Shapes'
    #node_tree.nodes['Style Switch'].inputs[1].name = 'Cylinder'
    #node_tree.nodes['Style Switch'].inputs[2].name = 'Cube'
    #node_tree.interface.items_tree[2].default_value = 'A'


    # make the Geo Nodes modifier the active mode at the end
    bpy.context.active_object.modifiers["GeometryNodes"].is_active = True


def main():
    """
    Python code to generate geo nodes node tree
    """
   
    create_centerpiece()

if __name__ == "__main__":
    main()

$\endgroup$
2
  • $\begingroup$ Could you share some code that would be possible to copy/paste and test? $\endgroup$ Commented May 21 at 20:00
  • $\begingroup$ Yes, I just add the code. $\endgroup$
    – baby boss
    Commented May 21 at 20:49

1 Answer 1

2
$\begingroup$

In your script you are changing menu_switch.inputs[1].name, but I suspect it gets updated and reset with the value from menu_switch.enum_definition at some point. Try changing the enum_definion in the first place instead. That would be:

menu_switch.enum_definition.enum_items[0].name = "Cylinder"

instead of

menu_switch.inputs[1].name = "Cylinder"

Seems to work fine and update the input name on it's own as well.

$\endgroup$
8
  • $\begingroup$ Super, this is the way to do it! It's different from other nodes, good to know! Any idea for the 2nd question? $\endgroup$
    – baby boss
    Commented May 21 at 23:51
  • $\begingroup$ Supposed to be one question. The properties for the items in the interface of the node group are in bpy.context.object.modifiers["GeometryNodes"].node_group.interface.items_tree Use tab key in Python console for autocomplete to explore and also enable Python Tooltips in the Preferences, Interface tab to see hints when hovering over stuff. $\endgroup$ Commented May 22 at 0:55
  • $\begingroup$ If you have multiple questions you should post them as separate questions. $\endgroup$ Commented May 22 at 0:59
  • $\begingroup$ I tried, no effect on the modifiers side and actual geometry tree, but no warnings or errors through the interface.items_tree[].default_value way to set. Okay, I will post another thread about this question, thank you very much for the help!!! $\endgroup$
    – baby boss
    Commented May 22 at 1:21
  • $\begingroup$ I think you should edit this one, delete the second question part here and post it as a separate question. $\endgroup$ Commented May 22 at 1:23

You must log in to answer this question.

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