参考学习链接 代码和上手难度比较简单,但是没有字幕…
18版的Maya官方API文档
关于python中的中文使用解决方法
写在最前面
干到啥就写点啥,不过也是从入门开始啦,暂定为第一篇吧。本次记录和往常一样用于自己复习,但如果能对别人有所帮助,那荣幸至极;
Maya默认的脚本支持Mel与python,写工具则需要使用C++或者C#或者Python,其中C++与C#不能直接使用,需要编译,且仅能写工具,但速度来说较Python好,但Python 上手简单且兼顾工具和脚本编写,故在此选择使用Python进行学习;
下面两张图大致讲了下关于各种API相关的介绍,看不懂可以使用有道翻译词典拍摄翻译!
另外,python对于格式的把握非常的严格,最好用Tab来控制缩进,否则一直报错,明明看起来写的一样,结果就是疯狂报错,调了一晚上,气的想骂人…
一.简单尝试及简单语法
1.使用Python脚本第一步,先在脚本里输入如下:
from maya import cmds
给我的感觉类似于#include的用法;
2.一串简易的代码,包括:创建物体,设置父子集,锁定
from maya import cmds
#创建物体
mysphere = cmds.polySphere()
mysphereShape = mysphere[0]
circle = cmds.circle()
circleShape = circle[0]
#父子集
cmds.parent(mysphereShape,circleShape)
#锁定
cmds.setAttr(mysphereShape+".translate",lock=True)
cmds.setAttr(mysphereShape+".rotate",lock=True)
cmds.setAttr(mysphereShape+".scale",lock=True)
cmds.select(circleShape)
3.python中的if esle
如下图
下面的代码是通过判断是否选择了物体,如果选择物体的话,输出选择的物体名,否则,输出场景内所有物体,结果看下面的图;
from maya import cmds
selection = cmds.ls(selection=True)
if len(selection) == 0:
selection = cmds.ls(dag=True,long=True)
print selection
4.排序
sort排序,reverse用来倒序
from maya import cmds
selection = cmds.ls(selection=True)
if len(selection) == 0:
selection = cmds.ls(dag=True,long=True)
selection.sort(key=len,reverse=False)#sort排序,reverse用来倒序
print selection
5.for循环
下面两个代码的区别在于最后一句话,两个都是用来输出场景中物体的名称的
from maya import cmds
selection = cmds.ls(selection=True)
if len(selection) == 0:
selection = cmds.ls(dag=True,long=True)
selection.sort(key=len,reverse=True)
for obj in selection:
print obj.split()
from maya import cmds
selection = cmds.ls(selection=True)
if len(selection) == 0:
selection = cmds.ls(dag=True,long=True)
selection.sort(key=len,reverse=True)
for obj in selection:
print obj.split("|")[-1]
6.while循环
用于输出物体的属性
from maya import cmds
selection = cmds.ls(selection=True)
if len(selection) == 0:
selection = cmds.ls(dag=True,long=True)
selection.sort(key=len,reverse=True)
for obj in selection:
shortName = obj.split("|")[-1]
children = cmds.listRelatives(obj,children=True,fullPath=True)or[]
if len(children) == 1:
child = children[0]
objType = cmds.objectType(child)
else:
objType = cmds.objectType(obj)
print objType
7.int to string,python中的强制转换
参考下图
8.另外三种字符串加减法
分别是创建字典和利用标识符%
以及.format,可以玩出很多花来
9.变量
全局变量,内部变量等,和往常其他语言一样就是了;
10.类
python中的类,和其他语言类似
python类的继承
当我们在子类里定义了新的同名函数,会覆盖原来的函数,否则就继承父类的其他函数
二.环境安装
1.pycharm安装第一步设置
百度直接搜索就好了,安装基本点下一步,完事后,需要改动的地方在设置里,如下图,找到Maya安装目录下的mayapy.exe;