Code Mode Overview

Create 3D models programmatically using TypeScript and the Manifold 3D library for precise, parametric designs.

Code Mode Overview

Code Mode allows you to create 3D models programmatically using TypeScript and the Manifold 3D library. This gives you precise control over geometry and enables parametric designs that can be easily modified.

Accessing Code Mode

There are two ways to enter Code Mode:

  1. Create a new code object: Select a primitive tool (Sphere, Torus, Cone) from the 3D toolbar, which creates a code-based object
  2. Edit an existing code object: Select a code object and click the "Toggle Code Mode" button in the context menu

The Code Editor

Code Mode opens a full-featured code editor based on Monaco (the same editor used in VS Code).

Editor Features

  • Syntax highlighting for TypeScript
  • Autocomplete for Manifold 3D methods and types
  • Error highlighting with inline diagnostics
  • Multi-file support with tabbed interface
  • Package management via package.json

Editor Layout

The code editor occupies the 2D panel area and includes:

  • File tabs: Switch between files in your project
  • Editor toolbar: Contains the run button and file management
  • Main editor area: Where you write your code
  • Error panel: Displays compilation and runtime errors

Creating 3D Models with Code

Basic Structure

Every code project needs to export a result variable containing the 3D geometry:

import { Manifold } from 'manifold-3d/manifoldCAD';

// Create a simple cube
const cube = Manifold.cube([10, 10, 10], true);

// Export the result to render it
export const result = cube;

Available Geometry Operations

The Manifold library provides methods for creating and combining 3D shapes:

Primitives:

  • Manifold.cube([x, y, z], center?) — Create a box
  • Manifold.cylinder(height, radiusLow, radiusHigh?) — Create a cylinder or cone
  • Manifold.sphere(radius, circularSegments?) — Create a sphere

2D Shapes (CrossSection):

import { CrossSection } from 'manifold-3d/manifoldCAD';

const circle = CrossSection.circle(10);
const shape3d = circle.extrude(5); // Extrude 2D to 3D

Boolean Operations:

  • shape.add(other) — Union of two shapes
  • shape.subtract(other) — Subtract one shape from another
  • shape.intersect(other) — Intersection of two shapes

Transformations:

  • shape.translate([x, y, z]) — Move the shape
  • shape.rotate([x, y, z]) — Rotate the shape (degrees)
  • shape.scale([x, y, z]) — Scale the shape

Example: Creating a Shape with a Hole

import { Manifold } from 'manifold-3d/manifoldCAD';

// Create a cube
const cube = Manifold.cube([50, 50, 50], true);

// Create a cylinder to subtract
const cylinder = Manifold.cylinder(25, 5);

// Subtract the cylinder from the cube
const difference = cube.subtract(cylinder);

export const result = difference;

Multi-File Projects

You can organize code across multiple files using the file tab bar.

Adding Files

  1. Click the "+" button in the file tabs
  2. Enter a filename (use .ts extension for TypeScript)
  3. The new file opens in the editor

package.json

Every project has a package.json file that defines:

  • name: Project name
  • main: Entry point file (default: main.ts)
  • dependencies: npm packages used in your code
{
  "name": "my-project",
  "main": "main.ts",
  "dependencies": {
    "manifold-3d": "^3.0.0"
  }
}

Importing Between Files

Use standard ES module imports:

// helper.ts
export function createRoundedBox(size: number) {
  // ... implementation
}

// main.ts
import { createRoundedBox } from './helper';

export const result = createRoundedBox(10);

Using npm Packages

You can import npm packages in your code. The editor will:

  1. Detect package imports
  2. Fetch type definitions for autocomplete
  3. Bundle the package when running your code

Supported Packages

Packages available include:

  • manifold-3d — The main 3D geometry library (always available)
  • Other npm packages (loaded on demand)

To add a package, either:

  • Import it directly in your code (auto-detected)
  • Add it to the dependencies in package.json

Running Your Code

The code runs automatically as you type (with a brief debounce delay). The 3D preview updates to show the result.

If auto-run is enabled:

  • Changes trigger a rebuild after you stop typing
  • The 3D panel shows the updated geometry
  • Errors display in the error panel

Error Handling

When errors occur:

  • TypeScript errors show as red underlines in the editor
  • Runtime errors appear in the error panel below the editor
  • The 3D preview retains the last successful result

Exiting Code Mode

To exit Code Mode:

  1. Click the "Exit Code Mode" button in the editor toolbar
  2. Your code object remains in the design with its generated geometry
  3. You return to the normal 2D/3D split view

Saving Code Objects

Code objects save as part of your design file. The saved data includes:

  • All source files
  • package.json configuration
  • Parameter values (if parametric)

When reopening a design, the code object recompiles automatically.

Parameters (Advanced)

Code objects can define parameters that appear in the parameter panel, allowing users to modify the design without editing code.

Parameters are defined using a specific format in your code and enable the "Maker Mode" interface for end users.

Tips

  • Use autocomplete: Type Manifold. and wait for suggestions
  • Check the error panel: Runtime errors will help you debug
  • Start simple: Begin with primitives and build up complexity
  • Use comments: Document your code for future reference
  • Save frequently: Complex code objects benefit from regular saves