Parsing XML using xml.dom in hython

   5081   2   1
User Avatar
Member
30 posts
Joined: July 2005
Offline
Okeydokey,

I'm parsing an xml file using the xml.dom.minidom module. Standalone in Python from a shell, and I get what I expect on output. When I put the script on a shelf in Houdini, I get the following failure popping up.

==========================================

File “Read_XML_Roads”, line 99, in <module>
File “Read_XML_Roads”, line 16, in handleGameData
File “Read_XML_Roads”, line 22, in handleRoadNetwork
File “Read_XML_Roads”, line 27, in handleElements
File “Read_XML_Roads”, line 63, in handleElement
File “CPROGRA~1/SIDEEF~1/HOUDIN~1.303/houdini/scripts/python\hou.py”, line 16269, in write
return _hou.ShellIO_write(*args)
TypeError: in method ‘ShellIO_write’, argument 2 of type ‘std::string const &’

==========================================

something is being changed within Houdini - the type of the data? Does this ring a bell?

Here's the code:

==========================================

import xml.dom.minidom

#dom = xml.dom.minidom.parseString(“./Python/DemoSlideShow.xml”)
#dom = xml.dom.minidom.parse(“/home/caleb/Work/Traffic/Python/L10R_NFS10_World_RoadNet.xml”)
dom = xml.dom.minidom.parse(“CDocuments and Settings/choward/My Documents/Work/Traffic/Python/test.xml”)

def getText(nodelist):
print “getText(%s)” % nodelist
rc = “”
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
print rc
return rc

def handleGameData(gamedata):
print “<GameData>”
handleRoadNetwork(gamedata)
print “</GameData>”

def handleRoadNetwork(gamedata):
print “ <RoadNetwork>”
elements = gamedata.getElementsByTagName(“Element”)
handleElements(elements)
print “ </RoadNetwork>”

def handleElements(elements):
for element in elements:
handleElement(element)

def handleElement(element):
#elementName = “foo”
elementName = element.getAttribute('name')
#print elementName
roadname = element.getElementsByTagName(“RoadName”)
type = element.getElementsByTagName(“Type”)
if type:
typeText = getText(type.childNodes)
else:
typeText = “”
startx = element.getElementsByTagName(“StartX”)
starty = element.getElementsByTagName(“StartY”)
startz = element.getElementsByTagName(“StartZ”)
startcontrolx = element.getElementsByTagName(“StartControlX”)
startcontroly = element.getElementsByTagName(“StartControlY”)
startcontrolz = element.getElementsByTagName(“StartControlZ”)
StartProfile = element.getElementsByTagName(“StartProfile”)

if not StartProfile:
startprofile = “none”
else:
startprofile = StartProfile

endx = element.getElementsByTagName(“EndX”)
endy = element.getElementsByTagName(“EndY”)
endz = element.getElementsByTagName(“EndZ”)
endcontrolx = element.getElementsByTagName(“EndControlX”)
endcontroly = element.getElementsByTagName(“EndControlY”)
endcontrolz = element.getElementsByTagName(“EndControlZ”)
EndProfile = element.getElementsByTagName(“EndProfile”)

if not EndProfile:
endprofile = “none”
else:
endprofile = EndProfile

print “ <Element name=\”%s\“>” % elementName
print “ <RoadName>%s</RoadName>” % getText(roadname.childNodes)
print “ <Type>%s</Type>” % typeText
print “ <StartX>%s</StartX>” % getText(startx.childNodes)
print “ <StartY>%s</StartY>” % getText(starty.childNodes)
print “ <StartZ>%s</StartZ>” % getText(startz.childNodes)
print “ <StartControlX>%s</StartControlX>” % getText(startcontrolx.childNodes)
print “ <StartControlY>%s</StartControlY>” % getText(startcontroly.childNodes)
print “ <StartControlZ>%s</StartControlZ>” % getText(startcontrolz.childNodes)
# handleStartProfile(startprofile)
print “********* %s” % startprofile
print “ <EndX>%s</EndX>” % getText(endx.childNodes)
print “ <EndY>%s</EndY>” % getText(endy.childNodes)
print “ <EndZ>%s</EndZ>” % getText(endz.childNodes)
print “ <EndControlX>%s</EndControlX>” % getText(endcontrolx.childNodes)
print “ <EndControlY>%s</EndControlY>” % getText(endcontroly.childNodes)
print “ <EndControlZ>%s</EndControlZ>” % getText(endcontrolz.childNodes)
# handleEndProfile(endprofile)
print “********* %s” % endprofile
print “ <Element>”

def handleStartProfile(startprofile):
print “ <StartProfile>”
handleLanes(startprofile.getElementsByTagName(“Type”))
print “ </StartProfile>”

def handleEndProfile(endprofile):
print “ <EndProfile>”
handleLanes(endprofile.getElementsByTagName(“Type”))
print “ </EndProfile>”

def handleLanes(lanes):
for lane in lanes:
handleLane(lane)

def handleLane(lane):
print “ <LType>%s</Type>” % getText(lane.childNodes)

handleGameData(dom)


======================================

(I notice the post has screwed my formatting. Sorry)

Help help!

Thanks!

-caleb
My avatar was rendered from PRISMS
User Avatar
Member
268 posts
Joined: July 2005
Offline
Run into similar issue but in a python sop. I'm guessing that when the code is run from shelf/node houdini has no way of knowing where to display the output. For now I'm redirecting output to a file

outfile = open('c:\temp\out.txt','w')
print >>outfile,“blah blah %s”%(var)

The strange thing is that print statements outside any method/class definitions work and output to the docked python shell?? Is there a way to redirect object print calls to the python shell as well?
User Avatar
Member
1908 posts
Joined: Nov. 2006
Offline
I've written a fairly large amount of Python code in Houdini over the years and definitely had some issues with printing outputs from withing certain areas but I don't think I've encountered anything exactly like this.

Some general observations about outputting text from Python in Houdini.

When you tell Python to print it of course attempts to print to stdout, whatever that might be set as. In Python, you can take a look at what stdout, stdin, and stderr are by importing the ‘sys’ module and inspecting the corresponding member. When Houdini is open and there is a Python Shell available, sys.stdout should be pointing to an instance of a hou.ShellIO object. This is what allows Houdini to output to the Python shell inside the application. On linux, if I launch my Houdini from the shell and either have no Python Shell open in the program or close it, sys.stdout should then be something like “<open file ‘<stdout>’, mode ‘w’ at 0x7f7ac4f4d150>”, signifying that it is outputting to the normal stdout, aka the console. If I close my console as well then all bets are off and it most likely won't print to anything though I don't think it should be giving an error. I can't remember exactly how it works on Windows but I'm pretty sure it would just pop up the console thing if no normal output was available.

That being said, nothing is ever perfect and if you are having problems there are usually workarounds. If you are going to be printing some sort of output and you want to ensure that Houdini has a Python Shell window open somewhere, before you need to do all your output you could do something like this:
if not isinstance(sys.stdout, hou.ShellIO):
hou.ui.curDesktop().createFloatingPanel(hou.paneTabType.PythonShell)
hou.hscript(“updateui”)
This checks to see what stdout is pointing to and if it isn't a Houdini Python Shell then it will create a floating one and update the interface so that when any following print statements are executed they print to a shell.

This is of course a particular solution to a particular problem so it may not be idea or even work around the problems you guys are having. If there are any simplified example files you could post it would be most appreciated.
Graham Thompson, Technical Artist @ Rockstar Games
  • Quick Links