how to code inkscape extensions without class and self?

Discussion about writing code for Inkscape.
User avatar
nitrofurano
Posts: 18
Joined: Fri Jul 18, 2008 5:42 am

how to code inkscape extensions without class and self?

Postby nitrofurano » Wed Oct 03, 2012 3:24 am

hi everyone!
hi can we code a extremelly simple inkscape extension in Python without using any class, self, and etc.?
i'm asking because i struggle a lot about how to use them, and i really only wanted to know how do draw some lines from a script.

for example, i coded this, but it works on the terminal only, and i wanted to run it somehow inside inkscape, with some small changes:

Code: Select all

# #! /usr/bin/python
#- creates a svg grid (with golden ratio, rectangle rabatment, etc.)
#- usage: python createsvggrid.py 851 315 > drawing.svg

# import os,sys
# xsize=640;ysize=480;xsize=sys.argv[1];ysize=sys.argv[2];xsize=float(xsize);ysize=float(ysize)

print "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
print "<svg width=\""+str(xsize)+"\" height=\""+str(ysize)+"\" id=\"svg2\" version=\"1.0\" >"

print "<rect x=\"0\" y=\"0\" width=\""+str(xsize)+"\" height=\""+str(ysize)+"\" style=\"fill:#FFFFFF;stroke:none;opacity:1\"/>"

#- x4 and x3 modules (blue)
for i in range(1,4,1):
  print "<path id=\"1\" style=\"stroke:#0000FF;opacity:1;fill:none\" d=\"M "+str((xsize*i)/4)+",0 L "+str((xsize*i)/4)+","+str(ysize)+" z\" />"
  print "<path id=\"1\" style=\"stroke:#0000FF;opacity:1;fill:none\" d=\"M 0,"+str((ysize*i)/4)+" L "+str(xsize)+","+str((ysize*i)/4)+" z\" />"
for i in range(1,3,1):
  print "<path id=\"1\" style=\"stroke:#0000FF;opacity:1;fill:none\" d=\"M "+str((xsize*i)/3)+",0 L "+str((xsize*i)/3)+","+str(ysize)+" z\" />"
  print "<path id=\"1\" style=\"stroke:#0000FF;opacity:1;fill:none\" d=\"M 0,"+str((ysize*i)/3)+" L "+str(xsize)+","+str((ysize*i)/3)+" z\" />"

#- rectangle rabatment (green)
if xsize>ysize:
  print "<path id=\"1\" style=\"stroke:#00FF00;opacity:1;fill:none\" d=\"M "+str(ysize)+",0 L "+str(ysize)+","+str(ysize)+" z\" />"
  print "<path id=\"1\" style=\"stroke:#00FF00;opacity:1;fill:none\" d=\"M 0,0 L "+str(ysize)+","+str(ysize)+" z\" />"
  print "<path id=\"1\" style=\"stroke:#00FF00;opacity:1;fill:none\" d=\"M 0,"+str(ysize)+" L "+str(ysize)+",0 z\" />"
  print "<path id=\"1\" style=\"stroke:#00FF00;opacity:1;fill:none\" d=\"M "+str(xsize-ysize)+",0 L "+str(xsize-ysize)+","+str(ysize)+" z\" />"
  print "<path id=\"1\" style=\"stroke:#00FF00;opacity:1;fill:none\" d=\"M "+str(xsize-ysize)+",0 L "+str(xsize)+","+str(ysize)+" z\" />"
  print "<path id=\"1\" style=\"stroke:#00FF00;opacity:1;fill:none\" d=\"M "+str(xsize-ysize)+","+str(ysize)+" L "+str(xsize)+",0 z\" />"
if xsize<ysize:
  print "<path id=\"1\" style=\"stroke:#00FF00;opacity:1;fill:none\" d=\"M 0,"+str(xsize)+" L "+str(xsize)+","+str(xsize)+" z\" />"
  print "<path id=\"1\" style=\"stroke:#00FF00;opacity:1;fill:none\" d=\"M 0,0 L "+str(xsize)+","+str(xsize)+" z\" />"
  print "<path id=\"1\" style=\"stroke:#00FF00;opacity:1;fill:none\" d=\"M "+str(xsize)+",0 L 0,"+str(xsize)+" z\" />"
  print "<path id=\"1\" style=\"stroke:#00FF00;opacity:1;fill:none\" d=\"M 0,"+str(ysize-xsize)+" L "+str(xsize)+","+str(ysize-xsize)+" z\" />"
  print "<path id=\"1\" style=\"stroke:#00FF00;opacity:1;fill:none\" d=\"M 0,"+str(ysize-xsize)+" L "+str(xsize)+","+str(ysize)+" z\" />"
  print "<path id=\"1\" style=\"stroke:#00FF00;opacity:1;fill:none\" d=\"M "+str(xsize)+","+str(ysize-xsize)+" L 0,"+str(ysize)+" z\" />"

#- golden ratio (red)
print "<path id=\"1\" style=\"stroke:#FF0000;opacity:1;fill:none\" d=\"M "+str(xsize*0.381966011)+",0 L "+str(xsize*0.381966011)+","+str(ysize)+" z\" />"
print "<path id=\"1\" style=\"stroke:#FF0000;opacity:1;fill:none\" d=\"M "+str(xsize*0.618033989)+",0 L "+str(xsize*0.618033989)+","+str(ysize)+" z\" />"
print "<path id=\"1\" style=\"stroke:#FF0000;opacity:1;fill:none\" d=\"M 0,"+str(ysize*0.381966011)+" L "+str(xsize)+","+str(ysize*0.381966011)+" z\" />"
print "<path id=\"1\" style=\"stroke:#FF0000;opacity:1;fill:none\" d=\"M 0,"+str(ysize*0.618033989)+" L "+str(xsize)+","+str(ysize*0.618033989)+" z\" />"

print "</svg>"


(the idea is to create some kind of composition grid, something not yet available officially from Inkscape, neither somewhere else that i found)

i were about to start with this:

Code: Select all

#!/usr/bin/env python
import inkex
from simplestyle import *

svg=inkex.getElementsByTagName('svg')[0]
xsize=inkex.unittouu(svg.get('width'))
ysize=inkex.unittouu(svg.get('height'))

layer=inkex.etree.SubElement('g')
layer.setAttribute('inkscape:label','grids_layer')
layer.setAttribute('inkscape:groupmode','layer')

path=inkex.createElement('path')
path.setAttribute("sodipodi:type","rect")
path.setAttribute("x",0);path.setAttribute("y",0)
path.setAttribute("width",xsize);path.setAttribute("height",ysize)
path.setAttribute("style","fill:#FF0000;stroke:none;opacity:1")

svg.appendChild(layer)


but i think i'm going to nowhere this way... :| - and since i code in Python for years and never used class or self (i really don't know how to use them and what for is needed...), i really believe that class and self are really not needed on our code

thanks! :)

Return to “Programming”