Open saroj31 opened 2 years ago
You need to have a definition of py::class
Thank @Skylion007 , OK I will try to do that.
Just to verify my understanding and to clarify. Here rs2::context is a C++ class and pyrealsense2.pyrealsense2.context is a python realsense context class.
Here do I have to write a my own pyclass_
py::class_<rs2::context>(m, "rs2_context");
I did the same.
also in another attempt I added a default constructor in it like this: py::class_ < rs2::context >(m, "rs2_context") .def(py::init());
but in both the cases I got the below error. I get this error now:
Traceback (most recent call last):
File "~/build/python_wrapper/test.py", line 5, in
Invoked with: <pyrealsense2.pyrealsense2.context object at 0x7fe65df68230>
So, Finally I wrote a custom type caster after that it worked. Now I can work around with this topic I think.
namespace PYBIND11_NAMESPACE { namespace detail {
template <> struct type_caster<rs2::context> {
public:
/**
* This macro establishes the name 'rs2::context' in
* function signatures and declares a local variable
* 'value' of type rs2::context
*/
PYBIND11_TYPE_CASTER(rs2::context, const_name("rs2::context"));
/**
* Conversion part 1 (Python->C++): convert a PyObject into a rs2::context.
*The second argument
* indicates whether implicit conversions should be applied.
*/
bool load(handle src, bool) {
std::cout<<"python to c++ conversion"<<std::endl; //debug code
rs2::context context;
value = context;
return true;
}
/**
* Conversion part 2 (C++ -> Python): convert an rs2::context instance into
* a Python object. The second and third arguments are used to
* indicate the return value policy and parent object (for
* ``return_value_policy::reference_internal``) and are generally
* ignored by implicit casters.
*/
static handle cast(rs2::context src, return_value_policy /* policy */, handle /* parent */) {
std::cout<<"C++ to python conversion"<<std::endl; //debug mode
return py::bool_(true).release();
}
};
}} // namespace PYBIND11_NAMESPACE::detail
This has made it work on my side. Here I am not understanding how it works. Just debugging it using some std::cout made me navigate the problem and understand where to put my code. Feel free to correct the code here if something needs to be changed. It would be great for understanding the mechanism and help me solve my problems in future.
Can I convert a PyObject object to a C++ object type ? I try to do it this way:
bool load(handle src, bool) {
PyObject *py_object_src = src.ptr();
rs2::context *tmp = reinterpret_cast<rs2::context *>(py_object_src);
I use this code in the load() of the type_caster. After that I get a segmentation fault in my Python test.py. Looks like wherever I try to use that tmp object in my C++ side it gives a seg fault. It would be best If I can access the C++ object from the PyObject that I get. I can assign that to my C++ Context object.
MY Goal is to access the context object that python has sent from python to make a similar or a copy in C++ side. Any help is welcome here.
I am ok to hear that,"pybind11 is not used for this purpose, and you are using it in a wrong way bro" too.
CppObject *tmp = py_object.cast<CppObject*>();
Discussed in https://github.com/pybind/pybind11/discussions/4357