我最近在重新学习Maya动画,在学习的时候我发现绑定需要脚本,所以我又重新开始复习maya脚本。总得来说maya的脚本完全没有难度,就是那些接口要记忆一下。我非常讨厌记代码,所以我把学习到的代码样本记在博客里,以后要用的时候直接copy
【1】Create window in maya
import maya.cmds as cmds
def create_window():
if cmds.window('mywindow', exists = True):
cmds.deleteUI('mywindow')
windowvar = cmds.window('mywindow')
cmds.columnLayout()
cmds.text(label = 'this is my window')
cmds.textField('pStatementInput')
cmds.floatField('pFloatInput')
cmds.intField('pIntInput')
cmds.button(label = 'print statement', command = 'printFunction()')
cmds.showWindow('mywindow')
def printFunction():
pStatement = cmds.textField('pStatementInput', q = True, text = True)
pInt = cmds.intField('pIntInput', q = True, value = True)
pFloat = cmds.floatField('pFloatInput', q = True, value = True)
print(pStatement + str(pInt) + str(pFloat))
create_window()
运行结果:
【2】CreatePoly
import maya.cmds as cmds
cubeList = cmds.ls('myCube')
sphereList = cmds.ls('mySphere')
if len(cubeList) > 0:
cmds.delete(cubeList)
if len(sphereList) > 0:
cmds.delete(sphereList)
pCube = cmds.polyCube(w = 10, h = 10, d = 10, name = 'myCube')
pSphere = cmds.polySphere(sx = 5, sy = 5, r = 5 ,n = 'mySphere')
【3】Create instance
import maya.cmds as cmds
cubeList = cmds.ls('myCube')
#sphereList = cmds.ls('mySphere')
if len(cubeList) > 0:
cmds.delete(cubeList)
#if len(sphereList) > 0:
#cmds.delete(sphereList)
pCube = cmds.polyCube(w = 10, h = 10, d = 10, name = 'myCube')
#pSphere = cmds.polySphere(sx = 5, sy = 5, r = 5 ,n = 'mySphere')
pCubeTransform = pCube[0]
pCubeInstance = cmds.instance(pCubeTransform, name = pCubeTransform + '_instance')
这里需要注意的是cubeList得到的是一个列表
而创建的poly得到的是列表的子集
【4】移动,旋转,缩放物体
import maya.cmds as cmds
cubeList = cmds.ls('myCube')
#sphereList = cmds.ls('mySphere')
if len(cubeList) > 0:
cmds.delete(cubeList)
#if len(sphereList) > 0:
#cmds.delete(sphereList)
pCube = cmds.polyCube(w = 10, h = 10, d = 10, name = 'myCube')
#pSphere = cmds.polySphere(sx = 5, sy = 5, r = 5 ,n = 'mySphere')
pCubeTransform = pCube[0]
pCubeInstance = cmds.instance(pCubeTransform, name = pCubeTransform + '_instance')
cmds.move(0, 10, 0, pCubeInstance)
cmds.scale(2, 2, 2, pCubeInstance)
cmds.rotate(45, 45, 0, pCubeInstance)
【5】循环创建五十个物体
import maya.cmds as cmds
cubeList = cmds.ls('myCube')
#sphereList = cmds.ls('mySphere')
if len(cubeList) > 0:
cmds.delete(cubeList)
#if len(sphereList) > 0:
#cmds.delete(sphereList)
pCube = cmds.polyCube(w = 10, h = 10, d = 10, name = 'myCube')
#pSphere = cmds.polySphere(sx = 5, sy = 5, r = 5 ,n = 'mySphere')
pCubeTransform = pCube[0]
for i in range(0, 50):
pCubeInstance = cmds.instance(pCubeTransform, name = pCubeTransform + '_instance' + str(i))
cmds.move(10 * i, 0, 0, pCubeInstance)
【6】随机摆放物体位置
import maya.cmds as cmds
import random
random.seed(1234)
cubeList = cmds.ls('myCube')
#sphereList = cmds.ls('mySphere')
if len(cubeList) > 0:
cmds.delete(cubeList)
#if len(sphereList) > 0:
#cmds.delete(sphereList)
pCube = cmds.polyCube(w = 10, h = 10, d = 10, name = 'myCube')
#pSphere = cmds.polySphere(sx = 5, sy = 5, r = 5 ,n = 'mySphere')
pCubeTransform = pCube[0]
for i in range(0, 50):
pCubeInstance = cmds.instance(pCubeTransform, name = pCubeTransform + '_instance' + str(i))
x = random.uniform(-100, 100)
y = random.uniform(0, 100)
z = random.uniform(-100, 100)
cmds.move(x, y, z, pCubeInstance)
【7】隐藏物体
cmds.hide(pCube)
【8】打组
import maya.cmds as cmds
import random
random.seed(1234)
cubeList = cmds.ls('myCube')
#sphereList = cmds.ls('mySphere')
if len(cubeList) > 0:
cmds.delete(cubeList)
#if len(sphereList) > 0:
#cmds.delete(sphereList)
pCube = cmds.polyCube(w = 10, h = 10, d = 10, name = 'myCube')
#pSphere = cmds.polySphere(sx = 5, sy = 5, r = 5 ,n = 'mySphere')
pCubeTransform = pCube[0]
instanceGroup = cmds.group(empty = True, name = 'MyInstanceGroup')
for i in range(0, 50):
pCubeInstance = cmds.instance(pCubeTransform, name = pCubeTransform + '_instance' + str(i))
cmds.parent(pCubeInstance, instanceGroup)
x = random.uniform(-100, 100)
y = random.uniform(0, 100)
z = random.uniform(-100, 100)
cmds.move(x, y, z, pCubeInstance)
cmds.hide(pCube)
【9】重置Pivot
import maya.cmds as cmds
import random
random.seed(1234)
cubeList = cmds.ls('myCube')
#sphereList = cmds.ls('mySphere')
if len(cubeList) > 0:
cmds.delete(cubeList)
#if len(sphereList) > 0:
#cmds.delete(sphereList)
pCube = cmds.polyCube(w = 10, h = 10, d = 10, name = 'myCube')
#pSphere = cmds.polySphere(sx = 5, sy = 5, r = 5 ,n = 'mySphere')
pCubeTransform = pCube[0]
instanceGroup = cmds.group(empty = True, name = 'MyInstanceGroup')
for i in range(0, 50):
pCubeInstance = cmds.instance(pCubeTransform, name = pCubeTransform + '_instance' + str(i))
cmds.parent(pCubeInstance, instanceGroup)
x = random.uniform(-100, 100)
y = random.uniform(0, 100)
z = random.uniform(-100, 100)
cmds.move(x, y, z, pCubeInstance)
cmds.hide(pCube)
cmds.xform(instanceGroup, centerPivots = True)