Skip to content

Commit

Permalink
Provide a clone function to produce linked clones of an object
Browse files Browse the repository at this point in the history
  • Loading branch information
spakin committed Oct 22, 2021
1 parent 39396f6 commit 45f5b25
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ Append to a previous piece of text (created with `text` or `more_text`), possibl

Include an image. `fname` is the name of the file to include. 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 will merely 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 will no longer appear in the SVG file. `fname` can be a URL. In this case, `embed` must be set to `False`. *Example*: `image('https://media.inkscape.org/static/images/inkscape-logo.png', (0, 0), embed=False)`

* `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)')`

### Transformations

All of the functions presented under [Shape API](#shape-api) accept an additional `transform` parameter. `transform` takes a string representing an [SVG transform](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform) (`rotate`, `scale`, etc.) and applies that transform to the object.
Expand Down
10 changes: 10 additions & 0 deletions examples/clone_wave.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Use Simple Inkscape Scripting to draw a wave of cloned objects.
#
# Try rotating, scaling, or recoloring the original (leftmost) object
# and seeing how all the other objects change accordingly.

mark = regular_polygon(5, (0, height/2), 10, fill='#55ddff')
for a in range(5, 360, 5):
x = a*width/360
y = sin(a*pi/180)*height/2 + height/2
clone(mark, transform='translate(%.5g, %.5g) translate(0, %.5g)' % (x, y, -height/2))
File renamed without changes.
11 changes: 6 additions & 5 deletions simple_inkscape_scripting.inx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
gui-description="See the help tab for more information" />
<hbox>
<spacer size="expand" />
<label>– or –</label>
<label>– and/or –</label>
<spacer size="expand" />
</hbox>
<param name="program" type="string" appearance="multiline"
Expand Down Expand Up @@ -43,6 +43,7 @@
• text(msg, base)
• more_text(msg, [base])
• image(fname, ul, [embed])
• clone(obj)
• group([obj1, obj2, …])
• inkex_object(iobj)
</label>
Expand All @@ -52,10 +53,10 @@
ang, angt, angb, round, random, and curve are floats. sides
is an integer. Each elt is either a string or a float. msg,
fname, and ctype are strings, but ctype must be either
"polyline" or "orthogonal". embed is a bool. obj1 and obj2
are objects returned from any of the object-creation functions
listed above. iobj is an arbitrary object created using
Inkscape's core API.
"polyline" or "orthogonal". embed is a bool. obj, obj1, and
obj2 are objects returned from any of the object-creation
functions listed above. iobj is an arbitrary object created
using Inkscape's core API.
</label>
<label />
<label>
Expand Down
7 changes: 7 additions & 0 deletions simple_inkscape_scripting.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,13 @@ def image(fname, ul, embed=True, transform=None, conn_avoid=False, **style):
return SimpleObject(obj, transform, conn_avoid, {}, style)


def clone(obj, transform=None, conn_avoid=False, **style):
'Return a linked clone of the object.'
c = inkex.Use(obj._inkscape_obj)
c.href = obj._inkscape_obj.get_id()
return SimpleObject(c, transform, conn_avoid, {}, style)


def group(objs=[], transform=None, conn_avoid=False, **style):
'Create a container for other objects.'
g = inkex.Group()
Expand Down

0 comments on commit 45f5b25

Please sign in to comment.