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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
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

to_dict()

Convert the text to a dictionary representation.

Returns:

Name Type Description
dict dict

The dictionary representation of the text.

Source code in npxpy/nodes/structures.py
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
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