Blog

Set Transform Limits On a List of Objects, Maya, Python, Script

Posted by:

Here is a script that will open a GUI window and allow you to set transform limits on a number of objects at the same time!  Now with lock/unlock channels!

 

  1. #jfm_set_transform_limits
  2. #created by Seth Meshko 12/21/2017
  3. #www.3danimationartist.com
  4. #Creates a gui interface that allows you to set min and max transform limits on a selection
  5. import maya.cmds as cmds
  6. import functools
  7. windowID = 'jfm_setTransformLimits'
  8. def applyLimits(applyLimits):
  9. translateXMaxLimit = cmds.checkBox(setLimitsTranslateXMax, editable = True, query = True, value = True)
  10. translateXMinLimit = cmds.checkBox(setLimitsTranslateXMin, editable = True, query = True, value = True)
  11. translateYMaxLimit = cmds.checkBox(setLimitsTranslateYMax, editable = True, query = True, value = True)
  12. translateYMinLimit = cmds.checkBox(setLimitsTranslateYMin, editable = True, query = True, value = True)
  13. translateZMaxLimit = cmds.checkBox(setLimitsTranslateZMax, editable = True, query = True, value = True)
  14. translateZMinLimit = cmds.checkBox(setLimitsTranslateZMin, editable = True, query = True, value = True)
  15. translateXMaxFieldValue = cmds.floatField(translateXMaxField, editable = True, query = True, value = True)
  16. translateXMinFieldValue = cmds.floatField(translateXMinField, editable = True, query = True, value = True)
  17. translateYMaxFieldValue = cmds.floatField(translateYMaxField, editable = True, query = True, value = True)
  18. translateYMinFieldValue = cmds.floatField(translateYMinField, editable = True, query = True, value = True)
  19. translateZMaxFieldValue = cmds.floatField(translateZMaxField, editable = True, query = True, value = True)
  20. translateZMinFieldValue = cmds.floatField(translateZMinField, editable = True, query = True, value = True)
  21. rotateXMaxLimit = cmds.checkBox(setLimitsRotateXMax, editable = True, query = True, value = True)
  22. rotateXMinLimit = cmds.checkBox(setLimitsRotateXMin, editable = True, query = True, value = True)
  23. rotateYMaxLimit = cmds.checkBox(setLimitsRotateYMax, editable = True, query = True, value = True)
  24. rotateYMinLimit = cmds.checkBox(setLimitsRotateYMin, editable = True, query = True, value = True)
  25. rotateZMaxLimit = cmds.checkBox(setLimitsRotateZMax, editable = True, query = True, value = True)
  26. rotateZMinLimit = cmds.checkBox(setLimitsRotateZMin, editable = True, query = True, value = True)
  27. rotateXMaxFieldValue = cmds.floatField(rotateXMaxField, editable = True, query = True, value = True)
  28. rotateXMinFieldValue = cmds.floatField(rotateXMinField, editable = True, query = True, value = True)
  29. rotateYMaxFieldValue = cmds.floatField(rotateYMaxField, editable = True, query = True, value = True)
  30. rotateYMinFieldValue = cmds.floatField(rotateYMinField, editable = True, query = True, value = True)
  31. rotateZMaxFieldValue = cmds.floatField(rotateZMaxField, editable = True, query = True, value = True)
  32. rotateZMinFieldValue = cmds.floatField(rotateZMinField, editable = True, query = True, value = True)
  33. scaleXMaxLimit = cmds.checkBox(setLimitsScaleXMax, editable = True, query = True, value = True)
  34. scaleXMinLimit = cmds.checkBox(setLimitsScaleXMin, editable = True, query = True, value = True)
  35. scaleYMaxLimit = cmds.checkBox(setLimitsScaleYMax, editable = True, query = True, value = True)
  36. scaleYMinLimit = cmds.checkBox(setLimitsScaleYMin, editable = True, query = True, value = True)
  37. scaleZMaxLimit = cmds.checkBox(setLimitsScaleZMax, editable = True, query = True, value = True)
  38. scaleZMinLimit = cmds.checkBox(setLimitsScaleZMin, editable = True, query = True, value = True)
  39. scaleXMaxFieldValue = cmds.floatField(scaleXMaxField, editable = True, query = True, value = True)
  40. scaleXMinFieldValue = cmds.floatField(scaleXMinField, editable = True, query = True, value = True)
  41. scaleYMaxFieldValue = cmds.floatField(scaleYMaxField, editable = True, query = True, value = True)
  42. scaleYMinFieldValue = cmds.floatField(scaleYMinField, editable = True, query = True, value = True)
  43. scaleZMaxFieldValue = cmds.floatField(scaleZMaxField, editable = True, query = True, value = True)
  44. scaleZMinFieldValue = cmds.floatField(scaleZMinField, editable = True, query = True, value = True)
  45. lockTranslateXValue = cmds.checkBox(lockTranslateX, editable = True, query = True, value = True)
  46. lockTranslateYValue = cmds.checkBox(lockTranslateY, editable = True, query = True, value = True)
  47. lockTranslateZValue = cmds.checkBox(lockTranslateZ, editable = True, query = True, value = True)
  48. lockRotateXValue = cmds.checkBox(lockRotateX, editable = True, query = True, value = True)
  49. lockRotateYValue = cmds.checkBox(lockRotateY, editable = True, query = True, value = True)
  50. lockRotateZValue = cmds.checkBox(lockRotateZ, editable = True, query = True, value = True)
  51. lockScaleXValue = cmds.checkBox(lockScaleX, editable = True, query = True, value = True)
  52. lockScaleYValue = cmds.checkBox(lockScaleY, editable = True, query = True, value = True)
  53. lockScaleZValue = cmds.checkBox(lockScaleZ, editable = True, query = True, value = True)
  54. selection = cmds.ls(selection = True)
  55. i = 0
  56. for eachObject in selection:
  57. transformPosition = cmds.xform(eachObject, query = True, translation = True)
  58. print('applyLimits(), the transformPositionValues for ' + str(eachObject) + ' are: ' + str(transformPosition))
  59. cmds.transformLimits(selection[i], enableTranslationX = [translateXMinLimit, translateXMaxLimit], tx=(transformPosition[0] + translateXMinFieldValue, transformPosition[0] + translateXMaxFieldValue))
  60. cmds.transformLimits(selection[i], enableTranslationY = [translateYMinLimit, translateYMaxLimit], ty=(transformPosition[1] + translateYMinFieldValue, transformPosition[1] + translateYMaxFieldValue))
  61. cmds.transformLimits(selection[i], enableTranslationZ = [translateZMinLimit, translateZMaxLimit], tz=(transformPosition[2] + translateZMinFieldValue, transformPosition[2] + translateZMaxFieldValue))
  62. cmds.transformLimits(selection[i], enableRotationX = [rotateXMinLimit, rotateXMaxLimit], rx =(rotateXMinFieldValue, rotateXMaxFieldValue))
  63. cmds.transformLimits(selection[i], enableRotationY = [rotateYMinLimit, rotateYMaxLimit], ry=(rotateYMinFieldValue, rotateYMaxFieldValue))
  64. cmds.transformLimits(selection[i], enableRotationZ = [rotateZMinLimit, rotateZMaxLimit], rz=(rotateZMinFieldValue, rotateZMaxFieldValue))
  65. cmds.transformLimits(selection[i], enableScaleX = [scaleXMinLimit, scaleXMaxLimit], sx =(scaleXMinFieldValue, scaleXMaxFieldValue))
  66. cmds.transformLimits(selection[i], enableScaleY = [scaleYMinLimit, scaleYMaxLimit], sy=(scaleYMinFieldValue, scaleYMaxFieldValue))
  67. cmds.transformLimits(selection[i], enableScaleZ = [scaleZMinLimit, scaleZMaxLimit], sz=(scaleZMinFieldValue, scaleZMaxFieldValue))
  68. print('the lockTranslateXValue is: ' + str(not lockTranslateXValue))
  69. cmds.setAttr(str(selection[i]) + '.translateX', lock = lockTranslateXValue, keyable = not lockTranslateXValue)
  70. cmds.setAttr(str(selection[i]) + '.translateY', lock = lockTranslateYValue, keyable = not lockTranslateYValue)
  71. cmds.setAttr(str(selection[i]) + '.translateZ', lock = lockTranslateZValue, keyable = not lockTranslateZValue)
  72. cmds.setAttr(str(selection[i]) + '.rotateX', lock = lockRotateXValue, keyable = not lockRotateXValue)
  73. cmds.setAttr(str(selection[i]) + '.rotateY', lock = lockRotateYValue, keyable = not lockRotateYValue)
  74. cmds.setAttr(str(selection[i]) + '.rotateZ', lock = lockRotateZValue, keyable = not lockRotateZValue)
  75. cmds.setAttr(str(selection[i]) + '.scaleX', lock = lockScaleXValue, keyable = not lockScaleXValue)
  76. cmds.setAttr(str(selection[i]) + '.scaleY', lock = lockScaleYValue, keyable = not lockScaleYValue)
  77. cmds.setAttr(str(selection[i]) + '.scaleZ', lock = lockScaleZValue, keyable = not lockScaleZValue)
  78. i = i + 1
  79. #Start GUI
  80. if cmds.window(windowID, exists=True):
  81. cmds.deleteUI(windowID)
  82. cmds.window(windowID, title='Set min, max transform limits', sizeable=True, width = 500)
  83. #Start of 1st frame
  84. cmds.rowColumnLayout(numberOfColumns=1, columnWidth=[(1,500)], columnOffset=[(1,'both', 3)])
  85. cmds.frameLayout(label='Translate Limits', collapsable=True)
  86. cmds.text(label = 'Select the obect to set limits, dial in limits and hit apply')
  87. #Start of 1st frame content
  88. cmds.rowColumnLayout(numberOfColumns=7, columnWidth=[ (1,130), (2,50), (3,10), (4,130), (5,50), (6,20), (7,120)], columnOffset=[(1,'both', 3)])
  89. #New Row
  90. setLimitsTranslateXMin = cmds.checkBox(label = 'translateX min value', editable = True)
  91. translateXMinField = cmds.floatField(editable = True)
  92. cmds.separator(h=10, style='none')
  93. setLimitsTranslateXMax = cmds.checkBox(label = 'translateX max value', editable = True)
  94. translateXMaxField = cmds.floatField(editable = True)
  95. cmds.separator(h=10, style='none')
  96. lockTranslateX = cmds.checkBox(label = 'lock and hide', editable = True)
  97. #New Row
  98. setLimitsTranslateYMin = cmds.checkBox(label ='translateY min value', editable = True)
  99. translateYMinField = cmds.floatField(editable = True)
  100. cmds.separator(h=10, style='none')
  101. setLimitsTranslateYMax = cmds.checkBox(label = 'translateY max value', editable = True)
  102. translateYMaxField = cmds.floatField(editable = True)
  103. cmds.separator(h=10, style='none')
  104. lockTranslateY = cmds.checkBox(label = 'lock and hide', editable = True)
  105. #New Row
  106. setLimitsTranslateZMin = cmds.checkBox(label ='translateZ min value', editable = True)
  107. translateZMinField = cmds.floatField(editable = True)
  108. cmds.separator(h=10, style='none')
  109. setLimitsTranslateZMax = cmds.checkBox(label = 'translateZ max value', editable = True)
  110. translateZMaxField = cmds.floatField(editable = True)
  111. cmds.separator(h=10, style='none')
  112. lockTranslateZ = cmds.checkBox(label = 'lock and hide', editable = True)
  113. cmds.setParent('..')
  114. #End of 1st frame
  115. #Start of 2nd frame
  116. cmds.frameLayout(label='Rotate Limits', collapsable=True)
  117. cmds.rowColumnLayout(numberOfColumns=7, columnWidth=[(1,130), (2,50), (3,10), (4,130), (5,50), (6,20), (7,120)], columnOffset=[(1,'both', 3)])
  118. setLimitsRotateXMin = cmds.checkBox(label = 'RotateX min value', editable = True)
  119. rotateXMinField = cmds.floatField(editable = True)
  120. cmds.separator(h=10, style='none')
  121. setLimitsRotateXMax = cmds.checkBox(label = 'RotateX max value', editable = True)
  122. rotateXMaxField = cmds.floatField(editable = True)
  123. cmds.separator(h=10, style='none')
  124. lockRotateX = cmds.checkBox(label = 'lock and hide', editable = True)
  125. #New Row
  126. setLimitsRotateYMin = cmds.checkBox(label = 'RotateY min value', editable = True)
  127. rotateYMinField = cmds.floatField(editable = True)
  128. cmds.separator(h=10, style='none')
  129. setLimitsRotateYMax = cmds.checkBox(label = 'RotateY max value', editable = True)
  130. rotateYMaxField = cmds.floatField(editable = True)
  131. cmds.separator(h=10, style='none')
  132. lockRotateY = cmds.checkBox(label = 'lock and hide', editable = True)
  133. #New Row
  134. setLimitsRotateZMin = cmds.checkBox(label = 'RotateZ min value', editable = True)
  135. rotateZMinField = cmds.floatField(editable = True)
  136. cmds.separator(h=10, style='none')
  137. setLimitsRotateZMax = cmds.checkBox(label = 'RotateZ max value', editable = True)
  138. rotateZMaxField = cmds.floatField(editable = True)
  139. cmds.separator(h=10, style='none')
  140. lockRotateZ = cmds.checkBox(label = 'lock and hide', editable = True)
  141. cmds.setParent('..')
  142. #End of Second Frame
  143. #Start of 3rd Frame
  144. cmds.frameLayout(label='Scale Limits', collapsable=True)
  145. cmds.rowColumnLayout(numberOfColumns=7, columnWidth=[(1,130), (2,50), (3,10), (4,130), (5,50), (6,20), (7,120)], columnOffset=[(1,'both', 3)])
  146. setLimitsScaleXMin = cmds.checkBox(label = 'ScaleX min value', editable = True)
  147. scaleXMinField = cmds.floatField(editable = True, value = 1)
  148. cmds.separator(h=10, style='none')
  149. setLimitsScaleXMax = cmds.checkBox(label = 'ScaleX max value', editable = True)
  150. scaleXMaxField = cmds.floatField(editable = True, value = 1)
  151. cmds.separator(h=10, style='none')
  152. lockScaleX = cmds.checkBox(label = 'lock and hide', editable = True)
  153. #New Row
  154. setLimitsScaleYMin = cmds.checkBox(label = 'ScaleY min value', editable = True)
  155. scaleYMinField = cmds.floatField(editable = True, value = 1)
  156. cmds.separator(h=10, style='none')
  157. setLimitsScaleYMax = cmds.checkBox(label = 'ScaleY max value', editable = True)
  158. scaleYMaxField = cmds.floatField(editable = True, value = 1)
  159. cmds.separator(h=10, style='none')
  160. lockScaleY = cmds.checkBox(label = 'lock and hide', editable = True)
  161. #New Row
  162. setLimitsScaleZMin = cmds.checkBox(label = 'ScaleZ min value', editable = True)
  163. scaleZMinField = cmds.floatField(editable = True, value = 1)
  164. cmds.separator(h=10, style='none')
  165. setLimitsScaleZMax = cmds.checkBox(label = 'ScaleZ max value', editable = True)
  166. scaleZMaxField = cmds.floatField(editable = True, value = 1)
  167. cmds.separator(h=10, style='none')
  168. lockScaleZ = cmds.checkBox(label = 'lock and hide', editable = True)
  169. cmds.setParent('..')
  170. #End of Second Frame
  171. #New Row
  172. cmds.button(label = 'Apply', command = functools.partial(applyLimits))
  173. cmds.showWindow()

 

0
  Related Posts
  • No related posts found.

You must be logged in to post a comment.