Skip to content

Export through structures #5

@leoll2

Description

@leoll2

Hello!
I'm having troubles to export data structures that contain eigen members.

Example
This is a minimal example of what I would be able to do, but doesn't work.
Suppose that I have a struct like this:

struct Foo {
    int a;
    Eigen::Vector3d b;
};

I export it:

BOOST_PYTHON_MODULE(libhello) {
  SetupEigenConverters();

  class_<Foo>("Foo", init<>())
    .def_readonly("a", &Foo::a)
    .def_readonly("b", &Foo::b)
  ;
}

Then I try to work with it in Python:

import libhello
foo = libhello.Foo()
print(foo.b)

Unfortunately, I get an error like this:

TypeError: No Python class registered for C++ class Eigen::Matrix<double, 3, 1, 0, 3, 1>

Another example
This example works, but it is not exactly what I want.

struct Foo {
    int a;
    Eigen::Vector3e b;
    Eigen::Vector3d getB(void) { return b; }
};
class_<Foo>("Foo", init<>())
    .def_readonly("a", &Foo::a)
    .add_property("b", &Foo::getB)    // <- note the explicit getter
  ;

What is the issue?
These are just minimal examples, but in a real use case I have a complex hierarchy of classes. If I were to define an explicit getter for each attribute, then the complexity would easily explode. I don't really understand why I get the above error, the class should be already defined, no? If not, then why do the methods that directly return an eigen type work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions