6
$\begingroup$

I am creating many boolean properties and want to be able to control the properties with a single worker function. The property name will be the logic driver. Is this possible? If so how? Or do I need to create a function for each property?

import bpy
from bpy.props import BoolProperty

def worker_func(self, context):
    r""" How to get the name of the envoked property"""
    print (self.name) #??
    """
        if prop_a:
            do stuff for prop_a
        if prop_b:
            do stuff for prop_b
        if prop_c:
            do stuff for prop_c
     """

bpy.types.Scene.prop_a = BoolProperty( update = worker_func )
bpy.types.Scene.prop_b = BoolProperty( update = worker_func )
bpy.types.Scene.prop_c = BoolProperty( update = worker_func )

class HelloWorldPanel(bpy.types.Panel):
    bl_label = "Hello"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'

    def draw(self, context):
        layout = self.layout
        layout.prop(context.scene, "prop_a", text="a")
        layout.prop(context.scene, "prop_b", text="b")
        layout.prop(context.scene, "prop_c", text="c")



def register():
    bpy.utils.register_class(HelloWorldPanel)


def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)


if __name__ == "__main__":
    register()

Thanks you very much. A.Kal

$\endgroup$

1 Answer 1

10
$\begingroup$

The update method only gets the reference to the ID-block who owns the property and the context passed. So you have to create a function for each property individually.

You can do this dynamically

import bpy

def callback(scene, prop):
    print("Property '%s' of %s updated to value %s" % (prop, scene.name, getattr(scene, prop)))

def create_update_func(prop):
    def update(self, context):
        callback(self, prop)
    return update

bpy.types.Scene.prop_a = bpy.props.BoolProperty(update=create_update_func("prop_a"))
bpy.types.Scene.prop_b = bpy.props.BoolProperty(update=create_update_func("prop_b"))
bpy.types.Scene.prop_c = bpy.props.BoolProperty(update=create_update_func("prop_c"))
$\endgroup$
4
  • 1
    $\begingroup$ That's pretty clever! $\endgroup$
    – TLousky
    Oct 7, 2015 at 8:16
  • 2
    $\begingroup$ or update=lambda self, context: some_func(self, context, 'a') $\endgroup$
    – zeffii
    Feb 28, 2017 at 14:08
  • $\begingroup$ Stumbled here by chance, but @zeffii thats precisely what I've been using since I started coding props. :-) Do you know any drawbacks to this method? I just used it as it seemed the fastest way to do it. $\endgroup$
    – Teck-freak
    Nov 28, 2017 at 8:52
  • $\begingroup$ @Teck-freak not really, it was ideasman (one of Blender's head developers) who proposed the lambda approach. I also like it because it's concise. $\endgroup$
    – zeffii
    Nov 30, 2017 at 14:21

You must log in to answer this question.

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