首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >玛雅ASCII Filepath替换

玛雅ASCII Filepath替换
EN

Stack Overflow用户
提问于 2018-04-16 13:57:55
回答 1查看 618关注 0票数 1

我在这里的第一篇文章,但我花了过去几个星期基本上生活在S.O.,寻找答案和解决方案。不幸的是,我没有找到一个答案,我目前的问题在这里,或任何其他地方,所以我希望你们中的一个可爱的人能帮助我。

我试图从Windows中批量处理Autodesk文件,以替换引用的文件,并将其放到一个单一目录中。目前,它只是在我尝试执行代码后返回一个"“。

这是我的代码,到目前为止

代码语言:javascript
复制
### 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()
EN

回答 1

Stack Overflow用户

发布于 2018-04-17 02:40:59

您将对此进行调整,以便与您的脚本一起工作,但我希望它给您提供了如何将引用路径替换为另一条路径的一般概念。

原来的守则有两个主要问题:

1)你实际上并没有改变内容。执行newLines =实际上不会重新分配previsReadlines,因此没有记录更改。

( 2)没有任何东西可以用来写作。writelines需要一个您打算使用的参数。

代码语言:javascript
复制
# 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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49859115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档