Skip to content

npxpy.nodes.project.Project

Bases: Node

Class: project nodes.

Attributes:

Name Type Description
presets list

List of presets for the project.

resources list

List of resources for the project.

project_info dict

Information about the project including author, objective, resin, substrate, and creation date.

apply_curvature_compensation bool

Whether curvature compensation is applied when printing.

stage_velocity float

Stage velocity in mm/s, between 0.1 and 50.0 (inclusive).

Source code in npxpy/nodes/project.py
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
class Project(Node):
    """
    Class: project nodes.

    Attributes:
        presets (list): List of presets for the project.
        resources (list): List of resources for the project.
        project_info (dict): Information about the project including author, objective, resin, substrate, and creation date.
        apply_curvature_compensation (bool): Whether curvature compensation is applied when printing.
        stage_velocity (float): Stage velocity in mm/s, between 0.1 and 50.0 (inclusive).
    """

    def __init__(
        self,
        objective: str,
        resin: str,
        substrate: str,
        auto_load_presets: bool = False,
        auto_load_meshes: bool = False,
        auto_load_images: bool = False,
        apply_curvature_compensation: bool = False,
        stage_velocity: float = 1.0,
    ):
        """
        Initialize the project with the specified parameters.

        Parameters:
            objective (str): Objective of the project.
            resin (str): Resin used in the project.
            substrate (str): Substrate used in the project.
            auto_load_presets (bool): Whether or not to automatically load any attached presets.
            auto_load_meshes (bool): Whether or not to automatically load any attached meshes.
            auto_load_images (bool): Whether or not to automatically load any attached images.
            apply_curvature_compensation (bool): Whether to apply curvature compensation when printing.
            stage_velocity (float): Stage velocity in mm/s. Must be between
                0.1 and 50.0 (inclusive). The value is rounded to the third
                decimal place.

        Raises:
            ValueError: If any of the parameters have invalid values.
            TypeError: If any of the parameters have invalid types.
        """
        super().__init__(node_type="project", name="Project")

        self.objective = objective
        self.resin = resin
        self.substrate = substrate

        self._presets = []
        self._resources = []
        self.project_info = {
            "author": os.getlogin(),
            "objective": self.objective,
            "resist": self.resin,
            "substrate": self.substrate,
            "creation_date": datetime.now().replace(microsecond=0).isoformat(),
        }
        self._visibility_in_plotter_disabled = []
        self._loaded_resource_ids = set()
        self._loaded_preset_ids = set()
        self._auto_load_presets = auto_load_presets
        self._auto_load_meshes = auto_load_meshes
        self._auto_load_images = auto_load_images

        self.apply_curvature_compensation = apply_curvature_compensation
        self.stage_velocity = stage_velocity
        self._custom_origin = False
        self._origin = [0.0, 0.0]

    # Setters for the attributes with validation
    @property
    def objective(self) -> str:
        return self._objective

    @objective.setter
    def objective(self, value: str):
        if value == "10x":
            self._objective = "10xW"
        else:
            valid_objectives = {
                "10xW",
                "25x",
                "63x",
                "*",
            }
            if value not in valid_objectives:
                raise ValueError(
                    f"Invalid objective: {value}. Must be one of {valid_objectives}."
                )
            self._objective = value

    @property
    def resin(self) -> str:
        return self._resin

    @resin.setter
    def resin(self, value: str):
        valid_resins = {
            "IP-Dip",
            "IP-Dip2",
            "IP-L",
            "IP-n162",
            "IP-PDMS",
            "IP-S",
            "IP-Visio",
            "IPX-Clear",
            "IPX-Q",
            "IPX-S",
            "*",
        }
        if value not in valid_resins:
            raise ValueError(
                f"Invalid resin: {value}. Must be one of {valid_resins}."
            )
        self._resin = value

    @property
    def substrate(self) -> str:
        return self._substrate

    @substrate.setter
    def substrate(self, value: str):
        valid_substrates = {"*", "Si", "FuSi"}
        if value not in valid_substrates:
            raise ValueError(
                f"Invalid substrate: {value}. Must be one of {valid_substrates}."
            )
        self._substrate = value

    @property
    def apply_curvature_compensation(self) -> bool:
        return self._apply_curvature_compensation

    @apply_curvature_compensation.setter
    def apply_curvature_compensation(self, value: bool):
        if not isinstance(value, bool):
            raise TypeError(
                "apply_curvature_compensation must be a bool, got "
                f"{type(value).__name__}."
            )
        self._apply_curvature_compensation = value

    @property
    def stage_velocity(self) -> float:
        return self._stage_velocity

    @stage_velocity.setter
    def stage_velocity(self, value: float):
        if isinstance(value, bool) or not isinstance(value, (int, float)):
            raise TypeError(
                "stage_velocity must be a number, got "
                f"{type(value).__name__}."
            )
        if not (0.1 <= value <= 50.0):
            raise ValueError(
                "stage_velocity must be between 0.1 and 50.0 (inclusive), "
                f"got {value}."
            )
        self._stage_velocity = round(float(value), 3)

    # Read-only public properties
    @property
    def presets(self) -> List[Preset]:
        """Get the list of presets."""
        return self._presets

    @property
    def resources(self) -> List[Resource]:
        """Get the list of resources."""
        return self._resources

    def start_from_current_stage_position(self) -> None:
        """
        Start the print from the current stage position.

        Disables any fixed origin previously set via
        :meth:`start_from_fixed_stage_position`.
        """
        self._custom_origin = False

    def start_from_fixed_stage_position(
        self, *, origin: Sequence[float] = (0.0, 0.0)
    ) -> None:
        """
        Start the print from a fixed stage position instead of the current
        stage position.

        Parameters:
            origin (Sequence[float]): The (x, y) stage coordinates to start
                from. Must be an iterable of at least length 2 containing
                numbers only. Values are rounded to the third decimal place.

        Raises:
            TypeError: If ``origin`` is not an iterable of at least length 2
                containing numbers only.
        """
        try:
            x, y = float(origin[0]), float(origin[1])
        except (TypeError, IndexError, ValueError, KeyError):
            raise TypeError(
                "Make sure that origin is an iterable of at least length 2 "
                "containing numbers only"
            )
        self._custom_origin = True
        self._origin = [round(x, 3), round(y, 3)]

    def load_resources(
        self, *resourcess: Union[Resource, List[Resource]]
    ) -> None:
        """
        Adds resources to the resources list, skipping duplicates based on UUID.
        """
        for resources in resourcess:
            if not isinstance(resources, list):
                resources = [resources]

            if not all(
                isinstance(resource, Resource) for resource in resources
            ):
                raise TypeError(
                    "All resources must be instances of the Resource class or its subclasses."
                )

            for resource in resources:
                if resource.id in self._loaded_resource_ids:
                    warnings.warn(
                        f"Resource with ID {resource.id} already loaded. Skipping."
                    )
                    continue
                self._resources.append(resource)
                self._loaded_resource_ids.add(resource.id)

    def load_presets(self, *presetss: Union[Preset, List[Preset]]) -> None:
        """
        Adds presets to the presets list, skipping duplicates based on UUID.
        """
        for presets in presetss:
            if not isinstance(presets, list):
                presets = [presets]

            if not all(isinstance(preset, Preset) for preset in presets):
                raise TypeError(
                    "All presets must be instances of the Preset class."
                )

            for preset in presets:
                if preset.id in self._loaded_preset_ids:
                    warnings.warn(
                        f"Preset with ID {preset.id} already loaded. Skipping."
                    )
                    continue
                self._presets.append(preset)
                self._loaded_preset_ids.add(preset.id)

    def _auto_load_resources_presets(self):
        """
        Loads all Resource/Preset nodes into the Project node they
        are attached to.
        """
        all_structures = self.grab_all_nodes_bfs("structure")
        all_marker_aligners = self.grab_all_nodes_bfs("marker_alignment")

        if self._auto_load_meshes:
            all_meshes = [
                structure.mesh
                for structure in all_structures
                if structure._mesh
            ]
            self.load_resources(all_meshes)

        if self._auto_load_presets:
            all_presets = [structure.preset for structure in all_structures]
            self.load_presets(all_presets)

        if self._auto_load_images:
            all_images = [
                marker_aligner.image for marker_aligner in all_marker_aligners
            ]
            self.load_resources(all_images)

    def _create_toml_data(
        self, presets: List[Any], resources: List[Any], nodes: List[Node]
    ) -> str:
        """
        Creates TOML data for the project.
        """
        data = {
            "presets": [preset._to_dict() for preset in presets],
            "resources": [resource._to_dict() for resource in resources],
            "nodes": [node._to_dict() for node in nodes],
        }
        return toml.dumps(data)

    def _create_project_info(self, project_info_json: Dict[str, Any]) -> str:
        """
        Creates JSON data for project info.
        """
        return json.dumps(project_info_json, indent=4)

    def _add_file_to_zip(
        self, zip_file: zipfile.ZipFile, file_path: str, arcname: str
    ):
        """
        Adds a file to a zip archive.
        """
        with open(file_path, "rb") as f:
            zip_file.writestr(arcname, f.read())

    def nano(self, project_name: str = "Project", path: str = "./") -> None:
        """
        Creates a .nano file for the project.
        """
        print("npxpy: Attempting to create .nano-file...")

        # Trigger user warning if project contains structures outside scenes
        for i_node in self.all_descendants:
            if i_node._type == "structure":
                if not "scene" in [i._type for i in self.all_ancestors]:
                    UserWarning("Structures have to be inside Scene nodes!")

        # Autoload presets/resources if desired
        if any(
            [
                self._auto_load_images,
                self._auto_load_meshes,
                self._auto_load_presets,
            ]
        ):
            self._auto_load_resources_presets()

        # Ensure the path ends with a slash
        if not path.endswith("/"):
            path += "/"

        # Prepare paths and data
        nano_file_path = os.path.join(path, f"{project_name}.nano")
        toml_data = self._create_toml_data(
            self._presets, self._resources, [self] + self.all_descendants
        )
        project_info_data = self._create_project_info(self.project_info)

        with zipfile.ZipFile(
            nano_file_path, "w", zipfile.ZIP_STORED
        ) as nano_zip:
            # Add the __main__.toml to the zip file
            nano_zip.writestr("__main__.toml", toml_data)

            # Add the project_info.json to the zip file
            nano_zip.writestr("project_info.json", project_info_data)

            # Add the resources to the zip file
            already_zipped_resources = set()
            for resource in self._resources:
                src_path = resource.file_path
                arcname = resource.safe_path
                if (
                    os.path.isfile(src_path)
                    and arcname not in already_zipped_resources
                ):
                    self._add_file_to_zip(nano_zip, src_path, arcname)
                    already_zipped_resources.add(arcname)
                elif not os.path.isfile(src_path):
                    print(f"File not found: {src_path}")
                else:
                    print(f"File already loaded: {src_path}")
        print("npxpy: .nano-file created successfully.")

    def _to_dict(self) -> Dict:
        """
        Convert the Project object into a dictionary.
        """
        node_dict = super()._to_dict()
        node_dict.update(
            {
                "objective": self.objective,
                "resin": self.resin,
                "substrate": self.substrate,
                "custom_origin": self._custom_origin,
                "origin": self._origin,
                "apply_curvature_compensation": self.apply_curvature_compensation,
                "stage_velocity": self.stage_velocity,
            }
        )
        return node_dict

presets property

Get the list of presets.

resources property

Get the list of resources.

__init__(objective, resin, substrate, auto_load_presets=False, auto_load_meshes=False, auto_load_images=False, apply_curvature_compensation=False, stage_velocity=1.0)

Initialize the project with the specified parameters.

Parameters:

Name Type Description Default
objective str

Objective of the project.

required
resin str

Resin used in the project.

required
substrate str

Substrate used in the project.

required
auto_load_presets bool

Whether or not to automatically load any attached presets.

False
auto_load_meshes bool

Whether or not to automatically load any attached meshes.

False
auto_load_images bool

Whether or not to automatically load any attached images.

False
apply_curvature_compensation bool

Whether to apply curvature compensation when printing.

False
stage_velocity float

Stage velocity in mm/s. Must be between 0.1 and 50.0 (inclusive). The value is rounded to the third decimal place.

1.0

Raises:

Type Description
ValueError

If any of the parameters have invalid values.

TypeError

If any of the parameters have invalid types.

Source code in npxpy/nodes/project.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def __init__(
    self,
    objective: str,
    resin: str,
    substrate: str,
    auto_load_presets: bool = False,
    auto_load_meshes: bool = False,
    auto_load_images: bool = False,
    apply_curvature_compensation: bool = False,
    stage_velocity: float = 1.0,
):
    """
    Initialize the project with the specified parameters.

    Parameters:
        objective (str): Objective of the project.
        resin (str): Resin used in the project.
        substrate (str): Substrate used in the project.
        auto_load_presets (bool): Whether or not to automatically load any attached presets.
        auto_load_meshes (bool): Whether or not to automatically load any attached meshes.
        auto_load_images (bool): Whether or not to automatically load any attached images.
        apply_curvature_compensation (bool): Whether to apply curvature compensation when printing.
        stage_velocity (float): Stage velocity in mm/s. Must be between
            0.1 and 50.0 (inclusive). The value is rounded to the third
            decimal place.

    Raises:
        ValueError: If any of the parameters have invalid values.
        TypeError: If any of the parameters have invalid types.
    """
    super().__init__(node_type="project", name="Project")

    self.objective = objective
    self.resin = resin
    self.substrate = substrate

    self._presets = []
    self._resources = []
    self.project_info = {
        "author": os.getlogin(),
        "objective": self.objective,
        "resist": self.resin,
        "substrate": self.substrate,
        "creation_date": datetime.now().replace(microsecond=0).isoformat(),
    }
    self._visibility_in_plotter_disabled = []
    self._loaded_resource_ids = set()
    self._loaded_preset_ids = set()
    self._auto_load_presets = auto_load_presets
    self._auto_load_meshes = auto_load_meshes
    self._auto_load_images = auto_load_images

    self.apply_curvature_compensation = apply_curvature_compensation
    self.stage_velocity = stage_velocity
    self._custom_origin = False
    self._origin = [0.0, 0.0]

load_presets(*presetss)

Adds presets to the presets list, skipping duplicates based on UUID.

Source code in npxpy/nodes/project.py
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
def load_presets(self, *presetss: Union[Preset, List[Preset]]) -> None:
    """
    Adds presets to the presets list, skipping duplicates based on UUID.
    """
    for presets in presetss:
        if not isinstance(presets, list):
            presets = [presets]

        if not all(isinstance(preset, Preset) for preset in presets):
            raise TypeError(
                "All presets must be instances of the Preset class."
            )

        for preset in presets:
            if preset.id in self._loaded_preset_ids:
                warnings.warn(
                    f"Preset with ID {preset.id} already loaded. Skipping."
                )
                continue
            self._presets.append(preset)
            self._loaded_preset_ids.add(preset.id)

load_resources(*resourcess)

Adds resources to the resources list, skipping duplicates based on UUID.

Source code in npxpy/nodes/project.py
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
def load_resources(
    self, *resourcess: Union[Resource, List[Resource]]
) -> None:
    """
    Adds resources to the resources list, skipping duplicates based on UUID.
    """
    for resources in resourcess:
        if not isinstance(resources, list):
            resources = [resources]

        if not all(
            isinstance(resource, Resource) for resource in resources
        ):
            raise TypeError(
                "All resources must be instances of the Resource class or its subclasses."
            )

        for resource in resources:
            if resource.id in self._loaded_resource_ids:
                warnings.warn(
                    f"Resource with ID {resource.id} already loaded. Skipping."
                )
                continue
            self._resources.append(resource)
            self._loaded_resource_ids.add(resource.id)

nano(project_name='Project', path='./')

Creates a .nano file for the project.

Source code in npxpy/nodes/project.py
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
def nano(self, project_name: str = "Project", path: str = "./") -> None:
    """
    Creates a .nano file for the project.
    """
    print("npxpy: Attempting to create .nano-file...")

    # Trigger user warning if project contains structures outside scenes
    for i_node in self.all_descendants:
        if i_node._type == "structure":
            if not "scene" in [i._type for i in self.all_ancestors]:
                UserWarning("Structures have to be inside Scene nodes!")

    # Autoload presets/resources if desired
    if any(
        [
            self._auto_load_images,
            self._auto_load_meshes,
            self._auto_load_presets,
        ]
    ):
        self._auto_load_resources_presets()

    # Ensure the path ends with a slash
    if not path.endswith("/"):
        path += "/"

    # Prepare paths and data
    nano_file_path = os.path.join(path, f"{project_name}.nano")
    toml_data = self._create_toml_data(
        self._presets, self._resources, [self] + self.all_descendants
    )
    project_info_data = self._create_project_info(self.project_info)

    with zipfile.ZipFile(
        nano_file_path, "w", zipfile.ZIP_STORED
    ) as nano_zip:
        # Add the __main__.toml to the zip file
        nano_zip.writestr("__main__.toml", toml_data)

        # Add the project_info.json to the zip file
        nano_zip.writestr("project_info.json", project_info_data)

        # Add the resources to the zip file
        already_zipped_resources = set()
        for resource in self._resources:
            src_path = resource.file_path
            arcname = resource.safe_path
            if (
                os.path.isfile(src_path)
                and arcname not in already_zipped_resources
            ):
                self._add_file_to_zip(nano_zip, src_path, arcname)
                already_zipped_resources.add(arcname)
            elif not os.path.isfile(src_path):
                print(f"File not found: {src_path}")
            else:
                print(f"File already loaded: {src_path}")
    print("npxpy: .nano-file created successfully.")

start_from_current_stage_position()

Start the print from the current stage position.

Disables any fixed origin previously set via :meth:start_from_fixed_stage_position.

Source code in npxpy/nodes/project.py
196
197
198
199
200
201
202
203
def start_from_current_stage_position(self) -> None:
    """
    Start the print from the current stage position.

    Disables any fixed origin previously set via
    :meth:`start_from_fixed_stage_position`.
    """
    self._custom_origin = False

start_from_fixed_stage_position(*, origin=(0.0, 0.0))

Start the print from a fixed stage position instead of the current stage position.

Parameters:

Name Type Description Default
origin Sequence[float]

The (x, y) stage coordinates to start from. Must be an iterable of at least length 2 containing numbers only. Values are rounded to the third decimal place.

(0.0, 0.0)

Raises:

Type Description
TypeError

If origin is not an iterable of at least length 2 containing numbers only.

Source code in npxpy/nodes/project.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
def start_from_fixed_stage_position(
    self, *, origin: Sequence[float] = (0.0, 0.0)
) -> None:
    """
    Start the print from a fixed stage position instead of the current
    stage position.

    Parameters:
        origin (Sequence[float]): The (x, y) stage coordinates to start
            from. Must be an iterable of at least length 2 containing
            numbers only. Values are rounded to the third decimal place.

    Raises:
        TypeError: If ``origin`` is not an iterable of at least length 2
            containing numbers only.
    """
    try:
        x, y = float(origin[0]), float(origin[1])
    except (TypeError, IndexError, ValueError, KeyError):
        raise TypeError(
            "Make sure that origin is an iterable of at least length 2 "
            "containing numbers only"
        )
    self._custom_origin = True
    self._origin = [round(x, 3), round(y, 3)]