I used to prefer JScript, because I was familiar with it from HTML scripting and I liked that it used curly brackets {} like C++.
But this is reason enough to prefer Python for scripting in Softimage:
// Log number of selected components LogMessage( VBArray(Selection(0).SubElements).toArray().length )
In Python, you don’t have to deal with the VBArray stuff:
Application.LogMessage( len( Application.Selection(0).SubElements ) )
And when you have convenience shortcuts defined, it becomes even nicer:
log( len( si.Selection(0).SubElements ) )
Python code looks very clean and nice.
But In my experiments I’ve noticed that for in loops in Python are very slow. And since looping components in high poly objects is already very slow with JScript and VBS I think I’ll stay with JScript for the moment.
I’ve realized that VBS “for each” loop is a little faster than JS’s “for” loop but not much if you buffer the lenght / count in a variable. (Maybe 15% slower)
But, Python “for in” loop is about 8-10 times slower.
My Python knowledge is quite limited so I may be doing something wrong. Is there any alternative or trick for for loops in Softimage with Python? Or I just should stick with JS with components looping?
With Python you may be taking a performance hit because of the pywin interface to COM (JScript/VBS having a more direct route to COM). I remember hearing that for scripted ops, JScript was faster than Python.
I think most Python people would say that the benefits of Python are worth it (and maybe for performance you might want to go C++ 🙂
Don’t forget to look at Bernhard Lebel’s “Looping with Python in XSI”:
http://www.softimageblog.com/archives/31
The first article he links to goes to an empty WIKI page, but this is the result of the URL-name-change of the WIKI, It can still be found here:
http://tinyurl.com/7vkfkzr
😉
Yes, I thought of that article and looked it up. I didn’t mention it because I don’t think it covers JScript vs Python. Still an interesting read…but the comments may take awhile to wade through.
Thanks for the Loopin’ Lizards link!
You’re obviously right, Bernhard Lebel’s article doesn’t talk about Jscript, but I mentioned it mainly, because it puts the whole “Looping in Python” dilemma into a nice context.
😉
sisel was a nice shortcut (siutils module) back in Softimage 2011 😦
Do you know why was eliminated?
Hi
There was a memory leak associated with these shortcuts, so we had to remove them.
Another huge benefit to learning and using Python is that you can also transfer your Python skills into many other areas of VFX:
a. Blender
b. Cinema 4D
c. Lightwave
d. Houdini
e. Maya
f. modo
g. Motion Builder
h. Nuke
i. Poser
j. Silhouette
k. Vue
l. Web Development
Python is THE language to learn.
I did some other tests, it seems that Python loops are just a little slower than JS and VBS.
The problem wasn’t the loops but XSI collections, Python can become easily 10 times slower than JS and VBS when it’s handling collections.
I guess I’ll have to stick with JS for heavy looping, and try Python for more casual scripts and use Python arrays when possible.
Thanks
You only need the VBSafeArray methods if you intend to modify and return the array data where it came from. Otherwise, you can read the array directly without the need for any conversions:
// get array of vertex indices from the object
var oVertexIndices = oObject.ActivePrimitive.Geometry.Vertices.IndexArray;
// get index of first vertex in array using the ‘VBSafeArray.getItem( )’ method.
LogMessage( oVertexIndices.getItem(0) );
Optionally, the GridData object can remove the need for the VBSafeArray as it’s essentially an ActiveX 2D array object. here’s an example reading/writing vertex colors:
// create a griddata object to store the color values
var oVertexColorData = XSIFactory.CreateGridData();
// Initialize griddata with the color values
var oVertexColorProperty = oObject.ActivePrimitive.Geometry.CurrentVertexColor;
oVertexColorData.Data = oVertexColorProperty.Elements.Array;
// modify a few color values – lets paint polygon node #3 opaque orange.
oVertexColorData.SetCell( 0, 2, 1.0 );
oVertexColorData.SetCell( 1, 2, 0.7 );
oVertexColorData.SetCell( 2, 2, 0.0 );
oVertexColorData.SetCell( 3, 2, 1.0 );
// update the mesh with the new colors
oVertexColorProperty.Elements.Array = oVertexColorData.Data;
As for getting subcomponents as shown in the initialize example of the article, there is another way that doesn’t involve arrays at all:
LogMessage( Selection(0).SubComponent.ComponentCollection.Count );
I’m not here to defend JScript to the extents of the Earth, but I do see a lot of misunderstanding of when/where the VBSafeArray needs to be employed which leads to a lot of the frustration voiced in the forums.