How to add existing custom node tree to geometry node modifier using python

Hi. I have a node tree “MyNT” that I am trying to add to a Geo Nodes modifer but can’t seem to find a way. bpy.ops.node.new_geometry_node_group_assign works fine for new trees but not existing ones. I tried also bpy.ops.node.add_group(“MyNT”) but this fails with:

TypeError: Calling operator "bpy.ops.node.add_group" error, expected a string enum in ('INVOKE_DEFAULT', 'INVOKE_REGION_WIN', 'INVOKE_REGION_CHANNELS', 'INVOKE_REGION_PREVIEW', 'INVOKE_AREA', 'INVOKE_SCREEN', 'EXEC_DEFAULT', 'EXEC_REGION_WIN', 'EXEC_REGION_CHANNELS', 'EXEC_REGION_PREVIEW', 'EXEC_AREA', 'EXEC_SCREEN')

Any help appreciated.

obj = bpy.data.objects['your_object']
nt = bpy.data.node_groups['MyNT']

gn_mod = obj.modifiers.new(type='NODES', name='fancy_name')
gn_mod.node_group = nt
2 Likes

Thanks so much. This is it!

Hello,

Would you mind sharing the complete code please.

I am new to python and blender and having the same issue of creating a custom node and getting it visible on the general Geometry Node. If you could share a simple script that shows creating a custom node and getting it shown on the Geo Node Editor, then it would be of a great help.

Many Thanks