Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #===============================================================================
- # Randomly Transform an object or selected objects
- # author: Bradon Webb
- # 03/18/13
- #
- #
- # Run script for UI
- #
- #
- # ==============================================================================
- import maya.cmds as mc
- import random
- def randomTransform(selectedNodes=None, minTranslation=0, maxTranslation=10, minRotation=0, maxRotation=360, minScale=.01, maxScale=1,
- axes=['X','Y','Z'], translateRandom=True, rotateRandom=True, scaleRandom=True, scaleUniform=True):
- '''
- @summary:
- randomly transforms an input object(s) OR if none it will randomly transform the selected objects
- @arguments:
- selectedNodes = list of nodes or will use a selection if none specified
- minTranslation = float for minimum translate
- maxTranslation = float for maximum translate
- minRotation = float for minimum translate
- maxRotation = float for maximum translate
- minScale = float for minimum translate
- maxScale = float for maximum translate
- axes = list of axis to do randomizations on
- translateRandom = Boolean to do translation channels
- rotateRandom = Boolean to do Rotation channels
- scaleRandom = Boolean to do Scale channels
- scaleUniform = Boolean to keep Scale values uniform
- '''
- # If no input mehses specified, then store selected objects in a list
- if not selectedNodes:
- selectedNodes = mc.ls(selection=True, long=True, type='transform')
- # If no objects are selected error informatively
- if not selectedNodes:
- raise RuntimeError, 'please select an object to randomly transform'
- # For each selected mesh, and each specified axis, loop with random values for translation rotation and scale
- for selectedNode in selectedNodes:
- # Random Scale for Uniform Scaling
- randomScaleValue = random.uniform(minScale, maxScale)
- if selectedNode and axes:
- for axis in axes:
- if translateRandom:
- randomTranslateValue = random.uniform(minTranslation, maxTranslation)
- mc.setAttr('{selectedNode}.translate{axis}'.format(selectedNode=selectedNode, axis=axis.upper()), randomTranslateValue)
- if rotateRandom:
- randomRotationValue = random.uniform(minRotation, maxRotation)
- mc.setAttr('{selectedNode}.rotate{axis}'.format(selectedNode=selectedNode, axis=axis.upper()), randomRotationValue)
- if scaleRandom:
- if not scaleUniform:
- randomScaleValue = random.uniform(minScale, maxScale)
- mc.setAttr('{selectedNode}.scale{axis}'.format(selectedNode=selectedNode, axis=axis.upper()), randomScaleValue)
- # Begin UI -----------------------------------------------------------
- def loadHelp(*args):
- pass
- def randomTransformUI():
- def getUIDetails(*args):
- # check the axis Checkbox
- Xaxis = mc.checkBox(XaxisCheckBox, query=True, value=True)
- Yaxis = mc.checkBox(YaxisCheckBox, query=True, value=True)
- Zaxis = mc.checkBox(ZaxisCheckBox, query=True, value=True)
- # deifnine axis variable to populate
- axes = []
- # Add values to the axis list
- if Xaxis:
- axes.append('X')
- if Yaxis:
- axes.append('Y')
- if Zaxis:
- axes.append('Z')
- # if user has not specified an axis warn user
- if not axes:
- raise RuntimeError, 'Please select at least one axis'
- # get UI detials
- translateRandom = mc.checkBox(translateCheckBox, query=True, value=True)
- rotateRandom = mc.checkBox(rotateCheckBox, query=True, value=True)
- scaleRandom = mc.checkBox(scaleCheckBox, query=True, value=True)
- scaleUniform = mc.checkBox(uniformScaleCheckBox, query=True, value=True)
- minTranslation = mc.floatField(translateMinFloat, query=True, value=True)
- maxTranslation = mc.floatField(translateMaxFloat, query=True, value=True)
- minRotation = mc.floatField(rotateMinFloat, query=True, value=True)
- maxRotation = mc.floatField(rotateMaxFloat, query=True, value=True)
- minScale = mc.floatField(scaleMinFloat, query=True, value=True)
- maxScale = mc.floatField(scaleMaxFloat, query=True, value=True)
- #-- Debug
- '''
- print axes
- print translateRandom
- print rotateRandom
- print scaleRandom
- print scaleUniform
- print minTranslation
- print maxTranslation
- print minRotation
- print maxRotation
- print minScale
- print maxScale
- '''
- # run function with args
- randomTransform(selectedNodes=None,
- minTranslation=minTranslation,
- maxTranslation=maxTranslation,
- minRotation=minRotation,
- maxRotation=maxRotation,
- minScale=minScale,
- maxScale=maxScale,
- axes=axes,
- translateRandom=translateRandom,
- rotateRandom=rotateRandom,
- scaleRandom=scaleRandom,
- scaleUniform=scaleUniform,)
- #End Get Details
- def updateUI(*args):
- # Check the state of the checkBoxes
- translateCheckBoxState = mc.checkBox(translateCheckBox, query=True, value=True)
- rotateCheckBoxState = mc.checkBox(rotateCheckBox, query=True, value=True)
- scaleCheckBoxState = mc.checkBox(scaleCheckBox, query=True, value=True)
- # update the enable state on or off depending on the check box state
- mc.floatField(translateMinFloat, edit=True, enable=translateCheckBoxState)
- mc.floatField(translateMaxFloat, edit=True, enable=translateCheckBoxState)
- mc.floatField(rotateMinFloat, edit=True, enable=rotateCheckBoxState)
- mc.floatField(rotateMaxFloat, edit=True, enable=rotateCheckBoxState)
- mc.floatField(scaleMinFloat, edit=True, enable=scaleCheckBoxState)
- mc.floatField(scaleMaxFloat, edit=True, enable=scaleCheckBoxState)
- # Define new window name
- newWindow = 'randomTransform'
- # Check to see if the Window exists, Delete Window
- if mc.window(newWindow, query=True, exists=True):
- mc.deleteUI(newWindow)
- # Define window attributes
- mc.window(newWindow, title='randomTransform', sizeable=True)
- # Create menu bar with help
- menuBarLayout = mc.menuBarLayout()
- helpMenu = mc.menu( label='help', tearOff=False )
- howToHelp = mc.menuItem( label='How to use', command=loadHelp)
- # Main Layout of all menu is a form
- mainFormLayout = mc.formLayout()
- menuSeparator = mc.separator(style='in')
- axisLabel = mc.text('Axis')
- XaxisCheckBox = mc.checkBox(label='X')
- YaxisCheckBox = mc.checkBox(label='Y')
- ZaxisCheckBox = mc.checkBox(label='Z')
- axisSeparator = mc.separator(style='in')
- translateLabel = mc.text('Translate')
- translateCheckBox = mc.checkBox(label='', onCommand=updateUI, offCommand=updateUI)
- translateMinMaxLabel = mc.text(label='Min/Max')
- translateMinFloat = mc.floatField(annotation='Translation Min', enable=False)
- translateMaxFloat = mc.floatField(annotation='Translation Max', enable=False)
- translateSeparator = mc.separator(style='in')
- rotateLabel = mc.text('Rotate')
- rotateCheckBox = mc.checkBox(label='', onCommand=updateUI, offCommand=updateUI)
- rotateMinMaxLabel = mc.text(label='Min/Max')
- rotateMinFloat = mc.floatField(annotation='Rotate Min', enable=False)
- rotateMaxFloat = mc.floatField(annotation='Rotate Max', enable=False)
- rotateSeparator = mc.separator(style='in')
- scaleLabel = mc.text('Scale')
- scaleCheckBox = mc.checkBox(label='', onCommand=updateUI, offCommand=updateUI)
- uniformScaleLabel = mc.text('Uniform')
- uniformScaleCheckBox = mc.checkBox(label='')
- scaleMinMaxLabel = mc.text(label='Min/Max')
- scaleMinFloat = mc.floatField(annotation='Scale Min', enable=False)
- scaleMaxFloat = mc.floatField(annotation='Scale Max', enable=False)
- scaleSeparator = mc.separator(style='in')
- randomizeButton = mc.button(label='Randomize Transforms', command=getUIDetails)
- # Position the controls
- mc.formLayout(mainFormLayout, edit=True, # attach to the form
- attachForm=[(menuSeparator, 'left', 3),
- (menuSeparator, 'right', 3),
- (menuSeparator, 'top', 3),
- (axisSeparator, 'left', 3),
- (axisSeparator, 'right', 3),
- (translateMinMaxLabel, 'left', 10),
- (translateMaxFloat, 'right', 10),
- (translateSeparator, 'left', 3),
- (translateSeparator, 'right', 3),
- (rotateMinMaxLabel, 'left', 10),
- (rotateMaxFloat, 'right', 10),
- (rotateSeparator, 'left', 3),
- (rotateSeparator, 'right', 3),
- (scaleMinMaxLabel, 'left', 10),
- (scaleMaxFloat, 'right', 10),
- (scaleSeparator, 'left', 3),
- (scaleSeparator, 'right', 3),
- (randomizeButton, 'left', 3),
- (randomizeButton, 'right', 3),
- (randomizeButton, 'bottom', 3),
- ],
- attachControl=[(XaxisCheckBox, 'top', 3, menuSeparator),
- (YaxisCheckBox, 'top', 3, menuSeparator),
- (ZaxisCheckBox, 'top', 3, menuSeparator),
- (axisLabel, 'top', 3, menuSeparator),
- (XaxisCheckBox, 'left', 3, axisLabel),
- (YaxisCheckBox, 'left', 3, XaxisCheckBox),
- (ZaxisCheckBox, 'left', 3, YaxisCheckBox),
- #--translate controls
- (axisSeparator, 'top', 3, XaxisCheckBox),
- (translateLabel, 'top', 10, axisSeparator),
- (translateCheckBox, 'top', 10, axisSeparator),
- (translateCheckBox, 'left', 3, translateLabel),
- (translateMinMaxLabel, 'top', 10, translateLabel),
- (translateMinFloat, 'top', 10, translateLabel),
- (translateMaxFloat, 'top', 10, translateLabel),
- (translateMinFloat, 'left', 3, translateMinMaxLabel),
- #--rotate controls
- (translateSeparator, 'top', 10, translateMinFloat),
- (rotateLabel, 'top', 10, translateSeparator),
- (rotateCheckBox, 'top', 10, translateSeparator),
- (rotateCheckBox, 'left', 3, rotateLabel),
- (rotateMinMaxLabel, 'top', 10, rotateLabel),
- (rotateMinFloat, 'top', 10, rotateLabel),
- (rotateMaxFloat, 'top', 10, rotateLabel),
- (rotateMinFloat, 'left', 3, rotateMinMaxLabel),
- #--scale controls
- (rotateSeparator, 'top', 10, rotateMinFloat),
- (scaleLabel, 'top', 10, rotateSeparator),
- (scaleCheckBox, 'top', 10, rotateSeparator),
- (scaleCheckBox, 'left', 3, scaleLabel),
- (uniformScaleLabel, 'top', 3, scaleLabel),
- (uniformScaleCheckBox, 'top', 3, scaleLabel),
- (uniformScaleCheckBox, 'left', 3, uniformScaleLabel),
- (scaleMinMaxLabel, 'top', 10, uniformScaleLabel),
- (scaleMinFloat, 'top', 10, uniformScaleLabel),
- (scaleMaxFloat, 'top', 10, uniformScaleLabel),
- (scaleMinFloat, 'left', 3, scaleMinMaxLabel),
- (scaleSeparator, 'top', 10, scaleMinFloat),
- ],
- attachPosition=[(translateLabel, 'right', 2, 45),
- (axisLabel, 'right', 2, 45),
- (translateMinFloat, 'right', 2, 50),
- (translateMaxFloat, 'left', 2, 50),
- (rotateLabel, 'right', 2, 45),
- (rotateMinFloat, 'right', 2, 50),
- (rotateMaxFloat, 'left', 2, 50),
- (scaleLabel, 'right', 2, 45),
- (uniformScaleLabel, 'right', 2, 45),
- (scaleMinFloat, 'right', 2, 50),
- (scaleMaxFloat, 'left', 2, 50),
- (translateSeparator, 'bottom', 2, 25),
- (rotateSeparator, 'bottom', 2, 50),
- (scaleSeparator, 'bottom', 2, 80),
- ]
- )
- mc.showWindow(newWindow)
- randomTransformUI()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement