Scale object based on distance between 2 vertices I made this script so that I can scale objects for 3D printing from Maya.
I had fun making it and I'm sure it could be cleaned up or improved. If it can be cleaned up please let me know.

##  ((Ax - Bx)**2 + (Ay - By)**2 + (Az - Bz)**2)**0.5

import maya.cmds as mc

def defineParameters():
    selPoints = mc.ls(sl = True)
    objName = selPoints[0].split('.')[0]
    print objName
    vtxCoordList = mc.xform(selPoints, q = True, translation = True, ws = True)
    Ax, Ay, Az = vtxCoordList[:-3]
    Bx, By, Bz = vtxCoordList[3:]
    d = float(((Ax - Bx)**2 + (Ay - By)**2 + (Az - Bz)**2)**0.5) #direct distance between both points
    return d


def getDist():
    d = defineParameters()
    #selPoints = mc.ls(sl = True)


    #vtxCoordList = mc.xform(selPoints, q = True, translation = True, ws = True)
    #Ax, Ay, Az = vtxCoordList[:-3]
    #Bx, By, Bz = vtxCoordList[3:]
    #d = float(((Ax - Bx)**2 + (Ay - By)**2 + (Az - Bz)**2)**0.5) #direct distance between both points
    '''
    dx = ((Ax - Bx)**2)**0.5
    print "Distance on X axis is: {0}".format(dx) #distance on X axis
    dy = ((Ay - By)**2)**0.5
    print "Distance on Y axis is: {0}".format(dy) #distance on Y axis
    dz = ((Az - Bz)**2)**0.5
    print "Distance on Z axis is: {0}".format(dz) #distance on Z axis
    '''
    mc.textFieldButtonGrp("currentLength", edit = True, tx = "{0}".format(d))
    print "the distance between points is {0}".format(d)
    return d  


def refreshScaleFactor(sf):
    userInput = sf       
    scaleFactor = (float(sf))/(float(d))
    print "dist btwn pnts is d: {0} ".format(d)
    print "sf is {0}".format(sf)
    print "user input is {0}".format(userInput)
    print "scale factor is {0}".format(scaleFactor)
    print "-"*10
    return float(scaleFactor)

## scale object to match measurement
def scaleDist():
    b = (float(mc.textFieldButtonGrp("desiredLength",q = True, tx = True))) / defineParameters()    
    mc.makeIdentity(objName, apply = True, t = 1, r = 1, s = 1, n = 0, pn = 1)#freeze transformations
    mc.DeleteAllHistory()     
    mc.scale(float(b), float(b), float(b), objName)
    print "you scaled by {0}".format(b)
    mc.makeIdentity(objName, apply = True, t = 1, r = 1, s = 1, n = 0, pn = 1)#freeze transformations

defineParameters()

## window
mc.window("Distance Tool", t = "Distance Tool")
mc.rowColumnLayout(numberOfColumns = 2, columnAttach = (1, 'left', 0), columnWidth = [(1,100), (2,300)])
mc.text(l = "Current length")
currentLength = mc.textFieldButtonGrp("currentLength", ed = False, tx = "{0}".format(d), buttonLabel = "Refresh", bc = getDist)

mc.text(l = "Desired length")
desiredLength = mc.textFieldButtonGrp("desiredLength", buttonLabel = "Scale",  bc = scaleDist, tcc = refreshScaleFactor )
mc.showWindow()
I’ll start with some general comments on Python style:


Line should be no more than 79 characters long. (PEP 8: Maximum Line Length)
When you’re specifying a list of keyword arguments to a function, there shouldn’t be a space around the equals sign. (PEP 8: Other Recommendations)

So for example, line 9 becomes:

vtxCoordList = mc.xform(selPoints, q=True, translation=True, ws=True)


When you have lots of keyword arguments, you can put them on newlines (aligned with the opening parens) to make them easier to read. For example, line 63 becomes:

currentLength = mc.textFieldButtonGrp("currentLength",
                                      ed=False,
                                      tx="{0}".format(d), 
                                      buttonLabel="Refresh",
                                      bc=getDist)

Python variable naming convention for variables and functions is lowercase_with_underscores rather than dromedaryCase.
Functions are documented with docstrings, which are triple-quoted strings that are the first item within the function body. So in getDist(), you want to move the d= to after your docstring, and you should move the comment above scaleDist() to within the function as a docstring.
I don’t understand any of the calls to maya.cmds, because I’m familiar with neither Maya nor this library. If somebody who knows this library could easily follow these calls, and why you’re making them, then this is probably fine. If not, some extra comments might not go amiss.
I would recommend using slightly longer variable names, particularly for d in defineParameters() and b in scaleDist(). Characters are cheap.


And a few more specific comments:

defineParameters():


I think the name of this function is unclear. It actually returns a distance between two points in 3D space, not parameters. I think it might be better to return the two tuples A, B, and move the distance calculation elsewhere.
Rather than using (…)**0.5, you can use the sqrt() function from the math module. I believe it’s equivalent to what you’ve written, but it makes the float() unnecessary and your intent clearer:

import math

d = math.sqrt( (Ax - Bx)**2 + (Ay - By)**2 + (Az - Cz)**2 )


If you want to be really fancy, you can use numpy.linalg.norm:

import numpy

A = vtxCoordList[:-3]
B = vtxCoordList[3:]
d = numpy.linalg.norm(A - B)


but if you don’t already have numpy installed, then setting it up for this small task is probably overkill.


getDist():


This is the function where you should have your code for computing the distance.


refreshScaleFactor():


I would get rid of the userInput variable, and just use the sf variable directly. 
You don’t need the float() around the return value; you can just return scaleFactor directly. As the quotient of two float values, this is automatically a float.


scaleDist():


I think this function is going to throw a NameError when you try to get the value of objName. This variable was only ever defined in the local scope of defineParameters(), but it disappears as soon as that function was done.

I see two possibilities:


Either set up objName as a global variable in defineParameters() (but you should try to avoid global variables where possible); or
Have defineParameters() return a dictionary of parameters: objName, A and B, and then access values as appropriate.

In the mc.scale(float(b), …) line, the float()s around b are unnecessary: again, it’s the quotient of two floats.
I don’t know Maya, so I don’t know what the variables in the two freeze transformation lines mean (t, r, s, …). If it’s obvious to somebody who knows Maya and/or the maya.cmds module, then perhaps you don’t need to explain it; but if not, perhaps a comment would be useful.


Finally, there’s some code that isn’t part of any function at the bottom of the script:


Rather than having this code in global scope, put it in a main() function and then have this at the end of the script:

if __name__ == '__main__':
    main()


This means you can run the file as a script, but you can also import it in another script without it kicking off the main code.