Created
May 20, 2018 13:20
-
-
Save haselwarter/cc373ea47523dc8fdbb50b0bfc1d15d4 to your computer and use it in GitHub Desktop.
Inkscape extension that randomly places an object along a path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension"> | |
<_name>Pattern randomly along Path</_name> | |
<id>math.univ-lille1.barraud.pathdeformrandomly</id> | |
<dependency type="executable" location="extensions">pathmodifier.py</dependency> | |
<dependency type="executable" location="extensions">randompathalongpath.py</dependency> | |
<dependency type="executable" location="extensions">inkex.py</dependency> | |
<param name="tab" type="notebook"> | |
<page name="Options" _gui-text="Options"> | |
<param name="copymode" type="enum" _gui-text="Copies of the pattern:"> | |
<_item value="Single">Single</_item> | |
<_item value="Single, stretched">Single, stretched</_item> | |
<_item value="Repeated">Repeated</_item> | |
<_item value="Repeated, stretched">Repeated, stretched</_item> | |
</param> | |
<param name="kind" type="enum" _gui-text="Deformation type:"> | |
<_item value="Snake">Snake</_item> | |
<_item value="Ribbon">Ribbon</_item> | |
</param> | |
<param name="noffset" type="float" _gui-text="Normal offset:" min="-10000.0" max="10000.0">0.0</param> | |
<param name="vertical" type="boolean" _gui-text="Pattern is vertical">false</param> | |
<param name="duplicate" type="boolean" _gui-text="Duplicate the pattern before deformation">true</param> | |
<param name="number_repetitions" type="int" _gui-text="Number of randomly picked points:" min="1" max="10000">0</param> | |
<param name="random_toffset_min" type="float" _gui-text="Minimal random tangential offset (%):" min="-10000.0" max="10000.0">0.0</param> | |
<param name="random_toffset_max" type="float" _gui-text="Maximum random tangential offset (%):" min="-10000.0" max="10000.0">100.0</param> | |
</page> | |
<page name="Help" _gui-text="Help"> | |
<_param name="title" type="description">This effect scatters or bends a pattern along arbitrary "skeleton" paths. The pattern is the topmost object in the selection. Groups of paths, shapes or clones are allowed.</_param> | |
</page> | |
</param> | |
<effect> | |
<object-type>all</object-type> | |
<effects-menu> | |
<submenu _name="Generate from Path"/> | |
</effects-menu> | |
</effect> | |
<script> | |
<command reldir="extensions" interpreter="python">randompathalongpath.py</command> | |
</script> | |
</inkscape-extension> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
''' | |
Copyright (C) 2018 Philipp G. Haselwarter, philipp+inkscape@haselwarter.org | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
barraud@math.univ-lille1.fr | |
Based on 'pathalongpath.py' by Jean-Francois Barraud. | |
Quick description: | |
This script randomly places and deforms an object (the pattern) | |
along other paths (skeletons). | |
The first selected object is the pattern. | |
The last selected ones are the skeletons. | |
Imagine a straight horizontal line l in the middle of the bounding box of the pattern. | |
Consider the normal bundle of l: the collection of all the vertical lines meeting l. | |
Consider this as the initial state of the plane; in particular, think of the pattern | |
as painted on these lines. | |
Now move and bend l to make it fit a skeleton, and see what happens to the normals: | |
they move and rotate, deforming the pattern. | |
''' | |
# standard library | |
import copy | |
import random | |
# local library | |
import inkex | |
import cubicsuperpath | |
import bezmisc | |
import pathmodifier | |
import simpletransform | |
def flipxy(path): | |
for pathcomp in path: | |
for ctl in pathcomp: | |
for pt in ctl: | |
tmp=pt[0] | |
pt[0]=-pt[1] | |
pt[1]=-tmp | |
def offset(pathcomp,dx,dy): | |
for ctl in pathcomp: | |
for pt in ctl: | |
pt[0]+=dx | |
pt[1]+=dy | |
def stretch(pathcomp,xscale,yscale,org): | |
for ctl in pathcomp: | |
for pt in ctl: | |
pt[0]=org[0]+(pt[0]-org[0])*xscale | |
pt[1]=org[1]+(pt[1]-org[1])*yscale | |
def linearize(p,tolerance=0.001): | |
''' | |
This function receives a component of a 'cubicsuperpath' and returns two things: | |
The path subdivided in many straight segments, and an array containing the length of each segment. | |
We could work with bezier path as well, but bezier arc lengths are (re)computed for each point | |
in the deformed object. For complex paths, this might take a while. | |
''' | |
zero=0.000001 | |
i=0 | |
d=0 | |
lengths=[] | |
while i<len(p)-1: | |
box = bezmisc.pointdistance(p[i ][1],p[i ][2]) | |
box += bezmisc.pointdistance(p[i ][2],p[i+1][0]) | |
box += bezmisc.pointdistance(p[i+1][0],p[i+1][1]) | |
chord = bezmisc.pointdistance(p[i][1], p[i+1][1]) | |
if (box - chord) > tolerance: | |
b1, b2 = bezmisc.beziersplitatt([p[i][1],p[i][2],p[i+1][0],p[i+1][1]], 0.5) | |
p[i ][2][0],p[i ][2][1]=b1[1] | |
p[i+1][0][0],p[i+1][0][1]=b2[2] | |
p.insert(i+1,[[b1[2][0],b1[2][1]],[b1[3][0],b1[3][1]],[b2[1][0],b2[1][1]]]) | |
else: | |
d=(box+chord)/2 | |
lengths.append(d) | |
i+=1 | |
new=[p[i][1] for i in range(0,len(p)-1) if lengths[i]>zero] | |
new.append(p[-1][1]) | |
lengths=[l for l in lengths if l>zero] | |
return(new,lengths) | |
class PathAlongPath(pathmodifier.Diffeo): | |
def __init__(self): | |
random.seed(None) | |
pathmodifier.Diffeo.__init__(self) | |
self.OptionParser.add_option("--title") | |
self.OptionParser.add_option("-n", "--noffset", | |
action="store", type="float", | |
dest="noffset", default=0.0, help="normal offset") | |
self.OptionParser.add_option("--random_toffset_min", | |
action="store", type="float", | |
dest="random_toffset_min", default=0.0, help="minimal random tangential offset") | |
self.OptionParser.add_option("--random_toffset_max", | |
action="store", type="float", | |
dest="random_toffset_max", default=100.0, help="maximum random tangential offset") | |
self.OptionParser.add_option("--number_repetitions", | |
action="store", type="int", | |
dest="number_repetitions", default=1, help="number of random repetitions") | |
self.OptionParser.add_option("-k", "--kind", | |
action="store", type="string", | |
dest="kind", default=True, | |
help="choose between wave or snake effect") | |
self.OptionParser.add_option("-c", "--copymode", | |
action="store", type="string", | |
dest="copymode", default=True, | |
help="repeat the path to fit deformer's length") | |
self.OptionParser.add_option("-v", "--vertical", | |
action="store", type="inkbool", | |
dest="vertical", default=False, | |
help="reference path is vertical") | |
self.OptionParser.add_option("-d", "--duplicate", | |
action="store", type="inkbool", | |
dest="duplicate", default=False, | |
help="duplicate pattern before deformation") | |
self.OptionParser.add_option("--tab", | |
action="store", type="string", | |
dest="tab", | |
help="The selected UI-tab when OK was pressed") | |
def prepareSelectionList(self): | |
idList=self.options.ids | |
idList=pathmodifier.zSort(self.document.getroot(),idList) | |
id = idList[-1] | |
self.patterns={id:self.selected[id]} | |
## ##first selected->pattern, all but first selected-> skeletons | |
## id = self.options.ids[-1] | |
## self.patterns={id:self.selected[id]} | |
if self.options.duplicate: | |
self.patterns=self.duplicateNodes(self.patterns) | |
self.expandGroupsUnlinkClones(self.patterns, True, True) | |
self.objectsToPaths(self.patterns) | |
del self.selected[id] | |
self.skeletons=self.selected | |
self.expandGroupsUnlinkClones(self.skeletons, True, False) | |
self.objectsToPaths(self.skeletons) | |
def lengthtotime(self,l): | |
''' | |
Recieves an arc length l, and returns the index of the segment in self.skelcomp | |
containing the corresponding point, to gether with the position of the point on this segment. | |
If the deformer is closed, do computations modulo the toal length. | |
''' | |
if self.skelcompIsClosed: | |
l=l % sum(self.lengths) | |
if l<=0: | |
return 0,l/self.lengths[0] | |
i=0 | |
while (i<len(self.lengths)) and (self.lengths[i]<=l): | |
l-=self.lengths[i] | |
i+=1 | |
t=l/self.lengths[min(i,len(self.lengths)-1)] | |
return i, t | |
def applyDiffeo(self,bpt,vects=()): | |
''' | |
The kernel of this stuff: | |
bpt is a base point and for v in vectors, v'=v-p is a tangent vector at bpt. | |
''' | |
s=bpt[0]-self.skelcomp[0][0] | |
i,t=self.lengthtotime(s) | |
if i==len(self.skelcomp)-1: | |
x,y=bezmisc.tpoint(self.skelcomp[i-1],self.skelcomp[i],1+t) | |
dx=(self.skelcomp[i][0]-self.skelcomp[i-1][0])/self.lengths[-1] | |
dy=(self.skelcomp[i][1]-self.skelcomp[i-1][1])/self.lengths[-1] | |
else: | |
x,y=bezmisc.tpoint(self.skelcomp[i],self.skelcomp[i+1],t) | |
dx=(self.skelcomp[i+1][0]-self.skelcomp[i][0])/self.lengths[i] | |
dy=(self.skelcomp[i+1][1]-self.skelcomp[i][1])/self.lengths[i] | |
vx=0 | |
vy=bpt[1]-self.skelcomp[0][1] | |
if self.options.wave: | |
bpt[0]=x+vx*dx | |
bpt[1]=y+vy+vx*dy | |
else: | |
bpt[0]=x+vx*dx-vy*dy | |
bpt[1]=y+vx*dy+vy*dx | |
for v in vects: | |
vx=v[0]-self.skelcomp[0][0]-s | |
vy=v[1]-self.skelcomp[0][1] | |
if self.options.wave: | |
v[0]=x+vx*dx | |
v[1]=y+vy+vx*dy | |
else: | |
v[0]=x+vx*dx-vy*dy | |
v[1]=y+vx*dy+vy*dx | |
def effect(self): | |
if len(self.options.ids)<2: | |
inkex.errormsg(_("This extension requires two selected paths.")) | |
return | |
self.prepareSelectionList() | |
self.options.wave = (self.options.kind=="Ribbon") | |
if self.options.copymode=="Single": | |
self.options.repeat =False | |
self.options.stretch=False | |
elif self.options.copymode=="Single, stretched": | |
self.options.repeat =False | |
self.options.stretch=True | |
bbox = simpletransform.computeBBox(self.patterns.values()) | |
if self.options.vertical: | |
#flipxy(bbox)... | |
bbox=(-bbox[3],-bbox[2],-bbox[1],-bbox[0]) | |
width=bbox[1]-bbox[0] | |
for id, node in self.patterns.iteritems(): | |
if node.tag == inkex.addNS('path','svg') or node.tag == 'path': | |
d = node.get('d') | |
p0 = cubicsuperpath.parsePath(d) | |
if self.options.vertical: | |
flipxy(p0) | |
newp = [] | |
for i in range(0, self.options.number_repetitions): | |
for skelnode in self.skeletons.itervalues(): | |
self.curSkeleton = cubicsuperpath.parsePath(skelnode.get('d')) | |
if self.options.vertical: | |
flipxy(self.curSkeleton) | |
for comp in self.curSkeleton: | |
p = copy.deepcopy(p0) | |
self.skelcomp, self.lengths = linearize(comp) | |
#!!!!>----> TODO: really test if path is closed! end point==start point is not enough! | |
self.skelcompIsClosed = (self.skelcomp[0] == self.skelcomp[-1]) | |
length = sum(self.lengths) | |
tangential_offset = (random.uniform(self.options.random_toffset_min, self.options.random_toffset_max) / 100.0) * length | |
xoffset = self.skelcomp[0][0] - bbox[0] + tangential_offset | |
yoffset = self.skelcomp[0][1] - (bbox[2]+bbox[3]) / 2 - self.options.noffset | |
for sub in p: | |
offset(sub,xoffset,yoffset) | |
if self.options.stretch: | |
if not width: | |
exit(_("The 'stretch' option requires that the pattern must have non-zero width :\nPlease edit the pattern width.")) | |
for sub in p: | |
stretch(sub,length/width,1,self.skelcomp[0]) | |
for sub in p: | |
for ctlpt in sub: | |
self.applyDiffeo(ctlpt[1],(ctlpt[0],ctlpt[2])) | |
if self.options.vertical: | |
flipxy(p) | |
newp += p | |
node.set('d', cubicsuperpath.formatPath(newp)) | |
return | |
if __name__ == '__main__': | |
e = PathAlongPath() | |
e.affect() | |
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment