10

I want to read out Custom Properties of a Blender Object using the scripting mode in Blender itself. So far I found only possibilities to read out Custom Properties you created yourself in the scripting mode. But I want to read out Custom Properties which I tagged myself per hand. This means I dont have a local variable to use.

I want this to be in the following context: I have a Loop going through all objects:

for obj in bpy.data.objects:
if not 'Camera' in obj.name and not 'Lamp' in obj.name and not 'Armature' in obj.name:
    #here I get the location of the current Object
    loc.append(obj.location)

Now what would be perfect, would be something like:

obj.getCustomProperties

Is there a way to do this with the Blender Python mode?

Thanks, Daniel

Daniel Töws
  • 347
  • 1
  • 5
  • 21
  • **Related**: This [github](https://github.com/MarioDelgadoSr/AddBlenderCustomPropertiesFromCSV) has a Python script to read from a csv file and update the Custom Properties for a mesh(es): – UberMario Jun 14 '19 at 03:47

1 Answers1

13

Let's say we add a custom property called 'testprop' to object 'Cube' - you can access that property within python as bpy.data.objects['Cube']['testprop']

If you don't know the property names you can get a list of available custom properties by calling keys() for the object.

This leads to the following to print the custom properties -

bad_obj_types = ['CAMERA','LAMP','ARMATURE']
for obj in bpy.data.objects:
    if obj.type not in bad_obj_types:
        if len(obj.keys()) > 1:
            # First item is _RNA_UI
            print("Object",obj.name,"custom properties:")
            for K in obj.keys():
                if K not in '_RNA_UI':
                    print( K , "-" , obj[K] )

You may also notice I test obj.type instead of obj.name which can be changed by the user and also multiple items may exist with numeric extensions in the name.

sambler
  • 6,917
  • 1
  • 16
  • 23
  • Thanks sambler! This works nearly perfect. The commands you gave me do exactly what i want. But there seems to be some requirments for the custom properties, because he will not do this for every custom property, or if none does met the requirements he won´t even run the script. Do you know something about those requirments? – Daniel Töws Jan 22 '14 at 14:02
  • 1
    Oh and thanks for the hint, that checking for the Object Type is actually better than checking for their name ;) – Daniel Töws Jan 22 '14 at 14:25
  • 1
    I think what you a referring to is that _RNA_UI only exists if a custom property has been added. It would be good practice to use `if hasattr(obj,'_RNA_UI'):` before the for loop. It may also be worth testing the value of K before doing anything. eg cycles_visibility is a common property that you probably want to skip. – sambler Jan 23 '14 at 04:07
  • You may have found that hasattr() doesn't work for custom properties. Also the list returned from `obj['_RNA_UI']` retains item names after they have been deleted. keys() for an object lists custom properties and proves reliable. – sambler Jan 27 '14 at 07:39
  • @sambler Could you help with this: https://stackoverflow.com/documentation/blender? You don't have to contribute, we just need one more person to commit. – 10 Replies Feb 25 '17 at 19:20