如何为Python的isinstance()指定正确类参数以筛选bpy_types.Object类型对象
bpy_types.Object type in Blender Python with isinstance() The issue you're hitting is that you're passing a string to isinstance() instead of the actual type object. Let's fix this quickly:
The Correct Type to Pass
Instead of using the string " <class 'bpy_types.Object'>", you need to use the actual type reference from Blender's Python API: bpy.types.Object.
Blender's Python API exposes all object types under the bpy.types module, so this is the valid type you should check against.
Fixed Code Example
Here's your updated function that works as expected:
# bpy is usually pre-imported in Blender's scripting environment, but include this if needed import bpy def tidy_names(l): for k in l.keys(): obj = l[k] # Check if the item is a Blender Object type if isinstance(obj, bpy.types.Object): # Set the object's name to its variable name in the locals() dict obj.name = k # Run the function with your local variables tidy_names(locals())
Why Your Original Attempt Failed
The error TypeError: isinstance() arg 2 must be a type or tuple of types happens because isinstance() requires its second argument to be a valid Python type (or a tuple of types), not a string representation of that type. The output <class 'bpy_types.Object'> you saw is just the string representation of bpy.types.Object—you need to use the type itself.
This will correctly target all Blender objects in your locals() dictionary and rename them to match their variable names.
内容的提问来源于stack exchange,提问作者simone

