Skip to content

add_node_component

add_node_component

Attributes

LOGGER = logging.getLogger(__name__) module-attribute

Classes

AddNodeComponentCommand(node, type_id, name=None, parent=None)

Bases: NyxCommand

Add a new component to a node.

Initialize AddNodeComponentCommand.

Parameters:

Name Type Description Default
node Node

node receiving the component.

required
type_id str

registered component type id.

required
name str | None

component name. Defaults to the type's default name.

None
parent QUndoCommand | None

parent command.

None
Source code in src/nyx/core/commands/add_node_component.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def __init__(
    self,
    node: Node,
    type_id: str,
    name: str | None = None,
    parent: QtGui.QUndoCommand | None = None,
) -> None:
    """Initialize AddNodeComponentCommand.

    Args:
        node (Node): node receiving the component.
        type_id (str): registered component type id.
        name (str | None): component name. Defaults to the type's default name.
        parent (QtGui.QUndoCommand | None): parent command.
    """
    super().__init__(
        stage=node.stage, text=f"Add component {type_id}", parent=parent
    )
    self.node_uuid = node.uuid
    self.component_uuid = None
    self.type_id = type_id
    self.name = name
Attributes
MODIFIES_STRUCTURE = True class-attribute instance-attribute
component_uuid = None instance-attribute
name = name instance-attribute
node_uuid = node.uuid instance-attribute
result_component property

NodeComponent: component created by the command.

type_id = type_id instance-attribute
Functions
redo()

Create and add the component.

Source code in src/nyx/core/commands/add_node_component.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def redo(self) -> None:
    """Create and add the component."""
    node = self.stage.get_object(self.node_uuid, expected_type=Node)
    component = node.components_model.create_component(
        self.type_id, uuid=self.component_uuid
    )
    self.name = NameValidator.get_valid_stage_object_name(
        self.name or component.DEFAULT_NAME
    )
    component.name = self.name
    node.components_model.add_component(component)
    self.component_uuid = component.uuid

    self.setText(f"Add component ({node.path}.{component.name} ({self.type_id})")
    return super().redo()
target_nodes()

list[Node]: nodes the command changes.

Source code in src/nyx/core/commands/add_node_component.py
47
48
49
def target_nodes(self):
    """list[Node]: nodes the command changes."""
    return [self.stage.get_object(self.node_uuid)]
undo()

Remove the added component.

Source code in src/nyx/core/commands/add_node_component.py
67
68
69
70
71
def undo(self) -> None:
    """Remove the added component."""
    node = self.stage.get_object(self.node_uuid)
    node.components_model.remove_component(self.component_uuid)
    return super().undo()
validate()

Check the node exists and the component type is registered.

Source code in src/nyx/core/commands/add_node_component.py
73
74
75
76
77
78
79
80
81
82
def validate(self):
    """Check the node exists and the component type is registered."""
    node = self.stage.get_object(self.node_uuid)
    if node is None:
        raise NyxCommandValidationError(
            "Target node doesn't exist %s:", str(self.node_uuid)
        )

    if self.type_id not in NYX_REGISTRY.node_components.keys():
        raise NyxCommandValidationError("Invalid type_id: %s", self.type_id)