# Getting data from a stored name attribute back to python

**URL:** <https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258>\
**Category:** Python Support\
**Created:** [December 5, 2024, 9:27am UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258 "2024-12-05T09:27:16Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oren\_Matar](https://blenderartists.org/user_avatar/blenderartists.org/oren_matar/32/1320871_2.png) [@Oren\_Matar](https://blenderartists.org/u/Oren_Matar)\
**Post date:** [December 5, 2024, 9:27am UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/1 "2024-12-05T09:27:16Z")

</div>

Following my previous question on how to generate textures without rendering, using the python module as a standalone:

> [@How to generate Textures without Rendering to File?](https://blenderartists.org/t/how-to-generate-textures-without-rendering-to-file/1560362/8):
>
> The only way I can imagine, is to replicate all nodes in OpenGL and use the gpu module to render the texture to an offscreen buffer. This is far more advanced than you might expect… If you download an older version of Blender (prior to Eevee), you can get the OpenGl code for most nodes (the older ones) from the material itself. But this option is no longer available (thought that glsl code would still work in current blender versions).

Someone suggested that I actually use geometry nodes! I can create a grid and then store the texture values for each vertex and access it from there without any need to render. This sounds like a great idea - I just want to get texture value, no need for lighting etc - but I can’t find how to access data in “stored name attribute” and bring it back to python.  
Here’s the code I have:

```auto
import bpy

def create_node(node_tree, type_name, node_x_location, node_location_step_x=0):
    """Creates a node of a given type, and sets/updates the location of the node on the X axis.
    Returning the node object and the next location on the X axis for the next node.
    """
    node_obj = node_tree.nodes.new(type=type_name)
    node_obj.location.x = node_x_location
    node_x_location += node_location_step_x

    return node_obj, node_x_location

bpy.ops.mesh.primitive_plane_add()

bpy.ops.node.new_geometry_nodes_modifier()
node_tree = bpy.data.node_groups["Geometry Nodes"]

out_node = node_tree.nodes["Group Output"]
in_node = node_tree.nodes["Group Input"]

node_x_location = 0
node_location_step_x = 300

grid, node_x_location = create_node(node_tree, "GeometryNodeMeshGrid", node_x_location, node_location_step_x)
grid.inputs[2].default_value = 512
grid.inputs[3].default_value = 512
voronoi, node_x_location = create_node(node_tree, "ShaderNodeTexVoronoi", node_x_location, node_location_step_x)
store_attr, node_x_location = create_node(node_tree, "GeometryNodeStoreNamedAttribute", node_x_location, node_location_step_x)
store_attr.inputs[2].default_value = 'out'
out_node.location.x = node_x_location

node_tree.links.new(grid.outputs[0], store_attr.inputs["Geometry"])
node_tree.links.new(store_attr.outputs["Geometry"], out_node.inputs["Geometry"])
node_tree.links.new(voronoi.outputs["Distance"], store_attr.inputs["Value"])

```

Any ideas how can I get the data back to python now?  
(The goal of this whole task is to be able to generate lots of random textures - randomly generating node trees with different combinations of textures, then generating their corresponding image)

---

<div class="post-metadata">

**Author:** ![Secrop](https://blenderartists.org/user_avatar/blenderartists.org/secrop/32/20093_2.png) [@Secrop](https://blenderartists.org/u/Secrop)\
**Post date:** [December 5, 2024, 9:51am UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/2 "2024-12-05T09:51:13Z")

</div>

Because you are creating a new mesh in GN, you need to evaluate your object first, and then get the attributes from the evaluated object.

```auto
dpg = bpy.context.view_layer.depsgraph
ev_obj = bpy.data.objects['Your Object'].evaluated_get(dpg)
attributes = ev_obj.data.attributes['out'].data

```

---

<div class="post-metadata">

**Author:** ![Oren\_Matar](https://blenderartists.org/user_avatar/blenderartists.org/oren_matar/32/1320871_2.png) [@Oren\_Matar](https://blenderartists.org/u/Oren_Matar)\
**Post date:** [December 5, 2024, 10:34am UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/3 "2024-12-05T10:34:42Z")

</div>

Now I have this:

```auto
import bpy

def create_node(node_tree, type_name, node_x_location, node_location_step_x=0):
    """Creates a node of a given type, and sets/updates the location of the node on the X axis.
    Returning the node object and the next location on the X axis for the next node.
    """
    node_obj = node_tree.nodes.new(type=type_name)
    node_obj.location.x = node_x_location
    node_x_location += node_location_step_x

    return node_obj, node_x_location

bpy.ops.node.new_geometry_nodes_modifier()
node_tree = bpy.data.node_groups["Geometry Nodes"]

out_node = node_tree.nodes["Group Output"]
in_node = node_tree.nodes["Group Input"]

node_x_location = 0
node_location_step_x = 300

grid, node_x_location = create_node(node_tree, "GeometryNodeMeshGrid", node_x_location, node_location_step_x)
grid.inputs[2].default_value = 512
grid.inputs[3].default_value = 512
voronoi, node_x_location = create_node(node_tree, "ShaderNodeTexVoronoi", node_x_location, node_location_step_x)
store_attr, node_x_location = create_node(node_tree, "GeometryNodeStoreNamedAttribute", node_x_location, node_location_step_x)
store_attr.inputs[2].default_value = 'out'
out_node.location.x = node_x_location

node_tree.links.new(grid.outputs[0], store_attr.inputs["Geometry"])
node_tree.links.new(store_attr.outputs["Geometry"], out_node.inputs["Geometry"])
node_tree.links.new(voronoi.outputs["Distance"], store_attr.inputs["Value"])

dpg = bpy.context.view_layer.depsgraph
ev_obj = bpy.data.objects['Cube'].evaluated_get(dpg)
attributes = ev_obj.data.attributes['out'].data

```

And I’m getting  
“KeyError: ‘bpy\_prop\_collection[key]: key “out” not found’”

Did I miss something?

---

<div class="post-metadata">

**Author:** ![Secrop](https://blenderartists.org/user_avatar/blenderartists.org/secrop/32/20093_2.png) [@Secrop](https://blenderartists.org/u/Secrop)\
**Post date:** [December 5, 2024, 10:54am UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/4 "2024-12-05T10:54:51Z")

</div>

You need to wait for the nodetree to be evaluated (after a context update).

If you run this after (in separated actions) your initial script has ran:

```auto
import bpy

dpg = bpy.context.view_layer.depsgraph
ev_obj = bpy.data.objects['Cube'].evaluated_get(dpg)
attributes = ev_obj.data.attributes['out'].data
#just check the first 20 entries, as there are 262144 of them.
slice = attributes[:20] 
for i, atr in enumerate(slice):
    print(i, atr.value)

```

you’ll get this:

```auto
0 0.43779173493385315
1 0.43665820360183716
2 0.43574145436286926
3 0.43504294753074646
4 0.43456369638442993
5 0.434304416179657
6 0.43426549434661865
7 0.4344469904899597
8 0.4348486363887787
9 0.4354698359966278
10 0.4363096356391907
11 0.437366783618927
12 0.43863970041275024
13 0.44012650847435
14 0.44182509183883667
15 0.44373294711112976
16 0.44584742188453674
17 0.4481655955314636
18 0.45068439841270447
19 0.4534003436565399
....

```

---

<div class="post-metadata">

**Author:** ![Oren\_Matar](https://blenderartists.org/user_avatar/blenderartists.org/oren_matar/32/1320871_2.png) [@Oren\_Matar](https://blenderartists.org/u/Oren_Matar)\
**Post date:** [December 5, 2024, 11:15am UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/5 "2024-12-05T11:15:54Z")

</div>

Amazing, thank!  
a few more questions though…

1. How do I get the nodetree evaluated? Sure, when I manually run this separately it works but the goal is to automate the whole process so I don’t have to manually separate it.
2. Is there a faster method to access the data and put it in a np.array? iterating with a for loop is very slow
3. Sadly this method is still somehow slower than rendering a full image! Which is insane because I really just need to access the evaluations of the textures, ignoring lights, camera etc. When I profiled the code, almost the entire runtime went to \_view\_layer\_update. I don’t know what that is, is there any way to remove or optimize it?

---

<div class="post-metadata">

**Author:** ![Secrop](https://blenderartists.org/user_avatar/blenderartists.org/secrop/32/20093_2.png) [@Secrop](https://blenderartists.org/u/Secrop)\
**Post date:** [December 5, 2024, 11:24am UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/6 "2024-12-05T11:24:41Z")

</div>

1. Just call `bpy.context.view_layer.update()` between the two parts

2. You need to create a np.array with length equal to the attributes (same dtype), and then you can use `.foreach_get`

```auto
import numpy as np
nparray = np.zeros(len(attributes), dtype=np.float32)
attributes.foreach_get('value', nparray)

```

1. The `view_layer.update` is quite fast… Of course, doing it in the GPU will be way faster.

---

<div class="post-metadata">

**Author:** ![MartinZ](https://blenderartists.org/user_avatar/blenderartists.org/martinz/32/1437938_2.png) [@MartinZ](https://blenderartists.org/u/MartinZ)\
**Post date:** [December 5, 2024, 11:29am UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/7 "2024-12-05T11:29:21Z")

</div>

I wonder how geometry nodes are updated. It surely is optimized somehow, but might it be faster to work with premade geometry?.. Another thing is - geometry has a lot of other stuff: edges, faces, normals… I wonder if that has anything to do with how fast it updates. Maybe using only points or only vertices might be faster.

---

<div class="post-metadata">

**Author:** ![Secrop](https://blenderartists.org/user_avatar/blenderartists.org/secrop/32/20093_2.png) [@Secrop](https://blenderartists.org/u/Secrop)\
**Post date:** [December 5, 2024, 11:36am UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/8 "2024-12-05T11:36:57Z")

</div>

> [@MartinZ](#):
>
> I wonder how geometry nodes are updated

some months ago, Iliya told me in the chat how all gets evaluated (just an overview)… it has some complex branched stuff, where some nodes require different steps… I cannot clarify it better than this… 🙂

---

<div class="post-metadata">

**Author:** ![MartinZ](https://blenderartists.org/user_avatar/blenderartists.org/martinz/32/1437938_2.png) [@MartinZ](https://blenderartists.org/u/MartinZ)\
**Post date:** [December 5, 2024, 11:41am UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/9 "2024-12-05T11:41:17Z")

</div>

I think if [Oren\_Matar](https://blenderartists.org/u/Oren_Matar) is after speed it might be worth testing.

---

<div class="post-metadata">

**Author:** ![Secrop](https://blenderartists.org/user_avatar/blenderartists.org/secrop/32/20093_2.png) [@Secrop](https://blenderartists.org/u/Secrop)\
**Post date:** [December 5, 2024, 11:46am UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/10 "2024-12-05T11:46:33Z")

</div>

Well… for speed, the best is to use the `gpu` module…  
A 512\*512 buffer there, would render in a fraction of a millisecond, and the only perk would be transfering the data from the GPU to the disk…

If it’s just to use a Voronoi texture, it’s rather a simple solution; but if one wants to use the complete shader nodetree, it will require that all nodes have their function written in glsl (which can be copied from the source), and a compiler for the nodetree. it’s doable, but it requires a good knowledge of glsl, and quite some code

---

<div class="post-metadata">

**Author:** ![Oren\_Matar](https://blenderartists.org/user_avatar/blenderartists.org/oren_matar/32/1320871_2.png) [@Oren\_Matar](https://blenderartists.org/u/Oren_Matar)\
**Post date:** [December 5, 2024, 12:04pm UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/11 "2024-12-05T12:04:15Z")

</div>

Amazing! it seems to work, but still takes 2-3x the runtime of a full render…  
Now, how can I make the view layer update use the GPU? I tried switching to cycles and use GPU device but that has no effect.

Now my code is:

```auto
import bpy
import numpy as np

def create_node(node_tree, type_name, node_x_location, node_location_step_x=0):
    """Creates a node of a given type, and sets/updates the location of the node on the X axis.
    Returning the node object and the next location on the X axis for the next node.
    """
    node_obj = node_tree.nodes.new(type=type_name)
    node_obj.location.x = node_x_location
    node_x_location += node_location_step_x

    return node_obj, node_x_location

bpy.ops.node.new_geometry_nodes_modifier()
node_tree = bpy.data.node_groups["Geometry Nodes"]

out_node = node_tree.nodes["Group Output"]
in_node = node_tree.nodes["Group Input"]

node_x_location = 0
node_location_step_x = 300

grid, node_x_location = create_node(node_tree, "GeometryNodeMeshGrid", node_x_location, node_location_step_x)
grid.inputs[2].default_value = 512
grid.inputs[3].default_value = 512
voronoi, node_x_location = create_node(node_tree, "ShaderNodeTexVoronoi", node_x_location, node_location_step_x)
store_attr, node_x_location = create_node(node_tree, "GeometryNodeStoreNamedAttribute", node_x_location, node_location_step_x)
store_attr.inputs[2].default_value = 'out'
out_node.location.x = node_x_location

node_tree.links.new(grid.outputs[0], store_attr.inputs["Geometry"])
node_tree.links.new(store_attr.outputs["Geometry"], out_node.inputs["Geometry"])
node_tree.links.new(voronoi.outputs["Distance"], store_attr.inputs["Value"])

bpy.context.view_layer.update()

dpg = bpy.context.view_layer.depsgraph
ev_obj = bpy.data.objects['Cube'].evaluated_get(dpg)
attributes = ev_obj.data.attributes['out'].data

nparray = np.zeros(len(attributes), dtype=np.float32)
attributes.foreach_get('value', nparray)
print(nparray)

```

---

<div class="post-metadata">

**Author:** ![Secrop](https://blenderartists.org/user_avatar/blenderartists.org/secrop/32/20093_2.png) [@Secrop](https://blenderartists.org/u/Secrop)\
**Post date:** [December 5, 2024, 12:24pm UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/12 "2024-12-05T12:24:03Z")

</div>

> [@Oren\_Matar](#):
>
> how can I make the view layer update use the GPU?

You can’t!  
All your code requires to run in CPU only. View\_layers and GN don’t work in GPU, so you’re doomed to use the CPU.

When I mentioned the GPU above, was in the same direction as I explained in the other thread…

For example, these are the vert and frag shaders for a simple voronoi (taken from 2.79).  
The frag shader, includes most nodes that existed at that time (thought the execution only calls the `node_tex_voronoi` function).  
[mat\_Material.001.vert](https://blenderartists.org/uploads/short-url/7jOGfLa1zhYDXTxW0Fz2YSXUfhH.vert) (2.3 KB)  
[mat\_Material.001.frag](https://blenderartists.org/uploads/short-url/vuIkzV9JhE2Zq8CwnhDp8GmR3h2.frag) (100.0 KB)

With a some work, one might be able to update this to 4.0+… but it’s enough to get the idea.

---

<div class="post-metadata">

**Author:** ![Oren\_Matar](https://blenderartists.org/user_avatar/blenderartists.org/oren_matar/32/1320871_2.png) [@Oren\_Matar](https://blenderartists.org/u/Oren_Matar)\
**Post date:** [December 5, 2024, 12:34pm UTC](https://blenderartists.org/t/getting-data-from-a-stored-name-attribute-back-to-python/1561258/13 "2024-12-05T12:34:20Z")

</div>

Right… that seems above my capabilities and effort I’m willing to put in. I guess I’ll have to work with the full render.  
Thanks!
