I'd like to use the same update function for several properties but I will need to know which property calls this function like in this script exemple.

import bpy
from bpy.types import Panel, PropertyGroup
from bpy.props import BoolProperty, PointerProperty

def update_function(self, context):
    #if firstProp use this function:
    #   do this
    #elif secondProp use this function:
    #   do that


class MyCollectionProperty(PropertyGroup):

    firstProp = BoolProperty(
            name = "Fisrt",
            default = False,
            update = update_function,
            )

    secondProp = BoolProperty(
            name = "Second",
            default = False,
            update = update_function,
            )

class TestPanel(Panel):
    bl_idname = "test_panel"
    bl_label = "Test Panel"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_category = "category"

    def draw(self, context):
        myProps = context.window_manager.myProps
        layout = self.layout

        layout.prop(myProps, "firstProp")
        layout.prop(myProps, "secondProp")

def register():
    bpy.utils.register_module(__name__)
    bpy.types.WindowManager.myProps = PointerProperty(type = MyCollectionProperty)

def unregister():
    del bpy.types.WindowManager.myProps
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()

Is this possible or are we obliged to create a function per property?

up vote 2 down vote accepted
def update_function(self, context, origin):
    if origin == 'firstProp':
        #   do this
    elif origin == 'secondProp':
        #   do that


class MyCollectionProperty(PropertyGroup):

    firstProp = BoolProperty(
            name = "Fisrt",
            default = False,
            update = lambda s, c: update_function(s, c, 'firstProp'),
            )

    secondProp = BoolProperty(
            name = "Second",
            default = False,
            update = lambda s, c: update_function(s, c, 'secondProp'),
            )

use a lambda, which passes the 'origin' to the update_function.

An update_function that prints out the value of the property

def update_function(self, context, origin):
    # - use "origin" argument to pass the member variable being modified
    # - "self" will refer to the instance of the MyCollectionProperty
    print(origin, ':' , getattr(self, origin))  

    if origin == 'firstProp':
        #   do this
    elif origin == 'secondProp':
        #   do that

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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