Scripting fundamentals
Graphic primitives
All of the primitive shapes that can be exported in Cuttle are called Graphics. There are four Graphic classes:
- An
Anchorrepresents an anchor point of aPath. It has a.position,.handleIn, and.handleOutproperty (allVecs). - A
Pathrepresents an arbitrary closed or open path. It has an.anchorsproperty which is an array ofAnchors and a.closedproperty which istrueorfalse. It also has Style properties.filland.stroke. - A
CompoundPathrepresents a collection of paths. These are used to create shapes with holes. You can also use these to “bundle” together multiple paths which can be useful in certain export situations (e.g. to force certain machines to trace those paths in order). ACompoundPathhas a.pathsproperty. It also has Style properties.filland.stroke. - A
Groupis an organizational unit. It has an.itemsproperty which is an array of otherGraphics.
Constructors
Here’s an example showing how to create each primitive. You can try pasting this code as a new Code Component.
Note that typing new before the constructors is optional.
The code above also demonstrates Path.fromPoints(...) to construct a Path of straight segments. You can find more convenience methods for creating Anchors, Paths, CompoundPaths, and Groups in the reference documentation.
Navigating the scene hierarchy
Graphics form a tree and that tree can be arbitrarily nested — Groups can contain other Groups. When writing modifiers, it’s often handy to just work with all the Anchors or all the Paths. Thus there are convenience methods that will traverse through the tree of a Graphic and return all the objects you’re looking for.
.allAnchors()returns an array of all theAnchorobjects..allPaths()returns an array of all thePathobjects..allCompoundPaths()returns an array of all theCompoundPathobjects..allPathsAndCompoundPaths()returns an array of allCompoundPaths and all top-levelPaths (meaning thePathis not contained within aCompoundPath). This is all the objects that can be styled.
Transforming Graphics
All of the Graphics have a .transform(...) method which is analogous to the Transform modifier in Cuttle.
To perform the above transform you could write:
That is, the transform method takes an object with the following properties, all optional:
position: aVecrotation: anumberin degreesscale: aVecfor non-uniform scale ornumberfor uniform scaleskew: anumberin degreesorigin: aVecfor where the center of rotation and scale should beGroups,Paths, andCompoundPaths do not have transformations.Note that unlike some other graphics frameworks that have a “scene graph” — a hierarchy of shapes — parent shapes in Cuttle do not have transformations. When you call
.transform(...)on a parent shape, it will mutate the positions and handles of theAnchors (the leaf nodes of the tree).
Mutating methods and .clone()
You can make a copy of any Graphic object with the .clone() method. This will return a deep copy of the object. “Deep” means that children will also be copied recursively — for example if you .clone() a Path then all the child Anchors will also be cloned.
Note that most of the methods (e.g. .transform(...)) that you can call on a Graphic will mutate the object or its children. So if you don’t want to mutate your Graphic, you’ll want to first copy it with .clone().
For example, here is a simple modifier that returns the original input along with a copy of the input moved one unit to the right.
The return value
All modifiers and code components need to return a Graphic. As a convenience, you can also return an array of Graphics which Cuttle will turn into a Group.