2010
06.05

This post surged from a question at a forum where somebody tried to use the ctr_dist() function — that returns distance between two object centers — in a Softimage script only to realise that actually it only exists for expressions, not scripting.

Here’s my take on said function for both Softimage and Maya…


First of all, we need to know the math behind it. Knowing global position vectors A and B, the formula for the distance (d) between them would be:

d = √  Ax-Bx2 + Ay-By2 + Az-Bz2  

I’ve decided to borrow sqrt() and pow() functions from Python’s math library, by the way.

First let’s see how to do this with Python in Softimage:

xsi = Application
lm = xsi.LogMessage
 
def ctr_dist( objA, objB ):
	from math import sqrt,pow
 
	Ax, Ay, Az = objA.Kinematics.Global.Transform.GetTranslationValues2()
	Bx, By, Bz = objB.Kinematics.Global.Transform.GetTranslationValues2()
 
	return sqrt(  pow(Ax-Bx,2) + pow(Ay-By,2) + pow(Az-Bz,2)  )
# -------------------------
 
 
# Try it out with 2 selected objects:
fromObj, toObj = xsi.Selection(0), xsi.Selection(1)
 
lm(
"Distance between <"+fromObj.FullName+"> to <"+toObj.FullName+"> is: "
+str( ctr_dist(fromObj, toObj) )
)

Now let’s repurpose it for PyMEL in Maya, without using distance nodes:

from pymel.core import *
 
def ctr_dist( objA, objB ):
	from math import sqrt,pow
 
	Ax, Ay, Az = objA.getTranslation(space="world")
	Bx, By, Bz = objB.getTranslation(space="world")
 
	return sqrt(  pow(Ax-Bx,2) + pow(Ay-By,2) + pow(Az-Bz,2)  )
# -------------------------
 
 
# Try it out with 2 selected objects:
sel = ls(sl=1)
print "Distance between <"+sel[0]+"> to <"+sel[1]+"> is: "+str( ctr_dist(sel[0],sel[1]) )

By the way, note the awesomeness of Python for letting you assign multiple variables from an array using a single line. That’s pretty nice. :)

Hope somebody finds this useful! You can use it a measuring tool or for distance-dependent rigging logic.

If you do use it for rigging, please remember that if you want the distance to stay the same while a character is scaled, you must divide the distance by the scaling value.

2 comments so far

Add Your Comment
  1. Thanks Alan, it’s useful for me:)

  2. Yo tiraría por aquí… el SDK de softimage tiene cosillas interesantes para trabajar con vectores :)

    def ctr_dist( objA, objB ):
    v = objA.Kinematics.Global.Transform.Translation
    v.SubInPlace(objB.Kinematics.Global.Transform.Translation)
    return v.Length()

Spam protection by WP Captcha-Free