Skip to content

Copying objects

Scott Pakin edited this page Feb 13, 2022 · 3 revisions

Duplicates

Shape objects can be duplicated just like by Inkscape's EditDuplicate feature. Duplicating an object produces an independent copy.

Function: duplicate(obj)

Modifications made in the Inkscape GUI to either the original or duplicate object do not alter the other object.

Example:

p1 = regular_polygon(5, (100, 100), 50, 360/5, fill='#6600ff', stroke_width=3)
p2 = duplicate(p1, transform='translate(150, 0)', fill='#00ff66')

duplicate

Clones

It is possible to clone a shape object just like Inkscape's EditCloneCreate Clone feature.

Function: clone(obj)

Return a linked clone of an object. Modifications made in the Inkscape GUI to the original object propagate to all clones. (This applies only to certain modifications such as rotations, style properties, and path control-point locations.)

Example:

r1 = rect((100, 100), (200, 200), fill='#668000')
r2 = clone(r1, transform='translate(200, 0)')

clone

The following method is defined on all shape objects. It converts the object to an object definition, essentially a template that can be cloned and transformed:

Method: to_def()

After to_def is invoked on a shape, the shape is no longer rendered in the image. However, clones are rendered. Typical usage is to define a base shape at the origin, unrotated, and drawn in a "natural" size then to transform clones of the object.

Example:

tmpl = rect((-50, -50), (50, 50), fill='#0088aa').to_def()
rot = 0.0
for y in range(5):
    for x in range(5):
        tr = inkex.Transform()
        tr.add_translate(x*200 + 100, y*200 + 100)
        tr.add_rotate(rot)
        clone(tmpl, transform=tr)
        rot -= 360/25

to_def

The first line of the above defines a template rectangle. Note that to_def returns the object, which enables an object to be defined, converted to a definition, and assigned to a variable in a single line. The rest of the code draws a 5×5 grid of rectangles, each with a different rotation.

Clone this wiki locally