我在这里的第一篇文章,但我花了过去几个星期基本上生活在S.O.,寻找答案和解决方案。不幸的是,我没有找到一个答案,我目前的问题在这里,或任何其他地方,所以我希望你们中的一个可爱的人能帮助我。
我试图从Windows中批量处理Autodesk文件,以替换引用的文件,并将其放到一个单一目录中。目前,它只是在我尝试执行代码后返回一个"“。
这是我的代码,到目前为止
### Reference Example
# file -rdi 2 -ns "platform3_MDL_MASTER" -rfn
# "Toyota_2000GT_Spider:Toyota2000GT_Spider_RN"
# -typ "mayaAscii"
# "C:/svn/TEST/previs/maya_references/Toyota_2000GT_Spider.ma";
import os
# Defining the reference paths - before and after
projectPath = "C:/projects/TEST/previs"
originalPath = "C:/projects/TEST/previs/maya_references/"
newPath = "R:/projects/FRAT/production/maya_references/"
# Makes a list of all previs files in the given directory.
previsFiles = [os.path.join(d, x)
for d, dirs, files in os.walk(projectPath)
for x in files if x.endswith("_PREVIS.ma")]
previsSuffix = '.ma";'
newLines = []
# Loops through each previs file found...
# for each line that contains the previsSuffix...
# and splits the filepath into a directory and a filename
# and then replaces that originalPath with the newPath.
for scene in previsFiles:
with open(scene, "r") as fp:
previsReadlines = fp.readlines()
for line in previsReadlines:
if previsSuffix in line:
# Splits the directory from the file name.
lines = os.path.split(line)
newLines = line.replace(lines[0], newPath)
else:
break
with open(scene, 'w') as fw:
previsWritelines = fw.writelines()发布于 2018-04-17 02:40:59
您将对此进行调整,以便与您的脚本一起工作,但我希望它给您提供了如何将引用路径替换为另一条路径的一般概念。
原来的守则有两个主要问题:
1)你实际上并没有改变内容。执行newLines =实际上不会重新分配previsReadlines,因此没有记录更改。
( 2)没有任何东西可以用来写作。writelines需要一个您打算使用的参数。
# Adjust these paths to existing maya scene files.
scnPath = "/path/to/file/to/open.ma"
oldPath = "/path/to/old/file.ma"
newPath = "/path/to/new/file.ma"
with open(scnPath, "r") as fp:
# Get all file contents in a list.
fileLines = fp.readlines()
# Use enumerate to keep track of what index we're at.
for i, line in enumerate(previsReadlines):
# Check if the line has the old path in it.
if oldPath in line:
# Replace with the new path and assign the change.
# Before you were assigning this to a new variable that goes nowhere.
# Instead it needs to re-assign the line from the list we first read from.
fileLines[i] = line.replace(oldPath, newPath)
# Completely replace the file with our changes.
with open(scnPath, 'w') as fw:
# You must pass the contents in here to write it.
fw.writelines(fileLines)https://stackoverflow.com/questions/49859115
复制相似问题