gpt4 book ai didi

c++ - 带有多个参数的 QProcess 启动进程(blender.exe)

转载 作者:行者123 更新时间:2023-11-28 05:40:32 28 4
gpt4 key购买 nike

我尝试使用 QProcess(在 Windows 上)从我的程序 (FaceModifier.exe) 内部启动 blender.exe。该命令遵循以下结构:

'path-to-blender' --background 'path-to-blend-file' --python 'path-to-python-script' -- 'additional-arg-for-python-script'

一个完整的例子(如果我把它输入 cmd.exe 就可以)是

"C:\Program Files\Blender Foundation\Blender\blender.exe" --background "C:\Program Files(x86)\FaceModifier\Resources\GenericHeadMesh.blend" --python "C:\Program Files(x86)\FaceModifier\python\local.py" -- "C:\Users\Gunnar\Documents\FaceModifier\Output\"

现在,在我的程序中,我转义了路径并用引号将它们括起来,所以我得到了这样的东西

std::string blenderPath := "\"C:\\Program Files\\Blender Foundation\\Blender\\blender.exe\""

对于 QProcess,我将我的所有参数都输入到带有前导 /c 的 QStringList 中,因此它被视为单个命令并将其传递给 cmd.exe

我的问题是我无法执行此操作。如果我手动将命令(我传递给 QProcess,而不是上面的命令)键入 cmd。

启动进程的函数如下所示:

void PythonConnector::createSubprocess(const QStringList &args)
{
QProcess *process = new Process();
process->start("cmd.exe", args);
process->waitForFinished(-1);

QString result(process->readAll());
qDebug() << "result: " << result; // this gives just an empty string

process->close();
}

我这样调用它:

// for the documents path
CoInitialize(NULL);
TCHAR *myDocuments = 0;
SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &myDocuments);
CString temp(myDocuments);
CT2CA tempConv(temp);
std::string outputFolderPath(tempConv);
outputFolderPath += "\\FaceModifier\\Output\\";
outputFolderPath = pH.ExcapeString(outputFolderPath);
CoTaskMemFree(myDocuments);

blenderPath = pH.EscapeString(blenderPath);

std::string meshGlobalPath = "\"" + pH.GetResourcesPath() + "GenericHeadMesh.blend" + "\"";
std::string pythonGlobalPath = "\"" + pH.GetPythonPath() + "global.py" + "\"";

QStringList args;
args << "/c" << QString::fromStdString(blenderPath) << "--background" << QString::fromStdString(meshGlobalPath) << "--python" << QString::fromStdString(pythonGlobalPath) << "--" << QString::fromStdString("\"" + outputFolderPath "\"");
pC.createSubprocess(args);

blenderPath 被传递给这个函数,并在另一个函数中从注册表中读取。
这些是我的其他辅助函数:

std::string PathHandler::GetResourcesPath()
{
if(resourcesPath.empty())
resourcesPath = GetFullPath("Resources\\");
return resourcesPath;
}

std::string PathHandler::GetPythonPath()
{
if(pythonPath.empty())
pythonPath = GetFullPath("python\\");
return pythonPath;
}

std::string PathHandler::GetFullPath(const std::string &relPath)
{
char full[_MAX_PATH];
_fullpath(full, relPath.c_str(), _MAX_PATH);
return EscapeString(std::string(full));
}

std::string PathHandler::EscapeString(const std::string &input)
{
std::regex toReplace("\\\\");
std::string output(input.begin(), input.end());
output = std::regex_replace(output, toReplace, "\\\\");
return output;
}

使用 qDebug 调试 args 列表会产生这些输出(这些实际上来自 Visual Studio 2013 中的调试,因此路径不同):

"/c"  
""C:\\Program Files\\Blender Foundation\\Blender\\blender.exe""
"--background"
""C:\\Users\\Gunnar\\documents\\visual studio 2013\\Projects\\FaceModifier\\x64\\Release\\Resources\\GenericHeadMesh.blend""
"--python"
""C:\\Users\\Gunnar\\documents\\visual studio 2013\\Projects\\FaceModifier\\x64\\Release\\python\\global.py""
"--"
""C:\\Users\\Gunnar\\Documents\\FaceModifier\\Output\\""

result 来自 createSubprocess 的调试只给出 ""

如果我像这样在 cmd 中手动输入此命令:

cmd /c "C:\Program Files\Blender Foundation\Blender\blender.exe" --background "C:\Users\Gunnar\documents\visual studio 2013\Projects\FaceModifier\x64\Release\Resources\GenericHeadMesh.blend" --python "C:\Users\Gunnar\documents\visual studio 2013\Projects\FaceModifier\x64\Release\python\global.py" -- "C:\Users\Gunnar\Documents\FaceModifier\Output\"

我得到 命令“C:\\Program”拼写错误或找不到。对于有和没有像这样转义的各种不同的引用都是一样的

cmd "/c \"C:\Program Files\Blender Foundation\Blender\blender.exe\" --background \"C:\Users\Gunnar\documents\visual studio 2013\Projects\FaceModifier\x64\Release\Resources\GenericHeadMesh.blend\" --python \"C:\Users\Gunnar\documents\visual studio 2013\Projects\FaceModifier\x64\Release\python\global.py\" -- \"C:\Users\Gunnar\Documents\FaceModifier\Output\\""

只需输入

cmd /c "C:\Program Files\Blender Foundation\Blender\blender.exe"

工作正常,但这显然不是我需要的。

我想不通我必须如何构建它才能正常工作。有人可以告诉我我的错误是什么吗?

更新:
好的,理论上它现在可以工作(感谢 ahmed),但它确实取决于路径。如果我在 ...\Documents\FaceModifier 中有 .blend 和 .py,如果它们在 C:\Program Files (x86)\FaceModifier\ 中,它就可以工作(安装我的程序的地方)它不起作用。我怎样才能做到这一点?我更喜欢这样,这样我就不必将那些文件复制到那里。

最佳答案

你不需要引用或双引号文件路径,QProccess 已经处理了,将这部分代码更改为:

std::string meshGlobalPath = pH.GetResourcesPath() + "GenericHeadMesh.blend" ;
std::string pythonGlobalPath = pH.GetPythonPath() + "global.py" ;

QStringList args;
args << "/c" << QString::fromStdString(blenderPath) << "--background"
<< QString::fromStdString(meshGlobalPath) << "--python"
<< QString::fromStdString(pythonGlobalPath) << "--"
<< QString::fromStdString(outputFolderPath);

关于c++ - 带有多个参数的 QProcess 启动进程(blender.exe),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37213854/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com