0
$\begingroup$

I have dozens of curves and I want to add a Geometry Node called test to all these selected objects.

The Geometry Node has already been created and I just want to add it to all curves that are selected.

How do I do this with Python script?

$\endgroup$

2 Answers 2

5
$\begingroup$

It will apply all the modifiers (and convert the curve to mesh)

import bpy
from bpy import context


node_group = bpy.data.node_groups['Geometry Nodes']


for object in context.selected_objects:
    modifier = object.modifiers.new(name='Geometry Nodes', type='NODES')
    modifier.node_group = node_group

    context.view_layer.objects.active = object

    if object.type == 'CURVE':
        bpy.ops.object.convert(target='MESH')
    elif object.type == 'MESH':
        bpy.ops.object.modifier_apply(modifier=modifier.name)
$\endgroup$
2
  • $\begingroup$ Unfortunately, this approach doesn't work because an error message appears: "Cannot apply constructive modifiers on a curve. Convert the curve to a mesh to apply." The modifier is only applied to the active object, not the inactive ones. In my case, I need to work with curves, not meshes. But I managed to find a workaround. Thank you, anyway. $\endgroup$
    – rdllngr
    Commented Jul 19, 2023 at 13:18
  • 1
    $\begingroup$ @rdllngr in case of curves, replace the last line with bpy.ops.object.convert(target='MESH') - but keep in mind it will apply all modifiers (and convert the curve to mesh) $\endgroup$ Commented Jul 19, 2023 at 14:48
0
$\begingroup$

AI Icon Warning, Potential AI generated content⚠️

This post is suspected to have been generated with the help of an artificial intelligence, chat bot, or other language model tools without proper attribution.

There is reason to believe this post was not written by a human due to its structure, and grammatical construction, and has been marked by our community.
Current AI chat bots are a language models, that means they are like a parrot, they can mimic human language well, but they are built to write seemingly logical, seemingly factual grammatically valid text, rather than actually be correct, like a human would.
The veracity of the following content cannot be ascertained. See the Concerns regarding AI generated content.

I tried to create with the help of ChatGPT and came up with this script. In my case it works. The only way I managed to do it ON CURVES (not meshes) was by creating the modifier and specifying the node name for a single active object. Then, simply copy the created modifier to the selected inactive objects.

import bpy

# Select the active object
obj = bpy.context.object

# Add a new Geometry Nodes modifier
modifier = obj.modifiers.new(name="Geometry Nodes", type='NODES')

# Get a reference to the Geometry Nodes modification node
node_group = bpy.data.node_groups["test"]

# Set the modification node as the "test" Geometry Nodes node
modifier.node_group = node_group

# Copy the modifier to other objects
bpy.ops.object.make_links_data(type='MODIFIERS')
$\endgroup$
2
  • $\begingroup$ This doesn't answer the question (it doesn't APPLY the modifier, just adds it), and is an unmarked chatGPT content. $\endgroup$ Commented Jul 19, 2023 at 14:46
  • $\begingroup$ @MarkusvonBroady Oh, you are correct about "apply". Actually my intention was to "add" the Geometry Node modifier to dozens of curves at once after importing an SVG vector (that's why they need to be curves). As English is not my native language, it was a literal translation and I didn't realize it until you commented. I'll edit the question to avoid confusion. $\endgroup$
    – rdllngr
    Commented Jul 19, 2023 at 15:48

You must log in to answer this question.

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