Omniverse kit支持pybind11,让我们可以在C++中编写好dll插件作为接口提供给Omniverse kit进行python调用。
一、创建C++ dll插件,通过pybind11绑定,提供C++接口
我们先通过Visual Studio创建一个空白工程:


空白工程创建好后,我们在工程中添加一个Test类,代码结构如下:

选中cppInterface项目,编辑属性页——将项目配置类型更改为dll,并将生成的文件扩展名更改为.pyd


同时我们添加对应的c++包含目录,这里一个是python库目录,一个是pybind11库目录(如pybind11未安装,可找到omniverse kit对应的pip文件进行安装:"###\AppData\Local\ov\data\Kit\Luban Workshop\2022.2\pip3-envs\default\bin\pip.exe" install pybind11)

添加对应库目录:

我们在Test类中添加导出方法SetFilePath与GetFilePath:
#pragma once
#include <string>
class Test
{
public:
void SetFilePath(const std::string& filePath);
const std::string& GetFilePath() const;
private:
std::string m_filePath;
};
#include "Test.h"
void Test::SetFilePath(const std::string& filePath)
{
m_filePath = filePath;
}
const std::string& Test::GetFilePath() const {
return m_filePath;
}
然后我们新建一个TestBinding的cpp文件,添加对应的绑定
#include <pybind11/pybind11.h> #include "Test.h" PYBIND11_MODULE(cppInterface, m) { py::class_<Test>(m, "Test") .def(py::init<>()) .def("SetFilePath", &Test::SetFilePath) .def("GetFilePath", &Test::GetFilePath) .def("__repr__", [](const Test& object) { return "<cppInterface.Test>"; } ); }
编译成功后,至此C++代码部分结束,我们便可以在Omniverse kit中使用python对Test.pyd进行接口调用
浙公网安备 33010602011771号