我在尝试用Python写一个批量执行脚本的程序;目前遇到了一个解决不了的问题,因为我Python是半桶水,也找不出问题所在以及怎么解决,所以想上这里提问。
代码是用for循环遍历文件夹内的文件,然后用if筛选出.ma格式的文件
执行代码后,只输出了3个文件,正常应该是输出4个文件的
下面是代码部分:
import subprocess
import os
from io import StringIO
import_lineEdit = "E:/Maya_Project/scenes/原始文件"
output_lineEdit = "E:/Maya_Project/scenes/输出文件"
melFile = "E:/Maya_Project/scenes/原始文件/CreatSphere.mel"
MAYABATCHPATH = r'C:/Program Files/Autodesk/Maya2019/bin/mayabatch.exe'
for i in os.listdir(import_lineEdit): #用for循环遍历文件夹内所有文件
# print(os.path.abspath(i))
if i.split(".")[1] == "ma": #用if筛选出ma文件
mapath = i
outputfile = output_lineEdit+"/"+os.path.basename(mapath)
#print(self.output_lineEdit.text()+"\"+os.path.basename(mapath)) #输出文件路径 E:\Maya_Project\scenes\输出文件\test.ma
#fixmelFile = melFile.os.path.splitext(melFile)[0] + "_batchtool_willdelete" + os.path.splitext(melFile)[1]
#print(melFile.split(".")[0] + "_batchtool_willdelete." + melFile.split(".")[1])
fixfile = open(melFile.split(".")[0] + "_batchtool_willdelete." + melFile.split(".")[1],"w") #打开或创建_batchtool_willdelete.mel文件
savefile = f'\nfile -rename "{outputfile}" ; '
savefiletype = '\nfile -save -type "mayaAscii" -options"v=0;" ; ////' #% outputfile
# print(savefile)
# print(f'{outputfile}')
# 把mel脚本写入内存,并且添加另存为ma文件的脚本
filestring = open(melFile) # 打开mel文件
inbuffer = StringIO() # 实例化StingIO
inbuffer.write(filestring.read()) # 把mel文件里的代码写入内存
inbuffer.write(savefile) # 把保存文件的代码(mel)写入内存
inbuffer.write(savefiletype) #把保存文件格式的代码(mel)写入内存
fixfile.write(inbuffer.getvalue()) # 把内存里的代码写入文件中
filestring.close() # 关闭fixfile文件
fixmelfile = melFile.split(".")[0] + "_batchtool_willdelete." + melFile.split(".")[1]
# print(fixmelfile)
# 设置cmd命令要运行的参数
# cmd命令示例:"C:**\mayabatch.exe"-batch -file "E:\test.ma" -script "E:\CreatSphere_batchtool_willdelete.mel"
cmd = '"{mayaBatchPath}" -batch -file "{maPath}" -script "{melFile}" "{plugins}"'.format(
mayaBatchPath=MAYABATCHPATH,
melFile=fixmelfile, #mel文件
maPath=import_lineEdit+"\"+mapath, #源maya文件路径
plugins="-noAutoloadPlugins",
)
#用子进程调用cmd命令运行mayabatch.exe
# subprocess.check_call(cmd, shell=True)
p = subprocess.Popen(cmd,
shell=True,
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE,
)
p.wait()
尝试过只用subprocess.Popen执行,但是只会输出最后一个文件
还有一个问题是,用subprocess.Popen.wait会导致程序阻塞(因为我有个ui界面的,ui界面会卡死),只用subprocess.Popen的话就不会发生阻塞,但是只会输出最后一个文件。