Saturday snippet: short-circuit evaluation


If you’re new[ish] to scripting, here’s a Python snippet that illustrates short-circuit evaluation of boolean expressions.

In this snippet, short-circuit evaluation is used to avoid errors. For example, if there is no such ICE attribute (nb is not None) then there is no attempt to try and use the ICE attribute methods like IsDefined.

The expression in the print statement also relies on operator precedence (not has higher precedence than and, so everything works as expected).

# Assume a polymesh is selected...
o = Application.Selection(0)

# Check the intrinsic ICE attribute
nb =  o.ActivePrimitive.ICEAttributes("NbPoints")
print nb is not None and nb.IsDefined and nb.DataArray[0] <= 0

The above snippet also relies on operator precedence (not has higher precedence than and, so everything works as expected).

Since or has higher precedence than and, you could write something like this:

nb.IsDefined and nb.DataArray[0] <= 0 or o.ActivePrimitive.Geometry.Polygons.Count <= 0

But I’d probably put in the parentheses just to be clear:

(nb.IsDefined and nb.DataArray[0] <= 0) or o.ActivePrimitive.Geometry.Polygons.Count <= 0

Finding empty polygon meshes


Now that there are intrinsic ICE attributes like NbPoints and NbPolygons, there a couple of ways you can check for an empty mesh:

# Assume a polymesh is selected...
o = Application.Selection(0)

# Check the intrinsic ICE attribute
nb =  o.ActivePrimitive.ICEAttributes("NbPoints")
print nb.IsDefined and nb.DataArray[0] <= 0

# Check using the object model
print o.ActivePrimitive.Geometry.Points.Count <= 0

The typical way to package this up for users it to define a filter. Then a user just has to select the filter and press CTRL+A. Here’s the Match callback for a filter that finds empty polygon meshes. Note that I switched to checking the number of polygons. That way, if somehow there was something weird like a mesh with just one point, you’d still find it.

The intrinsic attribute NbPolygons should always exist, but just to be sure I check IsDefined, and if that is False, I fall back to checking Geometry.Polygons.Count.

# Match callback for the EmptyPolygonMesh custom filter.
def EmptyPolygonMesh_Match( in_ctxt ):
	Application.LogMessage("EmptyPolygonMesh_Match called",constants.siVerbose)

	o = in_ctxt.GetAttribute( 'Input' )
	if o.type == 'polymsh':
		nb =  o.ActivePrimitive.ICEAttributes("NbPolygons")
		return (nb.IsDefined and nb.DataArray[0] <= 0) or o.ActivePrimitive.Geometry.Polygons.Count <= 0
	else:
		return False

I packaged the filter as an addon. Get it here.

Importing multiple FBX files with drag-and-drop


As an exercise, I updated Tim Crowson’s Multi_ImporterPPG addon with a DragAndDrop event, so you can import multiple files with a single drag-and-drop. You can download the modified version here.

Here’s the DragAndDrop event handler. The doit() function is also used by the Import menu command; I just had to generalize it a bit to work in either case (menu or drag-and-drop).

def Multi_Importer_DragAndDrop_OnEvent( in_ctxt ):

	action = in_ctxt.GetAttribute( "DragAndDropAction" )
	source = in_ctxt.GetAttribute( "DragSource" )

	if action == constants.siSourceDragAction:
		if re.search( r"\obj$", source, re.I ):
			in_ctxt.SetAttribute( "DragSourceSupported", True )
		elif re.search( r"fbx$", source, re.I ): 
			in_ctxt.SetAttribute( "DragSourceSupported", True )
		elif re.search( r"emdl$", source, re.I ): 
			in_ctxt.SetAttribute( "DragSourceSupported", True )
		elif re.search( r"lwo$", source, re.I ): 
			in_ctxt.SetAttribute( "DragSourceSupported", True )
		else:
			in_ctxt.SetAttribute( "DragSourceSupported", False )
		
	
	if action == constants.siSourceDropAction:

		Application.SetValue('preferences.Interaction.autoinspect', False, '')

		if not Application.ActiveSceneRoot.Properties( 'Multi_Importer' ):
			vtcol = Application.AddProp('Multi_Importer','Scene_Root')
			p = Application.Dictionary.GetObject( vtcol.Value("Value") )

			# Set the flag that hides certain parts of the PPG layout
			p.Parameters("bMenuCommand").Value = False

			# Inspect the PPG in modal mode
			Application.InspectObj( vtcol.Value("Value"), "", "", 4 )

			p.Parameters("bMenuCommand").Value = True
			
		p = Application.ActiveSceneRoot.Properties('Multi_Importer')

		options = { 
			'OBJgrouping' : p.Parameters('importOBJgrouping').Value,
			'OBJhrc' : p.Parameters('importOBJhrc').Value,
			'importOBJnormals' : p.Parameters('importOBJNormals').Value,
			'includeOBJmat' : p.Parameters('includeOBJMaterial').Value,
			'includeOBJuv' : p.Parameters('includeOBJUV').Value,
			'includeOBJwrap' : p.Parameters('includeOBJUVWrap').Value,
			'fbxScale' : p.Parameters('fbxScale').Value,
			'importEMDLasRef' : p.Parameters('importEMDLasRef').Value,
			'lwoScaleFactor' : p.Parameters('lwoScaleFactor').Value
			}
		
		doit( source, options )
	
	return True

Saturday snippet: Using the connection stack to find all expressions driven by a given parameter


#
# Softimage 2013 SP1 Python snippet
#
from sipyutils import si			# win32com.client.Dispatch('XSI.Application')
from sipyutils import siut		# win32com.client.Dispatch('XSI.Utils')
from sipyutils import siui		# win32com.client.Dispatch('XSI.UIToolkit')
from sipyutils import simath	# win32com.client.Dispatch('XSI.Math')
from sipyutils import log		# LogMessage
from sipyutils import disp		# win32com.client.Dispatch
from sipyutils import C			# win32com.client.constants

si=si()
siut=siut()
from xml.etree import ElementTree as ET

def getExpressionsDrivenByLocalParameter( obj, param="posx" ):
	stack = siut.DataRepository.GetConnectionStackInfo( obj.Parameters(param) )
#	print stack

	expressions = XSIFactory.CreateObject("XSI.Collection")
	expressions.Unique = True
	xmlRoot = ET.fromstring(stack)
	for xmlCnx in xmlRoot.findall('connection'):
		if xmlCnx.find('type').text == 'out' and xmlCnx.find('localparameter') is not None and xmlCnx.find('localparameter').text == param:
			item = xmlCnx.find('object').text
			if item.endswith('.Expression'):
				expressions.AddItems(item)

	return expressions


# Create an expression where the Scene Material diffuse red drives the diffuse red of a Lambert shader
Application.SetExpr("Sources.Materials.DefaultLib.Lambert.Lambert.diffuse.red", "Sources.Materials.DefaultLib.Scene_Material.Phong.diffuse.red") 

x = getExpressionsDrivenByLocalParameter( si.Dictionary.GetObject("Sources.Materials.DefaultLib.Scene_Material.Phong.diffuse"), param="red" )
log( x )
# INFO : Sources.Materials.DefaultLib.Lambert.Lambert.diffuse.red.Expression

Reference and credits:

Finding phantom passes created with Duplicate


As I mentioned last year around this time, it is possible to end up with passes that don’t show up in the explorer. This happens when you Duplicate a pass, and the Hierarchy option “preferences.duplicate.hierarchy” is set to None. So your new pass has no parent, and is disconnected from the rest of the scene (aka floating).

These phantom passes have names like “#Pass”, and you can select them if you know how.

Here’s how, in Python, with the 2013 SP1 Python shortcuts.

from sipyutils import si			# win32com.client.Dispatch('XSI.Application')
from sipyutils import siut		# win32com.client.Dispatch('XSI.Utils')
from sipyutils import siui		# win32com.client.Dispatch('XSI.UIToolkit')
from sipyutils import simath	# win32com.client.Dispatch('XSI.Math')
from sipyutils import log		# LogMessage
from sipyutils import disp		# win32com.client.Dispatch
from sipyutils import C			# win32com.client.constants

si=si()

sClassID = siut().DataRepository.GetIdentifier( si.ActiveProject.ActiveScene.Passes(0), C.siObjectCLSID )
passes = si.FindObjects( None, sClassID ).Filter( "", None, "#Pass*" )
print passes.Count
print passes.GetAsText()

#
# The following don't work! At least not all the time 😦
#
si.DeleteObj( passes )

#si.ParentObj( "FloatingPasses.Passes", "#Pass<61>" )
#si.CopyPaste( passes(0), "", si.Dictionary.GetObject("FloatingPasses.Passes") )

si.SelectObj( passes )

When I was testing this, sometimes I was able to delete those phantom passes, and sometimes I wasn’t.
Sometimes those passes disappeared when I saved the scene, did New Scene, and then reloaded the scene. Sometimes they didn’t (disappear that is).

When DeleteObj didn’t work, I’d use the explorer to view the phantom passes (since I called SelectObj on them, I could just show selected in the explorer).

Copying the global transform into a 4×4 Matrix ICE node


I saw–via an email notification–the question “how do I use the Global Transform of an object to create a 4×4 Matrix” posted on xsibase (sorry, I don’t go to xsibase anymore because of the “attack site” and “malware” warnings).

One way to do this is to add a “Copy Global Transform” command to the ICE node context menu. After you install this plugin, right click a 4×4 matrix node in an ICE tree, and it will copy the Global Transform from the first object in the selection list.

Note: error checking and stuff like that is left as an exercise for the reader (or for another blog post).

Here’s the plugin code for 2013. For 2012 or earlier, you have to change the AddCallbackItem2 call to AddCallbackItem.

si = Application
import win32com.client
from win32com.client import constants as C

null = None
false = 0
true = 1

def XSILoadPlugin( in_reg ):
	in_reg.Author = "blairs"
	in_reg.Name = "CopyTransfo2MatrixNodePlugin"
	in_reg.Major = 1
	in_reg.Minor = 0

	in_reg.RegisterMenu(C.siMenuICENodeContextID,"CopyTransfo2MatrixNode_Menu",false,false)

	return true

def XSIUnloadPlugin( in_reg ):
	strPluginName = in_reg.Name
	Application.LogMessage(str(strPluginName) + str(" has been unloaded."),C.siVerbose)
	return true

def CopyTransfo2MatrixNode_Init( in_ctxt ):
	oCmd = in_ctxt.Source
	oCmd.Description = ""
	oCmd.ReturnValue = true

	oArgs = oCmd.Arguments
	oArgs.AddWithHandler("Arg0","Collection")
	return true

def CopyTransfo2MatrixNode_Menu_Init( in_ctxt ):
	oMenu = in_ctxt.Source
	oMenu.AddCallbackItem2("Copy Global Transform","CopyTransfo2MatrixNode")
	return true
	
def CopyTransfo2MatrixNode( in_ctxt ):
	oNodeName = in_ctxt.GetAttribute("Target")

	o = si.Selection(0)
	t = o.Kinematics.Global.GetTransform2( None )
	m = t.Matrix4.Get2()

	# Get a matrix node
	n = si.Dictionary.GetObject( oNodeName )
	
	n.Parameters( "value_00" ).Value = m[0]
	n.Parameters( "value_01" ).Value = m[1]
	n.Parameters( "value_02" ).Value = m[2]
	n.Parameters( "value_03" ).Value = m[3]

	n.Parameters( "value_10" ).Value = m[4]
	n.Parameters( "value_11" ).Value = m[5]
	n.Parameters( "value_12" ).Value = m[6]
	n.Parameters( "value_13" ).Value = m[7]

	n.Parameters( "value_20" ).Value = m[8]
	n.Parameters( "value_21" ).Value = m[9]
	n.Parameters( "value_22" ).Value = m[10]
	n.Parameters( "value_23" ).Value = m[11]

	n.Parameters( "value_30" ).Value = m[12]
	n.Parameters( "value_31" ).Value = m[13]
	n.Parameters( "value_32" ).Value = m[14]
	n.Parameters( "value_33" ).Value = m[15]

Scripting – How to get the active objects for component selection


When you’re in a component selection mode (such as Edge, Polygon, or Point), the active objects are highlighted in orange. The “active objects” are the objects that are “active for component selection”.

When Softimage is in a component selection mode, the Selection will either by empty or it will contain CollectionItems (one for each object with selected components).

So, how do you get the active objects? Here’s one way, using the little known, magical “.[obj].”:

# Python
import win32com.client
oActiveObjects = win32com.client.Dispatch( "XSI.Collection" )
oActiveObjects.Items = ".[obj]."
// JScript
var oActiveObjects = new ActiveXObject( "XSI.Collection" );
oActiveObjects.Items = ".[obj].";

Whoops I guess Softimage did need that sitecustomize.py file…


Awhile back, I wrote a little script that used ElementTree to parse the XML returned by GetConnectionStackInfo. I posted the 2013sp1 version of the script here.

Anyway, I was a little surprised when I couldn’t get the script to run in 2012 SAP. I was sure that it used to work, but now it was giving me this error:

# ERROR : Traceback (most recent call last):
#   File "<Script Block >", line 17, in <module>
#     connections = ET.XML(stackInfo)
#   File "C:\Program Files\Autodesk\Softimage 2012.SAP\Application\python\Lib\xml\etree\ElementTree.py", line 962, in XML
#     parser = XMLTreeBuilder()
#   File "C:\Program Files\Autodesk\Softimage 2012.SAP\Application\python\Lib\xml\etree\ElementTree.py", line 1118, in __init__
#     "No module named expat; use SimpleXMLTreeBuilder instead"
# ImportError: No module named expat; use SimpleXMLTreeBuilder instead
#  - [line 17]

After digging into the problem a bit, I found that the Softimage install did indeed include expat, but Softimage wasn’t finding it…because I had deleted the Application\python\Lib\sitecustomize.py file!!! Doh.

I had deleted sitecustomize.py while investigating a problem report from a customer, and then I didn’t bother putting it back, because Python was still working in general.

sitecustomize.py adds paths like %XSI_HOME%\Application\python\DLLs to the sys.path, and pyexpat.pyd is in that DLLs folder.

Getting a list of all shaders in a render tree


Here’s a Python snippet that gets all the shaders in the render tree for a specific material. As usual, I always feel that my Python snippet could be made more pythonic; for now, this will have to do…

PS The Shader reference page has a VBScript example, but that doesn’t work anymore because it was written before ShaderParameters were introduced.

from sipyutils import si		# win32com.client.Dispatch('XSI.Application')
si=si()

import win32com.client
coll = win32com.client.Dispatch( "XSI.Collection" )

def doit( s, coll ):

	for p in s.Parameters:
		if p.Source and p.Source.IsClassOf( C.siShaderParameterID ):
			print p.Source.Parent.Name
			coll.Add( p.Source.Parent )
			doit( p.Source.Parent, coll )
	
	for l in s.TextureLayers:
		for y in l.Parameters:
			if y.Source and y.Source.IsClassOf( C.siShaderParameterID ):
				print y.Source.Parent.Name
				coll.Add( y.Source.Parent )
				doit( y.Source.Parent, coll )

# Get a material or shader to use as a starting point
mat = si.Dictionary.GetObject("Sources.Materials.DefaultLib.Architectural")
doit( mat, coll )

coll.Unique = True
coll.Add( mat.AllImageClips.GetAsText() )

This snippet worked for this [nonsensical test] render tree:

Python – Getting the Softimage version numbers from the registry


This Python snippet will get the version numbers for the different versions of Softimage installed on the local system.

#http://docs.python.org/library/_winreg.html
from _winreg import *
t = OpenKey(HKEY_LOCAL_MACHINE, r"SOFTWARE\Softimage\CoExistence", 0, KEY_READ )

try:
    count = 0
    while 1:
		name = EnumKey(t, count)
		sKey = "SOFTWARE\Softimage\CoExistence\%s" % name
		t1 = OpenKey( t, name, 0, KEY_READ )
		value = QueryValueEx(t1, "AppVersion" )
		Application.LogMessage( "%s: %s" % (name.rsplit('|',3)[1], value[0]) )
		count = count + 1
except WindowsError:
    pass

On my machine, the script output looks like this:

# INFO : Softimage 2011 SP1: 9.1.91.0
# INFO : Softimage 2011 SP2: 9.2.102.0
# INFO : Softimage 2011 Subscription Advantage Pack SP1: 9.6.194.0
# INFO : Softimage 2011: 9.0.243.0
# INFO : Softimage 2012.SAP: 10.5.98.0
# INFO : Softimage 2012: 10.0.422.0
# INFO : Softimage 2013 SP1: 11.1.1.0
# INFO : Softimage 2013: 11.0.525.0
# INFO : Softimage_2010_SP1_x64: 8.0.249.0
# INFO : Softimage_2010_x64: 8.0.201.0
# INFO : Softimage_7.5_x64: 7.5.191.0
# INFO : XSI_7.01_x64: 7.01.698.0