Skip to main content

Simplified 2D/3D modeling for Python with fluent API and boolean operations

Project description

ScadPy

PyPI CI coverage doc coverage

Programmatic CAD in Pure Python.Documentation

ScadPy provides a fluent, type-safe API for 2D and 3D parametric modeling, built on Shapely and trimesh. Write designs with the conciseness of OpenSCAD and the full power of Python.

Installation

pip install scadpy

Requirements: Python ≥ 3.12.

Quick examples

# 2D — chamfered mounting plate
from scadpy import *
import numpy as np

PLATE_WIDTH  = 80
PLATE_HEIGHT = 50
HOLE_RADIUS  = 4
HOLE_MARGIN  = 10
CHAMFER_SIZE = 8

base               = rectangle([PLATE_WIDTH, PLATE_HEIGHT])
plate              = base.chamfer(CHAMFER_SIZE)
corner_coordinates = base.vertex_coordinates[base.corner_to_vertex[:, 1]]

for position, normal in zip(corner_coordinates, base.corner_normals):
    hole_center = position - HOLE_MARGIN * np.sqrt(2) * normal
    plate -= circle(HOLE_RADIUS).translate(hole_center)
    
plate.to_screen()
chamfered mounting plate
# 3D — parametric ball bearing
import numpy as np
from scadpy import *

BALL_RADIUS    = 3
RACE_RADIUS    = 15
NB_BALLS       = 11
CLEARANCE      = 0.1
RING_HEIGHT    = 7
RACE_THICKNESS = 10

groove  = circle(BALL_RADIUS + CLEARANCE) | rectangle([BALL_RADIUS, RING_HEIGHT])
race    = rectangle([RACE_THICKNESS, RING_HEIGHT]) - groove
bearing = race.radial_extrude(axis=y(1), pivot=x(RACE_RADIUS))

for angle in np.linspace(0, 360, NB_BALLS, endpoint=False):
    bearing += sphere(BALL_RADIUS).rotate(angle, axis=y(1), pivot=x(RACE_RADIUS))

bearing.to_screen()
parametric ball bearing

Cheat sheet

Parameters shown in # comments are optional, with their default values.

2D — Shape

from scadpy import *

# primitives
circle(radius=3)                                # segment_count=64
polygon(points=[(-2, -2), (2, -2), (0, 2)])
rectangle(size=[6, 3])
Shape.from_dxf("file.dxf")
Shape.from_svg("file.svg")
square(size=4)

# boolean operations
s = square(size=4);  c = circle(radius=3)
s | c    # union
s - c    # difference
s & c    # intersection
s ^ c    # symmetric difference
s + c    # concat (no merge)

# transforms
s.chamfer(size=0.8)              # corner_filter=None, epsilon=1e-8
s.color(color=RED)
s.convexify()                    # part_filter=None
s.fill()                         # part_filter=None
s.fillet(size=0.8)               # corner_filter=None, segment_count=32, epsilon=1e-8
s.grow(distance=0.5)             # part_filter=None
s.linear_cut(axis=x(1))          # pivot=0
s.linear_slice(thickness=2, direction=x(1))  # pivot=0, part_filter=None
s.mirror(normal=[1, 0])          # pivot=0
s.pull(distance=1.0)             # pivot=0, vertex_filter=None
s.push(distance=1.0)             # pivot=0, vertex_filter=None
s.radial_slice(start=0, end=180) # pivot=0, part_filter=None
s.resize(size=[6, None])         # auto=False, pivot=None, vertex_filter=None
s.rotate(angle=30)               # pivot=0, vertex_filter=None
s.scale(scale=[2, 0.5])          # pivot=0, vertex_filter=None
s.shrink(distance=0.5)           # part_filter=None
s.translate(translation=[2, 1])  # vertex_filter=None

# topology — coordinates & attributes
s.are_corners_convex             # (n_corners,)    — convexity mask
s.corner_angles                  # (n_corners,)    — interior angles (°)
s.corner_normals                 # (n_corners,  2) — outward unit normals
s.directed_edge_directions       # (2*n_edges, 2)
s.edge_lengths                   # (n_edges,)
s.edge_midpoints                 # (n_edges,  2)
s.edge_normals                   # (n_edges,  2)
s.ring_types                     # (n_rings,)  — "exterior"|"interior"
s.vertex_coordinates             # (n_vertices, 2)

# topology — bridges (*_to_*)
s.corner_to_incoming_directed_edge  # corner        → directed_edge
s.corner_to_outgoing_directed_edge  # corner        → directed_edge
s.corner_to_vertex                  # corner        → [prev, curr, next]
s.directed_edge_to_corner           # directed_edge → [source, target]
s.directed_edge_to_edge             # directed_edge → edge
s.directed_edge_to_vertex           # directed_edge → [start, end]
s.edge_to_vertex                    # edge          → [start, end]
s.ring_to_part                      # ring          → part
s.vertex_to_part                    # vertex        → part
s.vertex_to_ring                    # vertex        → ring

# extrusions → Solid
s.linear_extrude(height=3)
s.radial_extrude(axis=y(1), pivot=x(5))  # start=0, end=360, segment_count=64

# export
s.to_dxf_file("output.dxf")
s.to_html_file("output.html")
s.to_screen()
s.to_svg_file("output.svg")

3D — Solid

from scadpy import *

# primitives
cone(radius=2, height=4)         # section_count=32
cuboid(size=[4, 3, 2])
cylinder(radius=2, height=4)     # section_count=32
polyhedron(vertices=vertices, faces=faces)
sphere(radius=3)                 # subdivision_count=4
Solid.from_stl("model.stl")

# boolean operations
a = cuboid(size=[4, 3, 2]);  b = sphere(radius=2)
a | b    # union
a - b    # difference
a & b    # intersection
a ^ b    # symmetric difference
a + b    # concat (no merge)

# transforms
a.color(color=RED)
a.convexify()                    # part_filter=None
a.mirror(normal=[1, 0, 0])       # pivot=0
a.pull(distance=1.0)             # pivot=0, vertex_filter=None
a.push(distance=1.0)             # pivot=0, vertex_filter=None
a.resize(size=[6, None, None])   # auto=False, pivot=None, vertex_filter=None
a.rotate(angle=30, axis=z(1))    # pivot=0, vertex_filter=None
a.scale(scale=[2, 1, 0.5])       # pivot=0, vertex_filter=None
a.translate(translation=[1, 0, 0])  # vertex_filter=None

# topology — coordinates & bridges (*_to_*)
a.triangle_to_vertex    # triangle → [v0, v1, v2]
a.vertex_coordinates    # (n_vertices,  3)
a.vertex_to_part        # vertex   → part

# export
a.to_html_file("output.html")
a.to_screen()
a.to_stl_file("output.stl")

Roadmap

  • Improve documentation
  • Richer topology for Shape and Solid
  • Richer transformations for Shape and Solid
  • Chamfer and fillet on Solid
  • New assembly types: PointCloud2d, Wire2d, PointCloud3d, Wire3d
  • Better error messages
  • More import/export formats

Development

# Create and activate venv
python3 -m venv .venv
source .venv/bin/activate

# Install with dev dependencies
pip install -e .[dev]

# Run doctests & generate documentation
cd docs && make doctest && make html

License

See LICENSE.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scadpy-0.2.3.tar.gz (233.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scadpy-0.2.3-py3-none-any.whl (188.2 kB view details)

Uploaded Python 3

File details

Details for the file scadpy-0.2.3.tar.gz.

File metadata

  • Download URL: scadpy-0.2.3.tar.gz
  • Upload date:
  • Size: 233.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for scadpy-0.2.3.tar.gz
Algorithm Hash digest
SHA256 b7067ad2c05330651f70eec805ee52e63da2935d52d42bc19b92c0089e309b32
MD5 e320458bb8b5a81882794e2ff1be055d
BLAKE2b-256 c0d28c32466f6f8c538aeb3c07dbf86d30fc772e7032c99cf64462ebae22fe6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for scadpy-0.2.3.tar.gz:

Publisher: release.yml on m-fabregue/scadpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file scadpy-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: scadpy-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 188.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for scadpy-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a14aacad5cee22c71ec78f54cd1cec9e7f2367769cf45acf90642b73ffc8b15e
MD5 bd3f073d341af6eb313f5a9d5c53e198
BLAKE2b-256 6979349e00e168ec82156bcfb4bb42efc0706ec843833d452e99459910406ef4

See more details on using hashes here.

Provenance

The following attestation bundles were made for scadpy-0.2.3-py3-none-any.whl:

Publisher: release.yml on m-fabregue/scadpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page