PolyScript Language Reference
Overview
PolyScript is a pipe-based language for parametric CAD modeling. Shapes are created with primitives and transformed through a chain of pipe operations.
box 80 60 10 | fillet 2 | diff cylinder 10 10
Primitives
3D
box width height depth
cylinder height radius
sphere radius
2D
rect width height
circle radius
ellipse rx ry
polygon points
text content size
2D primitives produce wire profiles. Use | extrude, | cut, or | revolve to create solids.
Paths
line start end
arc through end
bezier points
helix pitch height radius
Pipe Operations
Operations are chained with |:
box 50 50 10 | fillet 2 | diff cylinder 10 5
Modifiers
| fillet expr # fillet all edges
| chamfer expr # chamfer all edges
| shell expr open:sel # hollow out; optionally remove a face
box 100 60 40
| edges =Z | fillet 3
| shell 2 open:top
Color
形状に色を付けます。色はエクスポート時(glTF/STEP)にパーツ単位で反映されます。
| color "red" # 名前付き色
| color "#FF0000" # HEXカラー(文字列リテラル)
| color "#F00" # HEX短縮形
| color 0.8 0.2 0.1 # RGB float (0..1)
| color 255 128 0 # RGB int (0..255、自動正規化)
| color "red" alpha:0.5 # 透明度付き
色の指定方法は3種類です:
- 名前付き色:
"red","blue","steel"など - HEXカラー:
"#FF0000"または短縮形"#F00"(#はコメント記号と衝突するため、必ず文字列リテラルとして書きます) - RGB値: 3つの数値を並べます。全て1以下なら 0..1 範囲、いずれかが1を超えれば 0..255 範囲として自動的に正規化されます
alpha: キーワード引数で透明度を指定できます(0..1、デフォルト 1.0)。
パーツごとに色を分けることもできます:
union [
box 10 10 10 | color "red"
cylinder 3 15 | color "blue"
]
名前付き色パレット
3段階の色パレットを使えます:
| 階層 | 色名 |
|---|---|
| 基本色(16色) | red, green, blue, yellow, cyan, magenta, orange, purple, white, black, gray/grey, brown, pink, lime, navy, teal |
| CAD素材色 | silver, gold, steel, copper, brass, aluminum, darkgray/darkgrey, lightgray/lightgrey |
| CSS Named Colors | steelblue, coral, darkslategray など、CSS準拠の色名すべて |
未知の色名を指定するとエラーになります。
Boolean
| diff shape # subtract shape
| union shape # add shape
| inter shape # intersect with shape
Shapes can be placed with at:
box 50 50 10
| diff cylinder 10 3 at 15 15
Extrude / Revolve / Sweep
| extrude height draft:angle
| revolve degrees axis:"X"
| sweep path
rect 60 40 | extrude 15
circle 20 | revolve 360
Cut / Hole
| cut depth # cut through or to depth
| hole radius depth:d # drill hole (radius, not diameter)
rect 60 40 | extrude 15
| faces top
| rect 40 20 | cut 10
Face / Edge / Vertex Selection
| faces selector # select faces
| edges selector # select edges
| verts selector # select vertices
Selectors use short symbols or name aliases:
| Symbol | Meaning | Name alias |
|---|---|---|
>Z |
maximum (top) | top |
<Z |
minimum (bottom) | bottom |
>X |
maximum (right) | right |
<X |
minimum (left) | left |
>Y |
maximum (front) | front |
<Y |
minimum (back) | back |
=Z |
parallel to Z | |
=X |
parallel to X | |
=Y |
parallel to Y | |
+Z |
perpendicular to Z | |
+X |
perpendicular to X | |
+Y |
perpendicular to Y |
Face selection creates an implicit workplane, so you can pipe 2D primitives directly:
box 50 50 10
| faces top
| circle 5 | cut
| edges <Z | fillet 1
In a 2D context, verts returns the vertices of the current shape as a Vertex selection:
box 80 60 10
| faces top
| rect 70 50 | verts | circle 1 | cut
Workplane / Points
Face selection implicitly creates a workplane for 2D operations. Use explicit workplane only when you need a specific orientation:
| workplane "XZ" # explicit workplane with axis
| points (polar 6 20) # arrange points in circle
| points (grid nx ny 20) # arrange points in grid (3rd arg = pitch)
cylinder 5 30
| faces top
| points (polar 4 10)
| hole 3
Transform
| translate x y z
| rotate rx ry rz
| move dx dy # 2D offset on workplane
| moveto x y # 2D absolute position
Variables
変数名には $ プレフィックスが必須です。
$w = 80
$h = 60
box $w $h 10
@param アノテーション
変数に @param を付けると、GUIカスタマイザーでスライダーやドロップダウンとして操作できるパラメーターになります。変数宣言の直前の行に書きます。
@param 10..200 step:5 desc:"Box width"
$width = 80
box $width 60 10
これだけで、GUIには 10 から 200 まで 5 刻みのスライダーが表示されます。
基本構文
@param オプション...
$変数名 = デフォルト値
範囲ショートハンド
よく使う min/max/step パターンを短く書けます:
| 記法 | 意味 |
|---|---|
@param 1..100 |
min:1 max:100 |
@param 1..100..0.5 |
min:1 max:100 step:0.5 |
@param -10..10..1 |
min:-10 max:10 step:1 |
ショートハンドの後にオプションを追加することもできます:
@param 1..100 desc:"Height"
オプション一覧
| キー | 型 | 説明 |
|---|---|---|
min |
数値 | 最小値(スライダー下限) |
max |
数値 | 最大値(スライダー上限) |
step |
数値 | ステップ幅(スライダーの刻み) |
desc |
文字列 | 説明テキスト(ツールチップ/ラベル) |
choices |
リスト | 選択肢リスト(ドロップダウン) |
group |
文字列 | GUIのグループ名(デフォルト: "General") |
type |
文字列 | 型ヒント: "int", "float", "string", "bool" |
hidden |
真偽値 | true でGUIに非表示 |
type を省略すると、デフォルト値から自動推論されます(42 なら int、2.5 なら float、"PLA" なら string、true なら bool)。
選択肢(ドロップダウン)
choices を使うと、ドロップダウンメニューとして表示されます:
@param choices:["M3", "M4", "M5", "M6"] desc:"Bolt size"
$bolt = "M4"
実用的な例
@param 40..200 step:5 group:"Dimensions" desc:"Case width"
$case_w = 100
@param 30..150 step:5 group:"Dimensions" desc:"Case depth"
$case_d = 60
@param 1..5 step:0.5 group:"Wall" desc:"Wall thickness"
$wall = 2
@param type:"bool" group:"Features" desc:"Add ventilation holes"
$vents = true
box $case_w $case_d 40
| color "steel"
| shell $wall open:>Z
パラメーターセット(JSON)
PolyScript ファイル model.ps に対して model.ps.params.json を同じディレクトリに置くと、パラメーターのメタ情報やプリセットを外部から定義できます:
{
"params": {
"width": { "min": 10, "max": 200, "step": 5 }
},
"parameterSets": {
"Default": {},
"Small": { "width": 30, "height": 20 },
"Large": { "width": 150, "height": 80 }
}
}
JSON側の設定はソース内の @param より優先されます。ただし、ソースに変数宣言がないパラメーターはJSON側に記述があっても無視されます。
Functions
Single-expression functions with def:
def standoff($r, $h, $hole_r) = cylinder $h $r | diff cylinder $h $hole_r
box 80 60 3
| union standoff 4 10 1.5 at 10 10
Import
Load function definitions from another .poly file:
import "gear"
spur_gear 12 2 | extrude 8
Imports are resolved relative to the importing file's directory.
Expressions
Arithmetic
+ - * / // % **
Comparison and Logic
== != < > <= >=
and or
Conditional
if condition then expr else expr
List Comprehension
[$expr for $var in range($n)]
Math Functions
sin, cos, tan, asin, acos, atan, atan2, sqrt, radians (alias: rad), degrees (alias: deg), floor, ceil
Constant: pi
Tuples and Lists
(10, 20, 30) # tuple
[(0,0), (10,5), (20,0)] # list
Groups
Square brackets create a group of shapes (boolean union):
[rect 40 10, rect 10 40] | extrude 5
Placement
at places a shape at a position or pattern.
Parenthesis rules:
- Simple coordinates (literals, variables, arithmetic): parentheses can be omitted.
- Coordinate lists: brackets are required.
- Parenthesized form
at (x, y)is always valid (backward compatible). - For array placement, use
| gridor| polarpipe operations instead.
sphere 5 at 20 0 0 # simple position (no parens needed)
sphere 5 at $x $y # variables work too
sphere 5 at [(0,0), (10,0), (20,0)] # explicit positions (brackets required)
sphere 5 | polar 6 20 # circular array (pipe operation)
sphere 5 | grid 3 3 20 # rectangular array (pipe operation)
Comments
# This is a comment