6
$\begingroup$

I'm creating a script for data visualization and I want my data thats been loaded into an attribute to be selected for an input.

I like to write my blender code without using ops and context where possible.

How can I turn this input from a 'float' to this? enter image description here

I feel like this should be possible somewhere here

bpy.data.node_groups['Geonodes'].inputs['Attribute']

I tried setting the default value as the attribute but the default value only takes float. I can use the ops command in the console, but it's really ugly and I'd like to avoid that if possible.

bpy.ops.object.geometry_nodes_input_attribute_toggle(prop_path="[\"Input_3_use_attribute\"]", modifier_name="Geonodes")

Is this possible without ops?

$\endgroup$

1 Answer 1

7
$\begingroup$

Sure.

First, get the modifier:

obj = context.object # or get the object in other way
gnmod = None
for gnmod in obj.modifiers:
    if gnmod.type == "NODES":
        break

Then get identifier of socket. (Since "Input_3" is quite random number you have to get identifier from socket name) In my code, I create new one it if it doesn't exist, just in case:

inputs = gnmod.node_group.inputs
if "MyAttr" not in inputs:
    inputs.new("NodeSocketColor", "MyAttr")
id = inputs["MyAttr"].identifier

Then you can set use attribute and set it (Let's say you want to set vertex color "Col"):

gnmod[id+'_attribute_name'] = "Col"
gnmod[id+'_use_attribute'] = True
$\endgroup$
3
  • $\begingroup$ _use_attribute_name was what I was looking for. Awesome! thank you. $\endgroup$
    – TheJeran
    Commented Feb 4, 2022 at 12:09
  • 2
    $\begingroup$ Incase anyone has the issue of the object not updating after changing modifier, you can use obj.data.update() $\endgroup$
    – cak3_lover
    Commented Nov 19, 2022 at 20:37
  • $\begingroup$ See blender.stackexchange.com/a/308016/86891 to access inputs in versions > 4.0 $\endgroup$
    – Gorgious
    Commented Feb 26 at 14:04

You must log in to answer this question.

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