Blog

Setup Transform Limits on an Object, Maya, Python Function

Posted by:

There is a GUI for this here. but this is a function that can be used in other scripts to do the same thing by passing the limits into the function:

 

  1. #The form for calling this script will look like:
  2. #applyLimits(listOrObjectToPass, False, [1, 1, 1, 1, 1, 1], [-1.5, 1.5, -1.5, 1.5, -1.5, 1.5], [1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1])
  3. #note that if you are passing a single item into the function you will need to bundle the item into a list:
  4. #applyLimits([listOrObjectToPass], False, [1, 1, 1, 1, 1, 1], [-1.5, 1.5, -1.5, 1.5, -1.5, 1.5], [1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1])
  5. #listOrObjectToPass is either a single object or a list that you want to setup limits on
  6. #maintainTranslationOffsets is a bool that tells the function whether it should read the existing translation values and lock them into place (used primarily for joints)
  7. #translateLimits are essentially bools (0 or 1) that tells whether upper or lower limits for each transform channel (xyz) should be imposed or not
  8. #translateValues are the upper and lower max limits for the translate x,y and z channels
  9. #rotateLimits are essentially bools (0 or 1) that tells whether upper or lower limits for each rotation channel (xyz) should be imposed or not
  10. #rotateValues are the upper and lower max limits for the rotate x,y and z channels
  11. #scaleLimits are essentially bools (0 or 1) that tells whether upper or lower limits for each scale channel (xyz) should be imposed or not
  12. #scaleValues are the upper and lower max limits for the scale x,y and z channels
  13. def applyLimits(objectListForLimits, maintainTranslationOffsets, transformLimits, translateValues, rotateLimits, rotateValues, scaleLimits, scaleValues):
  14. print('applyLimits(), the objectListForLimits is : ' + str(objectListForLimits))
  15.  
  16. i = 0
  17. for eachObject in objectListForLimits:
  18. transformPosition = cmds.xform(eachObject, query = True, translation = True)
  19. print('applyLimits(), the transformPositionValues for ' + str(eachObject) + ' are: ' + str(transformPosition))
  20. print('applyLimits(), maintainTranslationOffsets for ' + str(eachObject) + ' is: ' + str(maintainTranslationOffsets))
  21. if maintainTranslationOffsets == True and transformLimits[0] == 1 and transformLimits[1] == 1:
  22. print('applyLimits(), the max and min translation value for ' + str(eachObject) + ' is: ' + str(transformPosition[0]))
  23. translateValues[0] = transformPosition[0]
  24. translateValues[1] = transformPosition[0]
  25. if maintainTranslationOffsets == True and transformLimits[2] == 1 and transformLimits[3] == 1:
  26. print('applyLimits(), the max and min translation value for ' + str(eachObject) + ' is: ' + str(transformPosition[1]))
  27. translateValues[2] = transformPosition[1]
  28. translateValues[3] = transformPosition[1]
  29. if maintainTranslationOffsets == True and transformLimits[4] == 1 and transformLimits[5] == 1:
  30. print('applyLimits(), the max and min translation value for ' + str(eachObject) + ' is: ' + str(transformPosition[2]))
  31. translateValues[4] = transformPosition[2]
  32. translateValues[5] = transformPosition[2]
  33.  
  34. cmds.transformLimits(eachObject, enableTranslationX = [transformLimits[0], transformLimits[1]], tx =(translateValues[0], translateValues[1]))
  35. cmds.transformLimits(eachObject, enableTranslationY = [transformLimits[2], transformLimits[3]], ty=(translateValues[2], translateValues[3]))
  36. cmds.transformLimits(eachObject, enableTranslationZ = [transformLimits[4], transformLimits[5]], tz=(translateValues[4], translateValues[5]))
  37.  
  38. cmds.transformLimits(eachObject, enableRotationX = [rotateLimits[0], rotateLimits[1]], rx =(rotateValues[0], rotateValues[1]))
  39. cmds.transformLimits(eachObject, enableRotationY = [rotateLimits[2], rotateLimits[3]], ry=(rotateValues[2], rotateValues[3]))
  40. cmds.transformLimits(eachObject, enableRotationZ = [rotateLimits[4], rotateLimits[5]], rz=(rotateValues[4], rotateValues[5]))
  41.  
  42. cmds.transformLimits(eachObject, enableScaleX = [scaleLimits[0], scaleLimits[1]], sx =(scaleValues[0], scaleValues[1]))
  43. cmds.transformLimits(eachObject, enableScaleY = [scaleLimits[2], scaleLimits[3]], sy=(scaleValues[2], scaleValues[3]))
  44. cmds.transformLimits(eachObject, enableScaleZ = [scaleLimits[4], scaleLimits[5]], sz=(scaleValues[4], scaleValues[5]))
  45. i = i + 1
0
  Related Posts

You must be logged in to post a comment.