fake-bpy-module icon indicating copy to clipboard operation
fake-bpy-module copied to clipboard

BlendData is missing overloads to check data-blocks from specific libraries (`(name, library)`)

Open Andrej730 opened this issue 5 months ago • 0 comments
trafficstars

fake-bpy-module-latest==20250604

In Blender it's possible to check object being present not just in .blend file in general, but whether it's present as a local object or object linked from the library. Example:

# Get any object in .blend file with name xxx
bpy.data.objects[xxx]
# Get object with name xxx, discarding objects from linked libaries
bpy.data.objects[xxx, None]
# Get object with name xxx from library bpy.data.libraries[0]
bpy.data.objects[xxx, bpy.data.libraries[0].filepath]

Related - https://docs.blender.org/api/current/info_gotchas_internal_data_and_python_objects.html#library-collisions

Checking source code https://github.com/blender/blender/blob/main/source/blender/python/intern/bpy_rna.cc for PyTuple_Check, it appears this kind of (name, library) tuple is only supported by 3 methods: get, __getitem__, __contains__ and this is true for all ID data-blocks collections (I assume for all elements of BlendData - bpy.data.objects, bpy.data.meshes, etc).

Currently those uses are not supported, see a list of examples below:

import bpy

# No overloads for "__getitem__" match the provided arguments
obj = bpy.data.objects["xxx", None]
# No overloads for "__getitem__" match the provided arguments
obj = bpy.data.objects["xxx", "filepath.blend"]

# Argument of type "tuple[Literal['xxx'], None]" cannot be assigned to parameter "key" of type "str" in function "get"
# "tuple[Literal['xxx'], None]" is not assignable to "str"
obj = bpy.data.objects.get(("xxx", None))
# Argument of type "tuple[Literal['xxx'], Literal['filepath.blend']]" cannot be assigned to parameter "key" of type "str" in function "get"
#   "tuple[Literal['xxx'], Literal['filepath.blend']]" is not assignable to "str"
obj = bpy.data.objects.get(("xxx", "filepath.blend"))

# Operator "in" not supported for types "tuple[Literal['xxx'], None]" and "BlendDataObjects"
test = ("xxx", None) in bpy.data.objects

test = ("xxx", "filepath.blend") in bpy.data.objects  # OK both statically and runtime.

test = ("xxx", "filepath.blend", "yyy") in bpy.data.objects  # Will error at runtime, but doesn't error on type check.

Andrej730 avatar Jun 05 '25 06:06 Andrej730