The standard python hasattr() doesn't work for me, giving me False in every case:

hasattr(bpy.data.objects["object"], 'char')

How I can check for the existence of a custom property?

Thank you


There are two types of custom properties:

  1. Properties global to a type, bpy.props.* for e.g. bpy.types.Object
    (ob.char = "foobar")
  2. ID properties local to a type instance
    (ob["char"] = "foobar")

To test for bpy.props properties / attributes:

hasattr(ob, "char")

Or get the attribute:

# note: throws AttributeError if not existing
getattr(ob, "char") 

# don't throw exception but return default (None) value if not existing
getattr(ob, "char", None)

To test for a certain ID property, use:

bpy.data.objects['Object'].get('char') is not None

get() returns the value of the custom property assigned to the given key (the property name), or None if it couldn't find the key. You can make it return anything in case the key doesn't exist, like a string:

Object.get('char', default="Not found.")

For custom properties, Blender mimics Python dictionaries (which the following example applies to as well)

To check if any datablock has property you can use Pythons __contains__

'char' in obj

Used in a script:

obj = bpy.data.objects["object"]
if 'char' in obj:
    print("has char")

You can also use get method, matching Pythons dict.get

value = obj.get('char', fallback)
if value is not fallback:
    print("char is:", value)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.