# How can I access (read/write) a mesh object's index vi Python?

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.

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

• 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. May 8 '19 at 21:33
• 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. May 9 '19 at 7:09

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

bpy.context.object.pass_index