PyMel技术集锦

这篇文章记录一些在maya中的编程技巧;主要是记录一些实用的工具型代码。

1)创建一个球:

import maya.cmds;
maya.cmds.polySphere();

2) Python在maya中的位置:

PyMel技术集锦_第1张图片

3)命令行帮助(当然可以参考:Python Command Reference)

import maya.cmds;
print(maya.cmds.help('polyCube'));

4)Pymel的Edit和query模式

import maya.cmds as mc;
# 创建一个Cube,名称为pCube1,width为10
mc.polyCube(n='pCube1', width=10);
# 修改其width为20;
mc.polyCube('pCube1', edit=True, width=20);
# 查询pCube1的width属性
w = mc.polyCube('pCube1', query=True, width=True);  #MEL为 polyCube -q -w pCube1;
print(w)

5)获取Python的版本号

import sys;
print(sys.version);

6)编辑属性

import maya.cmds as mc;
loc = mc.spaceLocator()[0];
sx = mc.getAttr(loc+'.scaleX');
sx*=2;
mc.setAttr(loc+'.scaleX', sx);
print(mc.xform(loc, q=True, translation=True));
mc.xform(loc, translation=[0,1,0]);

7)链接属性

import maya.cmds as mc;
sphere = mc.polySphere()[0];
cube = mc.polyCube()[0];
mc.connectAttr(cube+'.ry', sphere+'.ty');
mc.disconnectAttr(cube+'.ry', sphere+'.ty');

mult = mc.createNode('multiplyDivide');
mc.connectAttr(cube+'.ry', mult+'.input1X');
mc.setAttr(mult+'.input2X', 1.0/90.0);
mc.connectAttr(mult+'.outputX', sphere+'.ty');
mc.select(cube);

8)从Python命令行执行MEL,把Object改成fbx格式

import os 
import glob
from os import listdir
from os.path import isfile, join

# see http://stackoverflow.com/questions/4128144/replace-string-within-file-contents
def inplace_change(filename, old_string, new_string):
        s=open(filename).read()
        if old_string in s:
                s=s.replace(old_string, new_string)
                f=open(filename, 'w')
                f.write(s)
                f.flush()
                f.close()
                print "Done!"
        else:
                print "No Change!"
				

# see http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python
curPath = os.getcwd().replace("\\", "/");
objectPath = curPath + "/objs"
exportPath = curPath + "/fbx"
objPathTmp = objectPath + "/%s.obj"
fbxPathTmp =  exportPath + "/%s.fbx"


if not os.path.exists(exportPath):
    os.makedirs(exportPath)
else:
    files = glob.glob(exportPath + '/*')
    for f in files:
        os.remove(f)

		
onlyObjFiles = [ f for f in listdir(objectPath) if isfile(join(objectPath, f)) and f.endswith(".obj") ]
cmdsTmp = 'file -import -type "OBJ" -gr  -ignoreVersion -rdn -mergeNamespacesOnClash true -rpr "xx" -options "mo=1"  -pr "%s";string $transforms[] = `ls -tr`;  string $polyMeshes[] = `filterExpand -sm 12 $transforms`; select -r $polyMeshes; file -force -options "mo=1" -typ "FBX export" -pr -es "%s"; select -r $transforms; delete;'
cmds = ""		

for fn in onlyObjFiles:
	print '%s -> ' % fn,
	inplace_change(objectPath + "/" + fn, "f 1/1/1 1/1/1 1/1/1", "")
	name = fn[:-4]
	selName = name[:name.rfind('_')]
	cmds += cmdsTmp % (objPathTmp % fn[:-4], fbxPathTmp % fn[:-4]);

print cmds
melfile = "%s\obj2fbx.mel" % os.getcwd()
melcon = open(melfile, 'w' )
melcon.truncate()
melcon.write(cmds)
melcon.close()
os.system("mayabatch -log obj2fbx.log -script %s" % melfile) 



文章参考:

Python Command Reference 

PyMEL for Maya

《Maya Python for Games and Film》

Maya Help

你可能感兴趣的:(python,Maya,mel,pymel)