Advertisement
bradon

RandomTransform

Mar 18th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.78 KB | None | 0 0
  1. #===============================================================================
  2. # Randomly Transform an object or selected objects
  3. # author: Bradon Webb
  4. # 03/18/13
  5. #
  6. #
  7. # Run script for UI
  8. #
  9. #
  10. # ==============================================================================
  11.  
  12.  
  13. import maya.cmds as mc
  14. import random
  15.  
  16. def randomTransform(selectedNodes=None, minTranslation=0, maxTranslation=10, minRotation=0, maxRotation=360, minScale=.01, maxScale=1,
  17.                                axes=['X','Y','Z'], translateRandom=True, rotateRandom=True, scaleRandom=True, scaleUniform=True):
  18.     '''
  19.    @summary:
  20.        randomly transforms an input object(s) OR if none it will randomly transform the selected objects
  21.    @arguments:
  22.        selectedNodes = list of nodes or will use a selection if none specified
  23.        minTranslation = float for minimum translate
  24.        maxTranslation = float for maximum translate
  25.        minRotation = float for minimum translate
  26.        maxRotation = float for maximum translate
  27.        minScale = float for minimum translate
  28.        maxScale = float for maximum translate
  29.        axes = list of axis to do randomizations on    
  30.        translateRandom = Boolean to do translation channels
  31.        rotateRandom = Boolean to do Rotation channels    
  32.        scaleRandom = Boolean to do Scale channels
  33.        scaleUniform = Boolean to keep Scale values uniform
  34.    '''
  35.  
  36.     # If no input mehses specified, then store selected objects in a list    
  37.     if not selectedNodes:
  38.         selectedNodes = mc.ls(selection=True, long=True, type='transform')
  39.    
  40.     # If no objects are selected error informatively
  41.     if not selectedNodes:
  42.         raise RuntimeError, 'please select an object to randomly transform'
  43.                  
  44.     # For each selected mesh, and each specified axis, loop with random values for translation rotation and scale                  
  45.     for selectedNode in selectedNodes:
  46.         # Random Scale for Uniform Scaling
  47.         randomScaleValue = random.uniform(minScale, maxScale)
  48.        
  49.         if selectedNode and axes:
  50.             for axis in axes:
  51.                 if translateRandom:
  52.                     randomTranslateValue = random.uniform(minTranslation, maxTranslation)
  53.                     mc.setAttr('{selectedNode}.translate{axis}'.format(selectedNode=selectedNode, axis=axis.upper()), randomTranslateValue)
  54.                
  55.                 if rotateRandom:
  56.                     randomRotationValue = random.uniform(minRotation, maxRotation)
  57.                     mc.setAttr('{selectedNode}.rotate{axis}'.format(selectedNode=selectedNode, axis=axis.upper()), randomRotationValue)
  58.                
  59.                 if scaleRandom:
  60.                     if not scaleUniform:      
  61.                         randomScaleValue = random.uniform(minScale, maxScale)
  62.                     mc.setAttr('{selectedNode}.scale{axis}'.format(selectedNode=selectedNode, axis=axis.upper()), randomScaleValue)                    
  63.  
  64. # Begin UI -----------------------------------------------------------
  65.  
  66. def loadHelp(*args):
  67.     pass
  68.  
  69. def randomTransformUI():
  70.    
  71.     def getUIDetails(*args):
  72.  
  73.         # check the axis Checkbox
  74.         Xaxis = mc.checkBox(XaxisCheckBox, query=True, value=True)
  75.         Yaxis = mc.checkBox(YaxisCheckBox, query=True, value=True)
  76.         Zaxis = mc.checkBox(ZaxisCheckBox, query=True, value=True)
  77.        
  78.         # deifnine axis variable to populate
  79.         axes = []
  80.        
  81.         # Add values to the axis list
  82.         if Xaxis:
  83.             axes.append('X')
  84.         if Yaxis:
  85.             axes.append('Y')
  86.         if Zaxis:
  87.             axes.append('Z')
  88.            
  89.         # if user has not specified an axis warn user    
  90.         if not axes:
  91.             raise RuntimeError, 'Please select at least one axis'
  92.  
  93.         # get UI detials
  94.         translateRandom = mc.checkBox(translateCheckBox, query=True, value=True)
  95.         rotateRandom = mc.checkBox(rotateCheckBox, query=True, value=True)
  96.         scaleRandom = mc.checkBox(scaleCheckBox, query=True, value=True)
  97.         scaleUniform = mc.checkBox(uniformScaleCheckBox, query=True, value=True)
  98.        
  99.         minTranslation = mc.floatField(translateMinFloat, query=True, value=True)
  100.         maxTranslation = mc.floatField(translateMaxFloat, query=True, value=True)
  101.        
  102.         minRotation = mc.floatField(rotateMinFloat, query=True, value=True)
  103.         maxRotation = mc.floatField(rotateMaxFloat, query=True, value=True)
  104.        
  105.         minScale = mc.floatField(scaleMinFloat, query=True, value=True)
  106.         maxScale = mc.floatField(scaleMaxFloat, query=True, value=True)
  107.        
  108.        
  109.         #-- Debug
  110.         '''        
  111.        print axes
  112.        print translateRandom
  113.        print rotateRandom
  114.        print scaleRandom
  115.        print scaleUniform
  116.        print minTranslation
  117.        print maxTranslation
  118.        print minRotation
  119.        print maxRotation
  120.        print minScale
  121.        print maxScale
  122.        '''
  123.        
  124.         # run function with args
  125.         randomTransform(selectedNodes=None,
  126.                         minTranslation=minTranslation,
  127.                         maxTranslation=maxTranslation,
  128.                         minRotation=minRotation,
  129.                         maxRotation=maxRotation,
  130.                         minScale=minScale,
  131.                         maxScale=maxScale,
  132.                         axes=axes,
  133.                         translateRandom=translateRandom,
  134.                         rotateRandom=rotateRandom,
  135.                         scaleRandom=scaleRandom,
  136.                         scaleUniform=scaleUniform,)
  137.        
  138.         #End Get Details
  139.        
  140.     def updateUI(*args):
  141.         # Check the state of the checkBoxes
  142.         translateCheckBoxState = mc.checkBox(translateCheckBox, query=True, value=True)
  143.         rotateCheckBoxState = mc.checkBox(rotateCheckBox, query=True, value=True)
  144.         scaleCheckBoxState = mc.checkBox(scaleCheckBox, query=True, value=True)
  145.        
  146.         # update the enable state on or off depending on the check box state
  147.         mc.floatField(translateMinFloat, edit=True, enable=translateCheckBoxState)
  148.         mc.floatField(translateMaxFloat, edit=True, enable=translateCheckBoxState)
  149.        
  150.         mc.floatField(rotateMinFloat, edit=True, enable=rotateCheckBoxState)
  151.         mc.floatField(rotateMaxFloat, edit=True, enable=rotateCheckBoxState)
  152.        
  153.         mc.floatField(scaleMinFloat, edit=True, enable=scaleCheckBoxState)
  154.         mc.floatField(scaleMaxFloat, edit=True, enable=scaleCheckBoxState)
  155.        
  156.        
  157.        
  158.        
  159.     # Define new window name
  160.     newWindow = 'randomTransform'    
  161.    
  162.     # Check to see if the Window exists, Delete Window
  163.     if mc.window(newWindow, query=True, exists=True):
  164.         mc.deleteUI(newWindow)
  165.    
  166.     # Define window attributes
  167.     mc.window(newWindow, title='randomTransform', sizeable=True)
  168.    
  169.     # Create menu bar with help    
  170.     menuBarLayout = mc.menuBarLayout()
  171.     helpMenu = mc.menu( label='help', tearOff=False )
  172.     howToHelp = mc.menuItem( label='How to use', command=loadHelp)
  173.    
  174.     # Main Layout of all menu is a form
  175.     mainFormLayout = mc.formLayout()
  176.    
  177.     menuSeparator = mc.separator(style='in')
  178.    
  179.     axisLabel = mc.text('Axis')
  180.    
  181.     XaxisCheckBox = mc.checkBox(label='X')
  182.     YaxisCheckBox = mc.checkBox(label='Y')
  183.     ZaxisCheckBox = mc.checkBox(label='Z')
  184.    
  185.     axisSeparator = mc.separator(style='in')
  186.    
  187.     translateLabel = mc.text('Translate')
  188.     translateCheckBox = mc.checkBox(label='', onCommand=updateUI, offCommand=updateUI)
  189.  
  190.     translateMinMaxLabel = mc.text(label='Min/Max')
  191.     translateMinFloat = mc.floatField(annotation='Translation Min', enable=False)
  192.     translateMaxFloat = mc.floatField(annotation='Translation Max', enable=False)
  193.    
  194.     translateSeparator = mc.separator(style='in')
  195.    
  196.     rotateLabel = mc.text('Rotate')
  197.     rotateCheckBox = mc.checkBox(label='', onCommand=updateUI, offCommand=updateUI)
  198.  
  199.     rotateMinMaxLabel = mc.text(label='Min/Max')
  200.     rotateMinFloat = mc.floatField(annotation='Rotate Min', enable=False)
  201.     rotateMaxFloat = mc.floatField(annotation='Rotate Max', enable=False)
  202.    
  203.     rotateSeparator = mc.separator(style='in')
  204.    
  205.     scaleLabel = mc.text('Scale')
  206.     scaleCheckBox = mc.checkBox(label='', onCommand=updateUI, offCommand=updateUI)
  207.    
  208.     uniformScaleLabel = mc.text('Uniform')
  209.     uniformScaleCheckBox = mc.checkBox(label='')
  210.  
  211.     scaleMinMaxLabel = mc.text(label='Min/Max')
  212.     scaleMinFloat = mc.floatField(annotation='Scale Min', enable=False)
  213.     scaleMaxFloat = mc.floatField(annotation='Scale Max', enable=False)
  214.    
  215.     scaleSeparator = mc.separator(style='in')
  216.    
  217.     randomizeButton = mc.button(label='Randomize Transforms', command=getUIDetails)
  218.    
  219.    
  220.     # Position the controls
  221.     mc.formLayout(mainFormLayout, edit=True, # attach to the form
  222.                                            attachForm=[(menuSeparator, 'left', 3),
  223.                                                        (menuSeparator, 'right', 3),
  224.                                                        (menuSeparator, 'top', 3),
  225.  
  226.                                                        (axisSeparator, 'left', 3),
  227.                                                        (axisSeparator, 'right', 3),
  228.                                                        
  229.                                                        (translateMinMaxLabel, 'left', 10),
  230.                                                        (translateMaxFloat, 'right', 10),
  231.                                                        
  232.                                                        (translateSeparator, 'left', 3),
  233.                                                        (translateSeparator, 'right', 3),
  234.                                                        
  235.                                                        (rotateMinMaxLabel, 'left', 10),
  236.                                                        (rotateMaxFloat, 'right', 10),
  237.                                                    
  238.                                                        (rotateSeparator, 'left', 3),
  239.                                                        (rotateSeparator, 'right', 3),
  240.                                                        
  241.                                                        (scaleMinMaxLabel, 'left', 10),
  242.                                                        (scaleMaxFloat, 'right', 10),
  243.                                                        
  244.                                                        (scaleSeparator, 'left', 3),
  245.                                                        (scaleSeparator, 'right', 3),                                                      
  246.                                                        
  247.                                                        (randomizeButton, 'left', 3),
  248.                                                        (randomizeButton, 'right', 3),
  249.                                                        (randomizeButton, 'bottom', 3),
  250.                                                                                                              
  251.                                            ],
  252.  
  253.                                            attachControl=[(XaxisCheckBox, 'top', 3, menuSeparator),
  254.                                                           (YaxisCheckBox, 'top', 3, menuSeparator),
  255.                                                           (ZaxisCheckBox, 'top', 3, menuSeparator),
  256.                                                           (axisLabel, 'top', 3, menuSeparator),
  257.                                                                                                                    
  258.                                                           (XaxisCheckBox, 'left', 3, axisLabel),
  259.                                                           (YaxisCheckBox, 'left', 3, XaxisCheckBox),
  260.                                                           (ZaxisCheckBox, 'left', 3, YaxisCheckBox),
  261.                                                          
  262.                                                           #--translate controls
  263.                                                           (axisSeparator, 'top', 3, XaxisCheckBox),
  264.                                                          
  265.                                                           (translateLabel, 'top', 10, axisSeparator),
  266.                                                           (translateCheckBox, 'top', 10, axisSeparator),
  267.                                                           (translateCheckBox, 'left', 3, translateLabel),
  268.                                                          
  269.                                                           (translateMinMaxLabel, 'top', 10, translateLabel),
  270.                                                           (translateMinFloat, 'top', 10, translateLabel),
  271.                                                           (translateMaxFloat, 'top', 10, translateLabel),                                                          
  272.                                                           (translateMinFloat, 'left', 3, translateMinMaxLabel),
  273.                                                          
  274.                                                           #--rotate controls
  275.                                                           (translateSeparator, 'top', 10, translateMinFloat),
  276.                                                          
  277.                                                           (rotateLabel, 'top', 10, translateSeparator),
  278.                                                           (rotateCheckBox, 'top', 10, translateSeparator),
  279.                                                           (rotateCheckBox, 'left', 3, rotateLabel),
  280.                                                          
  281.                                                           (rotateMinMaxLabel, 'top', 10, rotateLabel),
  282.                                                           (rotateMinFloat, 'top', 10, rotateLabel),
  283.                                                           (rotateMaxFloat, 'top', 10, rotateLabel),                                                          
  284.                                                           (rotateMinFloat, 'left', 3, rotateMinMaxLabel),
  285.                                                          
  286.                                                           #--scale controls
  287.                                                           (rotateSeparator, 'top', 10, rotateMinFloat),
  288.                                                          
  289.                                                           (scaleLabel, 'top', 10, rotateSeparator),
  290.                                                           (scaleCheckBox, 'top', 10, rotateSeparator),
  291.                                                           (scaleCheckBox, 'left', 3, scaleLabel),
  292.                                                          
  293.                                                           (uniformScaleLabel, 'top', 3, scaleLabel),
  294.                                                           (uniformScaleCheckBox, 'top', 3, scaleLabel),
  295.                                                           (uniformScaleCheckBox, 'left', 3, uniformScaleLabel),
  296.                                                            
  297.                                                           (scaleMinMaxLabel, 'top', 10, uniformScaleLabel),
  298.                                                           (scaleMinFloat, 'top', 10, uniformScaleLabel),
  299.                                                           (scaleMaxFloat, 'top', 10, uniformScaleLabel),                                                          
  300.                                                           (scaleMinFloat, 'left', 3, scaleMinMaxLabel),
  301.                                                          
  302.                                                           (scaleSeparator, 'top', 10, scaleMinFloat),
  303.                                                          
  304.                                            ],
  305.                                            
  306.                                            attachPosition=[(translateLabel, 'right', 2, 45),
  307.                                                            (axisLabel, 'right', 2, 45),
  308.  
  309.                                                            (translateMinFloat, 'right', 2, 50),
  310.                                                            (translateMaxFloat, 'left', 2, 50),
  311.                                                            
  312.                                                            (rotateLabel, 'right', 2, 45),
  313.                                                            
  314.                                                            (rotateMinFloat, 'right', 2, 50),
  315.                                                            (rotateMaxFloat, 'left', 2, 50),
  316.                                                            
  317.                                                            (scaleLabel, 'right', 2, 45),
  318.                                                            (uniformScaleLabel, 'right', 2, 45),
  319.                                                            
  320.                                                            (scaleMinFloat, 'right', 2, 50),
  321.                                                            (scaleMaxFloat, 'left', 2, 50),
  322.                                                            
  323.                                                            (translateSeparator, 'bottom', 2, 25),
  324.                                                            (rotateSeparator, 'bottom', 2, 50),
  325.                                                            (scaleSeparator, 'bottom', 2, 80),
  326.                                            ]
  327.                   )
  328.  
  329.     mc.showWindow(newWindow)
  330.    
  331. randomTransformUI()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement