inkex.py - bug on custom size, at document properties?

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

inkex.py - bug on custom size, at document properties?

Postby nitrofurano » Wed Oct 03, 2012 10:09 pm

finally i got some code working on Inkscape extensions, but i think there is a bug when we run it on a canvas size changed at Inkscape document properties? is that a bug on inkex.py, or should we bypass this in another way?

compositiongrid.py

Code: Select all

#!/usr/bin/env python
#- bug: only works fine on new documents (templates)
#-   drawing lines on wrong places when changing orientation and custom size (document properties)

import inkex,simplestyle,math
class anotherclass(inkex.Effect):
  def __init__(self):inkex.Effect.__init__(self)
  def effect(self):
    svg=self.document.getroot()
    xsize=inkex.unittouu(svg.get('width'))
    ysize=inkex.unittouu(svg.get('height'))

    #- x4 and x3 modules (blue)
    for i in range(1,4,1):
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#0000FF","fill":"none","opacity":"1"}))
      new.set("d","M "+str((xsize*i)/4)+",0 L "+str((xsize*i)/4)+","+str(ysize)+" z")
      self.current_layer.append(new)
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#0000FF","fill":"none","opacity":"1"}))
      new.set("d","M 0,"+str((ysize*i)/4)+" L "+str(xsize)+","+str((ysize*i)/4)+" z")
      self.current_layer.append(new)
    for i in range(1,3,1):
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#0000FF","fill":"none","opacity":"1"}))
      new.set("d","M "+str((xsize*i)/3)+",0 L "+str((xsize*i)/3)+","+str(ysize)+" z")
      self.current_layer.append(new)
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#0000FF","fill":"none","opacity":"1"}))
      new.set("d","M 0,"+str((ysize*i)/3)+" L "+str(xsize)+","+str((ysize*i)/3)+" z")
      self.current_layer.append(new)

    #- rectangle rabatment (green)
    if xsize>ysize:
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#00FF00","fill":"none","opacity":"1"}))
      new.set("d","M "+str(ysize)+",0 L "+str(ysize)+","+str(ysize)+" z")
      self.current_layer.append(new)
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#00FF00","fill":"none","opacity":"1"}))
      new.set("d","M 0,0 L "+str(ysize)+","+str(ysize)+" z")
      self.current_layer.append(new)
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#00FF00","fill":"none","opacity":"1"}))
      new.set("d","M 0,"+str(ysize)+" L "+str(ysize)+",0 z")
      self.current_layer.append(new)
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#00FF00","fill":"none","opacity":"1"}))
      new.set("d","M "+str(xsize-ysize)+",0 L "+str(xsize-ysize)+","+str(ysize)+" z")
      self.current_layer.append(new)
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#00FF00","fill":"none","opacity":"1"}))
      new.set("d","M "+str(xsize-ysize)+",0 L "+str(xsize)+","+str(ysize)+" z")
      self.current_layer.append(new)
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#00FF00","fill":"none","opacity":"1"}))
      new.set("d","M "+str(xsize-ysize)+","+str(ysize)+" L "+str(xsize)+",0 z")
      self.current_layer.append(new)
    if xsize<ysize:
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#00FF00","fill":"none","opacity":"1"}))
      new.set("d","M 0,"+str(xsize)+" L "+str(xsize)+","+str(xsize)+" z")
      self.current_layer.append(new)
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#00FF00","fill":"none","opacity":"1"}))
      new.set("d","M 0,0 L "+str(xsize)+","+str(xsize)+" z")
      self.current_layer.append(new)
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#00FF00","fill":"none","opacity":"1"}))
      new.set("d","M "+str(xsize)+",0 L 0,"+str(xsize)+" z")
      self.current_layer.append(new)
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#00FF00","fill":"none","opacity":"1"}))
      new.set("d","M 0,"+str(ysize-xsize)+" L "+str(xsize)+","+str(ysize-xsize)+" z")
      self.current_layer.append(new)
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#00FF00","fill":"none","opacity":"1"}))
      new.set("d","M 0,"+str(ysize-xsize)+" L "+str(xsize)+","+str(ysize)+" z")
      self.current_layer.append(new)
      new=inkex.etree.Element(inkex.addNS("path","svg"))
      new.set("style",simplestyle.formatStyle({"stroke":"#00FF00","fill":"none","opacity":"1"}))
      new.set("d","M "+str(xsize)+","+str(ysize-xsize)+" L 0,"+str(ysize)+" z")
      self.current_layer.append(new)

    #- golden ratio (red)
    new=inkex.etree.Element(inkex.addNS("path","svg"))
    new.set("style",simplestyle.formatStyle({"stroke":"#FF0000","fill":"none","opacity":"1"}))
    new.set("d","M "+str(xsize*0.381966011)+",0 L "+str(xsize*0.381966011)+","+str(ysize)+" z")
    self.current_layer.append(new)
    new=inkex.etree.Element(inkex.addNS("path","svg"))
    new.set("style",simplestyle.formatStyle({"stroke":"#FF0000","fill":"none","opacity":"1"}))
    new.set("d","M "+str(xsize*0.618033989)+",0 L "+str(xsize*0.618033989)+","+str(ysize)+" z")
    self.current_layer.append(new)
    new=inkex.etree.Element(inkex.addNS("path","svg"))
    new.set("style",simplestyle.formatStyle({"stroke":"#FF0000","fill":"none","opacity":"1"}))
    new.set("d","M 0,"+str(ysize*0.381966011)+" L "+str(xsize)+","+str(ysize*0.381966011)+" z")
    self.current_layer.append(new)
    new=inkex.etree.Element(inkex.addNS("path","svg"))
    new.set("style",simplestyle.formatStyle({"stroke":"#FF0000","fill":"none","opacity":"1"}))
    new.set("d","M 0,"+str(ysize*0.618033989)+" L "+str(xsize)+","+str(ysize*0.618033989)+" z")
    self.current_layer.append(new)

if __name__=='__main__':e=anotherclass();e.affect()


compositiongrid.inx

Code: Select all

<inkscape-extension>
  <_name>Composition Grid</_name>
  <id>org.ekips.filter.compositiongrid</id>
  <dependency type="executable" location="extensions">compositiongrid.py</dependency>
  <dependency type="executable" location="extensions">inkex.py</dependency>
  <effect><object-type>all</object-type><effects-menu><submenu _name="Render"/></effects-menu></effect>
  <script><command reldir="extensions" interpreter="python">compositiongrid.py</command></script>
</inkscape-extension>

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: inkex.py - bug on custom size, at document properties?

Postby brynn » Thu Oct 04, 2012 2:54 am

I can't entirely and definitively answer your question. But I know of one bug involving canvas size in Doc Prop. It has something to do with transform attribute when document size or orientation is changed (including 'Resize page to content'). Evidence of this includes objects ending up in entirely the wrong place.

I can't understand or advise you about the code. But if the info about the transform attribute doesn't help, or isn't the problem at all, I'm sure someone else will reply :D

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: inkex.py - bug on custom size, at document properties?

Postby ~suv » Thu Oct 04, 2012 10:22 am

nitrofurano wrote:(…), but i think there is a bug when we run it on a canvas size changed at Inkscape document properties? is that a bug on inkex.py, or should we bypass this in another way?
Your extension AFAICT does not consider any transforms on parent containers (layers are groups, groups in SVG can have explicit 'transform' attributes affecting the positions of all contained objects. 'transform' attributes on objects and containers like <g> (groups) is a basic SVG feature not a bug. Nor is it a bug in itself that Inkscape transforms the top-level layer(s) if the page height, page orientation or both is changed, to compensate the differences due to having to keep two coordinate systems in sync (GUI coordinate system with origin in lower left corner of page, y pointing upwards, SVG coordinate system with origin in upper left corner of page, y pointing downwards). If you want your grid lines to positioned 'absolutely' on the page, you have to consider / compensate parent transforms…

Simple test: apply your extension while you are inside a rotated group [1]: the grid is inserted relative to the viewport of the group, which then gets rotated - this is probably not your intention (it could well be though - depends on the goal and use cases of the extension).

Several extensions have been affected by similar problems (most known with 'Perspective' and 'Envelope', and also with DXF export) - these have been "fixed" in the mean time (some maybe only in the development version), to handle transforms on parent containers as expected (or as needed).

[1] sample SVG file:
  1. draw a square, snap it to the top-left corner of the page
  2. duplicate 3x and position the duplicated rects at the other corners of the page
  3. select all, group
  4. rotate the group
  5. enter the group (with double-click, or 'Ctrl+Enter')
  6. apply 'Extensions > Render > Composition Grid
-> the grid lines are created relative to the viewport of the rotated group.


Return to “Programming”