You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Python的isinstance()指定正确类参数以筛选bpy_types.Object类型对象

How to correctly check for 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 04:07:31