simone
simone

Reputation: 5221

How do I define the class for isinstance()

I am trying to build a function that affects only items in a certain class. This happens within the Python API - intro here

Currently I do

def tidy_names(l):
    for k in l.keys():
        print(k, type(l[k]))
        
tidy_names(locals())

which outputs (for example)

parte_B1 <class 'bpy_types.Object'>
parte_B2 <class 'bpy_types.Object'>
parte_B3 <class 'bpy_types.Object'>
parte_B4 <class 'bpy_types.Object'>
parte_G1 <class 'bpy_types.Object'>
parte_G2 <class 'bpy_types.Object'>
parte_C1 <class 'bpy_types.Object'>

what Id like to do is

def tidy_names(l):
    for k in l.keys():
        if isinstance(l[k], some_class):
            l[k].name = k

where some class points to <class 'bpy_types.Object'> - trying

isinstance(l[k], "<class 'bpy_types.Object'>"))

throws an error:

TypeError: isinstance() arg 2 must be a type or tuple of types

So the question is: what do I need to pass to isinstance() so it works like I'd like it to?

Upvotes: 1

Views: 190

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191701

Seems like you want the following

from bpy import types as bpy_types

print(isinstance(list_element, bpy_types.Object))

Upvotes: 0

Jin Wei
Jin Wei

Reputation: 21

You can try this instead.

isinstance(l[k], bpy_types.Object))

Example usage to check if an object is an integer (or sub class of an integer) is

isinstance(1, int)

Upvotes: 2

Related Questions