seamless_pattern.py revision dafc2449ba780d0240ee41f9025534cdc5646cd6
#!/usr/bin/env python
# Written by Jabiertxof
# V.04
import inkex
import re
import os
from lxml import etree
class C(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.OptionParser.add_option("-w", "--width", action="store", type="int", dest="desktop_width", default="100", help="Custom width")
self.OptionParser.add_option("-z", "--height", action="store", type="int", dest="desktop_height", default="100", help="Custom height")
def effect(self):
width = self.options.desktop_width
height = self.options.desktop_height
path = os.path.dirname(os.path.realpath(__file__))
self.document = etree.parse(os.path.join(path, "seamless_pattern.svg"))
root = self.document.getroot()
root.set("id", "SVGRoot")
root.set("width", str(width) + 'px')
root.set("height", str(height) + 'px')
root.set("viewBox", "0 0 " + str(width) + " " + str(height) )
xpathStr = '//svg:rect[@id="clipPathRect"]'
clipPathRect = root.xpath(xpathStr, namespaces=inkex.NSS)
if clipPathRect != []:
clipPathRect[0].set("width", str(width))
clipPathRect[0].set("height", str(height))
xpathStr = '//svg:g[@id="designTop"] | //svg:g[@id="designBottom"]'
designZone = root.xpath(xpathStr, namespaces=inkex.NSS)
if designZone != []:
designZone[0].set("transform", "scale(" + str(width/100.) + ","+ str(height/100.) + ")")
designZone[1].set("transform", "scale(" + str(width/100.) + ","+ str(height/100.) + ")")
xpathStr = '//svg:use[@id="top1"] | //svg:use[@id="bottom1"]'
pattern1 = root.xpath(xpathStr, namespaces=inkex.NSS)
if pattern1 != []:
pattern1[0].set("transform", "translate(" + str(-width) + "," + str(-height) + ")")
pattern1[1].set("transform", "translate(" + str(-width) + "," + str(-height) + ")")
xpathStr = '//svg:use[@id="top2"] | //svg:use[@id="bottom2"]'
pattern2 = root.xpath(xpathStr, namespaces=inkex.NSS)
if pattern2 != []:
pattern2[0].set("transform", "translate(0," + str(-height) +")" )
pattern2[1].set("transform", "translate(0," + str(-height) +")" )
xpathStr = '//svg:use[@id="top3"] | //svg:use[@id="bottom3"]'
pattern3 = root.xpath(xpathStr, namespaces=inkex.NSS)
if pattern3 != []:
pattern3[0].set("transform", "translate(" + str(width) + "," + str(-height) + ")" )
pattern3[1].set("transform", "translate(" + str(width) + "," + str(-height) + ")" )
xpathStr = '//svg:use[@id="top4"] | //svg:use[@id="bottom4"]'
pattern4 = root.xpath(xpathStr, namespaces=inkex.NSS)
if pattern4 != []:
pattern4[0].set("transform", "translate(" + str(-width) + ",0)" )
pattern4[1].set("transform", "translate(" + str(-width) + ",0)" )
xpathStr = '//svg:use[@id="top5"] | //svg:use[@id="bottom5"]'
pattern5 = root.xpath(xpathStr, namespaces=inkex.NSS)
if pattern5 != []:
pattern5[0].set("transform", "translate(0,0)" )
pattern5[1].set("transform", "translate(0,0)" )
xpathStr = '//svg:use[@id="top6"] | //svg:use[@id="bottom6"]'
pattern6 = root.xpath(xpathStr, namespaces=inkex.NSS)
if pattern6 != []:
pattern6[0].set("transform", "translate(" + str(width) + ",0)" )
pattern6[1].set("transform", "translate(" + str(width) + ",0)" )
xpathStr = '//svg:use[@id="top7"] | //svg:use[@id="bottom7"]'
pattern7 = root.xpath(xpathStr, namespaces=inkex.NSS)
if pattern7 != []:
pattern7[0].set("transform", "translate(" + str(-width) + "," + str(height) + ")" )
pattern7[1].set("transform", "translate(" + str(-width) + "," + str(height) + ")" )
xpathStr = '//svg:use[@id="top8"] | //svg:use[@id="bottom8"]'
pattern8 = root.xpath(xpathStr, namespaces=inkex.NSS)
if pattern8 != []:
pattern8[0].set("transform", "translate(0," + str(height) + ")" )
pattern8[1].set("transform", "translate(0," + str(height) + ")" )
xpathStr = '//svg:use[@id="top9"] | //svg:use[@id="bottom9"]'
pattern9 = root.xpath(xpathStr, namespaces=inkex.NSS)
if pattern9 != []:
pattern9[0].set("transform", "translate(" + str(width) + "," + str(height) + ")" )
pattern9[1].set("transform", "translate(" + str(width) + "," + str(height) + ")" )
xpathStr = '//svg:use[@id="clonePreview1"]'
clonePreview1 = root.xpath(xpathStr, namespaces=inkex.NSS)
if clonePreview1 != []:
clonePreview1[0].set("transform", "translate(0," + str(height) + ")" )
xpathStr = '//svg:use[@id="clonePreview2"]'
clonePreview2 = root.xpath(xpathStr, namespaces=inkex.NSS)
if clonePreview2 != []:
clonePreview2[0].set("transform", "translate(0," + str(height * 2) + ")" )
xpathStr = '//svg:use[@id="clonePreview3"]'
clonePreview3 = root.xpath(xpathStr, namespaces=inkex.NSS)
if clonePreview3 != []:
clonePreview3[0].set("transform", "translate(" + str(width) + ",0)" )
xpathStr = '//svg:use[@id="clonePreview4"]'
clonePreview4 = root.xpath(xpathStr, namespaces=inkex.NSS)
if clonePreview4 != []:
clonePreview4[0].set("transform", "translate(" + str(width) + "," + str(height) + ")" )
xpathStr = '//svg:use[@id="clonePreview5"]'
clonePreview5 = root.xpath(xpathStr, namespaces=inkex.NSS)
if clonePreview5 != []:
clonePreview5[0].set("transform", "translate(" + str(width) + "," + str(height * 2) + ")" )
xpathStr = '//svg:use[@id="clonePreview6"]'
clonePreview6 = root.xpath(xpathStr, namespaces=inkex.NSS)
if clonePreview6 != []:
clonePreview6[0].set("transform", "translate(" + str(width*2) + ", 0)" )
xpathStr = '//svg:use[@id="clonePreview7"]'
clonePreview7 = root.xpath(xpathStr, namespaces=inkex.NSS)
if clonePreview7 != []:
clonePreview7[0].set("transform", "translate(" + str(width*2) + "," + str(height) + ")" )
xpathStr = '//svg:use[@id="clonePreview8"]'
clonePreview8 = root.xpath(xpathStr, namespaces=inkex.NSS)
if clonePreview8 != []:
clonePreview8[0].set("transform", "translate(" + str(width*2) + "," + str(height*2) + ")" )
xpathStr = '//svg:use[@id="fullPatternClone"]'
patternGenerator = root.xpath(xpathStr, namespaces=inkex.NSS)
if patternGenerator != []:
patternGenerator[0].set("transform", "translate(" + str(width * 2) + ",-" + str(height) + ")" )
patternGenerator[0].set("{http://www.inkscape.org/namespaces/inkscape}tile-cx", str(width/2))
patternGenerator[0].set("{http://www.inkscape.org/namespaces/inkscape}tile-cy", str(height/2))
patternGenerator[0].set("{http://www.inkscape.org/namespaces/inkscape}tile-w", str(width))
patternGenerator[0].set("{http://www.inkscape.org/namespaces/inkscape}tile-h", str(height))
patternGenerator[0].set("{http://www.inkscape.org/namespaces/inkscape}tile-x0", str(width))
patternGenerator[0].set("{http://www.inkscape.org/namespaces/inkscape}tile-y0", str(height))
patternGenerator[0].set("width", str(width))
patternGenerator[0].set("height", str(height))
namedview = root.find(inkex.addNS('namedview', 'sodipodi'))
if namedview is None:
namedview = inkex.etree.SubElement( root, inkex.addNS('namedview', 'sodipodi') );
namedview.set(inkex.addNS('document-units', 'inkscape'), 'px')
namedview.set(inkex.addNS('cx', 'inkscape'), str((width*8)/2.0) )
namedview.set(inkex.addNS('cy', 'inkscape'), "0" )
namedview.set(inkex.addNS('zoom', 'inkscape'), str(0.5 / (((width+height)/2)/100.)) )
c = C()
c.affect()