Skip to content

command

knob_values(values)

Context manager to set knob values temporarily.

Parameters:

Name Type Description Default
values dict[Knob, Any]

Dictionary of knob and value pairs to set.

required

Returns:

Type Description
None

Generator to yield the context manager.

Source code in client/ayon_nuke/api/command.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@contextlib.contextmanager
def knob_values(
    values: dict[nuke.Knob, typing.Any],
) -> typing.Generator[None, None, None]:
    """Context manager to set knob values temporarily.

    Args:
        values: Dictionary of knob and value pairs to set.

    Returns:
        Generator to yield the context manager.

    """
    original_values = {knob: knob.value() for knob in values}
    try:
        for knob, value in values.items():
            knob.setValue(value)
        yield
    finally:
        for knob, value in original_values.items():
            knob.setValue(value)

undo_chunk(name='')

Context manager to wrap multiple actions into a single undo chunk.

The name of the undo chunk can either be given when entering the block, or updated later using the nuke.Undo.name(name) method.

Parameters:

Name Type Description Default
name str

Name of the undo chunk.

''

Examples:

As a context manager:

>>> with undo_chunk("Load Image"):
>>>    # do multiple actions here
>>>    ...

As a decorator:

>>> @undo_chunk("Load Image")
>>> def load_image():
>>>    # do multiple actions here
>>>    ...
Source code in client/ayon_nuke/api/command.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@contextlib.contextmanager
def undo_chunk(name: str = ""):
    """Context manager to wrap multiple actions into a single undo chunk.

    The name of the undo chunk can either be given when entering the block,
    or updated later using the `nuke.Undo.name(name)` method.

    Args:
        name (str): Name of the undo chunk.

    Examples:
        As a context manager:
        >>> with undo_chunk("Load Image"):
        >>>    # do multiple actions here
        >>>    ...

        As a decorator:
        >>> @undo_chunk("Load Image")
        >>> def load_image():
        >>>    # do multiple actions here
        >>>    ...

    """
    nuke.Undo.begin()
    if name:
        nuke.Undo.name(name)

    try:
        yield
    finally:
        nuke.Undo.end()

viewer_update_and_undo_stop()

Lock viewer from updating and stop recording undo steps

Source code in client/ayon_nuke/api/command.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
@contextlib.contextmanager
def viewer_update_and_undo_stop():
    """Lock viewer from updating and stop recording undo steps"""
    try:
        # stop active viewer to update any change
        viewer = nuke.activeViewer()
        if viewer:
            viewer.stop()
        else:
            log.warning("No available active Viewer")
        nuke.Undo.disable()
        yield
    finally:
        nuke.Undo.enable()