This repository has been archived on 2023-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
mightyscape-0.92-deprecated/extensions/fablabchemnitz_scatter.py

141 lines
6.1 KiB
Python

#!/usr/bin/env python
'''
scatter.py
Sunabe kazumichi 2009/9/29
http://dp48069596.lolipop.jp/
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
attention:when rotation degree become large, patterns are deformed largely.
therefore,restricted the rotation in range -30<deg<30.
'''
import inkex, cubicsuperpath, bezmisc
import pathmodifier,simpletransform
import copy, math, re, random
def translate(pathcomp,dx,dy):
for ctl in pathcomp:
for pt in ctl:
pt[0]+=dx
pt[1]+=dy
def scale(pathcomp,scale,org):
for ctl in pathcomp:
for pt in ctl:
pt[0]=org[0]+(pt[0]-org[0])*scale
pt[1]=org[1]+(pt[1]-org[1])*scale
def rotate(pathcomp,angle):
for ctl in pathcomp:
for pt in ctl:
pt[0]=pt[0]*math.cos(angle)-pt[1]*math.sin(angle)
pt[1]=pt[0]*math.sin(angle)+pt[1]*math.cos(angle)
class ScatterAlongPath(pathmodifier.Diffeo):
def __init__(self):
pathmodifier.Diffeo.__init__(self)
self.OptionParser.add_option("--title")
self.OptionParser.add_option("-r", "--random",
action="store", type="inkbool",
dest="random", default=True,
help="random pattern on the skeleton")
self.OptionParser.add_option("-d", "--density",
action="store", type="int",
dest="density", default=3)
self.OptionParser.add_option("-o", "--offset",
action="store", type="float",
dest="offset", default=0.0, help="offset")
self.OptionParser.add_option("-s", "--scale",
action="store", type="float",
dest="scale", default=100, help="scale")
self.OptionParser.add_option("-p", "--rotation",
action="store", type="float",
dest="rotation", default=0,
help="rotation pattern on the skeleton")
def prepareSelectionList(self):
idList=self.options.ids
idList=pathmodifier.zSort(self.document.getroot(),idList)
id = idList[-1]
self.patterns={id:self.selected[id]}
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 effect(self):
if len(self.options.ids)<2:
inkex.debug("This extension requires that you select two paths.")
return
self.prepareSelectionList()
bbox=simpletransform.computeBBox(self.patterns.values())
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)
newp=[]
for skelnode in self.skeletons.itervalues():
self.curSekeleton=cubicsuperpath.parsePath(skelnode.get('d'))
den=1
while den <= self.options.density:
for comp in self.curSekeleton:
p=copy.deepcopy(p0)
if self.options.random:
xoffset=-bbox[0]-(bbox[1]-bbox[0])/2+random.uniform(-self.options.offset, self.options.offset)
yoffset=-(bbox[2]+bbox[3])/2-random.uniform(-self.options.offset, self.options.offset)
else:
self.options.density=1
self.options.offset=0
xoffset=-bbox[0]-(bbox[1]-bbox[0])/2+self.options.offset
yoffset=-(bbox[2]+bbox[3])/2-self.options.offset
NbCopies=len(comp)
new=[]
for sub in p:
for i in range(0,NbCopies,1):
new.append(copy.deepcopy(sub))
p=new
for sub in p:
translate(sub,xoffset,yoffset)
for sub in p:
if self.options.random:
scale(sub,random.uniform(self.options.scale, 100)/100,(0,0))
else:
scale(sub,self.options.scale/100,(0,0))
for sub in p:
if self.options.random:
rotate(sub,random.uniform(-self.options.rotation*math.pi/180, self.options.rotation*math.pi/180))
else:
rotate(sub,self.options.rotation*math.pi/180)
for i,sub in enumerate(p):
try:
translate(sub,comp[i][1][0],comp[i][1][1])
except:
pass
newp+=p
den+=1
node.set('d', cubicsuperpath.formatPath(newp))
e = ScatterAlongPath()
e.affect()