Python example constraining nulls to components

Just a little example that uses the Object Model to create a null and a ObjectToCluster constraint for each selected component (point, edge, polygon, …).
Note line 13. I can use CollectionItem.SubElements to get the indices of the selected components.

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

if si.Selection.Count > 0 and si.ClassName(si.Selection(0)) == 'CollectionItem' and si.Selection(0).SubComponent is not None:

	# pnt, poly, edge, ...
	clusterType = si.Selection(0).Type.replace( 'SubComponent','' )
	o = si.Selection(0).SubComponent.Parent3DObject
	for i in si.Selection(0).SubElements:
		c = o.ActivePrimitive.Geometry.AddCluster( clusterType, "", [i] )
		n = si.ActiveSceneRoot.AddNull()
		n.Kinematics.AddConstraint( "ObjectToCluster", c )

11 thoughts on “Python example constraining nulls to components

  1. Is it possible to constrain a null to point in a point cloud that is simulated? I have no results with Object to Cluster constrain, ICE Kinematics is not an option because mainly I deal with a large number of positions. Thanks!

  2. Oh, ok, I got it, I’ve created nulls first and then applied Transform Objects by Particles command to all of them at once. In this case I got a faster result! Thanks!

  3. Any reason you dispatched the Application object instead of just using “Application”? (I know you have to for things like imported modules in plugins, and I think relational views too, but otherwise not really, right?)

    While we’re at it, dispatching XSI.Collection vs XSIFactory.CreateObject(“XSI.Collection”)… Which one is better? Is there a difference?

  4. Is there a reason why in this example you dispatch Application instead of using it directly as is?

    Also, while we’re at it, is it better to dispatch XSI.Collection or use XSIFactory.CreateObject()? Is there a difference, downside or overhead at all to using either?

    • RE: dispatching Application

      I don’t remember why I didn’t use the siutils shortcuts, but when I removed those shortcuts I was too literal and just pasted in what was in the module. You’re right, I don’t need to dispatch Application.

      RE: dispatching XSI.Collection vs using XSIFactory.CreateObject()?
      I don’t know. The Syntax Help in the script ed uses dispatch, so that suggests dispatch is the preferred way to do it.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s