一半君的总结纸

听话只听一半君

#122 如何把mel script调用“转化”为python函数调用以避免使用eval?

有maya python,也有pymel,他们都能帮你把mel command的写法转化为python的写法,但是有种情况下不行,比如你要调用大量历史遗留的古董级的mel(可能是公司里的很久以前写的,或者是maya自带的安装目录下script文件夹下的)这样就不可避免的要用到mel.eval(‘很长的字符串’),有时候字符串还是多行的,而且要往里传变量,这样看起来很不pythonic

上面这段想解决的问题是假如你有这么一段

mel.eval(
    '''
	global string $gCommandExecuterName[];
	global string $gCommandExecuterType[];

	int $len = size($gCommandExecuterName);
	int $i;
	for ($i = 0; $i < $len; ++$i) {
	    buildNewExecuterTab($i, $gCommandExecuterName[$i], $gCommandExecuterType[$i], 0);                       
	}
	'''
)

有了上面的解法,你就可以把他变成

import pyToMelCall as pyMC

def get_mel_global(var):
    return mel.eval('$tmp_var=%s' % var)

gCommandExecuterName = get_mel_global('$gCommandExecuterName')
gCommandExecuterType = get_mel_global('$gCommandExecuterType')

for i, (name, _type) in enumerate(zip(gCommandExecuterName, gCommandExecuterType)):
    pyMC.buildNewExecuterTab(i, name, _type, 0)

其实只有pyMC.buildNewExecuterTab(i, name, _type, 0) 这行是关键的,总之这样就避免了eval一个很长的字符串

参考:

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: