Skip to main content

Preview Build123d projects in the browser with ocp_vscode.

Project description

codecov

About

This package provides a fast edit-preview workflow for build123d CAD projects, especially when working in editors like Emacs.

The ocp123d command starts a single long-lived ocp_vscode viewer server, watches your project directory recursively, and re-runs your entry script whenever Python files change.

Key behavior:

  • starts the viewer server once and keeps it running
  • watches the entire project recursively
  • reloads when indirectly imported Python modules change
  • avoids stale imports by running each reload in a fresh child process

Table of Contents

Installation

Install this tool into the same virtual environment as your CAD project:

pip install build123d-ocp-preview

For local development from this repository:

pip install -e /path/to/build123d-ocp-preview

build123d is not a required dependency of this watcher because your CAD project environment should normally provide it. For a fresh demo environment, install the optional extra:

python -m pip install "build123d-ocp-preview[build123d]"

Quick Start

Given a project like:

/Users/me/my-build123-project/
  assembly.py
  my_cad/
    scratch.py

where assembly.py calls ocp_vscode.show(...), run:

cd /Users/me/my-build123-project
source .venv/bin/activate
ocp123d -p . assembly.py

Open the viewer URL printed by ocp_vscode, usually:

http://127.0.0.1:3939

By default, ocp123d also opens this viewer page in your browser before the initial preview run. This gives ocp_vscode time to register the browser client, so the first model is loaded immediately instead of leaving the splash preview on screen.

Now saving assembly.py or any project Python module such as picar_cad/scratch.py triggers a debounced re-run of assembly.py.

CLI

ocp123d [options] ENTRY [ENTRY ...]

Options:

-p, --project PATH        Project directory to watch. Defaults to cwd.
-o, --port PORT           ocp_vscode port. Defaults to 3939.
-d, --debounce-ms N       Debounce window in milliseconds. Defaults to 250.
-i, --ignore PATH         Project-relative Python file path to ignore. Repeatable.
-c, --config PATH         TOML config file.
-n, --no-initial-run      Start watching without running entries immediately.
-b, --no-open             Do not open the browser viewer before the initial run.
-h, --help                Show help.

Examples:

ocp123d -p . assembly.py
ocp123d -p . -o 3940 -d 500 assembly.py
ocp123d -p . -i picar_cad/temp.py assembly.py
ocp123d -p . --no-open assembly.py
ocp123d -p . assembly.py second_preview.py

Config File

The optional config file is TOML. Currently it supports exact ignored file paths:

ignore = [
  "picar_cad/temp.py",
  "picar_cad/generated.py",
]

Use it with:

ocp123d -p . -c ocp123d.toml assembly.py

CLI ignores and config ignores are merged:

ocp123d -p . -c ocp123d.toml -i picar_cad/scratch_experiment.py assembly.py

Ignore paths are resolved relative to the project directory unless absolute. Wildcard and glob matching are not implemented yet.

Entry Scripts

Your entry script should be a normal Python script that sends geometry to ocp_vscode, for example:

from build123d import Box
from ocp_vscode import show

show(Box(10, 20, 30), names=["box"])

ocp123d sets the viewer port before running the entry script. It also prepends the project directory to PYTHONPATH, so project-local imports work when the command is run from elsewhere.

What Gets Watched

The watcher observes .py files under the project directory recursively.

It always ignores common noisy directories:

  • .git
  • .venv
  • venv
  • __pycache__
  • .mypy_cache
  • .pytest_cache
  • .ruff_cache
  • node_modules

How Reloading Works

On each reload, ocp123d launches a fresh child process for each entry file. This is intentionally simple and reliable:

entry.py imports A
A imports B
B imports C

If C.py changes, the watcher sees the file change and re-runs entry.py in a new process. Python imports the whole graph from disk again, so stale imported objects from a previous run do not survive.

The tradeoff is that heavy CAD scripts pay their normal script execution cost on each reload. The viewer server itself stays running.

Current Limitations

  • No persistent IPython/Jupyter console.
  • No partial in-process module reload.
  • Ignore rules are exact paths only, not globs.
  • Only Python source changes trigger reloads.

Development

python -m venv .venv
source .venv/bin/activate
python -m pip install -e ".[dev]"
pytest -q
ruff check .
pyright

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

build123d_ocp_preview-0.1.0.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

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

build123d_ocp_preview-0.1.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file build123d_ocp_preview-0.1.0.tar.gz.

File metadata

  • Download URL: build123d_ocp_preview-0.1.0.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for build123d_ocp_preview-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3edd94b0393f1d4758449da8081259fed5d7f9b03fafc77515efa62c040d0d38
MD5 a7d915b5899d988d5452f26d6bfd7015
BLAKE2b-256 169108bc50da454efc809de953984a7acb68ced2bb5565bbeadb94c4f46723b2

See more details on using hashes here.

File details

Details for the file build123d_ocp_preview-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for build123d_ocp_preview-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c91978d6dd08da0b98a1cc8391202d01158944b5cb053dbbce15dedd43c5f64c
MD5 afb293880147ea6f25fc0f81b5d99a49
BLAKE2b-256 17710ea9b5e453426050a1a66dfcadec12de727485309a58c1b2f14cb74a46ed

See more details on using hashes here.

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