2
$\begingroup$

I need to check if a Blender class (In my case, GPencilLayer) has a certain property before executing some code.

I have tried:

hasattr(bpy.types.GPencilLayer, the_name_of_the_property)

But it doesnt work. For example:

hasattr(bpy.types.GPencilLayer, "hide") # returns False

But "hide" is a property of GPencilLayer

So Im kinda lost. I know (well, I think) that I could call the same code with a GPencilLayer object. But in my code, is not guaranted that exists already a GPencilLayer in the scene, so I can't always "grab an instance" and I would prefer to check it with the class.

Any idea?

Thank you so much for the help!

$\endgroup$

1 Answer 1

4
$\begingroup$

You can use this little function:

def has_property(btype, propname):
    for prop in btype.bl_rna.properties:
        if prop.identifier == propname:
            return True
    return False

# test
btype = bpy.types.GPencilLayer
prop = 'hide'
print(btype, 'has property "' + prop + '":', has_property(btype, prop))

# >>> <class 'bpy.types.GPencilLayer'> has property "hide": True
$\endgroup$
1
  • 1
    $\begingroup$ Awesome, works like a charm! Thank you so much friend, much appreciated! $\endgroup$
    – Ommadawn
    Commented Sep 5, 2023 at 21:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .