Maya python scripting(1)

我最近在重新学习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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值