0
$\begingroup$

I would like to retrieve from python the index of an object -- the same index that you get when using an Object info node in shader nodes.

I have a reference to the object, but I don't see any attribute named index, or any function that returns it.

$\endgroup$

2 Answers 2

1
$\begingroup$

You can access to the Object Index by using pass_index property.

Example code:

# reference to object
obj = bpy.data.objects['ObjectName']

# get object pass index
obj_index = obj.pass_index

# set object pass index
obj.pass_index = 1
$\endgroup$
2
  • $\begingroup$ Is the pass_index the same as the object index? I just tested this with two cubes (Cube and Cube.001), and bpy.data.objects["Cube"].pass_index and bpy.data.objects["Cube.001"].pass_index both have the same value of 0. I see that I can manually update the pass_index value of one, and it does not affect the other, but I thought this index was unique for each object. $\endgroup$
    – Dazotaro
    May 8, 2019 at 21:33
  • $\begingroup$ Yes, it's the same idea. In Blender you can set a unique number which refers to the Object Index, and that number is the Pass Index. The output socket "Object Index" - in the "Object Info" node - refers exactly to that number. $\endgroup$ May 9, 2019 at 7:09
1
$\begingroup$

https://docs.blender.org/manual/en/latest/render/cycles/nodes/types/input/object_info.html

bpy.context.object.pass_index
$\endgroup$

You must log in to answer this question.

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