Skip to content

npxpy.nodes.structures.Text

Bases: Structure

A class representing a text node.

Attributes:

Name Type Description
text str

The text content.

font_size Union[float, int]

The font size of the text.

height Union[float, int]

The height of the text.

Source code in npxpy/nodes/structures.py
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
class Text(Structure):
    """
    A class representing a text node.

    Attributes:
        text (str): The text content.
        font_size (Union[float, int]): The font size of the text.
        height (Union[float, int]): The height of the text.
    """

    def __init__(
        self,
        preset: Preset,
        name: str = "Text",
        text: str = "Text",
        font_size: Union[float, int] = 10.0,
        height: Union[float, int] = 5.0,
        slicing_origin: str = "scene_bottom",
        slicing_offset: Union[float, int] = 0.0,
        priority: int = 0,
        expose_individually: bool = False,
        position: List[Union[float, int]] = [0, 0, 0],
        rotation: List[Union[float, int]] = [0.0, 0.0, 0.0],
        color="lightblue",
    ):
        """
        Initialize a Text node.

        Parameters:
            preset (Preset): The preset associated with the text.
            name (str): The name of the text.
            text (str): The text content.
            font_size (Union[float, int]): The font size of the text. Must be greater than 0.
            height (Union[float, int]): The height of the text. Must be greater than 0.
            slicing_origin (str): The origin for slicing. Must be one of
                                  'structure_center', 'zero', 'scene_top', 'scene_bottom',
                                  'structure_top', 'structure_bottom', 'scene_center'.
            slicing_offset (Union[float, int]): The offset for slicing.
            priority (int): The priority of the text. Must be >= 0.
            expose_individually (bool): Flag to expose the text individually.
            position (List[Union[float, int]]): The position of the text [x, y, z].
            rotation (List[Union[float, int]]): The rotation of the text [psi, theta, phi].
        """
        super().__init__(
            preset=preset,
            mesh=None,
            name=name,
            slicing_origin=slicing_origin,
            slicing_offset=slicing_offset,
            priority=priority,
            expose_individually=expose_individually,
            position=position,
            rotation=rotation,
            color=color,
        )

        # Setters for validation
        self.text = text
        self.font_size = font_size
        self.height = height

        self.load_mesh = False
        self.load_preset = True

        #  This guy sits in Structure. Ensures no mesh is passed.
        self._mesh = False

    @property
    def text(self):
        """The text content of the node."""
        return self._text

    @text.setter
    def text(self, value: str):
        if not isinstance(value, str):
            raise TypeError("text must be a string.")
        self._text = value

    @property
    def font_size(self):
        """The font size of the text."""
        return self._font_size

    @font_size.setter
    def font_size(self, value: Union[float, int]):
        if not isinstance(value, (float, int)) or value <= 0:
            raise ValueError("font_size must be a positive number.")
        self._font_size = value

    @property
    def height(self):
        """The height of the text."""
        return self._height

    @height.setter
    def height(self, value: Union[float, int]):
        if not isinstance(value, (float, int)) or value <= 0:
            raise ValueError("height must be a positive number.")
        self._height = value

    def auto_load(
        self,
        project: Project,
    ):
        """
        Load passed presets to passed project if the flags are set.
        """
        self.project = project

        if self.load_preset:
            self.project.load_presets(self.preset)

        if self.load_mesh:
            if self.mesh._type != "mesh_file":
                raise TypeError(
                    "Images are used only for MarkerAligner class."
                )
            self.project.load_resources(self.mesh)
        return self

    def _to_dict(self) -> dict:
        """
        Convert the text to a dictionary representation.

        Returns:
            dict: The dictionary representation of the text.
        """
        self.geometry = {
            "type": "text",
            "text": self.text,
            "font_size": self.font_size,
            "height": self.height,
        }
        node_dict = super()._to_dict()
        node_dict["geometry"] = self.geometry
        return node_dict

font_size property writable

The font size of the text.

height property writable

The height of the text.

text property writable

The text content of the node.

__init__(preset, name='Text', text='Text', font_size=10.0, height=5.0, slicing_origin='scene_bottom', slicing_offset=0.0, priority=0, expose_individually=False, position=[0, 0, 0], rotation=[0.0, 0.0, 0.0], color='lightblue')

Initialize a Text node.

Parameters:

Name Type Description Default
preset Preset

The preset associated with the text.

required
name str

The name of the text.

'Text'
text str

The text content.

'Text'
font_size Union[float, int]

The font size of the text. Must be greater than 0.

10.0
height Union[float, int]

The height of the text. Must be greater than 0.

5.0
slicing_origin str

The origin for slicing. Must be one of 'structure_center', 'zero', 'scene_top', 'scene_bottom', 'structure_top', 'structure_bottom', 'scene_center'.

'scene_bottom'
slicing_offset Union[float, int]

The offset for slicing.

0.0
priority int

The priority of the text. Must be >= 0.

0
expose_individually bool

Flag to expose the text individually.

False
position List[Union[float, int]]

The position of the text [x, y, z].

[0, 0, 0]
rotation List[Union[float, int]]

The rotation of the text [psi, theta, phi].

[0.0, 0.0, 0.0]
Source code in npxpy/nodes/structures.py
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
def __init__(
    self,
    preset: Preset,
    name: str = "Text",
    text: str = "Text",
    font_size: Union[float, int] = 10.0,
    height: Union[float, int] = 5.0,
    slicing_origin: str = "scene_bottom",
    slicing_offset: Union[float, int] = 0.0,
    priority: int = 0,
    expose_individually: bool = False,
    position: List[Union[float, int]] = [0, 0, 0],
    rotation: List[Union[float, int]] = [0.0, 0.0, 0.0],
    color="lightblue",
):
    """
    Initialize a Text node.

    Parameters:
        preset (Preset): The preset associated with the text.
        name (str): The name of the text.
        text (str): The text content.
        font_size (Union[float, int]): The font size of the text. Must be greater than 0.
        height (Union[float, int]): The height of the text. Must be greater than 0.
        slicing_origin (str): The origin for slicing. Must be one of
                              'structure_center', 'zero', 'scene_top', 'scene_bottom',
                              'structure_top', 'structure_bottom', 'scene_center'.
        slicing_offset (Union[float, int]): The offset for slicing.
        priority (int): The priority of the text. Must be >= 0.
        expose_individually (bool): Flag to expose the text individually.
        position (List[Union[float, int]]): The position of the text [x, y, z].
        rotation (List[Union[float, int]]): The rotation of the text [psi, theta, phi].
    """
    super().__init__(
        preset=preset,
        mesh=None,
        name=name,
        slicing_origin=slicing_origin,
        slicing_offset=slicing_offset,
        priority=priority,
        expose_individually=expose_individually,
        position=position,
        rotation=rotation,
        color=color,
    )

    # Setters for validation
    self.text = text
    self.font_size = font_size
    self.height = height

    self.load_mesh = False
    self.load_preset = True

    #  This guy sits in Structure. Ensures no mesh is passed.
    self._mesh = False

auto_load(project)

Load passed presets to passed project if the flags are set.

Source code in npxpy/nodes/structures.py
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
def auto_load(
    self,
    project: Project,
):
    """
    Load passed presets to passed project if the flags are set.
    """
    self.project = project

    if self.load_preset:
        self.project.load_presets(self.preset)

    if self.load_mesh:
        if self.mesh._type != "mesh_file":
            raise TypeError(
                "Images are used only for MarkerAligner class."
            )
        self.project.load_resources(self.mesh)
    return self