Skip to content

npxpy.nodes.space.Array

Bases: _GatekeeperSpace

Class representing an array node with additional attributes.

Attributes:

Name Type Description
position List[float]

Position of the array [x, y, z].

rotation List[float]

Rotation of the array [psi, theta, phi].

count List[int]

Number of grid points in [x, y] direction.

spacing List[float]

Spacing of the grid in [width, height].

order str

Order of the array ('Lexical' or 'Meander').

shape str

Shape of the array ('Rectangular' or 'Round').

array_type str

Type of array ('rect', 'hex', 'custom')

Source code in npxpy/nodes/space.py
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
class Array(_GatekeeperSpace):
    """
    Class representing an array node with additional attributes.

    Attributes:
        position (List[float]): Position of the array [x, y, z].
        rotation (List[float]): Rotation of the array [psi, theta, phi].
        count (List[int]): Number of grid points in [x, y] direction.
        spacing (List[float]): Spacing of the grid in [width, height].
        order (str): Order of the array ('Lexical' or 'Meander').
        shape (str): Shape of the array ('Rectangular' or 'Round').
        array_type (str): Type of array ('rect', 'hex', 'custom')
    """

    def __init__(
        self,
        name: str = "Array",
        position: List[float] = [0, 0, 0],
        rotation: List[float] = [0.0, 0.0, 0.0],
        count: List[int] = [5, 5],
        spacing: List[float] = [100.0, 100.0],
        order: str = "Lexical",
        shape: str = "Rectangular",
    ):
        """
        Initialize an Array node.
        """
        super().__init__("array", name)
        self.position = position
        self.rotation = rotation
        self.count = count
        self.spacing = spacing
        self.order = order
        self.shape = shape
        self._array_type = "RECTANGULAR_GRID"

    # Setters for other attributes
    @property
    def count(self):
        return self._count

    @count.setter
    def count(self, value: List[int]):
        if len(value) < 2 or not all(
            isinstance(c, int) and c > 0 for c in value
        ):
            raise ValueError(
                "Count must be a list of at least two (max. three) integers greater than zero."
            )
        if len(value) > 3:
            value = value[:2]
        self._count = value

    @property
    def spacing(self):
        return self._spacing

    @spacing.setter
    def spacing(self, value: List[float]):
        if len(value) < 2 or not all(
            isinstance(s, (int, float)) for s in value
        ):
            raise ValueError(
                "Spacing must be a list of at least two (max. three) numeric values."
            )
        if len(value) > 3:
            value = value[:2]
        self._spacing = value

    @property
    def order(self):
        return self._order

    @order.setter
    def order(self, value: str):
        if value not in ["Lexical", "Meander"]:
            raise ValueError("order must be either 'Lexical' or 'Meander'.")
        self._order = value

    @property
    def shape(self):
        return self._shape

    @shape.setter
    def shape(self, value: str):
        if value not in ["Rectangular", "Round"]:
            raise ValueError("shape must be either 'Rectangular' or 'Round'.")
        self._shape = value

    def set_grid(
        self, count: List[int] = count, spacing: List[float] = spacing
    ):
        """
        Set the count and spacing of the array grid.

        Parameters:
            count (List[int]): The new grid point count.
            spacing (List[float]): The new grid spacing.

        Returns:
            Array: The updated Array object.
        """
        self._array_type = "RECTANGULAR_GRID"
        self.count = count
        self.spacing = spacing
        return self

    def set_hexagonal_grid(
        self, count: List[int] = count, hex_spacing: float = 100.0
    ):
        """
        Set the count and spacing of the hexagonal array grid.

        Parameters:
            count (List[int]): The new grid point count.
            hex_spacing (float): The hexagonal grid spacing.

        Returns:
            Array: The updated Array object.
        """

        if not isinstance(hex_spacing, (int, float)):
            raise ValueError("hexagonal distance must be a numeric value.")

        self._array_type = "HEXAGONAL_GRID"
        self.count = count
        self._hex_spacing = hex_spacing
        return self

    def set_custom_grid(
        self,
        custom_instance_positions: List[List[float]],
    ):
        """
        Set the grid positions of the custom array grid.

        Parameters:
            custom_instance_positions (List[List[float]]): The custom grid points count.

        Returns:
            Array: The updated Array object.
        """
        self._irregular_instance_positions = []
        self._array_type = "IRREGULAR"
        try:
            for custom_instance_position in custom_instance_positions:

                if len(custom_instance_position) < 3:
                    raise ValueError(
                        "Position must be an iterable (list, tuple, etc.) of three numeric values."
                    )
                elif len(custom_instance_position) > 3:
                    custom_instance_position = custom_instance_position[:3]

                try:
                    custom_instance_position = [
                        float(s) for s in custom_instance_position
                    ]
                    self._irregular_instance_positions.append(
                        custom_instance_position
                    )
                except (TypeError, ValueError) as e:
                    raise TypeError(
                        f"Position must be an iterable (list, tuple, etc.) of three numeric values."
                    ) from e

        except (TypeError, ValueError) as e:
            raise TypeError(
                f"custom_instance_positions must be an iterable (list, tuple, etc.), containing iterables with numeric values with three entries!"
                f"got {type(custom_instance_positions).__name__}"
            ) from e

        return self

    def to_dict(self) -> Dict:
        """
        Convert the Array object into a dictionary.
        """
        node_dict = super().to_dict()
        node_dict["position"] = self.position
        node_dict["rotation"] = self.rotation
        node_dict["count"] = self.count
        node_dict["spacing"] = self.spacing
        node_dict["order"] = self.order
        node_dict["shape"] = self.shape
        node_dict["array_type"] = self._array_type

        if self._array_type == "HEXAGONAL_GRID":
            node_dict["hexagonal_spacing"] = self._hex_spacing
        elif self._array_type == "IRREGULAR":
            node_dict["irregular_instance_positions"] = (
                self._irregular_instance_positions
            )
        return node_dict

__init__(name='Array', position=[0, 0, 0], rotation=[0.0, 0.0, 0.0], count=[5, 5], spacing=[100.0, 100.0], order='Lexical', shape='Rectangular')

Initialize an Array node.

Source code in npxpy/nodes/space.py
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
def __init__(
    self,
    name: str = "Array",
    position: List[float] = [0, 0, 0],
    rotation: List[float] = [0.0, 0.0, 0.0],
    count: List[int] = [5, 5],
    spacing: List[float] = [100.0, 100.0],
    order: str = "Lexical",
    shape: str = "Rectangular",
):
    """
    Initialize an Array node.
    """
    super().__init__("array", name)
    self.position = position
    self.rotation = rotation
    self.count = count
    self.spacing = spacing
    self.order = order
    self.shape = shape
    self._array_type = "RECTANGULAR_GRID"

set_custom_grid(custom_instance_positions)

Set the grid positions of the custom array grid.

Parameters:

Name Type Description Default
custom_instance_positions List[List[float]]

The custom grid points count.

required

Returns:

Name Type Description
Array

The updated Array object.

Source code in npxpy/nodes/space.py
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
def set_custom_grid(
    self,
    custom_instance_positions: List[List[float]],
):
    """
    Set the grid positions of the custom array grid.

    Parameters:
        custom_instance_positions (List[List[float]]): The custom grid points count.

    Returns:
        Array: The updated Array object.
    """
    self._irregular_instance_positions = []
    self._array_type = "IRREGULAR"
    try:
        for custom_instance_position in custom_instance_positions:

            if len(custom_instance_position) < 3:
                raise ValueError(
                    "Position must be an iterable (list, tuple, etc.) of three numeric values."
                )
            elif len(custom_instance_position) > 3:
                custom_instance_position = custom_instance_position[:3]

            try:
                custom_instance_position = [
                    float(s) for s in custom_instance_position
                ]
                self._irregular_instance_positions.append(
                    custom_instance_position
                )
            except (TypeError, ValueError) as e:
                raise TypeError(
                    f"Position must be an iterable (list, tuple, etc.) of three numeric values."
                ) from e

    except (TypeError, ValueError) as e:
        raise TypeError(
            f"custom_instance_positions must be an iterable (list, tuple, etc.), containing iterables with numeric values with three entries!"
            f"got {type(custom_instance_positions).__name__}"
        ) from e

    return self

set_grid(count=count, spacing=spacing)

Set the count and spacing of the array grid.

Parameters:

Name Type Description Default
count List[int]

The new grid point count.

count
spacing List[float]

The new grid spacing.

spacing

Returns:

Name Type Description
Array

The updated Array object.

Source code in npxpy/nodes/space.py
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
def set_grid(
    self, count: List[int] = count, spacing: List[float] = spacing
):
    """
    Set the count and spacing of the array grid.

    Parameters:
        count (List[int]): The new grid point count.
        spacing (List[float]): The new grid spacing.

    Returns:
        Array: The updated Array object.
    """
    self._array_type = "RECTANGULAR_GRID"
    self.count = count
    self.spacing = spacing
    return self

set_hexagonal_grid(count=count, hex_spacing=100.0)

Set the count and spacing of the hexagonal array grid.

Parameters:

Name Type Description Default
count List[int]

The new grid point count.

count
hex_spacing float

The hexagonal grid spacing.

100.0

Returns:

Name Type Description
Array

The updated Array object.

Source code in npxpy/nodes/space.py
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
def set_hexagonal_grid(
    self, count: List[int] = count, hex_spacing: float = 100.0
):
    """
    Set the count and spacing of the hexagonal array grid.

    Parameters:
        count (List[int]): The new grid point count.
        hex_spacing (float): The hexagonal grid spacing.

    Returns:
        Array: The updated Array object.
    """

    if not isinstance(hex_spacing, (int, float)):
        raise ValueError("hexagonal distance must be a numeric value.")

    self._array_type = "HEXAGONAL_GRID"
    self.count = count
    self._hex_spacing = hex_spacing
    return self

to_dict()

Convert the Array object into a dictionary.

Source code in npxpy/nodes/space.py
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
def to_dict(self) -> Dict:
    """
    Convert the Array object into a dictionary.
    """
    node_dict = super().to_dict()
    node_dict["position"] = self.position
    node_dict["rotation"] = self.rotation
    node_dict["count"] = self.count
    node_dict["spacing"] = self.spacing
    node_dict["order"] = self.order
    node_dict["shape"] = self.shape
    node_dict["array_type"] = self._array_type

    if self._array_type == "HEXAGONAL_GRID":
        node_dict["hexagonal_spacing"] = self._hex_spacing
    elif self._array_type == "IRREGULAR":
        node_dict["irregular_instance_positions"] = (
            self._irregular_instance_positions
        )
    return node_dict