Skip to content

Shape construction

Scott Pakin edited this page Jan 21, 2024 · 10 revisions

All of the following functions return a Simple Inkscape Scripting object, which can be passed to the connector and group functions.

Circles and ellipses

Function: circle((cx, cy), radius)

Draw a circle with center (cx, cy) and radius radius.

Example:

circle((canvas.width/2, canvas.height/2), 50)

circle

Function: ellipse((cx. cy), (rx, ry))

Draw an ellipse with center (cx, cy) and radii rx and ry.

Example:

ellipse((canvas.width/2, canvas.height/2), (75, 50))

ellipse

Rectangles

Function: rect((x1, y1), (x2, y2), round)

Draw a rectangle from (x1, y1) to (x2, y2). If round is provided, produce a rounded rectangle. round can be either a tuple, specifying separate x and y radii, or a number, specifying a single radius to use in both the x and y directions.

Example:

rect((canvas.width/2 - 50, canvas.height/2 - 30), (canvas.width/2 + 50, canvas.height/2 + 30))

rect

Lines

Function: line((x1, y1), (x2, y2))

Draw a line from (x1, y1) to (x2, y2).

Example:

line((canvas.width, 0), (0, canvas.height))

line

Function: polyline([(x1, y1), (x2, y2), …, (xn, yn)])

Draw a polyline (open polygon) from the given coordinates.

Example:

polyline([(0, 300), (150, 0), (300, 300), (150, 200)])

polyline

Polygons

Function: polygon([(x1, y1), (x2, y2), …, (xn, yn)])

Draw an arbitrary polygon from the given coordinates.

Example:

polygon([(0, 300), (150, 0), (300, 300), (150, 200)])

polygon

Function: regular_polygon(sides, (cx, cy), radius, angle, round, random)

Draw a sides-sided regular polygon centered at (cx, cy) with radius radius. All of the remaining arguments are optional. angle is the initial angle in radians and default to −𝜋/2 (upwards). round specifies how much to round the corners and defaults to 0.0 (sharp). random adds an amount of randomness to all vertex coordinates (default 0.0).

Example:

regular_polygon(5, (100, 100), 80)

regular_polygon

Function: star(sides, (cx, cy), (rt, rb), (angt, angb), round, random)

Draw a sides-sided star centered at (cx, cy) with tip radius rt and base radius rb. All of the remaining arguments are optional. angt and angb are the tip and base angles in radians and default to angles that do not skew the star and that point it upwards. round specifies how much to round the corners and defaults to 0.0 (sharp). random adds an amount of randomness to all vertex coordinates (default 0.0).

Example:

star(5, (100, 100), (80, 30))

star

Arcs

Function: arc((cx, cy), (rx, ry), (ang1, ang2), arc_type)

Draw an arc as a segment of an ellipse with center (cx, cy) and radii rx and ry, ranging clockwise from angle ang1 to angle ang2. A scalar can be provided instead of (rx, ry), in which case the same value is used for both the x and y directions. If the optional arc_type argument is arc (the default), draw an ordinary arc; if slice, draw a pie slice; if chord, draw an arc with the endpoints connected with a straight line.

Example:

arc((canvas.width/2, canvas.height/2), 100, (pi/5, 9*pi/5), 'slice', fill='yellow', stroke_width=2)

arc

Paths

Function: path([elt, …])

Draw a path from a list of path commands (strings) and arguments (floats) or a list of PathCommands from inkex.paths.

Example:

path(['M', 226, 34, 'V', 237, 'L', 32, 185, 'C', 32, 185, 45, -9, 226, 34, 'Z'])

or equivalently,

Example:

path([Move(226, 34),
      Vert(237),
      Line(32, 185),
      Curve(32, 185, 45, -9, 226, 34),
      ZoneClose()])

path

Connectors

Function: connector(obj1, obj2, ctype, curve)

Draw a path that routes automatically between two Simple Inkscape Scripting objects. (All functions in the shape API return such an object.) ctype specifies the connector type and must be either polyline (any angle, the default) or orthogonal (only 90° bends). curve specifies the curvature amount (default 0).

Example:

r = rect((50, 50), (100, 100))
c = circle((200, 200), 25)
connector(r, c, ctype='orthogonal', curve=15)

connector

Text

Function: text(msg, (x, y), obj)

Draw a piece of text starting at coordinates (x, y). The text can be aligned to a path by passing in a path object as the optional obj argument.

Example:

text('Simple Inkscape Scripting', (0, canvas.height), font_size='36pt')

text

Method: add_text(msg, (x, y))

Append to a previous piece of text, possibly changing the style. The starting coordinates (x, y) are optional and can be used, e.g., to begin a new line.

Example:

t = text('Hello, ', (canvas.width/2, canvas.height/2), font_size='24pt', text_anchor='middle')
t.add_text('Inkscape', font_weight='bold', fill='#800000')
t.add_text('!!!')

add_text

Images

Function: image(fname, (x, y), embed)

Include an image. fname is the name of the file to include and can be either a local file or a URL. A variety of image formats are supported, but only PNG, JPEG, and SVG are guaranteed to be supported by all SVG viewers. The upper-left corner of the image will lie at coordinates (x, y). If embed is True (the default), the image data will be embedded in the SVG file. This results in a larger SVG file, but it can be viewed without access to the original image file. If embed is False, the SVG file merely will reference the named file. This results in a smaller SVG file, but it requires access to the image file; if the image file is moved or deleted, it no longer can be rendered in the SVG file.

Example:

image('https://media.inkscape.org/static/images/inkscape-logo.png', (0, 0), embed=False)

image

Clone this wiki locally