0
$\begingroup$

I need to update mesh vertex colors when color has been selected from color wheel. I don't want to do this all the time when color item is updated, because that would be too slow.

Is there a way to know when color wheel is closed? I also looked into timer-based solutions but unfortunately context seemed to be wrong and I could not use them. Also I cannot do this within draw function for the same reason.

$\endgroup$

1 Answer 1

2
$\begingroup$
import bpy
from functools import partial
from time import time

from ..third_party.boltons.funcutils import wraps

def parametrized(dec):
    def layer(*args, **kwargs):
        def repl(f):
            return dec(f, *args, **kwargs)
        return repl
    return layer

@parametrized
def delay_execution(func, delay_sec=1.0):

    lock = False
    def timer(*args, **kwargs):
        nonlocal lock
        lock = False

        func(*args, **kwargs)

    @wraps(func)
    def wrapped(*args, **kwargs):
        nonlocal lock

        if not lock:
            lock = True
            bpy.app.timers.register(partial(timer, *args, **kwargs), first_interval=delay_sec)

    return wrapped

@parametrized
def on_release(func, delay_sec=1.5):

    exec_time = time()

    def timer(*args, **kwargs):
        nonlocal exec_time
        if not abs(exec_time - time()) < delay_sec:
            func(*args, **kwargs)

    @wraps(func)
    def wrapped(*args, **kwargs):
        nonlocal exec_time
        exec_time = time()

        bpy.app.timers.register(partial(timer, *args, **kwargs), first_interval=max(1.0, delay_sec))

    return wrapped

I achieved this by creating these two useful decorators. It'd require the library 'boltons' to be added to your addon.

@on_release() is the one you are interested in. Simply put that over your update callback, you can also specify delay (default is 1.5 sec) after which the function gets executed when you stop changing the property by doing this @on_release(delay_sec=your_delay)

$\endgroup$
1
  • $\begingroup$ In case you are wondering why I used boltons @wraps instead of its equivalent from standard library functools is because unlike the standard implementation of wraps, it preserves the function's signature which is essential for the wrapped update callback to run at all. $\endgroup$
    – D. Skarn
    Commented May 8, 2020 at 12:02

You must log in to answer this question.

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