Detect StringProperty textfield change?

Hi,

how do I detect an input field has changed in ui panel ?

image

I tried this but the function does not trigger

def changeActiveBoneName():
    print("Active bone name changed")

class SomeRegisteredClass:

    boneName: StringProperty(
        name="boneName",
        description="Bone name",
        update=changeActiveBoneName()
        )

thanks for your precious help

I’ve gone in quite some length to explain why this doesn’t work on BSE.

TLDR : You’re assigning the result of changeActiveBoneName() which is None. You want to use the function, not the result of the function. Even then, the function must take two mandatory arguments, self and context. See https://docs.blender.org/api/current/bpy.props.html#bpy.props.StringProperty

You can circumvent that by using

 update=lambda self, context: changeActiveBoneName()

or modifying you function

def changeActiveBoneName(self, context):
    print("Active bone name changed")
    print(f"The value is now {self.boneName}")

class SomeRegisteredClass:

    boneName: StringProperty(
        name="boneName",
        description="Bone name",
        update=changeActiveBoneName
        )
2 Likes

Current setup will call update function once you enter the text and press enter to confirm the change.
What you have is correct but you have made couple of syntax errors.
update = changeActiveBoneName instead of update = changeActiveBoneName()
and def changeActiveBoneName(self,context): instead of def changeActiveBoneName():

If you want to call update function as you write/change the text field you need to define options and set it to TEXTEDIT_UPDATE in your property like this,

    boneName: StringProperty(
        name="boneName",
        description="Bone name",
        options = {'TEXTEDIT_UPDATE'}
        )

So your final code with all correction and update call while editing text should look like this,

def changeActiveBoneName(self, context):
    print("Active bone name changed")

class SomeRegisteredClass:

    boneName: StringProperty(
        name="boneName",
        description="Bone name",
        update=changeActiveBoneName,
        options = {'TEXTEDIT_UPDATE'}
        )
1 Like