Skip to main content

Tessellate OCP (https://github.com/cadquery/OCP) objects to use with threejs

Project description

OCP-Tessellate

Tessellate OCP (Python bindings for OpenCascade) shapes into a JSON-friendly format suitable for three-cad-viewer and downstream tooling such as ocp_vscode.

It accepts geometry from CadQuery, build123d, and raw OCP TopoDS_* shapes, walks the structure, computes triangulations / discretized edges, and emits a hierarchical group of meshed instances.

Installation

pip install ocp-tessellate

Requires Python 3.10+ and OCP 7.8 or 7.9.

Quick start

Programmatic pipeline

For embedding in a tool (the path used by ocp_vscode):

from build123d import Box, Sphere, Compound
from ocp_tessellate.convert import to_ocpgroup, tessellate_group

box = Box(1, 2, 3)
sphere = Sphere(0.5)
sphere.label = "ball"
assembly = Compound(label="root", children=[box, sphere])

# Step 1: convert to an OcpGroup hierarchy + instance list
group, instances = to_ocpgroup(assembly)

# Step 2: tessellate the instances
meshed_instances, shapes, mapping = tessellate_group(group, instances)
# `shapes` is a JSON-serializable tree consumable by three-cad-viewer

Direct export to three-cad-viewer

The simplest path — emit a .js file that a three-cad-viewer page can load directly:

from build123d import Box
from ocp_tessellate.convert import export_three_cad_viewer_js

box = Box(1, 2, 3)
export_three_cad_viewer_js("box", box, filename="box.js")
# writes box.js with `var box = {...}` ready for three-cad-viewer/examples

This is the bridge to three-cad-viewer: bundles tessellated instances + scene tree into a single JSON payload assigned to a JS variable.

Public API

Three entry points in ocp_tessellate.convert:

  • export_three_cad_viewer_js(var, *objs, names=None, colors=None, alphas=None, modes=None, filename=None, keep_instances=False) — runs the full pipeline (to_ocpgrouptessellate_group → JSON encode) and writes a var <name> = {...}; JS file. The direct integration point with three-cad-viewer.
  • to_ocpgroup(*objs, names=None, colors=None, alphas=None, modes=None, ...) — converts CAD objects (build123d / CadQuery / OCP / nested dicts and lists) into an OcpGroup hierarchy plus an instance list. Accepts per-object names, colors, alphas, materials, modes ((state_faces, state_edges) 2-tuples), and a default_color. Renderer-control flags include helper_scale, render_joints, render_mates, show_parent, show_sketch_local.
  • tessellate_group(group, instances, kwargs=None, progress=None, timeit=False) — meshes the instances and returns the structures consumed by three-cad-viewer. kwargs can carry tessellation parameters like deviation, angular_tolerance, edge_accuracy, and render_normals.

For lower-level use, OcpConverter exposes to_ocp(...), which is what to_ocpgroup calls internally.

What gets shown

Inputs are walked recursively. The resulting tree mirrors the input structure:

input result
build123d / CadQuery / OCP shape a single OcpObject
dict a Dict group with one named child per key
list / tuple a List group with default-named children
ShapeList a ShapeList group exposing each item individually
Compound(children=[...]) (assembly) a group preserving the assembly hierarchy
Compound(...) (standard, no children) unwrapped to its inner shape
BuildPart / BuildSketch / BuildLine a single OcpObject for the builder result; BuildSketch adds a sketch_local sibling unless disabled

Compatibility

  • Python 3.10+
  • OCP 7.8 and 7.9
  • build123d (current)
  • CadQuery 2.x

License

Apache 2.0. See LICENSE.

Changelog

See CHANGELOG.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

ocp_tessellate-3.3.0.tar.gz (74.0 kB view details)

Uploaded Source

Built Distribution

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

ocp_tessellate-3.3.0-py3-none-any.whl (84.1 kB view details)

Uploaded Python 3

File details

Details for the file ocp_tessellate-3.3.0.tar.gz.

File metadata

  • Download URL: ocp_tessellate-3.3.0.tar.gz
  • Upload date:
  • Size: 74.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for ocp_tessellate-3.3.0.tar.gz
Algorithm Hash digest
SHA256 f37010328f5fa821d6d3010610d70dd205eb26020ae422ccd4405a8e1aca8ead
MD5 05d1fe75083296d000a3875db279127b
BLAKE2b-256 5a9e29d6c937a4825801e3d47ffe616d3bbb18b4bd70541a2accbd4b5771f523

See more details on using hashes here.

File details

Details for the file ocp_tessellate-3.3.0-py3-none-any.whl.

File metadata

  • Download URL: ocp_tessellate-3.3.0-py3-none-any.whl
  • Upload date:
  • Size: 84.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for ocp_tessellate-3.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3de3c66b7c623ae80e8a2c84eb1c6657e7683ab72a66f1ac75f7b639aaea6352
MD5 d03194dede0b4b70ba108b900407dd6a
BLAKE2b-256 9c1331f4fd2f95c850d0c7d09e82379ec7b60d40662b119777c5c24296019f6a

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