Skip to content

Instantly share code, notes, and snippets.

@gregtemp
Last active August 29, 2015 14:02
Show Gist options
  • Save gregtemp/b96ee2925ec3478bcab6 to your computer and use it in GitHub Desktop.
Save gregtemp/b96ee2925ec3478bcab6 to your computer and use it in GitHub Desktop.
REX - Random Extrusion - Maya Python
# 1. Select the object you want to randomly extrude
# or use the "Make eCube" button to make one.
# 2. Press Check Selection.
# 3. Select the amount of random extrusion loops
# you would like to make and click Random Extrude
# 4. Click Keyframe to key the current frame for all
# vertices
import maya.cmds as cmds
import random
shapeName =''
objectName =''
def makeNewCube(*args):
global objectName
global shapeName
cubes = list()
cubes = cmds.ls('eCube')
if (len(cubes)>0):
cmds.delete(cubes)
else:
print('no cubes')
cmds.polyCube(n='eCube', sx=10, sy=15, sz=5, h=5, w=5, d=5 )
selectionList = cmds.ls(selection=True)
objectName = selectionList[0]
selectionList = cmds.listRelatives(s=True)
shapeName = selectionList[0]
#print(shapeName + ' ' + objectName)
def checkSelectName(*args):
global objectName
global shapeName
selectionList = cmds.ls(selection=True)
objectName = selectionList[0]
selectionList = cmds.listRelatives(s=True)
shapeName = selectionList[0]
#print(shapeName + ' ' + objectName)
def moveFace(*args):
global objectName
global shapeName
inputInt = cmds.intSliderGrp('extI', query=True, value=True)
cmds.select(objectName)
amountOfFaces = cmds.polyEvaluate(f=True)
for mmm in range(0, inputInt):
n1 = random.randrange(0,amountOfFaces)
n2 = random.randrange(0,amountOfFaces)
if n1>n2:
faceStr = '%s.f[%i:%i]' %(objectName, n2, n1)
else:
faceStr = '%s.f[%i:%i]' %(objectName, n1, n2)
#print ('selected faces -> ' + faceStr)
cmds.select(faceStr)
m1 = random.uniform(-3, 3)
direction = random.randrange(0,3)
if direction == 0:
cmds.move(m1, 0, 0, r=True, os=True, wd=True)
elif direction == 1:
cmds.move(0, m1, 0, r=True, os=True, wd=True)
else:
cmds.move(0, 0, m1, r=True, os=True, wd=True)
def setKey(*args):
global objectName
global shapeName
kFrameStr = '%s.pt[:]' %(shapeName)
cmds.setKeyframe(kFrameStr)
#cmds.selectKey(shapeName, time=(pStartTime, pEndTime), attribute=pTargetAttribute, keyframe=True)
cmds.keyTangent(inTangentType='linear', outTangentType='linear')
print('keyed')
cmds.autoKeyframe(state=False)
#makeNewCube()
#moveFace(thisManyTimes)
#setKey()
class rexUI():
def __init__(self):
rexUi = 'rexUi'
if cmds.window(rexUi, exists=True):
cmds.deleteUI(rexUi)
rexUi = cmds.window(rexUi, t='REX', tb=True, rtf=True)
form = cmds.columnLayout(adjustableColumn=True)
bChSel = cmds.button('Check Selection', command=checkSelectName)
bBuild = cmds.button('Build eCube', command=makeNewCube)
cmds.separator(h=10, style="none")
cmds.text(label="Extrude Iterations")
cmds.intSliderGrp('extI', field=True, minValue=1, maxValue=200, value=10)
cmds.separator(h=5, style="none")
bRandomize = cmds.button('Random Extrude', command=moveFace)
bKeyframe = cmds.button('Keyframe', command=setKey)
cmds.showWindow(rexUi)
cmds.window(rexUi, edit=True, widthHeight=(150,150))
rexUser = rexUI()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment