How to pass a python class to a C++ class

Asked by Reza Housseini

Hello

I try to write a ClumpsFactory class and want therefore pass a utils.clumpTemplate class from python to my C++ class.
I tried to pass it as a python::object

    ((python::object,clumpTemplate,NaN,,"ClumpTemplate for clumps to be generated"))

But it gives my odd compilation errors regarding a missing ´serialize´ function. Does somebody know how I can achieve this?

Cheers
Reza

Question information

Language:
English Edit question
Status:
Answered
For:
Yade Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Christian Jakob (jakob-ifgt) said :
#1

Hi,

> I tried to pass it as a python::object

Yes this is tricky. An example how you can pass python lists and objects is here:

https://github.com/yade/trunk/blob/master/py/wrapper/yadeWrapper.cpp#L201

Can you explain what your ClumpsFactory should do?

Creating/modifying clumps is already implemented in several methods.
Please also have a look at the manual section about clumps

https://yade-dem.org/doc/user.html#clumping-particles-together

regards,

christian

Revision history for this message
Reza Housseini (reza-housseini-3) said :
#2

Hi Christian

Thanks for your answer. I took my implementation from the yadeWrapper.cpp file. But nonetheless it's not working with a python::object as an argument type.

The ClumpsFactory is similar to the SpheresFacory, it's spitting out clumps for a defined massFlowRate during the simulation.

Revision history for this message
Bruno Chareyre (bruno-chareyre) said :
#3

Hi Reza,

Your compilation problem is because you are not simply trying to pass an
argument. The syntax you are using is for registering an attribute (*),
which is more demanding in terms of object type (it has to inherit from
serializable).
It should be possible to achieve what you want by passing arguments, as
shown by Christian. The main bad side is that the template list will be
lost if you save the simulation and reload it latter. If it is really a
problem, you can probably register standard vectors to handle the data,
in the form below:

class Factory {
void setTemplates(python::list templates) {//convert templates into
lists of Vector3r and Real registered below }
YADE_CLASS_...
((vector<vector<Vector3r> >, positions, ...))
((vector<vector<Real> >, radii, ...))
}

HTH

Bruno

(*) https://www.yade-dem.org/doc/prog.html#attribute-registration

On 11/09/13 13:56, Reza Housseini wrote:
> Question #235552 on Yade changed:
> https://answers.launchpad.net/yade/+question/235552
>
> Reza Housseini gave more information on the question:
> Hi Christian
>
> Thanks for your answer. I took my implementation from the
> yadeWrapper.cpp file. But nonetheless it's not working with a
> python::object as an argument type.
>
> The ClumpsFactory is similar to the SpheresFacory, it's spitting out
> clumps for a defined massFlowRate during the simulation.
>

--
_______________
Bruno Chareyre
Associate Professor
ENSE³ - Grenoble INP
Lab. 3SR
BP 53
38041 Grenoble cedex 9
Tél : +33 4 56 52 86 21
Fax : +33 4 76 82 70 43
________________

Revision history for this message
Christian Jakob (jakob-ifgt) said :
#4

> it's not working with a python::object as an argument type

Can you send the error message when you try to compile?

Can you help with this problem?

Provide an answer of your own, or ask Reza Housseini for more information if necessary.

To post a message you must log in.