Hi,
When I looked up how to go about accessing the instances in a Geometry Nodes network I found several examples where they used list comprehension to build a list of instances. However, when I tried to replicate these examples, my code would always fail with an DepsgraphObjectInstance invalid error. If, however, I change the list comprehension to be a generator instead, it works. I am trying to figure out if this is something that is expected, is something that is new, or if it is a bug. I am running 4.2.2 LTS.
Basically my original code was this:
import bpy
depsgraph = bpy.context.evaluated_depsgraph_get()
obj = bpy.data.objects["Cube"]
eval_obj = obj.evaluated_get(depsgraph)
instances = [inst for inst in depsgraph.object_instances if inst.is_instance and inst.parent is eval_obj]
for instance in instances:
print(instance)
But when I run this code on a GN network that has 8 instances, I get the following output:
<bpy_struct, DepsgraphObjectInstance invalid>
<bpy_struct, DepsgraphObjectInstance invalid>
<bpy_struct, DepsgraphObjectInstance invalid>
<bpy_struct, DepsgraphObjectInstance invalid>
<bpy_struct, DepsgraphObjectInstance invalid>
<bpy_struct, DepsgraphObjectInstance invalid>
<bpy_struct, DepsgraphObjectInstance invalid>
<bpy_struct, DepsgraphObjectInstance invalid>
However, if I change the list comprehension to be a generator, then suddenly it works:
import bpy
depsgraph = bpy.context.evaluated_depsgraph_get()
obj = bpy.data.objects["Cube"]
eval_obj = obj.evaluated_get(depsgraph)
instances = (inst for inst in depsgraph.object_instances if inst.is_instance and inst.parent is eval_obj)
for instance in instances:
print(instance)
This results in the following output:
<bpy_struct, DepsgraphObjectInstance at 0x294813240>
<bpy_struct, DepsgraphObjectInstance at 0x294813220>
<bpy_struct, DepsgraphObjectInstance at 0x294813240>
<bpy_struct, DepsgraphObjectInstance at 0x294813220>
<bpy_struct, DepsgraphObjectInstance at 0x294813240>
<bpy_struct, DepsgraphObjectInstance at 0x294813220>
<bpy_struct, DepsgraphObjectInstance at 0x294813240>
<bpy_struct, DepsgraphObjectInstance at 0x294813220>
But just about every example I have seen on stack exchange (and other places) have shown this type of code running using the list comprehension method.
Example: https://blender.stackexchange.com/questions/266021/python-get-geometry-node-instances
Am I missing something? Or has the method of accessing these instances changed? Or is this bug that I should report?
Thanks!
(Here is the blend file in case it helps anyone)
instance_python_eval_test_v001.blend (952.1 KB)