首页 > 解决方案 > 尝试更新命令行输入

问题描述

我的命令行输入如下所示:

python rerun_edit.py examples/Testing/config.yaml examples/Testing/0.blend examples/Testing/output

我的程序如下所示:

import subprocess
import sys
import os
import pathlib
    
# this sets the amount of scenes
amount_of_scenes = 2
# this sets the amount of runs, which are performed
amount_of_runs = 5

# set the folder in which the run.py is located
rerun_folder = os.path.abspath(os.path.dirname(__file__))

blend_path = ["examples/Testing/0.blend"]

for scene_id in range(amount_of_scenes):
    
    # the first one is the rerun.py script, the last is the output
    used_arguments = str(sys.argv[1]) + (blend_path) + str(sys.argv[-1])
    output_location = os.path.abspath(sys.argv[-1])
    for run_id in range(amount_of_runs):
        # in each run, the arguments are reused
        cmd = ["python", os.path.join(rerun_folder, "run.py")]
        cmd.extend(used_arguments)
        # the only exception is the output, which gets changed for each run, so that the examples are not overwritten
        #cmd.append(os.path.join(output_location, str(run_id)))
        cmd.append(output_location)
        print(" ".join(cmd))
        # execute one BlenderProc run
        subprocess.call(" ".join(cmd), shell=True)
        print(used_arguments)
        print(cmd)
        
    #get the blend file 
    old_blend_file = str(scene_id) + ".blend"
    new_blend_file = str(scene_id + 1) + ".blend"
    blend_path = pathlib.Path(str(blend_path).replace(old_blend_file, new_blend_file))
    print(blend_path)

它读取命令行输入并执行run.py程序进行一定数量的运行。

运行后,我需要将命令行输入更新为以下内容:

python rerun_edit.py examples/Testing/config.yaml examples/Testing/1.blend examples/Testing/output

这样它就会再次执行一定数量的运行,但使用不同的 .blend 文件作为输入。

我尝试实现一个 for 循环并在运行完成后调整路径名称,但我总是收到一条错误消息:

used_arguments = str(sys.argv[1]) + (blend_path) + str(sys.argv[-1])
TypeError: can only concatenate str (not "list") to str

谁能帮我吗?非常感谢任何帮助。

非常感谢 :)

标签: pythoncommand-linetypeerrorblendersys

解决方案


这里是

import subprocess
import sys
import os
import pathlib
    
# this sets the amount of scenes
amount_of_scenes = 2
# this sets the amount of runs, which are performed
amount_of_runs = 5

# set the folder in which the run.py is located
rerun_folder = os.path.abspath(os.path.dirname(__file__))

blend_path = "examples/Testing/0.blend"

for scene_id in range(amount_of_scenes):
    
    # the first one is the rerun.py script, the last is the output
    used_arguments = [sys.argv[1], blend_path, sys.argv[-1]]
    output_location = os.path.abspath(sys.argv[-1])
    for run_id in range(amount_of_runs):
        # in each run, the arguments are reused
        cmd = ["python", os.path.join(rerun_folder, "run.py")]
        cmd += used_arguments
        # the only exception is the output, which gets changed for each run, so that the examples are not overwritten
        #cmd.append(os.path.join(output_location, str(run_id)))
        cmd.append(output_location)
        print(" ".join(cmd))
        # execute one BlenderProc run
        subprocess.call(" ".join(cmd), shell=True)
        print(used_arguments)
        print(cmd)
        
    blend_path = pathlib.Path(blend_path.replace(("%d.blend" % scene_id), ("%d.blend" % (scene_id + 1))))
    print(blend_path)

请检查它是否有效,如果有任何错误,请评论我


推荐阅读