
image.png

image.png
OpenMaya 對比 cmds優(yōu)勢:
1.適合插件的開發(fā),因?yàn)閾碛衏mds未公開的功能
2.由于是Maya C++ API的Python庫,速度上也會(huì)比cmds來的更快
3.在組件級別的對象(點(diǎn)邊面),處理的效率會(huì)大幅度超越cmds

image.png
測試環(huán)境:
對場景中的10000 polyCube 做位置 旋轉(zhuǎn) 隨機(jī),縮放統(tǒng)一設(shè)置 0.2,并改名字:

image.png

image.png
import maya.cmds as cmds
for i in xrange(0, 10000):
cmds.polyCube(ch=False)[0]
Mel:

image.png
$sel = `ls -tr "pCube*" `;
$start_time = `about -ct`;
for ($x=0; $x<size($sel); $x++)
{
float $x_v=rand(-12.0,12.0);
float $y_v=rand(-12.0,12.0);
float $z_v=rand(-12.0,12.0);
// string $new_name ="AAAA_" + ($x+1);
setAttr ($sel[$x] + ".translate") -type "double3" $x_v $y_v $z_v;
setAttr ($sel[$x] + ".rotate") -type "double3" $x_v $y_v $z_v;
setAttr ($sel[$x] + ".scale") -type "double3" 0.2 0.2 0.2;
rename($sel[$x], "AAAAA");
}
$end_time = `about -ct`;
由于用的是about -ct 來看的系統(tǒng)時(shí)間,可以看到大約是1秒的間隔
Cmds:

image.png
# coding=utf8
import time
time_start = time.time()
sel = cmds.ls('pCube*', tr=True)
for obj in sel:
x_val = random.uniform(-12, 12)
y_val = random.uniform(-12, 12)
z_val = random.uniform(-12, 12)
cmds.setAttr('{}.t'.format(obj), x_val, y_val, z_val)
cmds.setAttr('{}.r'.format(obj), x_val, y_val, z_val)
cmds.setAttr('{}.s'.format(obj), 0.2, 0.2, 0.2)
cmds.rename(obj, 'bbbbb')
time_end = time.time()
print(u'time:', time_end - time_start)
耗時(shí)大約2.01秒
Python Api:

image.png
# coding=utf8
import maya.OpenMaya as om
time_start = time.time()
original_sel = om.MSelectionList()
sel = cmds.ls('pCube*', tr=True)
for item in sel:
original_sel.add(item)
mob = om.MObject()
mTransform = om.MFnTransform()
api_lit_iter = om.MItSelectionList(original_sel)
while not api_lit_iter.isDone():
# random values
x_val = random.uniform(-12, 12)
y_val = random.uniform(-12, 12)
z_val = random.uniform(-12, 12)
api_lit_iter.getDependNode(mob)
mTransform = om.MFnTransform(mob)
for attr in ['translate', 'rotate', 'scale']:
plug = mTransform.findPlug(attr)
if attr == 'scale':
x_val, y_val, z_val = [0.2, 0.2, 0.2]
for index, val in enumerate([x_val, y_val, z_val]):
child_plug = plug.child(index)
t_attr_val = plug.asDouble()
child_plug.setDouble(val)
mTransform.setName('ccccc')
api_lit_iter.next()
time_end = time.time()
print(u'time:', time_end - time_start)
大約是2.39秒
結(jié)論是看得出常規(guī)操作中PythonApi并沒有什么優(yōu)勢,
PythonApi。。。未完待續(xù)。。。。。。