我在这里的第一篇文章,但我花了最后几周基本上生活在S.O.,寻找答案和解决方案。不幸的是,我在这里或其他任何地方找不到我当前问题的答案,所以我希望你们中的一个可爱的人能帮助我。
我正在尝试从Windows中批量处理Autodesk Maya文件,以将引用的文件路径替换为一个单一目录。目前,它只是在我尝试执行代码后返回“”。
到目前为止,这是我的代码 - 请尽可能多地选择它,我需要做得更好!
### 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()
答案 0 :(得分:0)
你可以调整它以使用你的脚本,但我希望它能让你大致了解如何用另一个替换参考路径。
原始代码有两个主要问题:
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)