0
$\begingroup$

Make sure the Geometry Nodes editor area is visible then select the default cube and execute the following script to add a Geometry Nodes modifier to it with id name GeometryNodes

import bpy

ctx_override = bpy.context.copy()

for area in bpy.context.screen.areas:
    if area.type == "NODE_EDITOR":
        ctx_override['area'] = area
        break;

bpy.context.active_object.modifiers.new('GeometryNodes', 'NODES')

It creates a new modifier in the modifiers list as shown below. So far all is fine.

enter image description here

Then execute the following script below to add another Geometry Nodes modifier. This script will also set the newly created modifier named GeometryNodes.001 as the selected or active modifier. But notice that the Input is added to the previously selected modifier. How do I fix that?

import bpy

ctx_override = bpy.context.copy()

for area in bpy.context.screen.areas:
    if area.type == "NODE_EDITOR":
        ctx_override['area'] = area
        break;

bpy.context.active_object.modifiers.new('GeometryNodes.001', 'NODES')
bpy.ops.object.modifier_set_active(modifier='GeometryNodes.001')
bpy.ops.node.tree_socket_add(ctx_override, in_out='IN')

enter image description here

How do I fix that?

$\endgroup$
5
  • $\begingroup$ Does this answer solve your problem? $\endgroup$ Jun 24, 2022 at 13:48
  • $\begingroup$ Does this answer your question? Python Geometry Node Trees $\endgroup$ Jun 24, 2022 at 13:49
  • $\begingroup$ I'm sorry but i tried tinkering with the code in that link but i can't figure out how that can specifically fix this problem. could you maybe kindly post a small snippet that does exactly what i asked for? I tried the code but it doesn't create any input socket for the selected/active node. $\endgroup$ Jun 24, 2022 at 14:07
  • $\begingroup$ or just anything that accomplishes bpy.ops.node.tree_socket_add(ctx_override, in_out='IN') which adds the extra input socket. $\endgroup$ Jun 24, 2022 at 14:23
  • 1
    $\begingroup$ add node_group.inputs.new("NodeSocketColor", "Color") after the last statement in the first part. $\endgroup$ Jun 24, 2022 at 15:04

2 Answers 2

1
$\begingroup$

This question requires a slight variation on this answer. After or instead of the statement

nodes = node_group.nodes

add

node_group.inputs.new("NodeSocketColor", "Color")

so the complete code becomes, in your example,

import bpy
from mathutils import Vector

bpy.ops.object.modifier_add(type='NODES')  

curve = bpy.context.active_object

def new_GeometryNodes_group():
    ''' Create a new empty node group that can be used
        in a GeometryNodes modifier.
    '''
    node_group = bpy.data.node_groups.new('GeometryNodes', 'GeometryNodeTree')
    inNode = node_group.nodes.new('NodeGroupInput')
    inNode.outputs.new('NodeSocketGeometry', 'Geometry')
    outNode = node_group.nodes.new('NodeGroupOutput')
    outNode.inputs.new('NodeSocketGeometry', 'Geometry')
    node_group.links.new(inNode.outputs['Geometry'], outNode.inputs['Geometry'])
    inNode.location = Vector((-1.5*inNode.width, 0))
    outNode.location = Vector((1.5*outNode.width, 0))
    return node_group

# In 3.2 Adding the modifier no longer automatically creates a node group.
# This test could be done with versioning, but this approach is more general
# in case a later version of Blender goes back to including a node group.
if curve.modifiers[-1].node_group:
    node_group = curve.modifiers[-1].node_group    
else:
    node_group = new_GeometryNodes_group()
    curve.modifiers[-1].node_group = node_group

nodes = node_group.nodes
node_group.inputs.new("NodeSocketColor", "Color")

By the way, the use of the context override dictionary is deprecated in 3.2 and scheduled to be removed from 3.1 If you want to override context in 3.2 and later you should use temp_override instead

$\endgroup$
2
  • $\begingroup$ thank u very much for your help very much appreciated sorry i ran out of votes, il give a vote up tomorrow. $\endgroup$ Jun 24, 2022 at 15:14
  • 1
    $\begingroup$ @HarryMcKenzie you're very welcome. No problem on the vote; just glad I could help. $\endgroup$ Jun 24, 2022 at 15:46
-2
$\begingroup$

You have context override from previous step, when new modifier hadn't been created yet.

But I suggest not using ops in the scripts. It is very annoying to set up a context override without information about what has to be overridden. I see that you have some non-ops functions, so maybe replace every one?

3.1:

import bpy

gnmod =  bpy.context.active_object.modifiers.new('GeometryNodes.001', 'NODES')
ins = gnmod.node_group.inputs
ins.new("NodeSocketColor", "Color")

Is that simple without using ops. But in the case if you want to run this in 3.2:

3.2/3.1:

import bpy

gnmod =  bpy.context.active_object.modifiers.new('GeometryNodes.001', 'NODES')
# 3.2 check
if gnmod.node_group is None:
    ng = bpy.data.node_groups.new("Vox", "GeometryNodeTree")
    input = ng.nodes.new("NodeGroupInput")
    output = ng.nodes.new("NodeGroupOutput")
    input.location = (-1000,0)
    output.location = (1000,0) 
    ng.inputs.new("NodeSocketGeometry", "Geometry")
    ng.outputs.new("NodeSocketGeometry", "Geometry")
    ng.links.new(input.outputs[0], output.inputs[0])
    gnmod.node_group = ng
ins = gnmod.node_group.inputs
ins.new("NodeSocketColor", "Color")
$\endgroup$
10
  • $\begingroup$ i already tried rearranging it, same result. please test it first. and can you please post the working code as answer. $\endgroup$ Jun 24, 2022 at 10:53
  • 1
    $\begingroup$ Foremost - that version do you use? It looks like 3.1, because in 3.2 you have to make node tree also. $\endgroup$
    – Crantisz
    Jun 24, 2022 at 12:35
  • $\begingroup$ oh yeah i remember in 3.2 they have this temp context thing. im on 3.1 $\endgroup$ Jun 24, 2022 at 13:20
  • $\begingroup$ No, In 3.2 you have to set up node group also $\endgroup$
    – Crantisz
    Jun 24, 2022 at 13:23
  • $\begingroup$ yeah anyway im on 3.1. can u help me solve this? $\endgroup$ Jun 24, 2022 at 13:28

You must log in to answer this question.

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