PlaneShift
Development => PlaneShift Mods => Topic started by: derula on July 11, 2009, 06:55:12 am
-
Hi.
I'm not quite sure where to post this (feel free to move it around if you like) or if there's any interest at all. But I think it's quite neat so here I am.
I've written a little utility in Ruby that can convert between SVG files and the XML format Planeshift uses to export sketches / user drawn maps.
The direction Sketch -> SVG is pretty trivial and lossless, however the other way round isn't. In fact my tool ignores mostly everything in the SVG file - except things Planeshift understands. And by "understands", I mean: can also be achieved through the in-game editor. I don't know if that's the same. Anyway, here an example:
(http://img33.imageshack.us/img33/8218/psmapsvg.png)
For those of you who know Ruby (I'ld think most of you do ;D), there's also the (fully documented) PlaneShiftSketchXML class which allows you to easily modify the XML files yourself. Example (from the documentation):
require 'planeshift_sketch_xml'
Dir.foreach('~/.PlaneShift/sketches') do |name|
sketch = PlaneShiftSketchXML.load_from(name)
sketch.add_text(sketch.width - 300, sketch.height - 50, 'signature')
sketch.save_to(name)
end
This would add a signature to all of your saved sketches... I know, somewhat limited example, just to show how simple it is.
Main drawback of converting SVG files to sketches is the 200 elements limit Planeshift sets on the imported sketches. On the other hand, if this limit wasn't there I wouldn't have released this, because who knows what noob would spam the server with Megabytes of XML...
To run the thing, you need to have Ruby 1.8 installed, either through your distribution's package system (Linux) or as a download package from here (http://www.ruby-lang.org/en/downloads). Simply open a console window, navigate to the folder you've extracted the zip file to, and type 'ruby svg2sketchxml.rb' or 'ruby sketchxml2svg.rb', without the quotes, appending the name of the source and dest files. For example:
ruby svg2sketchxml mymap.svg mymap.xml
You can then copy the mymap.xml file into the sketches subfolder of your PS settings directory and import it from within the game.
[Under Linux, you can alternatively run './<script>.rb' instead of 'ruby <script>.rb']
Feel free to report any bug, but please don't ask about how to get ruby running here or how to code in it... this is not the place for it.
I hope some of you find it useful. Here's the download: PlaneShift Sketch XML API, SVG converters and documentation (http://derula.uglyhorst.de/uploads/sonst/sketchxmlapi.zip)
Please read the text file in the icons subdirectory if you can't see any icons in SVG files converted from sketches. AFAIK, PS license forbids me to bundle those icons with the zip, thus I've written a description where to find them.
-
Some very nice work you've done here. *geloren is quite impressed*
Thumbs up, that's all i can say.
greetz
-
i agree quite a nice work keep it up :thumbup:
-
Thanks a lot to both of you... I'ld love to see any result if anyone actually uses it :)
(and I can't wait to continue playing... just one more week... ^.^)
-
i got this error while trying to convert a pic which was a bit complex (most probably using unsupported features). maybe it could be an idea to skip these unsupported statement?
SVG2SketchXML 0.1
Simple SVG path to PlaneShift Sketch XML converter
Copyright (c) 2009 by Simon Paridon
Converting /home/weltall/Desktop/disegno.svg...
./svg2sketchxml.rb:345:in `x': wrong number of arguments (0 for 1) (ArgumentError)
from ./svg2sketchxml.rb:345:in `parse_path'
from ./svg2sketchxml.rb:196:in `parse_group'
from /usr/lib/ruby/1.8/rexml/element.rb:892:in `each'
from /usr/lib/ruby/1.8/rexml/xpath.rb:53:in `each'
from /usr/lib/ruby/1.8/rexml/element.rb:892:in `each'
from /usr/lib/ruby/1.8/rexml/element.rb:393:in `each_element'
from ./svg2sketchxml.rb:195:in `parse_group'
from ./svg2sketchxml.rb:243:in `parse_group'
from /usr/lib/ruby/1.8/rexml/element.rb:892:in `each'
from /usr/lib/ruby/1.8/rexml/xpath.rb:53:in `each'
from /usr/lib/ruby/1.8/rexml/element.rb:892:in `each'
from /usr/lib/ruby/1.8/rexml/element.rb:393:in `each_element'
from ./svg2sketchxml.rb:241:in `parse_group'
from ./svg2sketchxml.rb:95:in `convert'
from ./svg2sketchxml.rb:66:in `convert'
from ./svg2sketchxml.rb:419
from ./svg2sketchxml.rb:9:in `each_pair'
from ./svg2sketchxml.rb:8:in `upto'
from ./svg2sketchxml.rb:8:in `each_pair'
from ./svg2sketchxml.rb:414
-
i got this error while trying to convert a pic which was a bit complex (most probably using unsupported features). maybe it could be an idea to skip these unsupported statement?
No, this was a bug I only discovered today... along with many others... I'm sorry, they were all pretty obvious :)
I've also added a more precise text conversion. It now uses the Cup and Talon font in converted SVG files (can either be viewed with Firefox or another browser that supports downloadable fonts, or if you install the Cup and Talon font into your system). I've added the font to the package, since it's from here (http://www222.pair.com/sjohn/fonts2.htm), and it's Freeware, so doesn't apply to the Planeshift license (please correct me if I'm wrong, and I'll remove it from the download package again).
The new version can be downloaded from the same source (here (http://derula.uglyhorst.de/uploads/sonst/sketchxmlapi.zip)).
Edit: Sorry, it had another stupid bug... I think I fixed it from here, but couldn't really test it. This should only apply to svg2sketchxml.
-
similar one
SketchXML2SVG 0.2
Simple PlaneShift Map XML to SVG converter
Copyright (c) 2009 by Simon Paridon
Converting /home/stefano/Desktop/disegno.svg...
./sketchxml2svg.rb:54: undefined method `width' for nil:NilClass (NoMethodError)
from ./sketchxml2svg.rb:9:in `each_pair'
from ./sketchxml2svg.rb:8:in `each_index'
from ./sketchxml2svg.rb:8:in `each_pair'
from ./sketchxml2svg.rb:48
-
similar one
Er... okay that's not my fault :) you tried to use XML2SVG on an svg file. Probably typo.
Yeah, I know, I should probably make it write an error in that case and not simply crash... Functionality first, error handling last lol. Proper error handling is a todo :)
When I try to use the svg2sketchxml I get this:
This is what you get for not actually testing the darned thing. I'm sorry, I've tested everything first, then added some features and just assumed they would work as well... I'll take a look at this on sunday, probably.
-
similar one
Er... okay that's not my fault :) you tried to use XML2SVG on an svg file. Probably typo.
Yeah, I know, I should probably make it write an error in that case and not simply crash... Functionality first, error handling last lol. Proper error handling is a todo :)
When I try to use the svg2sketchxml I get this:
This is what you get for not actually testing the darned thing. I'm sorry, I've tested everything first, then added some features and just assumed they would work as well... I'll take a look at this on sunday, probably.
ops :sweatdrop: sorry eheh
my result: http://img195.imageshack.us/img195/3751/schermata41.png really nice indeed :) (this was without limits)
-
my result: http://img195.imageshack.us/img195/3751/schermata41.png really nice indeed :) (this was without limits)
Nice ;) though the lines look a bit weird in PS. And I think some lines are missing, no?
@Llyn: I have a quick fix four your error: open svg2sketchxml.rb in a text editor, navigate to line 152, and replace the following:
node.attributes['style'].match STROKE or node.attributes['style'].match FILL
with
style = (node.attributes['style'] or '')
style.match STROKE or style.match FILL
(that's not a really good fix... hell it's stupid, it is very specific to your error. But it will fix it until I make a better solution. This might include using a proper css parser... but I'm not sure yet.
-
yes it seems some lines are missing i don't know why
-
yes it seems some lines are missing i don't know why
Most likely you've exceeded the 200 elements limit of Planeshift, I'ld assume... did the script show a warning that it did? It might help if you use Inkscape to simplify the paths before converting (might also reduce some of the "scratchy" look of some lines)
Edit: I've opluaded a new version (0.3). News:
- Better reading of color values (now respects svg stroke and fill attributes, parses color values more like in the specification; uses color inheritage where applicable, doesn't write objects with no color at all, .... Still no complete css support though.)
- Error message when trying to convert an invalid file
- Added command line parameter to svg2sketchxml: -cf . This will use the fill color as a stroke color if stroke color is set to none. This was the default in earlier versions, but I figured it's not always wise to do so.
-
Hi Derula,
I'd love to be able to use your converter, but so far have not been able to get it to work successfully. The only program I have to produce vector files with is Adobe Illustrator. I've attempted saving very extremely basic files from this program in svg format to test convert such as only 1 simple oval or line but I continually get the following result. I'm using Mac OS 10.5.8 and MacRuby 4.
Converting 1.svg...
svg2sketchxml.rb:480:in `parse_path': undefined method `x' for nil:NilClass (NoMethodError)
If you have any idea about what to try next, I'd appreciate it. Thanks.
-
Hi Dannae :D
Okay, the problem here is probably that I was ever only able to test it with svg files created by Inkscape.
If you have any idea about what to try next, I'd appreciate it. Thanks.
The first idea would be to call the converter with the -v switch (ruby svg2sketchxml.rb -v 1.svg 1.xml) and tell me what it says. That might already help.
If it doesn't, there's two other ideas: either you get Inkscape (http://inkscape.org/) (there's a mac version) and try it with that... or you could somehow send me your SVG file and I'll have a look at why it does that (either through ICQ/MSN per my profile, or via e-mail (PN me and you'll get my e-mail address))...
Also: welcome to the forum :P
-
Hi, thanks for SVG file and output, helped a lot. I've fixed that particular bug, and added support for the <line> element that was in that svg file. New version still downloadable from here (http://derula.uglyhorst.de/uploads/sonst/sketchxmlapi.zip).
-
Thanks Derula,
I gave it a try with your suggestion, but unfortunately still didn't get a conversion.
Here are the rresults:
Macintosh:sketchxmlapi Hobbes$ ruby svg2sketchxml.rb -v 1.svg 1.xml
./planeshift_sketch_xml.rb:143: warning: parenthesize argument(s) for future version
SVG2SketchXML 0.3
Simple SVG path to PlaneShift Sketch XML converter
Copyright (c) 2009 by Simon Paridon
Converting 1.svg...
Opening SVG...
Converting SVG...
Processing group 'Layer_1'...
Converting paths...
Converting path 'M74.1,180C206.8,98.7,72.5-71.1,183.3,33.7c0,0,67.5-77.1,112,30.7
c44.5,107.8-189.2,91.6-165.1,162c24.1,70.4,36.1,252.4-41,132.5C12.1,239-21.2,198.5,111.2,117.2' with color '0'...
Reading set of points from SVG...
Got Vector[74.1, 180.0, 1]. Deciding what to do with them...
Starting new subpath.
Reading set of points from SVG...
Got Vector[206.8, 98.7, 1]Vector[72.5, 183.3, 1]Vector[33.7, 0.0, 1]. Deciding what to do with them...
Applying transformation matrix to points...
Adding cubic curve from Vector[74.1, 180.0, 1] to Vector[33.7, 0.0, 1].
Reading set of points from SVG...
Got Vector[0.0, 0.0, 1]Vector[67.5, 112.0, 1]Vector[30.7, 0.0, 1]. Deciding what to do with them...
Applying transformation matrix to points...
Adding cubic curve from Vector[33.7, 0.0, 1] to Vector[64.4, 0.0, 1].
Reading set of points from SVG...
Got Vector[44.5, 107.8, 1]Vector[91.6, 162.0, 1]. Deciding what to do with them...
Applying transformation matrix to points...
Adding cubic curve from Vector[64.4, 0.0, 1] to .
svg2sketchxml.rb:480:in `parse_path': undefined method `x' for nil:NilClass (NoMethodError)
from svg2sketchxml.rb:289:in `parse_group'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rexml/element.rb:891:in `each'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rexml/xpath.rb:53:in `each'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rexml/element.rb:891:in `each'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rexml/element.rb:393:in `each_element'
from svg2sketchxml.rb:288:in `parse_group'
from svg2sketchxml.rb:103:in `convert'
from svg2sketchxml.rb:77:in `convert'
from svg2sketchxml.rb:530
from svg2sketchxml.rb:9:in `each_pair'
from svg2sketchxml.rb:8:in `upto'
from svg2sketchxml.rb:8:in `each_pair'
from svg2sketchxml.rb:525
I'll try the application you suggested next.
-
oops! javascript:void(0);
Sorry, I didn't see there was a page 2 until after posting and that you solved the problem.
Thanks so much Derula! Yay!!!
-
Thanks so much Derula! Yay!!!
You're welcome. Don't forget to show me the results :D
Edit: Fixed another bug where symmetric curves in the XML failed to convert.
-
Hi!
I run XP.
I went to http://www.ruby-lang.org/en/downloads and installed Ruby.
I went to http://inkscape.org/download/?lang=en and installed Inkscape
I made one line, three lines (a triangle) and a rectangle (with rectangle tool) in Inkscape.
In Inkscape I saved test4.svg
With Ruby I did
C:\Ruby\sketchxmlapi>ruby svg2sketchxml.rb test4.svg test4.xml
./planeshift_sketch_xml.rb:143: warning: parenthesize argument(s) for future ver
sion
SVG2SketchXML 0.3b
Simple SVG path to PlaneShift Sketch XML converter
Copyright (c) 2009 by Simon Paridon
Converting test4.svg...
Saving test4.xml...
Finished!
I loaded test4.xml to Planeshift map. But I only see the rectangle, not the triangle or the line. If I write only lines, I dont see them. My XML:
- <pages>
- <page l="24" w="744" h="1052" t="50">
<ln col="0" pts="326 98 326 98" />
<ln col="0" pts="103 87 254 87" />
<ln col="0" pts="103 87 103 195" />
<ln col="0" pts="254 87 254 195" />
<ln col="0" pts="103 195 254 195" />
</page>
</pages>
Any ideas?
-
Hi!
Hi! :)
I loaded test4.xml to Planeshift map. But I only see the rectangle, not the triangle or the line. If I write only lines, I dont see them. My XML:
- <pages>
- <page l="24" w="744" h="1052" t="50">
<ln col="0" pts="326 98 326 98" />
<ln col="0" pts="103 87 254 87" />
<ln col="0" pts="103 87 103 195" />
<ln col="0" pts="254 87 254 195" />
<ln col="0" pts="103 195 254 195" />
</page>
</pages>
Any ideas?
Huh, that's strange. The three latter lines in the XML are the rectangle. The first one:
<ln col="0" pts="326 98 326 98" />
is simple a line from one point (x=326, y=98) to the same, i.e. a dot.
It's probably a bug. Could you please send me the SVG somehow (e.g. by opening it in a text editor and copy-pasting it to Pastebin (http://pastebin.org/))? I'll have a look at it then.
(Note that the Stars/Polygons tool isn't supported yet, though I might be able to add it, but it's not really necessary for triangles... drawing one with the bezier tool should work though. If it's not bugged, which it just might be :P)
-
For this XML
<pages>
<page l='24' w='500' h='600' t='50'>
<ln col='0' pts='-71 -106 -71 -106'/>
<ln col='0' pts='43 137 191 137'/>
<ln col='0' pts='43 137 43 274'/>
<ln col='0' pts='191 137 191 274'/>
<ln col='0' pts='43 274 191 274'/>
</page>
</pages>
I used this SVG (in Pastebin):
http://pastebin.org/87039
And here SVG is in code tags:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<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"
width="500"
height="600"
id="svg2"
version="1.1"
inkscape:version="0.47 r22583"
sodipodi:docname="New document 1">
<defs
id="defs4">
<inkscape:perspective
sodipodi:type="inkscape:persp3d"
inkscape:vp_x="0 : 526.18109 : 1"
inkscape:vp_y="0 : 1000 : 0"
inkscape:vp_z="744.09448 : 526.18109 : 1"
inkscape:persp3d-origin="372.04724 : 350.78739 : 1"
id="perspective10" />
</defs>
<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="798"
inkscape:window-height="802"
inkscape:window-x="22"
inkscape:window-y="29"
inkscape:window-maximized="0" />
<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" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-452.36218)">
<rect
style="fill:#0000ff;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
id="rect2816"
width="148.57143"
height="137.14285"
x="42.857143"
y="137.14285"
transform="translate(0,452.36218)" />
<path
style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 217.14286,128.57143 2.85714,140 68.57143,-34.28572 -71.42857,-105.71428 z"
id="path2818"
transform="translate(0,452.36218)" />
<path
style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="M 345.71429,128.57143 340,254.28571"
id="path2820"
transform="translate(0,452.36218)" />
</g>
</svg>
-
Delete this post (cant find where I do it)...
-
Hi, i fixed your bugs (and I hope didn't add too many new ones!).
Well it was about relative paths. The SVG specification is so horribly imprecise. I just read it wrong.
On the way, I found another bug, which is ... pretty strange. I fixed it, but don't know why it works this way round and not the way I had it O.o
Anyway, this showed me much more that there are probably more bugs in there. Just tell me if you find another one!
You can download the new version from here (http://derula.uglyhorst.de/uploads/sonst/sketchxmlapi.zip).
Thanks for the find! :)
-
Hi, i fixed your bugs (and I hope didn't add too many new ones!).
Well it was about relative paths. The SVG specification is so horribly imprecise. I just read it wrong.
On the way, I found another bug, which is ... pretty strange. I fixed it, but don't know why it works this way round and not the way I had it O.o
Anyway, this showed me much more that there are probably more bugs in there. Just tell me if you find another one!
You can download the new version from here (http://derula.uglyhorst.de/uploads/sonst/sketchxmlapi.zip).
Thanks for the find! :)
:thumbup: Thank YOU for your fantastic work! :love:
I will try the new release when I get home this evening... Cant wait! \\o//
-
I will try the new release when I get home this evening... Cant wait! \\o//
Works great! Thanks again!
-
You're welcome!
If this helps you to bring some art into the world, I'd be happy to see it (ingame, or simply in this or another thread as a screen shot). Even if it's "just" maps :P
And yeah, don't hesitate to ask if you feel there's another bug, as there sure are still some left :)
-
Hey, I've just uploaded a new version (0.3d, hehe) which (fixes|adds) some smaller bugs (I hope it's "fixes").
Plus, it works in Ruby 1.9 now \รถ/ (at least the parts I've tested). Thanks to kaerli2 for reporting it didn't.
Download link (http://derula.uglyhorst.de/uploads/sonst/sketchxmlapi.zip) stays the same.
-
Hello,
I tried to convert a map I made with Inkscape 0.48.5 r10040 and received an error.
simplified inkscape file : https://www.dropbox.com/s/swdwkf237csj5kf/dome.svg?dl=0
error running program: https://www.dropbox.com/s/xcm61m6bpw79uvh/svg-sketches.txt?dl=0
Last I saw it worked with ruby 1.9, I do have a newer version of ruby installed and not sure how to downgrade to try it with 1.9 to see if that is the issue.
Thanks for looking
-
Hi feas,
it seems the file planeshift_sketch_xml is not loaded.
Change the first require line in svg2sketchxml.rb from
require('planeshift_sketch_xml')
to
require('./planeshift_sketch_xml')
With this it works, although your dome.svg reaches the limit of 200 elements. I guess you'll need to simplify the drawing. Happy sketches! :)
edit:
I have an old ruby 1.9.3p484, but I had your same error until I changed that line.
-
BoevenF Sir you are a rockstar! Thanks for the help!
-
Woah, I didn't know people still use this :o
I had trouble with the script on some SVG files, apparently there are things I'm not supporting. If you or anyone else needs further support or updates, you can always drop me a PM and I'll read it... eventually...
Cheers!
-
Update!
I fixed a bug that caused transformation matrices to be applied incorrectly if the SVG file used relative paths. Basically, that means that the converted file looked like a mess.
A few notes / reminders:
- If you want to put text in the SVG file to be used in the game, use font Cup and Talon and font size 19.5px / 15.6pt.
- The script only supports paths and some simple shapes, such as rectangles. Other shapes need to be converted to paths.
- In some cases - e.g., when converting a circle to a path - the SVG file might end up with "arc" type paths. The script ignores these. So if any parts of paths are missing, make sure that they're not precisely circle-y. (It may be enough to "convert" those paths by changing the type of their nodes without changing the path at all. This worked for me in Inkscape.)
Anyway, find the download links at the very bottom of the script's homepage (https://stuff.incertitu.de/planeshift/svg%20sketches).
-
I helped find this bug! Also, I can confirm this update works like a charm. GO USE THIS SCRIPT RIGHT MEOW.
-
I helped find this bug!
Yes you did :) thanks dude!
Also, I can confirm this update works like a charm.
I'm glad. That has been bugging me for a while...
GO USE THIS SCRIPT RIGHT MEOW.
Heh. Feel free to send me screenshots of any art my script helps you bring into the game :)
-
New update!
Feature: Altered the warning shown when using more than 200 elements to reflect new drawing skill
Bugfix: Fixed SVG straight vertical / horizontal lines with the v/h syntax
Additionally, when using the API, you can now get the needed skill level to import that sketch.
Download from here: https://stuff.incertitu.de/planeshift/svg%20sketches
Thanks Mishka for the heads up!