Skip to content
Overview

First Steps

The COMPAS Hello World.

These snippets run in any Python session that has COMPAS installed. Save them in a file and run it with python, or uv run if you installed with uv.

Points and vectors

from compas.geometry import Point, Vector

a = Point(0, 0, 0)
b = Point(1, 2, 3)
v = b - a

print(v)           # Vector(x=1.0, y=2.0, z=3.0)
print(v.length)    # 3.7416...
print(a + v == b)  # True

Subtracting two points gives a vector, and adding a vector to a point gives a point.

Frames

from compas.geometry import Frame

frame = Frame([1, 0, 0], [1, 0, 0], [0, 1, 0])

print(frame.point)   # Point(x=1.0, y=0.0, z=0.0)
print(frame.zaxis)   # Vector(x=0.0, y=0.0, z=1.0)

A frame is a local coordinate system: an origin and two axes. COMPAS uses frames to place geometry.

A box

import math
from compas.geometry import Box, Frame, Rotation, Translation

box = Box(1, 2, 3, frame=Frame.worldXY())

T = Translation.from_vector([5, 0, 0])
R = Rotation.from_axis_and_angle([0, 0, 1], math.radians(45))
box.transform(T * R)

print(box.frame.point)  # Point(x=5.0, y=0.0, z=0.0)
print(box.volume)       # 6.0

Transformations are 4x4 matrices. Combine them with *; the right-hand one applies first.

From shape to mesh

mesh = box.to_mesh()

print(mesh.number_of_vertices())  # 8
print(mesh.number_of_faces())     # 6

Shapes convert to meshes, which are what CAD tools and viewers display.

Save and load

import compas

compas.json_dump(box, "box.json")
same_box = compas.json_load("box.json")

print(same_box.volume)  # 6.0

Every COMPAS object serializes to JSON and comes back as the same type. This is how data moves between Python, Rhino, Grasshopper and Blender.