What module to use to parse the xml data in the Tools.shelf

   3501   2   1
User Avatar
Member
795 posts
Joined: April 2020
Offline
Hello all,

I am trying to change some HDA definitions through HOM. Once I get to the Tools.shelf section, I need to start parsing xml. Unfortunately I am not having much success using ElementTree. When I just get the section, convert it to a tree and back I loose information that makes the section unusable for houdini:

if s == “Tools.shelf”:

print definition.sections().contents()

# convert the tools section to an xml tree
import elementtree.ElementTree

tree = elementtree.ElementTree.XML(definition.sections().contents())


# write the new element tree to the section
definition.sections().setContents(elementtree.ElementTree.tostring(tree))
print “—new—”
print definition.sections().contents()

outputs:

<?xml version=“1.0” encoding=“UTF-8”?>
<shelfDocument>
<!– This file contains definitions of shelves, toolbars, and tools.
It should not be hand-edited when it is being used by the application.
Note, that two definitions of the same element are not allowed in
a single file. –>

<tool name=“$HDA_TABLE_$HDA_NAME” label=“$HDA_LABEL” icon=“SOP_subnet”>
<toolMenuContext name=“viewer”>
<contextNetType>SOP</contextNetType>
</toolMenuContext>
<toolMenuContext name=“pop_viewer”>
<contextNetType>SOP</contextNetType>
</toolMenuContext>
<toolMenuContext name=“network”>
<contextOpType>$HDA_TABLE/$HDA_NAME</contextOpType>
</toolMenuContext>
<toolSubmenu>Digital Assets</toolSubmenu>
<script scriptType=“python”><![CDATA[import soptoolutils

soptoolutils.genericTool(kwargs, ‘$HDA_NAME’)]]></script>
</tool>
</shelfDocument>

—new—
<shelfDocument>


<tool icon=“SOP_subnet” label=“$HDA_LABEL” name=“$HDA_TABLE_$HDA_NAME”>
<toolMenuContext name=“viewer”>
<contextNetType>SOP</contextNetType>
</toolMenuContext>
<toolMenuContext name=“pop_viewer”>
<contextNetType>SOP</contextNetType>
</toolMenuContext>
<toolMenuContext name=“network”>
<contextOpType>$HDA_TABLE/$HDA_NAME</contextOpType>
</toolMenuContext>
<toolSubmenu>Digital Assets</toolSubmenu>
<script scriptType=“python”>import soptoolutils

soptoolutils.genericTool(kwargs, ‘$HDA_NAME’)</script>
</tool>
</shelfDocument>

I have lost the xmlversion line and the warning.

Are there any tools in HOM to help me setting the tool options?

Any help is appreciated.

Koen
User Avatar
Member
1926 posts
Joined: Nov. 2006
Offline
Hey Koen,

I've had success dealing with xml in the past using the minidom module. I think it's xml.dom.minidom. I used that module for my project creator tool that I demoed in my Tool Building master class at Siggraph last year. That project uses/generates an xml file to store project settings and directory mappings. I don't see why it would have any problem dealing with a Houdini shelf definition. I've also been able to successfully parse .shelf files for some features of my website.

As for Houdini tools that can build tools, there was a discussion about ways to generate tools on the fly between Jason Iversen and I a while back on odforce but I can't seem to find the thread. From what I can recall you are able to generate tools, add them to shelves and whatnot by making use of the hou.Tool and maybe hou.Shelf* classes. Unfortunately I don't have H available right now to give you a specific answer but the above should point you in the right direction.

Cheers,

Graham
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
795 posts
Joined: April 2020
Offline
Thanks Graham, I will look at the minidom option.

I think I can get pretty far with what is there now. One thing that seem to be missing is the ability to get a hou.Tool from the Tools.shelf section of an hda, and also setting it. If that was there, I would not need to mess with xml at all.

(I want to change the tool script in existing hda definitions)

I will send an RFE for this.

Thanks again for the suggestion,

Koen
  • Quick Links