4
$\begingroup$

Say you generate some random points using Geometry Nodes, how can you get all those point coordinates using the python api?

The only hint I have is that the point coords are located under Point Cloud in the Spreadsheet Editor.

Basically, I want to use those coordinates for scripting other stuff I can't do with Geometry Nodes yet.

enter image description here

$\endgroup$

1 Answer 1

8
$\begingroup$

This is a workaround and I hope there is (or will eventually be) an easier way to access point cloud data in the API.

enter image description here

In your GN tree add an Instance on Points node, plug in a Mesh Line or a Points node set to a Count of 1 to get a single vertex at $(0, 0, 0)$.

enter image description here

In theory we should be able to access the vertices' geometry but for now the geometry is not "real". We need to realize the instances with a Realize Instances node.

enter image description here

Then, we need to access the evaluated mesh, after the GN modifier has been evaluated. For that we can use the context dependency graph.

import bpy
import numpy as np

depsgraph = bpy.context.evaluated_depsgraph_get()
obj = bpy.context.active_object.evaluated_get(depsgraph)

coords = np.zeros(len(obj.data.vertices) * 3, dtype=float)
obj.data.vertices.foreach_get("co", coords)

print(coords)  # This is a flattened array

coords = coords.reshape(len(obj.data.vertices), 3)

print(coords)  # This is a 3D array

Result :

enter image description here

$\endgroup$
5
  • 1
    $\begingroup$ i love workaround if they work!! Great job! $\endgroup$
    – Chris
    Jun 10, 2021 at 7:18
  • 1
    $\begingroup$ Yeah, hopefully they simplify this so vertex instancing isn't needed, but for now it works so thank you! $\endgroup$ Jun 10, 2021 at 14:41
  • $\begingroup$ There is a "points to vertices" node, I don't know if there was back in 2021. $\endgroup$ Dec 8, 2023 at 14:09
  • $\begingroup$ @MarkusvonBroady I don't think it did ! Have you tried if it works without the realize instances trick ? $\endgroup$
    – Gorgious
    Dec 8, 2023 at 15:23
  • 1
    $\begingroup$ @Gorgious I did: blender.stackexchange.com/a/289622/60486 $\endgroup$ Dec 8, 2023 at 15:53

You must log in to answer this question.

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