Wed, Nov 20, 11:25 AM CST

Renderosity Forums / Poser Python Scripting



Welcome to the Poser Python Scripting Forum

Forum Moderators: Staff

Poser Python Scripting F.A.Q (Last Updated: 2024 Sep 18 2:50 am)

We now have a ProPack Section in the Poser FreeStuff.
Check out the new Poser Python Wish List thread. If you have an idea for a script, jot it down and maybe someone can write it. If you're looking to write a script, check out this thread for useful suggestions.

Also, check out the official Python site for interpreters, sample code, applications, cool links and debuggers. This is THE central site for Python.

You can now attach text files to your posts to pass around scripts. Just attach the script as a txt file like you would a jpg or gif. Since the forum will use a random name for the file in the link, you should give instructions on what the file name should be and where to install it. Its a good idea to usually put that info right in the script file as well.

Checkout the Renderosity MarketPlace - Your source for digital art content!



Subject: XML Parsing (miniDOM) In PoserPython - The Very, Very Basics... :o)


3dcheapskate ( ) posted Fri, 15 February 2013 at 5:17 AM · edited Wed, 20 November 2024 at 9:35 AM

After doing a bit of reading up it seems that using the miniDOM module is a good way to start for parsing an XML file. It appears to be included in the Poser 6 runtime here...

E:e frontierPoser 6RuntimePythonlibxmldomminidom.py

Now I always like to start with a ready-made piece of code, and I found a nice simple miniDOM example in section 19.7.2. here http://docs.python.org/2/library/xml.dom.minidom.html

I just cut-and-pasted it into a test.py file on my desktop and ran it (File > Run Python Script...) from Poser 6.

ValueError: invalid x escape
Traceback (most recent call last):
  File "", line 20, in ?
  File "E:e frontierPoser 6RuntimePythonlibxmldomminidom.py", line 965, in parseString
    return doparse(pulldom.parseString, args, kwargs)
  File "E:e frontierPoser 6RuntimePythonlibxmldomminidom.py", line 951, in doparse
    events = apply(func, args, kwargs)
  File "E:e frontierPoser 6RuntimePythonlibxmldompulldom.py", line 340, in parseString
    parser = xml.sax.make_parser()
  File "E:e frontierPoser 6RuntimePythonlibxmlsax__init
.py", line 81, in make_parser
    return create_parser(parser_name)
  File "E:e frontierPoser 6RuntimePythonlibxmlsax__init
_.py", line 106, in _create_parser
    return drv_module.create_parser()
AttributeError: 'module' object has no attribute 'create_parser'

If I add 'import poser' I get this:

ValueError: invalid x escape
Traceback (most recent call last):
  File "", line 21, in ?
  File "E:e frontierPoser 6RuntimePythonlibxmldomminidom.py", line 965, in parseString
    return doparse(pulldom.parseString, args, kwargs)
  File "E:e frontierPoser 6RuntimePythonlibxmldomminidom.py", line 951, in doparse
    events = apply(func, args, kwargs)
  File "E:e frontierPoser 6RuntimePythonlibxmldompulldom.py", line 340, in parseString
    parser = xml.sax.make_parser()
  File "E:e frontierPoser 6RuntimePythonlibxmlsax__init
.py", line 93, in make_parser
    raise SAXReaderNotAvailable("No parsers found", None)
xml.sax._exceptions.SAXReaderNotAvailable: No parsers found

But when I run it from Poser 9 it seems fine, although I still get that invalid x escape:

ValueError: invalid x escape

Demo slideshow

Slide title

*...etc...*

Can anybody tell me what's wrong in Poser 6?


The 3Dcheapskate* occasionally posts sensible stuff. Usually by accident.
And it usually uses Poser 11, with units set to inches. Except when it's using Poser 6 or PP2014, or when its units are set to PNU.

*also available in ShareCG, DAZ, and HiveWire3D flavours (the DeviantArt and CGBytes flavour have been discontinued).



markschum ( ) posted Fri, 15 February 2013 at 5:28 PM · edited Fri, 15 February 2013 at 5:31 PM

file_491655.txt

Was rhat module available in Python 2.4 ?     I believe Poser9 is using python 2.7.

 

try the attached code. It works in Poser 7.


3dcheapskate ( ) posted Fri, 15 February 2013 at 10:54 PM

That's the exact code I already tried - I did a WinMerge file comparison to see if you'd made any subtle changes.

The minidom module seemed to be in the Poser 6 installation - the file minidom.py was under /Runtime/Pythom/lib/xml/dom/

Do you get the invalid escape message in Poser 7? (btw just noticed that all the backslashes were stripped out of the first post)


The 3Dcheapskate* occasionally posts sensible stuff. Usually by accident.
And it usually uses Poser 11, with units set to inches. Except when it's using Poser 6 or PP2014, or when its units are set to PNU.

*also available in ShareCG, DAZ, and HiveWire3D flavours (the DeviantArt and CGBytes flavour have been discontinued).



markschum ( ) posted Sat, 16 February 2013 at 11:43 AM

The file I posted ran without errors , the output is a listing of all the html tags that are in the data. 

I posted the code just in case you had missed a bit of the code.

This forum software messes up attempts to post code.


3dcheapskate ( ) posted Sun, 17 February 2013 at 1:06 AM · edited Sun, 17 February 2013 at 1:12 AM

Thanks markschum - I don't know where the 'ValueError: invalid x escape' came from - it doesn't appear now.

:rolleyes: Must be finger trouble on my part - I'm getting a lot of that recently!

It still doesn't work in Poser 6 though - same errors as I mentioned in the first post, minus the invalid escape. So I think I'll sidestep this issue - a simple CSV file is probably all I need, something like this...

FigureName,yTran,zTran,Scale,xRot

Aiko 3,-0.068,-0.019,1.0,0
Apollo,-0.007,0.0,1.01,5.0

The basic reading of each line and extraction of the individual parameters is so simple, I don't recall why I ever considered XML!

f = open("C:/Users/Web/Desktop/FileReader/FileReader.txt")
try:
    for line in f:
        # Check first character of line for non-data lines
        if line[0] == '#':
            print "(COMMENT LINE) "+line
        elif len(line) == 1:
           print "(BLANK LINE) "+line
        else:
           listOfEntries=string.split(line,',')
           print "(DATA (by default) len="+str(len(line))+") "+listOfEntries[0] + ", Y translate=" + str(float(listOfEntries[1])) + ", Z translate=" + str(float(listOfEntries[2])) + ", Scale=" + str(float(listOfEntries[3])) + " X rotate=" + str(float(listOfEntries[4]))    
finally:
    f.close()

Note: I used the old-style file open/close as defined in section 2.7.3 here http://docs.python.org/2/library/stdtypes.html#file-objects) so it'll work in Poser 6. And I'm aware that 'split()' is deprecated but, hey!, it works (in both Poser 6 and Poser 9)!

Of course the code needs to check that data lines are valid, but I think I'll go this way instead and forget about XML.


The 3Dcheapskate* occasionally posts sensible stuff. Usually by accident.
And it usually uses Poser 11, with units set to inches. Except when it's using Poser 6 or PP2014, or when its units are set to PNU.

*also available in ShareCG, DAZ, and HiveWire3D flavours (the DeviantArt and CGBytes flavour have been discontinued).



markschum ( ) posted Sun, 17 February 2013 at 10:49 AM

if you use tmp = line.lstrip()  the whitespace gets removed so you can test the first character without worrying about the indentations. 

 

line.strip(" tn")  will remove spaces, tabs and newline characters.


3dcheapskate ( ) posted Sun, 17 February 2013 at 9:03 PM

Thanks again - I knew it was easy to do, but it's the sort of thing it would take me ages to actually find!


The 3Dcheapskate* occasionally posts sensible stuff. Usually by accident.
And it usually uses Poser 11, with units set to inches. Except when it's using Poser 6 or PP2014, or when its units are set to PNU.

*also available in ShareCG, DAZ, and HiveWire3D flavours (the DeviantArt and CGBytes flavour have been discontinued).



markschum ( ) posted Mon, 18 February 2013 at 12:15 PM

there is also line.startswith("whatever")  which allows testing of a variable length string


3dcheapskate ( ) posted Mon, 18 February 2013 at 10:51 PM · edited Mon, 18 February 2013 at 10:58 PM

Since Poser 6 seems to be the one that's causing most problems I should really just RTFM! So does P6 use Python 2.4.1, 2.4.2, 2.4.3 or 2.4.4? I've found the Python docs for all versions and want to bookmark the P6 one.

In fact a sticky thread on this forum with links to the appropriate Python Library Reference's for each Poser version would be very useful for people like me (and help avoid a lot of stupid questions!

😉

Anyway, back to my file-handling queries - It's progressing well and successfully reading data from a CSV file (I abandoned the XML approach) into various lists and lists of lists. I've been using an absolute path for my data file, but I really want to use a relative path. The most logical location for the datafile is in the same folder as the script, i.e.

/Runtime/Python/poserScripts/3DCheapskate/

The following code seems to give me the correct filename when run from P6, P8 and P9 (I didn't use os.path.curdir since it doesn't won't work in P6) on a Windows 7 PC

import poser
import os
poserpath=os.path.split(poser.AppLocation())
print  os.path.join(poserpath[0],'Runtime','Python','poserScripts','3DCheapskate','datafile.txt')

This should work on other versions of Windows, and on a MAC too, yes?


The 3Dcheapskate* occasionally posts sensible stuff. Usually by accident.
And it usually uses Poser 11, with units set to inches. Except when it's using Poser 6 or PP2014, or when its units are set to PNU.

*also available in ShareCG, DAZ, and HiveWire3D flavours (the DeviantArt and CGBytes flavour have been discontinued).



Privacy Notice

This site uses cookies to deliver the best experience. Our own cookies make user accounts and other features possible. Third-party cookies are used to display relevant ads and to analyze how Renderosity is used. By using our site, you acknowledge that you have read and understood our Terms of Service, including our Cookie Policy and our Privacy Policy.