maya python 的简单使用
使用pycharm给玛雅写python
里边含有玛雅命令自动提示的配置
https://download.csdn.net/download/bigtail_cat/10543887
文件下载网址
基本图形的创建
# -*- coding: UTF-8 -*-
import maya.cmds as cmds
if __name__ == '__main__':
cmds.polySphere() # 创建默认的小球
cmds.polySphere(r=5) # 创建半径为5的小球
cmds.polyColorPerVertex(colorRGB=[0, 1, 1], colorDisplayOption=True) #设置小球的颜色
cmds.polyCube(name='myCube', depth=12.5, height=5) #创建立方体
print (cmds.help('polyCube')) # 查看polyCube函数的参数列表
#下面两种创建立方体方式等同
cmds.polyCube(w=1, h=1, d=1, sx=1, sy=1, sz=1, ax=(0, 1, 0), cuv=4, ch=1)
cmds.polyCube(width=1,
height=1,
depth=1,
subdivisionsX=1,
subdivisionsY=1,
subdivisionsZ=1,
axis=(0, 1, 0),
createUVs=4,
constructionHistory=1)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
根据创建的小球获得信息并改变小球的大小
# -*- coding: UTF-8 -*-
import maya.cmds as cmds
if __name__ == '__main__':
sphere = cmds.polySphere() # 创建默认的小球
cmds.polySphere(r=5) # 创建半径为5的小球
cmds.polyColorPerVertex(colorRGB=[0, 1, 1], colorDisplayOption=True) #设置小球的颜色
print(sphere)
rad = cmds.polySphere(sphere[1], q=True, radius=True) # 获取小球的半径
print('the sphere radius is %d' %rad)
cmds.polySphere(sphere[1], e=True, radius=rad * 2) # 将小球的半径扩大二倍
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
getAttr 和 setAttr 获得和设置属性
import maya.cmds as cmds
if __name__ == '__main__':
loc = cmds.spaceLocator() # 创建打印定位器
print loc
sx = cmds.getAttr(loc[0] + '.scaleX') # 获得locator的scaleX
cmds.setAttr(loc[0] + '.scaleX', sx * 2) # 修改locator的scaleX
# 同下效果
print(cmds.xform(loc[0], q=True, translation=True)) # 获得打印定位器的位置
cmds.xform(loc[0], translation=[0, 1, 0]) # 移动打印定位器的位置
#同上效果
print(cmds.getAttr(loc[0] + '.translate')) # 获得打印定位器的位置
cmds.setAttr(loc[0] + '.translate', 1, 2, 3) # 移动打印定位器的位置
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
connectAttr 和 disconnectAttr 连接与断开属性连接
# -*- coding: UTF-8 -*-
import maya.cmds as cmds
if __name__ == '__main__':
sphere = cmds.polySphere()[0] #创建小球
cube = cmds.polyCube()[0] # 创建立方体
cmds.connectAttr(cube + '.ry', sphere + '.ty') #将立方体的y-rotation连接到球体的y-translation
cmds.disconnectAttr(cube + '.ry', sphere + '.ty') # 断开连接
#立方体每旋转九十度 小球旋转一个单位
mult = cmds.createNode('multiplyDivide')
cmds.connectAttr(cube + '.ry', mult + '.input1X')
cmds.setAttr(mult + '.input2X', 1.0 / 90.0)
cmds.connectAttr(mult + '.outputX', sphere + '.ty')
cmds.select(cube)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
addAttr 增加属性
# -*- coding: UTF-8 -*-
import maya.cmds as cmds
if __name__ == '__main__':
cmds.file(new=True, f=True)
sphere_xform, sphere_shape = cmds.polySphere(n='Earth')
# 添加属性
cmds.addAttr(sphere_xform, attributeType='float', shortName='mass', longName='mass', defaultValue=5.9742e24)
cmds.addAttr(sphere_xform, dataType='string', shortName='alt', longName='alternateName')
# 向属性中添加值 设置属性
cmds.setAttr(sphere_xform + '.alternateName', 'Terra', type='string')
# 获得属性值
print(cmds.getAttr(sphere_xform + '.mass'))
print(cmds.getAttr(sphere_xform + '.alt'))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
创建窗口
# -*- coding: UTF-8 -*-
import maya.cmds as cmds
if __name__ == '__main__':
# 创建一个窗口
win = cmds.window('ar_optionsWindow', title='first window', widthHeight=(546, 350))
cmds.showWindow(win)
# 修改项目标题
cmds.deleteUI(win, window=True)
win = cmds.window('ar_optionsWindow', title='second window', widthHeight=(546, 350))
cmds.showWindow(win)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
ls 与 select 列出与选择节点
# -*- coding: UTF-8 -*-
import maya.cmds as cmds
if __name__ == '__main__':
# 打印场景里边的所有节点
nodes = cmds.ls()
print(nodes)
# 打印场景中所有transform类型的节点
nodes = cmds.ls(type='transform')
print(nodes)
# 打印场景中所有以persp开头名字的节点
nodes = cmds.ls('persp*')
print(nodes)
#打印当前场景中选中的节点
cmds.select('side*', 'top*')
print(cmds.ls(selection=True))
cmds.select(cmds.ls(type='shape'))
print(cmds.ls(sl=True))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
file 场景中文件的创建与打开
# -*- coding: UTF-8 -*-
import os
import maya.cmds as cmds
if __name__ == '__main__':
cmds.polyCube()
# 保存场景中的所有文件
path = os.path.join(os.getenv('HOME'), 'cuba.ma')
print path
cmds.file(rename=path)
cmds.file(save=True)
#新建场景并打开文件
cmds.file(new=True, force=True)
path = os.path.join(os.getenv('HOME'), 'cuba.ma') # 打开文件
print path
cmds.file(path, open=True, force=True)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18