2020-07-30 01:16:18 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
import math
|
|
|
|
import inkex
|
|
|
|
from lxml import etree
|
|
|
|
|
2021-06-02 23:30:37 +02:00
|
|
|
class clonesInPerspective(inkex.EffectExtension):
|
2021-04-15 17:03:47 +02:00
|
|
|
|
|
|
|
def add_arguments(self, pars):
|
|
|
|
pars.add_argument('--num', type = int, default = 5, help = 'Drag out center of rotation before calling')
|
|
|
|
pars.add_argument('--ratio', type = float, default = 0.9, help = 'Ratio of size of nearest neighbor to first. Must be < 1')
|
2020-07-30 01:16:18 +02:00
|
|
|
|
|
|
|
def effect(self):
|
2021-04-09 10:36:29 +02:00
|
|
|
if len(self.svg.selected) != 1:
|
|
|
|
inkex.errormsg("Select exactly 1 thing. If necessary, group your items before.")
|
2020-07-30 01:16:18 +02:00
|
|
|
sys.exit(1)
|
2021-04-09 10:36:29 +02:00
|
|
|
xpath = list(self.svg.selected.items())[0][0]
|
|
|
|
sel = self.svg.selected[xpath]
|
|
|
|
id = sel.get('id')
|
|
|
|
|
2020-07-30 01:16:18 +02:00
|
|
|
try :
|
2021-04-09 10:36:29 +02:00
|
|
|
tx = sel.attrib[inkex.addNS('transform-center-x','inkscape')]
|
|
|
|
except KeyError:
|
2020-07-30 01:16:18 +02:00
|
|
|
tx = '0.'
|
|
|
|
try :
|
2021-04-09 10:36:29 +02:00
|
|
|
ty = sel.attrib[inkex.addNS('transform-center-y','inkscape')]
|
|
|
|
except KeyError:
|
2020-07-30 01:16:18 +02:00
|
|
|
ty = '0.'
|
|
|
|
|
|
|
|
if float(tx) == 0. and float(ty) == 0. :
|
2021-04-09 10:36:29 +02:00
|
|
|
inkex.errormsg("Center of rotation ist at the center of object. Please move the center first.")
|
2020-07-30 01:16:18 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
2020-08-30 11:33:47 +02:00
|
|
|
bbox = sel.bounding_box()
|
|
|
|
width = bbox.height
|
|
|
|
height = bbox.width
|
|
|
|
cx = float(bbox.left) + 0.5 * width #Find center of selected object
|
|
|
|
cy = float(bbox.top) + 0.5 * height #Find center of selected object
|
2020-07-30 01:16:18 +02:00
|
|
|
tx = float(tx)
|
|
|
|
ty = float(ty)
|
|
|
|
crat = 1.0
|
|
|
|
otx = tx
|
|
|
|
oty = ty
|
|
|
|
|
|
|
|
parent = sel.getparent()
|
|
|
|
j = parent.index(sel)
|
2021-04-09 10:36:29 +02:00
|
|
|
for i in range(self.options.num) :
|
|
|
|
crat *= self.options.ratio
|
|
|
|
tx *= self.options.ratio
|
|
|
|
ty *= self.options.ratio
|
2020-07-30 01:16:18 +02:00
|
|
|
att = {
|
|
|
|
"id" : self.svg.get_unique_id("clone" + id),
|
|
|
|
inkex.addNS('href','xlink') : "#" + id,
|
|
|
|
inkex.addNS('transform-center-x','inkscape') : str(tx),
|
|
|
|
inkex.addNS('transform-center-y','inkscape') : str(ty),
|
2020-08-30 11:33:47 +02:00
|
|
|
'transform' : ("matrix(%f,0,0,%f,%f,%f)" % (crat, crat,(1. - crat)*(cx + otx), (1. - crat)*(cy - oty))),
|
|
|
|
"width" : "100%",
|
|
|
|
"height" : "100%",
|
|
|
|
}
|
|
|
|
parent.insert(j, etree.Element('use', att))
|
2020-07-30 01:16:18 +02:00
|
|
|
|
2020-08-31 21:25:41 +02:00
|
|
|
if __name__ == '__main__':
|
2021-06-02 23:30:37 +02:00
|
|
|
clonesInPerspective().run()
|