Effect Extensions Trouble Writing Output

Discussion about writing code for Inkscape.
User avatar
LiquidAsh
Posts: 71
Joined: Fri Apr 22, 2011 11:35 pm
Contact:

Effect Extensions Trouble Writing Output

Postby LiquidAsh » Fri Nov 04, 2011 5:03 am

After getting a simple python Effect Extension running, I'm now trying to implement one written in C# (with a simple Console Application). So far, my extension is able to read the command-line parameters sent from inkscape (including the tmp-file's path), and then read the svg-data out of inkscape's tmp-file (this data doesn't seem to be coming in through stdin however, as some documentation indicates).

I'm running into trouble writing my modified svg back for inkscape to use. I've tried writing the svg to stdout (using Console.Write) and also back to the original tmp-file, but neither has any effect on what I see inside Inkscape.

I have a couple of suspicions so far, but am interested in any other ideas about what might be wrong here:

1) I haven't correctly linked inkscape to my program with the .inx file: I have a program element with no interpreter attribute (this element contains my executable's name). I've also tried declaring the interpreter as powershell, which didn't seem to help.

2) Inkscape is looking for some specific formatting on the output before it is accepted. Can't find whether there is any important header or trailing data to add, or if the svg should be different in any other way from what I'm reading in.

In case this helps, here's my .inx file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
   <_name>spPart</_name>
   <id>com.sugarpillstudios.spPart</id>
 
  <param name="number" type="int" min="1" max="1000" _gui-text="Number of copies:">2</param>
  <param name="horizontal" type="float" min="-10000.0" max="10000.0" _gui-text="X Offset:">0.0</param>
  <param name="vertical" type="float" min="-10000.0" max="10000.0" _gui-text="Y Offset:">0.0</param>

  <effect>
      <object-type>all</object-type>
      <effects-menu>
         <submenu _name="SugarPills"/>
      </effects-menu>
   </effect>
 
   <script>
      <command reldir="extensions" interpreter="powershell">./spPartInkExt.exe</command>
   </script>

</inkscape-extension>


and here's a log of what my program is reading and writing (it's just removing the rectangle at the end):

Code: Select all

ARGS:
--number=2
--horizontal=100
--vertical=0
C:\Users\Gary\AppData\Local\Temp\ink_ext_XXXXXX.svg7GKO4V

INPUT:
<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" sodipodi:docname="C:\Users\Gary\AppData\Local\Temp\ink_ext_XXXXXX.svgN29T4V" inkscape:version="0.48.1 " version="1.1" id="svg2" height="1052.3622047" width="744.09448819">
  <sodipodi:namedview id="base" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="0.35" inkscape:cx="375" inkscape:cy="520" inkscape:document-units="px" inkscape:current-layer="layer1" showgrid="false" inkscape:window-width="469" inkscape:window-height="485" inkscape:window-x="175" inkscape:window-y="175" inkscape:window-maximized="0" />
  <defs id="defs4" />
  <metadata id="metadata7">
    <rdf:RDF>
      <cc:Work rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g id="layer1" inkscape:groupmode="layer" inkscape:label="Layer 1">
    <rect y="289.50504" x="88.571426" height="105.71429" width="80" id="rect2985" style="fill:none;stroke:#000000;stroke-width:5;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:0.5, 0.5;stroke-dashoffset:0" />
    <path inkscape:transform-center-y="-14.319871" inkscape:transform-center-x="-1.0149688" d="m 451.42857,652.36216 -93.07083,-51.39441 -95.10075,47.53324 20.11852,-104.39736 -74.59455,-75.75762 105.50476,-13.1267 48.99879,-94.35403 45.087,96.28462 104.87747,17.44362 -77.63946,72.63387 z" inkscape:randomized="0" inkscape:rounded="0" inkscape:flatsided="false" sodipodi:arg2="1.5913129" sodipodi:arg1="0.96299433" sodipodi:r2="80.051003" sodipodi:r1="160.10201" sodipodi:cy="520.93359" sodipodi:cx="360" sodipodi:sides="5" id="path2987" style="fill:none;stroke:#000000;stroke-width:5;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:0.5, 0.5;stroke-dashoffset:0" sodipodi:type="star" transform="translate(97.142857,-8.5714286)" />
  </g>
</svg>

OUTPUT:
<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" sodipodi:docname="C:\Users\Gary\AppData\Local\Temp\ink_ext_XXXXXX.svgN29T4V" inkscape:version="0.48.1 " version="1.1" id="svg2" height="1052.3622047" width="744.09448819">
  <sodipodi:namedview id="base" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="0.35" inkscape:cx="375" inkscape:cy="520" inkscape:document-units="px" inkscape:current-layer="layer1" showgrid="false" inkscape:window-width="469" inkscape:window-height="485" inkscape:window-x="175" inkscape:window-y="175" inkscape:window-maximized="0" />
  <defs id="defs4" />
  <metadata id="metadata7">
    <rdf:RDF>
      <cc:Work rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g id="layer1" inkscape:groupmode="layer" inkscape:label="Layer 1">
    <path inkscape:transform-center-y="-14.319871" inkscape:transform-center-x="-1.0149688" d="m 451.42857,652.36216 -93.07083,-51.39441 -95.10075,47.53324 20.11852,-104.39736 -74.59455,-75.75762 105.50476,-13.1267 48.99879,-94.35403 45.087,96.28462 104.87747,17.44362 -77.63946,72.63387 z" inkscape:randomized="0" inkscape:rounded="0" inkscape:flatsided="false" sodipodi:arg2="1.5913129" sodipodi:arg1="0.96299433" sodipodi:r2="80.051003" sodipodi:r1="160.10201" sodipodi:cy="520.93359" sodipodi:cx="360" sodipodi:sides="5" id="path2987" style="fill:none;stroke:#000000;stroke-width:5;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:0.5, 0.5;stroke-dashoffset:0" sodipodi:type="star" transform="translate(97.142857,-8.5714286)" />
  </g>
</svg>


Let me know, if you have any ideas or suggestion for how I might fix or further inspect what is happening here. Thanks in advance.

User avatar
LiquidAsh
Posts: 71
Joined: Fri Apr 22, 2011 11:35 pm
Contact:

Re: Effect Extensions Trouble Writing Output

Postby LiquidAsh » Fri Nov 04, 2011 12:14 pm

Still working on this... Quick update: I just tried trapping the output of a working python extension in a file and then having my C# application push those same characters to stdout. Still no effect on what is in inkscape, but I'm less suspicious of the output's formatting. One other difference I've noticed between the working python and non-working c# extensions is that the c# console application brings up a window that displays the running application, and invoking the python interpreter doesn't. Thinking the interpreter flag has something more to do with how stdin and stdout are being directed(?).

User avatar
LiquidAsh
Posts: 71
Joined: Fri Apr 22, 2011 11:35 pm
Contact:

Re: Effect Extensions Trouble Writing Output

Postby LiquidAsh » Sat Nov 05, 2011 1:36 am

Still curious about where my console application was getting held up before... but I seem to have found a reasonable work around. Although it really seems like there should be a way to make this work without the "around". After poking through inkscape's source code for a bit I ran into a couple of places to pursue investigating this:
- It looks like src/extension/implementation/script.cpp's Script::execute() uses Glib::spawn_async_with_pipes(...) to run the extension and hook it's stdout and stderr... I started testing this, but didn't very far before changing directions.
- There are some places that interpreter language values are limited: like src/extension/script/InkscapeScript.cpp. I'm still not sure how/where these would prevent a standalone console application's output from being directed back to inkscape.

SOLUTION:
My temporary solution (until the extension system works with standalone console apps) is to have a python extension that runs my csharp application, and directs it's stdout back to inkscape. It seems to be working, so I may try to generalize it a bit and find a place to upload. I've just picked up enough knowledge of python to get this working, so I'll be interested in hearing if there are better ways of doing this as well (even though it's not much code).

Anyone interested in an extension like this? Or a tutorial about how to get an extension written in any language working with inkscape?

patrischer
Posts: 2
Joined: Sat Nov 05, 2011 3:20 am

Re: Effect Extensions Trouble Writing Output

Postby patrischer » Sat Nov 05, 2011 4:52 am

I try the same things with VB.NET which you describe at the beginning and make the same experiences. But I can't access read/write the temporary file from inkscape. I get an IO.Exception because the file is already used from another process. So I think inkscape opens the file exclusive. I use the following code.
Dim DocXML As Xml.Linq.XDocument = Xml.Linq.XDocument.Load(TmpSvgFile, LoadOptions.PreserveWhitespace)
To resolve this I make a copy of the temporary file before reading.

1.) Okay obviously "write back" helps nothing but nevertheless which classes did you use to read the temp file directly and which classes to write back ?
2.) I'am very interested in your python extension or an tutorial because at time I couldn't find anything in the web. It seems that nobody use .NET to write inkscape extensions ?

User avatar
LiquidAsh
Posts: 71
Joined: Fri Apr 22, 2011 11:35 pm
Contact:

Re: Effect Extensions Trouble Writing Output

Postby LiquidAsh » Sat Nov 05, 2011 2:00 pm

Hi Patrischer, I ran into the same problem. Here's the C# code that correctly opens the temporary file with shared read and write (strangely shared read will not work, it must be shared read and write since that's how it is opened/created by inkscape).

using (FileStream fs = File.Open(args[args.Length - 1], FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
element = XElement.Load(fs);

I'll try to put a little tutorial together by the beginning of next week. Thanks for sharing your interest in this topic.

patrischer
Posts: 2
Joined: Sat Nov 05, 2011 3:20 am

Re: Effect Extensions Trouble Writing Output

Postby patrischer » Wed Nov 09, 2011 3:51 am

Hi LiquidAsh

Reading/Writing works fine in this way.
Also your Python EXE-Launcher which you describe here: http://sugarpillstudios.com/wp/?p=142

Many thanks for your support.


Return to “Programming”