1
$\begingroup$

When I choose an option for one of my enumators in my script, I can tell from the "info" space that there is a command:

bpy.context.scene.extra.[name of enumerator] = '[option]'

I want to have the script do something whenever this command happens, or just whenever the enumerator is changed in general, but I don't know how.

$\endgroup$
4
  • 1
    $\begingroup$ Try to use Message Bus: docs.blender.org/api/current/bpy.msgbus.html $\endgroup$
    – X Y
    Commented Sep 24, 2022 at 10:54
  • $\begingroup$ Well right now I just have: owner = bpy.types.Scene.extra key = bpy.context.scene.extra.Enumerator_name bpy.msgbus.subscribe_rna(key=key, owner=owner, args=(), notify=doSomething,). To be honest I have no idea what I'm doing, but it did work when I filled in owner = object() and key = bpy.context.object.location, like they did on the link that you sent. @XY $\endgroup$
    – ColinT
    Commented Oct 9, 2022 at 12:31
  • $\begingroup$ Update: I managed to make it work! I did a little bit more research and thanks to this helpful q&a: blender.stackexchange.com/questions/224010/… I managed to make it work: Here is the final result: owner = bpy.types.Scene.extra key = bpy.context.scene.path_resolve("extra.Enumerator_name", False) bpy.msgbus.subscribe_rna(key=key, owner=owner, args=(), notify=doSomething,) $\endgroup$
    – ColinT
    Commented Oct 9, 2022 at 12:46
  • $\begingroup$ If you found an answer, please answer yourself, it will help other people with similar problems in the future. $\endgroup$ Commented Oct 9, 2022 at 17:53

2 Answers 2

2
$\begingroup$

I figured out how to do it with a "Message Bus". I put the following script under def register()::

def register():
    owner = bpy.types.Scene.extra
    key = bpy.context.scene.path_resolve("extra.Enumerator_name", False)
    bpy.msgbus.subscribe_rna(key=key, owner=owner, args=(), notify=doSomething,)

What the code does is it effects doSomething whenever the enumerator is changed. To set up doSomething, you just do the following: type "def doSomething(*args):" somewhere in your script and then put whatever it is you want to happen after that. Here are the sources I used in case you want to check it out: https://docs.blender.org/api/current/bpy.msgbus.html and Using Message Bus to trigger events based on custom property changes

$\endgroup$
1
$\begingroup$

Why don't you use Update? It seems to be very easy:

import bpy

def update_func(self, context):
    print("Updated: ", self.testprop)

bpy.types.Scene.testprop = bpy.props.FloatProperty(update=update_func)

bpy.context.scene.testprop = 11.0
$\endgroup$

You must log in to answer this question.

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