Accessing ID elements by name and link behavior

I don’t understand why the API allows to access ID elements in bpy.data.* arrays with their name, when the behavior becomes undefined when the file links to other files having elements with the same name.
For exemple, if I create a scene with a “Cube” object, and link a collection from another blend file that also contains an “Cube” object, then doing bpy.data.objects[“Cube”] gives me one of them but I have no garanty about which one.
So my question is: why not use the full name instead of name for this kind of access ?

1 Like

There is a way to specifically pull the object by library, by using a tuple for indexing:

bpy.data.objects['Cube', bpy.data.libraries['your_external_library'].filepath] 

This should pull the explicit ID from that library. If you need the un-linked element, pass None as the second parameter:

bpy.data.objects['Cube', None]
1 Like