Main
|
Resume
|
Showreel
|
Blog
|
Script
|
Contact
connexion
go to footer
expand all
|
collapse all
languages
class
All
JScript|XSI
Python|XSI
JScript|WEB
Python|Maya
All
Memo
learning
DDD
php
tool
keyboard tooltips
rig
learn python 01
- learning - felixlecha
# 2012.11.21 - Learning Scripting Python/Maya session 03 # Create some PolyCube parented in beteween them or not. And Randomize their Transforms. With a UI to launch the Command. # Load Module import maya.cmds as mc import random as rand def create_Random_Object( inName, inType = 'Cube', inNbr = 10, inTrsStr = 10, inRotStr = 10, inParentMode=True ): ''' Create some PolyCube parented in beteween them. And Randomize their Transforms. Arguments : inName : string | The baseName object inType : string | Select the type of PolyMesh inNbr : integer | How many cube you want inTrsStr : float | What strength apply to random Translation inRotStr : float | What strength apply to random Rotation inMode : bolean | Parent Cube in beteween them or not ''' # Create Transform list lTrs = [ 'tx', 'ty', 'tz', 'rx', 'ry', 'rz' ] if inParentMode: # Create an empty Parent sParent = None # Create ProgressWindow mc.progressWindow( title='Create Object', progress=0, maxValue=inNbr, status='Create : ', isInterruptable=True ) for i in range( inNbr ): mc.progressWindow( edit=True, progress=i, status='Create : %s_%s_mesh' %(inName, (i+1)) ) # Create Object if inType == 'Sphere': sObj = mc.polySphere( n='%s_%s_mesh' %(inName, (i+1)) )[0] elif inType == 'Cube': sObj = mc.polyCube( n='%s_%s_mesh' %(inName, (i+1)) )[0] elif inType == 'Cylinder': sObj = mc.polyCylinder( n='%s_%s_mesh' %(inName, (i+1)) )[0] elif inType == 'Cone': sObj = mc.polyCone( n='%s_%s_mesh' %(inName, (i+1)) )[0] elif inType == 'Plane': sObj = mc.polyPlane( n='%s_%s_mesh' %(inName, (i+1)) )[0] elif inType == 'Torus': sObj = mc.polyTorus( n='%s_%s_mesh' %(inName, (i+1)) )[0] elif inType == 'Prism': sObj = mc.polyPrism( n='%s_%s_mesh' %(inName, (i+1)) )[0] elif inType == 'Pyramid': sObj = mc.polyPyramid( n='%s_%s_mesh' %(inName, (i+1)) )[0] elif inType == 'Pipe': sObj = mc.polyPipe( n='%s_%s_mesh' %(inName, (i+1)) )[0] elif inType == 'Helix': sObj = mc.polyHelix( n='%s_%s_mesh' %(inName, (i+1)) )[0] elif inType == 'Soccer_Ball': sObj = mc.polyPrimitive( n='%s_%s_mesh' %(inName, (i+1)) )[0] elif inType == 'Platonic_Solids': sObj = mc.polyPlatonicSolid( n='%s_%s_mesh' %(inName, (i+1)) )[0] else : sObj = mc.polyCube( n='%s_%s_mesh' %(inName, (i+1)) )[0] # Set Random Value in Transform for j in range(len(lTrs)): # For Translate : { -0.5, 0.5 } * Strength if j <= 2: mc.setAttr( '%s.%s' %(sObj, lTrs[j]), (rand.random()-0.5)*inTrsStr ) # For Rotate : { 0, 360 } else: mc.setAttr( '%s.%s' %(sObj, lTrs[j]), rand.random()*(36*inRotStr) ) if inParentMode: # If Parent is loaded : Parent to previous PolyCube if sParent: mc.parent( sObj, sParent, relative=False ) # Update Parent sParent = sObj mc.progressWindow( endProgress=True ) mc.warning( 'I create %i PoluCube. Bisous' %inNbr ) def create_Random_Object_Exe( *args ): ''' Execute the "create_Random_Object()" def with the value from "create_Random_Object_UI()" call : create_Random_Object() ''' # Get Values from UI cro_Name = mc.textField( 'cro_name', q=True, tx=True ) cro_Type = mc.optionMenuGrp( 'cro_object_type', q=True, v=True ) cro_Nbr = mc.intSliderGrp( 'cro_nbr', q=True, v=True ) cro_Trs_Str = mc.floatSliderGrp( 'cro_translation_strength', q=True, v=True ) cro_Rot_Str = mc.floatSliderGrp( 'cro_rotate_strength', q=True, v=True ) cro_Mode_P = mc.checkBox( 'cro_mode_parent', q=True, v=True ) create_Random_Object( inName = cro_Name, inType = cro_Type, inNbr = cro_Nbr, inTrsStr = cro_Trs_Str, inRotStr = cro_Rot_Str, inParentMode = cro_Mode_P ) def create_Random_Object_UI(): ''' Interface User to launchthe "create_Random_Object()" def call : create_Random_Object_UI() ''' # Define new Window name sWindow = 'tool' # Test if Window already existe, if is True Delete it if mc.window( sWindow, q=True, ex=True ): mc.deleteUI( sWindow ) # Create Window mc.window( sWindow, t='Create Randomized Objects' ) # Create Layout sLay = mc.columnLayout( columnAttach=('both', 5), rowSpacing=3, adj=True, p = sWindow ) # Create TextField mc.text( label='Base Name ', p=sLay ) mc.textField( 'cro_name', p=sLay, text='newObject', annotation='Define the Base Name of the Object' ) lPolyMesh = ['Sphere','Cube','Cylinder','Cone','Plane','Torus','Prism','Pyramid','Pipe','Helix','Soccer_Ball','Platonic_Solids' ] mc.optionMenuGrp( 'cro_object_type', label='Object Type', p=sLay ) for sPolyMesh in lPolyMesh: mc.menuItem( label = sPolyMesh ) mc.text( label='nObjects ', p=sLay ) mc.intSliderGrp( 'cro_nbr', field=True, minValue=1, value=10, p=sLay, annotation='Define how many objects you want to create' ) mc.separator( style='in' ) mc.text( label='Randomize Settings', p=sLay ) mc.separator( style='out' ) mc.text( label='Translation Strength ', p=sLay ) mc.floatSliderGrp( 'cro_translation_strength', min=-0, max=100, value=10, field=True, p=sLay, annotation='Define the random of the Translation' ) mc.text( label='Rotate Strength ', p=sLay ) mc.floatSliderGrp( 'cro_rotate_strength', min=-0, max=10, value=10, field=True, p=sLay, annotation='Define the random of the Rotate' ) # Create a CheckBox to check the Mode mc.checkBox( 'cro_mode_parent', label='Mode Parent', v=True, p=sLay, annotation='Check to parent in between Object or not' ) # Create Button mc.button( label='CREATE', h=40, w=180, p=sLay, c='create_Random_Object_Exe()', annotation='Create Randomized Objects' ) mc.separator( style='none', height=2 ) # Show Window mc.showWindow( sWindow ) create_Random_Object_UI()