How to check if an object exists with no error handling


Yes, this old chestnut…I thought I had posted this ages ago, but I don’t see in the archives, so:

If you want to check if an object exists, and you don’t want to deal with any error handling, then do it this way:

from sipyutils import disp		# win32com.client.Dispatch

def objExists( name ):
    c = disp( "XSI.Collection" )
    c.Items = name
    return not( c.Count == 0 )

print 'Object does exist' if objExists( "XSI_Man.geom" ) else 'Does not exist'

Getting the plugin path


You can use the OriginPath property to get the location of a plugin, but OriginPath is available in the scope of a plugin callback only.

__sifile__ and __sipath__, however, can be used in the global scope.

import win32com.client
from win32com.client import constants

import sipyutils

Application.LogMessage( "Global: __sipath__=%s" % __sipath__ )
Application.LogMessage( "Global: __sifile__=%s" % __sifile__ )

null = None
false = 0
true = 1

def XSILoadPlugin( in_reg ):
	in_reg.Author = "SOLIDANGLE"
	in_reg.Name = "TestPlugin"
	in_reg.Major = 1
	in_reg.Minor = 0

	#Register plugin items

	Application.LogMessage( "XSILoadPlugin: __sipath__=%s" % __sipath__ )
	Application.LogMessage( "XSILoadPlugin: __sifile__=%s" % __sifile__ )
	Application.LogMessage( "XSILoadPlugin: in_reg.OriginPath=%s" % in_reg.OriginPath )

	return true

The above will output something like the following:

# INFO : Global: __sipath__=C:\Users\SOLIDANGLE\Autodesk\Softimage_2015\Application\Plugins
# INFO : Global: __sifile__=C:\Users\SOLIDANGLE\Autodesk\Softimage_2015\Application\Plugins\TestPlugin.py
# INFO : XSILoadPlugin: __sipath__=C:\Users\SOLIDANGLE\Autodesk\Softimage_2015\Application\Plugins
# INFO : XSILoadPlugin: __sifile__=C:\Users\SOLIDANGLE\Autodesk\Softimage_2015\Application\Plugins\TestPlugin.py
# INFO : XSILoadPlugin: in_reg.OriginPath=C:\Users\SOLIDANGLE\Autodesk\Softimage_2015\Application\Plugins\

Getting all shaders under a light


Given something like this:
light_disconnected shaders
Here’s how you get all shaders under a light, even the disconnected ones:

from sipyutils import si		# win32com.client.Dispatch('XSI.Application')
from sipyutils import log		# LogMessage
from sipyutils import C			# win32com.client.constants
from sipyutils import disp		# win32com.client.Dispatch

si = si()

def dispFix( badDispatch ):
    import win32com.client.dynamic
    # Re-Wraps a bad dispatch into a working one:
    return win32com.client.dynamic.Dispatch(badDispatch)

oLight = si.Selection(0)


#import win32com.client
oDisconnected = disp( "XSI.Collection" )

if oLight.IsClassOf( C.siLightID ):
	for oShader in oLight.GetAllShaders():
		oOut = dispFix( oShader.Parameters( "out" ) )
		if oOut.Targets.Count == 0:
			oDisconnected.Add( oShader )
			
log( oDisconnected.GetAsText() )

Hat tip: Matt Lind, who provided the GetAllShaders answer to the question “how to get all shaders in a light, even the disconnected ones”

Getting the selected FxTree nodes


You can use the undocumented selectednodes attribute, which was added in Softimage 2014.

views = Application.Desktop.ActiveLayout.Views
v = views.Find("Fx Tree") or views.Find("View Manager").Views.Find("Fx Tree")
if v:
    print v.GetAttributeValue("selectednodes")  # None

The FxTree view also has a targetcontent attribute, which is documented.

* hat tips to csaez (code) and luceric (attribute)

Installing PyQtForSoftimage


I read somewhere that installing PyQtForSoftimage could be difficult, so I gave it a try. I didn’t have any problems.

For my test, I installed PyQtForSoftimage in Softimage 2014 SP2, using Python 2.7.3 and pywin32 218.

Here’s a recipe that will work with Softimage 2014 and earlier. But if you’re using Softimage 2014, you can skip the part about using the system Python. Instead, use PYTHONPATH to point to folder where PyQt4 is installed (hat tip: Tim C).

On-sentence-summary
You need to switch from the Python installed with Softimage to the system Python (and pywin32), install PyQT, and then install the PyQtForSoftimage addon.

General Recipe

  1. Download and install Python 2.7.x (Note that Softimage ships with Python v2.7.3).
  2. Download and install pywin32 (Softimage ships with pywin32 217).
  3. In the Softimage Scripting preferences, clear the Use Python Installed with Softimage check box. Then restart Softimage.
    PythonInstalledWithSoftimage

    Or edit your prefs file (%XSI_USERHOME%\Data\Preferences\default.xsipref) and add this line: scripting.sipython = False

  4. Check that Python is working in Softimage. In the script editor, run a Python snippet like this: Application.LogMessage( “Hello World” )
  5. Download and install PyQt.
  6. Download (right-click and then click Save As) and install the PyQtForSoftimage addon.
  7. Check that everything is working. Open the Plug-in Manager, find PyQtForSoftimage, and run some of the examples.
    PyQt_Example

    I found that the ExampleDialog and ExampleSignalSlot examples did pop up dialogs, but the Log Text button didn’t log anything to the history log.

Softimage 2014 Recipe

  1. Download and install PyQt.
  2. Set the PYTHONPATH environment variable to point to the location of PyQt4. You could do this in setenv.bat, or in the System environment variables.
  3. Download (right-click and then click Save As) and install the PyQtForSoftimage addon.
  4. Check that everything is working. Open the Plug-in Manager, find PyQtForSoftimage, and run some of the examples.

Scripting FBX export and import


You can use the FBXExport command to create the ExportFBXOptions property, and then use either the OM or SetValue to set the FBX options. For example, this Python snippet creates the FBX property by calling FBXExport( “option” ), and then sets the FBX SDK version (for which there is no separate FBXSetExport command).

si = Application
si.FBXExport( "option" )
o = si.Dictionary.GetObject( "ExportFBXOptions" )
o.Parameters( "FBXSDKVersion" ).Value = "FBX201300"

You can do the same thing for FBXImport with FBXImport( “option” ), but I always got an error, even if the property was created.

si = Application

try:
	si.FBXImport( "option" )
except:
	o = si.Dictionary.GetObject( "ImportFBXOptions" )
	
si.LogMessage( o )

Adding ReferenceWidgets to PPGs?


It used to be possible to add a ReferenceWidget by calling oLayout.AddItem( “Param”, “ref”, “ReferenceWidget” ) in your DefineLayout. But that was four or five years ago, and it no longer works.

def MyProperty3333_DefineLayout( in_ctxt ):
	oLayout = in_ctxt.Source
	oLayout.Clear()

	# Test some other control types
	oLayout.AddItem( "Param", "txt", "Texture Space" )
	oLayout.AddItem( "Param", "scnref", "SceneReferenceWidget" )

	# Doesn't work
	oLayout.AddItem( "Param", "ref", "ReferenceWidget" )

	# Doesn't work
	oPPGItem = oLayout.AddItem( "Param", "", "dscontrol" ) ;
	oPPGItem.SetAttribute( "UIType", "ReferenceWidget" );

For reference ;) this is a ReferenceWidget:
ReferenceWidget

And this is how a ReferenceWidget is added in SPDL:

	Parameter "inst_source" input
	{
		title = "Instance source";
		guid = "{5620730D-AEFC-4C4A-B1DF-9377E808E27B}";
		type = reference;
	}
...
	inst_source
	{
		Name = "Source Object";
		UIType = "ReferenceWidget.ReferenceWidget.1"
		{
			filter = "{6B579D20-3DC5-11D0-9449-00AA006D3165}";
			mode = "single";
		}
	}

Scripting: Getting the StrandPosition arrays


StrandPosition is an array of arrays: one array of positions for each strand.

Here’s a Python snippet:

from win32com.client import constants
xsi = Application

def dispFix( badDispatch ):
    import win32com.client.dynamic
    # Re-Wraps a bad dispatch into a working one:
    return win32com.client.dynamic.Dispatch(badDispatch)

attr = xsi.Selection(0).ActivePrimitive.Geometry.ICEAttributes( "StrandPosition" )
dataType = attr.DataType
data2D = attr.DataArray2D
for data in data2D:
   for elem in data:
      elem = dispFix(elem)
      xsi.LogMessage( "Vector3: " + str(elem.X) + ":" + str(elem.Y) + ":" + str(elem.Z) )

And here’s a JScript snippet:

a = Selection(0).ActivePrimitive.Geometry.ICEAttributes( "StrandPosition" );
LogMessage( ClassName(a) );

x = a.DataArray2D.toArray();
LogMessage( x.length );
for ( var i = 0; i < x.length; i++ )
{
   y = x[i].toArray();
   LogMessage( "=======================" );
   for ( var j = 0; j < y.length; j++ )
   {
      LogMessage( y[j].X + ", " + y[j].Y + ", " + y[j].Z );
   }
}

Getting values and fcurves for the port parameters of an ICE node


ICE nodes have ports, and the ports have parameters. It’s the parameters that you work with in an ICE node PPG.

For simple types such as float, integer and boolean, you can access the port parameter value through ICENodeInputPort.Value. However, for more complex types, like a 3D vector, you need to go through the ICENodeInputPort.Parameters.

In either case (simple or complex types), to get an Fcurve, you get a parameter and then use Parameter.Source.

For example, suppose you have a Scalar node:
ports_params_Scalar
To get the value from a Scalar node, you’d do this:

si = Application
node = si.Dictionary.GetObject( "pointcloud.pointcloud.ICETree.ScalarNode" )
port = node.InputPorts(0)

# For scalars, you can just use the port.Value property
print port.Value

# Or you could go through the Parameters
print port.Parameters(0).Value
# 0.428628623486
# 0.428628623486

# It's a little confusing because the port and the parameter have the same name:
print port.FullName
print port.Parameters(0).FullName
# pointcloud.pointcloud.ICETree.ScalarNode.value
# pointcloud.pointcloud.ICETree.ScalarNode.value


# Now get the Fcurve
fcv = param.Source

Now consider the case of a 3D Vector node, where you have one port (value) and three parameters (value_x, value_y, and value_z):
ports_params_3DVector
In this case, you cannot use ICENodeInputPort.Value, so you have to go through the parameters collection:

si = Application
node = si.Dictionary.GetObject( "pointcloud.pointcloud.ICETree.3DVectorNode" )
port = node.InputPorts(0)
print port.FullName # pointcloud.pointcloud.ICETree.3DVectorNode.value
param = port.Parameters( 0 )
print param.FullName # pointcloud.pointcloud.ICETree.3DVectorNode.value_x
print param.Value # 0.933080613613

# Now get the Fcurve
fcv = param.Source

hat tip: Alan Fregtman

Scripting: Toggling the constraint compensation mode


Here’s one way, using the not operator.

si = Application
si.SetUserPref( "SI3D_CONSTRAINT_COMPENSATION_MODE", not si.GetUserPref("SI3D_CONSTRAINT_COMPENSATION_MODE") )

The “problem” with this approach is that you’re toggling between 0 and -1, not between 0 and 1 (when you click the CnsComp button in the UI, you set the pref to either 0 or 1). The -1 happens because not 0 is -1.

Application.LogMessage( True == 1 )
Application.LogMessage( False == 0 )
Application.LogMessage( not False == -1 )
# INFO : True
# INFO : True
# INFO : True

So here’s a couple of other ways to toggle the preference value:

si = Application
si.SetUserPref( "SI3D_CONSTRAINT_COMPENSATION_MODE", 0 if si.GetUserPref("SI3D_CONSTRAINT_COMPENSATION_MODE") else 1 )
si = Application
toggle = [1,0]
si.SetUserPref( "SI3D_CONSTRAINT_COMPENSATION_MODE", toggle[si.GetUserPref("SI3D_CONSTRAINT_COMPENSATION_MODE")] )