2
$\begingroup$

I'm creating a number of objects in a loop, how can I assign an existing Geometry Node group to these created objects?

I've tried to assign data.node_group.name the name of the group but that just renames the existing group!

import bpy

for i in range(1):
    bpy.ops.mesh.primitive_cube_add(size=2, location=(i*2, i*2, 0))
    bpy.ops.transform.resize(value=(1,1,1))
    bpy.ops.object.modifier_add(type='NODES') # This adds the GeometryNodes modifier
    data = bpy.context.object.modifiers.get("GeometryNodes")

# Assign 'MY GEOMETRY NODE GROUP' to the GeometryNodes modifier
#    data.node_group.name = "MY GEOMETRY NODE GROUP"

Any help would be appreciated.

Thanks.

$\endgroup$

2 Answers 2

4
$\begingroup$

You can get an existing node group using:

ng = bpy.data.node_groups['your node group']

Adding a Geometry Node modifier:

modifier=o.modifiers.new("MyName", "NODES")

(where o is your object)

The modifier is added, but adding it also creates a new node group that you can delete by:

 bpy.data.node_groups.remove(modifier.node_group)

Then you can add the 'MY GEOMETRY NODE GROUP' by:

modifier.node_group = bpy.data.node_groups['MY GEOMETRY NODE GROUP']
$\endgroup$
1
  • $\begingroup$ This works perfectly, thank you! $\endgroup$
    – Maj
    Nov 21, 2021 at 11:59
1
$\begingroup$

You can use the modifier_copy_to_selected operator with a context override :

object_to_copy_from = bpy.context.active_object
objects_to_copy_to = bpy.context.selected_objects

override = {
    "active_object": object_to_copy_from,
    "selected_objects": objects_to_copy_to
    }

bpy.ops.object.modifier_copy_to_selected(override, modifier="GeometryNodes")
$\endgroup$

You must log in to answer this question.

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