Source code for nukescripts.flags

# Copyright (c) 2009 The Foundry Visionmongers Ltd.  All Rights Reserved.

from __future__ import annotations

from types import TracebackType

import nuke_internal as nuke


[docs]class KnobFlagClearer: def __init__(self: KnobFlagClearer, knob: nuke.Knob, flags: set[int]) -> None: self.knob: nuke.Knob = knob self.flags: set[int] = flags self.original_flags: dict[int, bool] = {} def __enter__(self: KnobFlagClearer) -> KnobFlagClearer: for flag in self.flags: self.original_flags[flag] = self.knob.getFlag(flag) if self.original_flags[flag]: self.knob.clearFlag(flag) return self def __exit__( self: KnobFlagClearer, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None, ) -> bool | None: for flag, was_set in self.original_flags.items(): if was_set: self.knob.setFlag(flag) return None
[docs]def get_fully_qualified_name(node): """ Returns the full name of the node, including any parents, separated by periods. :param node: Node to retrieve name from. :type node: nuke.Node :return: Full name of the node, including any parents, separated by periods. :rtype: str """ if node.Class() == "Root": return "" parent_name = get_fully_qualified_name(node.parent()) if parent_name == "": return node.name() return parent_name + "." + node.name()
[docs]def toggle(knob): """ "Inverts" some flags on the selected nodes. What this really does is set all of them to the same value, by finding the majority value and using the inverse of that.""" value = 0 n = nuke.selectedNodes(recursive=True) for i in n: try: val = i.knob(knob).value() if val: value += 1 else: value -= 1 except: pass status = value < 0 for i in n: knobbie_str = get_fully_qualified_name(i) + "." + knob if not nuke.exists(knobbie_str): continue knobbie = i.knob(knob) size = nuke.animation(knobbie_str, "size") if size is not None and int(size) > 0: knobbie.setKeyAt(nuke.frame()) knobbie.setValue(status) else: knobbie.setValue(status) nuke.modified(True)