From 89c6a39c2f92ee4716d42266aecc010f3273fcf3 Mon Sep 17 00:00:00 2001 From: Leni Aniva Date: Thu, 4 Jul 2024 10:02:58 -0700 Subject: [PATCH] feat: Name in target --- nhf/build.py | 37 ++++++++++++++++++++++--------- nhf/touhou/houjuu_nue/__init__.py | 1 + 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/nhf/build.py b/nhf/build.py index 7b2531d..4435508 100644 --- a/nhf/build.py +++ b/nhf/build.py @@ -42,14 +42,20 @@ class Target: self.kind = kind self.kwargs = kwargs def __str__(self): - return f"" + return f"" def __call__(self, obj, *args, **kwargs): """ Raw call function which passes arguments directly to `_method` """ return self._method(obj, *args, **kwargs) - def file_name(self, file_name): - return f"{file_name}.{self.kind.ext}" + + @property + def file_name(self): + """ + Output file name + """ + return f"{self.name}.{self.kind.ext}" + def write_to(self, obj, path: str): x = self._method(obj) if self.kind == TargetKind.STL: @@ -98,6 +104,8 @@ class Model: """ Base class for a parametric assembly """ + def __init__(self, name: str): + self.name = name @property def target_names(self) -> list[str]: @@ -107,19 +115,22 @@ class Model: return list(Target.methods(self).keys()) def check_all(self) -> int: + """ + Builds all targets but do not output them + """ total = 0 - for k, f in Target.methods(self).items(): - f(self) + for t in Target.methods(self).values(): + t(self) total += 1 return total def build_all(self, output_dir: Union[Path, str] = "build", verbose=1): """ - Build all targets in this model + Build all targets in this model and write the results to file """ output_dir = Path(output_dir) - for k, target in Target.methods(self).items(): - output_file = output_dir / target.file_name(k) + for t in Target.methods(self).values(): + output_file = output_dir / self.name / t.file_name if output_file.is_file(): if verbose >= 1: print(f"{Fore.GREEN}Skipping{Style.RESET_ALL} {output_file}") @@ -128,6 +139,10 @@ class Model: if verbose >= 1: print(f"{Fore.BLUE}Building{Style.RESET_ALL} {output_file}") - target.write_to(self, str(output_file)) - if verbose >= 1: - print(f"{Fore.GREEN}Built{Style.RESET_ALL} {output_file}") + + try: + t.write_to(self, str(output_file)) + if verbose >= 1: + print(f"{Fore.GREEN}Built{Style.RESET_ALL} {output_file}") + except Exception as e: + print(f"{Fore.RED}Failed to build{Style.RESET_ALL} {output_file}: {e}") diff --git a/nhf/touhou/houjuu_nue/__init__.py b/nhf/touhou/houjuu_nue/__init__.py index 8f5679e..b90eb8c 100644 --- a/nhf/touhou/houjuu_nue/__init__.py +++ b/nhf/touhou/houjuu_nue/__init__.py @@ -109,6 +109,7 @@ class Parameters(Model): )) def __post_init__(self): + super().__init__(name="houjuu-nue") assert self.wing_root_radius > self.hs_hirth_joint.radius,\ "Wing root must be large enough to accomodate joint"