MAYA对多个模型使用当前设置批量渲染并保存图片的MEL脚本

使用MAYA MEL脚本对一组文件名带序号的模型使用相同的材质和相同的渲染设置渲染并保存为图片。

更新日志


有时候需要对一组模型使用相同的材质和相同的渲染设置渲染并保存为图片,以往只能一个个手动导入、替换材质、渲染、移除,直到前阵子需要一次性这样渲染上百张图片……

于是终于咬牙写了一个批量渲染的MEL脚本——

下面的脚本会自动读取一系列文件路径只有序号差异的模型,使用给定的材质并设置线框颜色为RGB(0,0,0),然后使用当前渲染设置、当前摄像机位置,进行渲染并保存为和模型同名的图片。

//选择渲染的材质
//默认为lambert1
string $mat;
string $result = `promptDialog
    -title "材质选择"
    -message "输入渲染的材质:"
	-button "OK"`;
if ($result == "OK") {
    $mat = `promptDialog -query -text`;
} else {
	$mat = "lambert1";
}

//打开第1个模型
string $fileFirst;
$fileFirst = `fileDialog -mode 0 -title "打开要渲染的第1个模型(模型全路径必须出现1处整数表示序号)"`;

//获取第1个模型的序号
string $idFirst = `match "[0-9]+" $fileFirst`;
int $idLength = `size $idFirst`;
if ($fileFirst == "") {

} else if ($idFirst == "") {
	confirmDialog -title "错误" -message "模型全路径必须出现1处整数表示序号";
} else {
	for ($id = (int)$idFirst; $id < 1000; $id++) {
		string $idCurrent = (string)$id;
		string $fileCurrent = `substitute "[0-9]+" $fileFirst $idCurrent`;
		string $imageCurrent = `substitute "\.[^\.]+$" $fileCurrent ""`;

		//检测文件是否存在
		int $fid = `fopen $fileCurrent "r"`;
		if ($fid == 0) {
			//尝试定长数字
			for ($i = `size $idCurrent`; $i < $idLength; $i++) $idCurrent = "0" + $idCurrent;
			$fileCurrent = `substitute "[0-9]+" $fileFirst $idCurrent`;
			$imageCurrent = `substitute "\.[^\.]+$" $fileCurrent ""`;

			$fid = `fopen $fileCurrent "r"`;
			if ($fid == 0) break;
		}
		fclose $fid;

		//导入模型并获取对象名$newObj
		string $before[] = `ls`;
		file -import $fileCurrent;
		string $after[] = `ls`;
		string $newObjs[] = stringArrayRemove($before, $after);
		string $newObj = $newObjs[0];
		
		select -r $newObj;
		color -rgb 0 0 0 $newObj;	//设置黑色线框
		hyperShade -assign $mat;	//设置材质
		select -d;

		//使用当前设置渲染并保存图片
		RenderIntoNewWindow;
		renderWindowEditor -e -wi($imageCurrent) renderView;

		//移除模型
		select -r $newObj;
		doDelete;
	}
}








称谓(*)
邮箱
留言(*)