0
$\begingroup$

I am trying to call the values from an attribute defined in the Group Output for the use in a python script. To illustrate the problem I have made a simple GN-model (2 Mesh Lines with unique integer values related to the edge per Mesh Line i.e. 1/2): Simple GN-model

Within the GN-model the data is available, as the spreadsheet shows:

Spreadsheet with the attribute data

I want to store the values that the attribute stores into a simple TXT or CSV file.

So far I was only able to get the name/lable of the attribute but not its values via (note '6' may vary):

import bpy
import bmesh

print(bpy.data.objects["Cube"].modifiers["GeometryNodes"]["Output_6_attribute_name"])
$\endgroup$

1 Answer 1

0
$\begingroup$

After some tries I have come up with the following solution:

import bpy

# Make a temporary copy of the GN-object and convert it to a MESH
bpy.ops.object.convert(target='MESH', keep_original=True, merge_customdata=False)

# Deselect the GN-object
bpy.ops.object.select_all(action='DESELECT')

# Address the copy and readout the attributes
o = bpy.data.objects["Cube.001"]
for a in o.data.attributes:
    for d in a.data:
        print(d.value)

# Reference: https://wiki.blender.org/wiki/Reference/Release_Notes/2.80/Python_API/Scene_and_Object_API
# Select the temporary object copy and delete it
bpy.data.objects["Cube.001"].select_set(True) # Blender 2.8x
bpy.ops.object.delete() 

# Reselect the GN-object as active object so that the script can run again.
bpy.ops.object.select_all(action='SELECT')
bpy.context.view_layer.objects.active = bpy.data.objects['Cube'] # Blender 2.8

# At this point the model has returned to the original state.

This will return all the values of the attribute as I wanted.

$\endgroup$

You must log in to answer this question.

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