3
$\begingroup$

I am still gnawing stubbornly on the concept "dump active object to python script," and gathering snippets that I think can be glued together into the tool I want. But one piece of the puzzle still eludes me. I would like to find out, for any property of an object, what its default value is. I think if I were more familiar with Blender and/or Python this might be a no-brainer, but despite googling around a bit I still haven't found the clue. If someone can throw a small snippet my way that shows how to discover the default value of some property such as, say, Diffuse Intensity, I think I can generalise to other cases.

$\endgroup$

2 Answers 2

10
$\begingroup$

To get all the default values of all properties defined on the context object. Using the RNA (bl_rna) will apply to all objects in blender.

import bpy

context = bpy.context
obj = context.object

for prop in obj.bl_rna.properties:
    if hasattr(prop, "default"):
        default = prop.default
        print("%s (%s) default:" % (prop.identifier, prop.name), prop.default)
        # check arrays
        if getattr(prop, "is_array", False):
            default_array = [p for p in prop.default_array]
            print("default array", default_array)
            current_value = [p for p in getattr(obj, prop.identifier)]
            print("current value", current_value)
            print("is default value", current_value == default_array)

Also have a look at 2.78/scripts/modules/rna_xml.py which creates xml from blender objects.

$\endgroup$
0
6
$\begingroup$

More specifically to access the default value of a given property on a given object, you can use something like this :

import bpy


def get_default(holder, prop_name):
    prop = holder.bl_rna.properties[prop_name]
    if prop.default_array:
        return [v for v in prop.default_array]
    else:
        return prop.default
    
if __name__ == "__main__":
    bpy.types.Scene.test = bpy.props.IntProperty(default=5)
    bpy.types.Scene.test_array = bpy.props.IntVectorProperty(default=(5, 6, 7))
    scene = bpy.context.scene
    print(get_default(scene, "test"))
    print(get_default(scene, "test_array"))

$\endgroup$
1
  • $\begingroup$ This was really helpfull thanks $\endgroup$
    – soerensc_
    Commented Feb 2, 2023 at 9:06

You must log in to answer this question.

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