Skip to content

Parameters

bdbox provides typed, named parameters support for your build123d models!

  • Declare parameters as class attributes like standard Python dataclasses
  • Specify optional parameter constraints with simple field helpers
  • Automatically created Command line interface with usage information for running models with different parameter values
  • When all parameters have default values, running python model.py just works!

Choose from one of two declaration styles by subclassing either Params or Model:

from bdbox import Float, Int, Params, Preset, show
from build123d import Box

class P(Params):
    width = Float(10.0, min=5, max=100)
    length = Float(10.0, min=5, max=100)
    thickness = Int(3, min=1, max=10)

    presets = (Preset("large", width=50.0, thickness=8),)

show(Box(P.width, P.length, P.thickness))
from bdbox import Float, Int, Model, Preset
from build123d import Box

class MyBox(Model):
    width = Float(10.0, min=5, max=100)
    length = Float(10.0, min=5, max=100)
    thickness = Int(3, min=1, max=10)

    presets = (Preset("large", width=50.0, thickness=8),)

    def build(self):
        return Box(self.width, self.length, self.thickness)

Automatic dataclasses

Subclasses of Params and Model automatically function as dataclasses with no @dataclass decorator!

Declare parameters as class attributes just like with dataclasses.

  • Params subclasses provide parameter values as direct class attributes (for example, P.width). With Params, construct your model after the class declaration to access parameter values. Geometry is collected either by show, or by scanning the script's global variables as a fallback. Only one Params subclass per script is permitted.

  • Model subclasses provide parameter values as instance attributes within class methods (for example, self.width). With Model, implement the build method to construct your model geometry. Execute your model with the run method. If there is only one Model subclass, run will be called automatically if not invoked explicitly. Multiple Model subclasses in one script are supported, but run must then be called manually on each.

Which style to choose

  • Model subclasses are recommended for reusable models. They can be imported and used in other code and subclassed for reuse.

  • Params is convenient for single model scripts or experimentation with minimal scaffolding.

Both model styles are dataclasses, support the same fields, and provide the same command line interface.

Use one style per script

Subclassing both Params and Model in the same script is not supported and produces an error.