6
$\begingroup$

I would like to get Geometry Node Instances through Python. Instance Mesh, Position, Rotation, Scale.

enter image description here

$\endgroup$

1 Answer 1

7
$\begingroup$

There is an example in the docs for how to get instances from the dependency graph: https://docs.blender.org/api/3.2/bpy.types.Depsgraph.html#dependency-graph-iterate-over-all-object-instances

You can filter the object_instances list by checking if the parent property of the instance matches your source object. Use the matrix_world property to get the transform of the instance.

UPDATE: Here's an example for how to get instances from specific geometry nodes objects. Note that to identify the correct parent you actually have to check against the evaluated object in the depsgraph rather than the original object.

import bpy

depsgraph = bpy.context.evaluated_depsgraph_get()
objA = bpy.data.objects["Cube"]
objB = bpy.data.objects["Plane"]
evalA = objA.evaluated_get(depsgraph)
evalB = objB.evaluated_get(depsgraph)

instA = [inst for inst in depsgraph.object_instances if inst.is_instance and inst.parent == evalA]
instB = [inst for inst in depsgraph.object_instances if inst.is_instance and inst.parent == evalB]

print("Total instances: {}, A: {}, B: {}".format(len(depsgraph.object_instances), len(instA), len(instB)))
$\endgroup$
6
  • $\begingroup$ I've added an example file and some clarifications, hope it helps. $\endgroup$ Commented Jun 10, 2022 at 6:00
  • $\begingroup$ Alright, thanks :) $\endgroup$ Commented Jun 10, 2022 at 6:08
  • $\begingroup$ Amaizing, thanks a lot! Are you sure that we need to check "inst.is_instance"? As object_instances is a list of instances I suppose. $\endgroup$ Commented Jun 10, 2022 at 12:45
  • $\begingroup$ Also, istead of "inst.parent == evalA" I would use "inst.parent is evalA". $\endgroup$ Commented Jun 10, 2022 at 12:51
  • $\begingroup$ The object_instances list also contains regular objects, they count as a single instance of themselves. In my test file the 2 meshes, the camera, and the default light add 4 extra instances to the list. You probably don't need to check is_instance if you are also filtering by parent though. $\endgroup$ Commented Jun 10, 2022 at 12:55

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.