Re: Diving into python, some questions...

Date : Thu, 24 Aug 2006 16:25:27 +1000
To : XSI(at)Softimage.COM
From : "Aloys Baillet" <aloys.baillet(at)gmail.com>
Subject : Re: Diving into python, some questions...
Hi Steven,

Some random thoughts here...

for i in range(oPasses.Count):
    passElem = doc.createElement (oPasses(i).Name)

could become

for oPass in oPasses:
    passElem = doc.createElement (oPass.Name)

It's faster and you don't need to ask if [] is better than (). I confess I prefer to stick with the XSI way which is ().

For syntax errors: the greater source of Syntax Errors in Python for XSI is tabs instead of spaces.
Use a text editor where you can specify that 1 tab = 4 spaces and stick with it. Then when you copy and paste code from the documentation or other scripting languages, replace all tabs by 4 spaces!

That's all for now, and good luck!
Python is worth the frustration of the beginning, and the XSI implementation is not helping much unfortunately.

Cheers,

Aloys



On 8/24/06, Steven Caron <carons(at)gmail.com> wrote:
hey
i thought getting into python would be easier or at least more fun... I am not having much fun at the moment, maybe someone can share some insight.

I have read about the collection types in python. lists, dictionaries, tuples great stuff. but in XSI i have noticed that sometimes i can use [ ] brackets when indexing an XSICollection and I can also use ( ) as normal. i shouldn't do that right?

Does the extra collection types in python interfere with XSICollections?
When a method returns an XSICollection is python interpreting that some other way, maybe into its native collection types?

also i am getting syntax errors on loops that look exactly like other loops a few lines up... after i couldn't figure out what the syntax problem was, i just removed the whole loop. then the loop above that threw an error. then after removing all of the loops, stuff that was working only mins before starts throwing syntax errors. confusing and frustrating

so here is where my code stands now... if some python guru wouldn't mind pointing out something so close it could bite me

#start code

'''
playing with python and xml
'''

#lets import the xmldom module
import xml.dom.minidom

#assign Application to a smaller variable
app = Application

#get the all the passes in the scene
oPasses = app.ActiveProject.ActiveScene.Passes

#new XML doc
doc = xml.dom.minidom.Document()

#root element
passList = doc.createElement("PassList")
doc.appendChild(passList)

#pass elements
for i in range(oPasses.Count):
    passElem = doc.createElement (oPasses(i).Name)
    passList.appendChild(passElem)
   
    #render options for each pass
    rOptElem = doc.createElement("RenderOptions")
    rOptions = oPasses(i).NestedObjects(3).Parameters
   
    #render option's parameters and values
    for j in range(rOptions.Count): #first syntax error
        if rOptions.Value:
            parElem = doc.createElement(rOptions(j).Name)
            rOptElem.appendChild (parElem)
            parTxt = doc.createTextNode(rOptions(j).Value)
            parElem.appendChild(parTxt)
       
    #partitions for each pass
    partColl = oPasses(i).NestedObjects(0).NestedObjects #after removing the loop above this, this starts to thow errors :(
    for h in range(partColl.Count):
        partElem = doc.createElement(partColl(h).Name)
        passElem.appendChild(partElem)
       
        #memberships
        memberElem = doc.createElement("Memberships")
        partElem.appendChild(memberElem)
        members = partColl(h).Members.GetAsText()
        memberTxt = doc.createTextNode(members)
        memberElem.appendChild(memberTxt)

#write doc
fileName = "c:/pass.xml"
try:
   # Open a file stream in write mode
   output = open(fileName, 'w')
   output.write(doc.toprettyxml())

except OSError, e:
   print "Could not write XML file!"

#end code

thanks!
Steven



--
Aloys Baillet - XSI Technical Director
Character Dpt - Animal Logic
--

Search the XSI List archives here or use the advanced search form to search across mailing lists. Searching help is available.
This site supposedly brought to you by Benjamin Grosser and the Imaging Technology Group.