Date: Mon, 16 Jan 2012 10:24:00 +0100 From: Jasper van de Gronde <th.v.d.gronde@...528...> Subject: Re: [Inkscape-devel] Writing an extension To: inkscape-devel@lists.sourceforge.net
On 13-01-12 19:10, Trever L. Adams wrote:
This relates to https://bugs.launchpad.net/inkscape/+bug/247463 and comment 37 in particular.
I would like to be able to attach information to objects that are needed for high quality digitization. This includes stitch length and stitch direction. I am thinking of displaying the direction as gradient, although direction may be very complex. I am just learning about SVG so, I do not know if there are smart ways of doing this. (For example, think of a leaf, where you may have 20 different direction changes.) I also do not know XML very well (creating schema, etc).
So basically you want to embed information needed to convert a drawing to something that can be embroidered? And you want this information to be visible and manipulable in Inkscape's interface?
Yes, I am starting from information I have found here: http://jumpnslide.tumblr.com/post/2557920924/how-to-use-inkscape-as-a-game-d...
I am missing just a few pieces of information:
1) How do I set values in the interface (fetch them from the SVG)? I have defaults working, etc. 2) How do I insert my custom attributes back into the SVG?
The following partial code block doesn't do #2 properly and actually crashes inkscape:
#Loop through all the selected items in Inkscape for id, node in self.selected.iteritems():
#Check if the node is a Group ( "svg:g" tag in XML ) if node.tag == inkex.addNS('g','svg'): #DO SOME THINGS TO GROUPS outputBlock += "Found a Group!" + "\n" children = list(node) for node2 in children: outputBlock += "Group " + str(node) + " contains: " + str(node2.attrib["id"]) + "\n" node2.attrib["max_stitch_length"] = str(max_stitch_length)
I am sure I am going to have to create an XML schema right? If so can you point me in the right direction?
So, is there a standard way to extend SVG or embed such codes in SVG for a given object?
Extending SVG is in principle very easy, just define a namespace that hasn't been taken yet (and preferably as specific as possible) and use elements/attributes from that namespace. Ideally you would also create a schema to define which elements/attributes are in that namespace and so on, but I wouldn't necessarily start out that way (although it's obviously a good idea to think about a formal definition if you want this to be used by others as well, and/or save yourself some trouble in the future).
The idea is that viewers/editors will ignore anything they don't recognize. The extent to which this is true in practice varies a bit, and it's definitely not a given that an editor (Inkscape) will preserve the tags, but in principle it should "work" (in the sense that nothing will suddenly go wrong).
Can you provide me with any urls which show an example of how to create an XML schema? In the past when I have looked, I just walk away with a headache.
I would like to do this in SVG and Inkscape since I believe both are scriptable and both can be used for print as well. (This is a project for both print and embroidery.)
Inkscape itself is not really scriptable. What you can do is write a python extension that essentially gets a piece of SVG to transform. But this is fairly flexible. Also, if you know C/C++ you might be able to write something like an LPE to help out.
LPE? I do know several languages, including C/C++.
I thought Inkscape was. I would like to be able to change colors (and maybe remove them in some cases), resize and place objects and save/load files from scripts.
Additional request for information: Is it possible to define a custom color table? Embroidery machines usually have a fixed color table (which can be converted to various thread vendor product/color numbers).
Not sure, but I think there is at least some support for color management. Perhaps someone else knows more about this?
I think the palettes was what I was looking for and have since learned how to do this.
Is it possible to turn an outline on for an object and give it a different color than the object, or easily create an outline?
You mean outline as in stroke? If so, that's trivial in SVG, every object intrinsically has a fill and a stroke.
Yes, I finally figured this out. As I said, new to inkscape and SVG.
Finally, is there a way to do "Ungroup repeatedly until there are no groups left, and convert objects to paths." as part of an extension (http://www.jonh.net/~jonh/inkscape-embroidery/) without modifying the original (as part of an export, etc.)?
I think it's possible to create extensions for output as well, so conceivably this would be possible. In any case, you might want to have a look at this extension:
https://gitorious.org/various-small-stuff/inkscape-ungroup-deep/
I am starting to wonder if I actually need this, but on having looked at that code, I was able to at least start working on groups with my code.
Thank you, Trever
On 17-01-12 19:58, Trever L. Adams wrote:
... So basically you want to embed information needed to convert a drawing to something that can be embroidered? And you want this information to be visible and manipulable in Inkscape's interface?
Yes, I am starting from information I have found here: http://jumpnslide.tumblr.com/post/2557920924/how-to-use-inkscape-as-a-game-d...
I am missing just a few pieces of information:
- How do I set values in the interface (fetch them from the SVG)? I
have defaults working, etc. 2) How do I insert my custom attributes back into the SVG?
Depending on what exactly you mean, these are not exactly "just a few pieces of information" :) My best guess is that you are talking about the dialog generated based on the INX file. If so, I don't think you can fetch information from SVG there. If you haven't yet, it might be good to have a look here: http://wiki.inkscape.org/wiki/index.php/Script_extensions
The following partial code block doesn't do #2 properly and actually crashes inkscape:
#Loop through all the selected items in Inkscape for id, node in self.selected.iteritems(): #Check if the node is a Group ( "svg:g" tag in XML ) if node.tag == inkex.addNS('g','svg'): #DO SOME THINGS TO GROUPS outputBlock += "Found a Group!" + "\n" children = list(node) for node2 in children: outputBlock += "Group " + str(node) + " contains: "
- str(node2.attrib["id"]) + "\n" node2.attrib["max_stitch_length"] =
str(max_stitch_length)
Slightly more information would help, do you get any error message? (If so, what.) Have you tried commenting out parts of this code block to see where the problem lies? If so, what were the results? (For example, I would definitely try commenting out the last line.) But I never wrote an extension myself, so perhaps someone else will spot something obviously wrong with the above code.
I am sure I am going to have to create an XML schema right? If so can you point me in the right direction?
Please, forget about schemas. Creating a schema will not magically make things work, it is essentially just a description of what your XML is allowed to look like. Actually, I don't think any part of Inkscape even bothers with schemas.
... Can you provide me with any urls which show an example of how to create an XML schema? In the past when I have looked, I just walk away with a headache.
Keep walking :) Seriously, at this point, you really don't need them.
Inkscape itself is not really scriptable. What you can do is write a python extension that essentially gets a piece of SVG to transform. But this is fairly flexible. Also, if you know C/C++ you might be able to write something like an LPE to help out.
LPE? I do know several languages, including C/C++.
Live Path Effect (there is some information on the wiki if you're completely unfamiliar with them).
I thought Inkscape was. I would like to be able to change colors (and maybe remove them in some cases), resize and place objects and save/load files from scripts.
You can do a lot from scripts, but keep in mind that Inkscape extensions are separate programs that are essentially just given some SVG to work with and can return another bit of SVG.
It looks like you need to look at DBUS work to interactively do what you want. I.e examine state in inkscape and then do something with it. This was linux-only until very recently but now there is a dbus windows release. It needs testing. http://www.freedesktop.org/wiki/Software/dbus
I hope to see it more fully integrated eventually. Alas I can't work on this directly myself. Check out dbus in the inkscape archives for examples. Mark...
On 1/18/2012 10:55 PM, Jasper van de Gronde wrote:
On 17-01-12 19:58, Trever L. Adams wrote:
... So basically you want to embed information needed to convert a drawing to something that can be embroidered? And you want this information to be visible and manipulable in Inkscape's interface?
Yes, I am starting from information I have found here: http://jumpnslide.tumblr.com/post/2557920924/how-to-use-inkscape-as-a-game-d...
I am missing just a few pieces of information:
- How do I set values in the interface (fetch them from the SVG)? I
have defaults working, etc. 2) How do I insert my custom attributes back into the SVG?
Depending on what exactly you mean, these are not exactly "just a few pieces of information" :) My best guess is that you are talking about the dialog generated based on the INX file. If so, I don't think you can fetch information from SVG there. If you haven't yet, it might be good to have a look here: http://wiki.inkscape.org/wiki/index.php/Script_extensions
The following partial code block doesn't do #2 properly and actually crashes inkscape:
#Loop through all the selected items in Inkscape for id, node in self.selected.iteritems(): #Check if the node is a Group ( "svg:g" tag in XML ) if node.tag == inkex.addNS('g','svg'): #DO SOME THINGS TO GROUPS outputBlock += "Found a Group!" + "\n" children = list(node) for node2 in children: outputBlock += "Group " + str(node) + " contains: "
- str(node2.attrib["id"]) + "\n" node2.attrib["max_stitch_length"] =
str(max_stitch_length)
Slightly more information would help, do you get any error message? (If so, what.) Have you tried commenting out parts of this code block to see where the problem lies? If so, what were the results? (For example, I would definitely try commenting out the last line.) But I never wrote an extension myself, so perhaps someone else will spot something obviously wrong with the above code.
I am sure I am going to have to create an XML schema right? If so can you point me in the right direction?
Please, forget about schemas. Creating a schema will not magically make things work, it is essentially just a description of what your XML is allowed to look like. Actually, I don't think any part of Inkscape even bothers with schemas.
... Can you provide me with any urls which show an example of how to create an XML schema? In the past when I have looked, I just walk away with a headache.
Keep walking :) Seriously, at this point, you really don't need them.
Inkscape itself is not really scriptable. What you can do is write a python extension that essentially gets a piece of SVG to transform. But this is fairly flexible. Also, if you know C/C++ you might be able to write something like an LPE to help out.
LPE? I do know several languages, including C/C++.
Live Path Effect (there is some information on the wiki if you're completely unfamiliar with them).
I thought Inkscape was. I would like to be able to change colors (and maybe remove them in some cases), resize and place objects and save/load files from scripts.
You can do a lot from scripts, but keep in mind that Inkscape extensions are separate programs that are essentially just given some SVG to work with and can return another bit of SVG.
Keep Your Developer Skills Current with LearnDevNow! The most comprehensive online learning library for Microsoft developers is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, Metro Style Apps, more. Free future releases when you subscribe now! http://p.sf.net/sfu/learndevnow-d2d _______________________________________________ Inkscape-devel mailing list Inkscape-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/inkscape-devel
You could use Xerxes-C for C++ http://xerces.apache.org/xerces-c/.
For Python, you could use http://docs.python.org/library/xml.dom.minidom.html or http://lxml.de/
SVG data is stored as elements in an XML document, so you don't need inkex to read, update, or even create the SVG/XML file. XML tools will let you parse the file in, search for the tags & values you want, and update them.
- Susan
participants (4)
-
Jasper van de Gronde
-
Mark Schafer
-
Susan Spencer
-
Trever L. Adams