【笔记】【Maya工具】Python For Maya(Cmds)(二)Object Renamer & Create Gear

一、Object Renamer

1
2
from maya import cmds
print(cmds.ls())

返回了场景里面所有物体名称组成一个列表,包括一些隐藏物体和对象

1
2
3
4
5
6
7
selection = cmds.ls(selection = True)
if len(selection)==0:
selection = cmds.ls(dag=True,long=True)
selection.sort(key = len,reverse)
#名称越长越是子物体
#这个排序是为了让遍历列表的时候,子物体先进行改变,再改变父物体。如果先改变父物体,那么子物体会受到同样的影响。
print(selection)

image-20220628234233927

这里处理返回完整path name是为了放置不同层级物体同名的情况

image-20220628234344658

image-20220628234428788

Dag参数使得列表返回所有dag对象的物体(大纲中的仅显示Dag对象物体)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for obj in selection:
shortName = obj.split('|')[-1]#取dagpath最后一个,就是物体名字了
#print(cmds.objectType(obj))#transform我们已经知道它为什么是transform了,实际上在maya大纲中可以右键选择shape可见
children = cmds.listRelatives(obj,children = True, fullPath = True) or []#None则直接返回成空列表,因为这个东西很烦,没有东西返回None,不是空列表类型
if (len(children) == 1):
child = children[0]
objType = cmds.objectType(child)
else:
objType = cmds.objectType(obj)
if objType == "mesh":
suffix = "geo"
elif objType == "joint":
suffix = "jnt"
elif objType == "camera":
continue
else:
suffix = "grp"
newName = shortName + "_" + suffix

cmds.rename(obj, newName)

使用函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from maya import cmds
SUFFIXES = {
"mesh":"geo",
"joint":"jnt",
"camera": None,
"ambientLight":"lgt"
}
DEFAULT_SUFFIX = "grp"
def rename(selection = False):
"""
This function will rename any objects to have the correct suffix
Args:
selection: Whether or not we use the current selections

Returns:
A list of all the objects we operated on

"""

objects = cmds.ls(selection = selection,dag = True, long = True)

if selection and not objects:
raise RuntimeError("You dont have anything selected")


objects.sort(key = len, reverse = True)
for obj in objects:
shortName = obj.split('|')[-1]#取dagpath最后一个,就是物体名字了
#print(cmds.objectType(obj))#transform我们已经知道它为什么是transform了,实际上在maya大纲中可以右键选择shape可见
children = cmds.listRelatives(obj,children = True, fullPath = True) or []#None则直接返回成空列表,因为这个东西很烦,没有东西返回None,不是空列表类型
print(children)
if (len(children) == 1):
child = children[0]
objType = cmds.objectType(child)
else:
objType = cmds.objectType(obj)

suffix = SUFFIXES.get(objType, DEFUALT_SUFFIXES)
if not suffix:
continue
if obj.endswith('_'+suffix):
continue
newName = "{}_{}".format(shortName,suffix)

cmds.rename(obj, newName)
index = objects.index(obj)
objects[index] = obj.replace(shortName,newName)
return objects

在外面改变脚本后,maya不会直接识别到,需要使用reload方法

1
2
import someModule
reload(someModule)

二、Create Gear

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from maya import cmds

def createGear(teeth = 10, length = 0.3):
# Teeth are every alternate face, so spans x 2
print("Creating Gear", teeth,length)
spans = teeth * 2

transform,constructor = cmds.polyPipe(subdivisionsAxis = spans)
sideFaces = range(spans*2,spans*3,2)
#因为pipe侧面是从spans*2开始计数的,0-spans是顶面
cmds.select(clear = True)

for face in sideFaces:
cmds.select('{}.f[{}]'.format(transform, face), add = True)#添加到选择集,而不是重新选择
extrude = cmds.polyExtrudeFacet(localTranslateZ = length)[0]
return transform, constructor, extrude

def changeTeeth(constructor,extrude,teeth = 10, length = 0.3):
spans = teeth * 2
cmds.polyPipe(constructor, edit = True, subdivisionsAxis = spans)

sideFaces = range(spans*2,spans*3,2)
faceNames = []

for face in sideFaces:
faceName = 'f[{}]'.format(face)
faceNames.append(faceName)

cmds.setAttr('{}.inputComponents'.format(extrude),len(faceNames),*faceNames,type = "componentList")
cmds.polyExtrudeFacet(extrude, edit = True, ltz = length)


changeTeeth(constructor,extrude,teeth = 40)

2.1使用类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from maya import cmds
class Gear(object):
def __init__(self):
self.transofrm = None
self.extrude = None
self.constructor = None
def creatGear(self, teeth = 10, length = 0.3):
spans = teeth * 2

self.transform, self.constructor = cmds.polyPipe(subdivisionsAxis = spans)
sideFaces = range(spans*2,spans*3,2)
cmds.select(clear = True)

for face in sideFaces:
cmds.select('{}.f[{}]'.format(self.transform, face), add = True)#添加到选择集,
self.extrude = cmds.polyExtrudeFacet(localTranslateZ = length)[0]

def changeTeeth(self,teeth = 10, length = 0.3):
spans = teeth * 2
cmds.polyPipe(self.constructor, edit = True, subdivisionsAxis = spans)

sideFaces = range(spans*2,spans*3,2)
faceNames = []

for face in sideFaces:
faceName = 'f[{}]'.format(face)
faceNames.append(faceName)

cmds.setAttr('{}.inputComponents'.format(self.extrude),len(faceNames),*faceNames,type = "componentList")
cmds.polyExtrudeFacet(self.extrude, edit = True, ltz = length)