0
$\begingroup$

I am trying to use blender to create a visualisation, and I have set up a 'template' object with geometry nodes that I can copy and manually manipulate to set the desired shape and colour. This works.

The GN input panel is with inputs is: GN panel showing inputs

I am now trying to read a set of data from a CSV file and programmatically set up the visualisation elements. I can read the correct data from the file. I pass the data to a python routine, the relevant routine (ignoring the main code) is:

def createRFBandObject(name: str, lower: float, upper: float, power: float, category: int, showPower: bool):
    print(f"{name} is ({lower}, {upper}, {power}, {category}, {showPower})")
    obj = bpy.data.objects['RFBandTemplate'].copy()
    obj.name = name
    nodeGroup = obj.modifiers['RFBand'].node_group
    nodeGroup.inputs['Lower (GHz)'] = lower / 1000 # Convert to GHz
    nodeGroup.inputs[3] = upper / 1000 # Convert to GHz
    #nodeGroup.inputs[4] = power
    #nodeGroup.inputs[6] = category
    #nodeGroup.inputs[5] = showPower
    obj.data.update
    return obj

For completeness:

Found Geometry Node modifier RFBand
Input Input_0 is named Geometry with type GEOMETRY
Input Input_7 is named UoM (MHz or GHz) with type STRING
Input Input_2 is named Lower (GHz) with type VALUE
Input Input_3 is named Upper (GHz) with type VALUE
Input Input_4 is named EIRP (dBW) with type VALUE
Input Input_5 is named Show power? with type BOOLEAN
Input Input_6 is named Category with type INT

The routine creates the right type of object but does not set the correct attributes (does not set any attributes at all). The above code as written will not run due to TypeError: bpy_prop_collection[key] = value: invalid, expected a StructRNA type or None, not a float. My search of StackExchange and other sites suggest using .default_value which is not logical in the first place and, while not creating an error, does not change the attributes of the shape.

(Edit: default_value, on investigation, does exactly as it says - it changes the default value in the underlying GN definition - thus changing it for all shapes that are using that GN setup.)

I am completely lost. How can I use python to set the GN inputs on the new shape I have just created?

[For reasons, I will not be uploading a copy of the blend file, or the data set. I know the shapes work as intended when data is entered manually, I know the template shape is being copied and named correctly, and I know the data is being ingested correctly.]

$\endgroup$
0

3 Answers 3

4
$\begingroup$

I think you want to set values for modifier inputs and not the inputs of the node group.

enter image description here

(for tooltips like that enable Python Tooltips in the Preferences, Interface tab, Display panel)

So that would be:

...
#first of all you'll need the identifier for the input, not the name
identifier= obj.modifiers["RFBand"].node_group.inputs["Lower (GHz)"].identifier
obj.modifiers["RFBand"][identifier] = lower / 1000
...
$\endgroup$
1
  • $\begingroup$ Upgraded to Blender 4 and this code no longer worked. After half a day of mucking around (and one file that is doing something weird on the CSV import) I managed to make it work - added a new answer. $\endgroup$
    – AJD
    Commented Jan 2 at 4:33
6
$\begingroup$

I upgraded to Blender v4 and the accepted answer (https://blender.stackexchange.com/a/301431/49849) no longer worked.

The following is the modified code to get the identifier (Blender Version 4.0.1).

# First line is the old code
# idLower = obj.modifiers["RFBand"].node_group.inputs["Lower (GHz)"].identifier
#
idLower = obj.modifiers["RFBand"].node_group.interface.items_tree["Lower (GHz)"].identifier
obj.modifiers["RFBand"][idLower] = lower / 1000 # Convert to GHz
$\endgroup$
1
  • $\begingroup$ Amazing, thank you! I was having the exact same issue when upgrading to Blender 4.2 $\endgroup$
    – leota
    Commented Jul 29 at 9:15
-1
$\begingroup$

nodeGroup.inputs[3] this will give you the Input object which has multiple properties. Try to print it to explore more. Or try it in blender python console.

What you need to do is nodeGroup.inputs[3].default_value = something As far as my knowledge goes, most of the Input sockets value need to be assigned via default_value but some have other ways.

$\endgroup$
1
  • 2
    $\begingroup$ default_value, on investigation does exactly as it says - it changes the default value in the underlying GN definition - thus changing it for all shapes that are using that GN setup. $\endgroup$
    – AJD
    Commented Sep 20, 2023 at 6:47

You must log in to answer this question.

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