problem with PyRunner when importing Yade in python

Asked by Alexander Eulitz [Eugen]

Hi,
I imported Yade in python like described in [1].
This works but when I create a class in which Yade is called and add a PyRunner that calls a function of the class, I get an error:
NameError: name 'self' is not defined
Traceback (most recent call last):
  File "<string>", line 1, in <module

Can somebody help me with that, please?

here is the code:
import sys
sys.path.append('/home/dummy/Yade/YadePythonImport')
from yadePythonImport import *

class MyYadeSim():
    def __init__(self):
        self.data = []
        O.engines=[ForceResetter(),
            InsertionSortCollider([Bo1_Sphere_Aabb(),Bo1_Facet_Aabb()]),
            InteractionLoop(
            [Ig2_Sphere_Sphere_ScGeom(),Ig2_Facet_Sphere_ScGeom()],
            [Ip2_FrictMat_FrictMat_MindlinPhys()],
            [Law2_ScGeom_MindlinPhys_Mindlin()]
            ),NewtonIntegrator(gravity=(0,0,-9.81), label='newtonIntegrator'),
            PyRunner(iterPeriod=1, command="self.storeData()", label='storage')
         ]
    def run(self):
        O.run(10,True)
        print self.data
    def storeData(self):
        self.data.append(O.time)
if __name__ == "__main__":
    mySim = MyYadeSim()
    mySim.run()

------
[1] https://yade-dem.org/doc/user.html#importing-yade-in-other-python-applications

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Alexander Eulitz [Eugen]
Solved:
Last query:
Last reply:
Revision history for this message
Jan Stránský (honzik) said :
#1

Hi Alexander,
try to call specific object, like

command="mySim.storeData()"

if this does not work, add

import __builtin__
__builtin__.mySim = mySim

after you create mySim

If nothing of this works, let us know :-)

cheers
Jan

2013/9/17 Alexander Eulitz [Eugen] <email address hidden>

> New question #235866 on Yade:
> https://answers.launchpad.net/yade/+question/235866
>
> Hi,
> I imported Yade in python like described in [1].
> This works but when I create a class in which Yade is called and add a
> PyRunner that calls a function of the class, I get an error:
> NameError: name 'self' is not defined
> Traceback (most recent call last):
> File "<string>", line 1, in <module
>
> Can somebody help me with that, please?
>
> here is the code:
> import sys
> sys.path.append('/home/dummy/Yade/YadePythonImport')
> from yadePythonImport import *
>
> class MyYadeSim():
> def __init__(self):
> self.data = []
> O.engines=[ForceResetter(),
>
> InsertionSortCollider([Bo1_Sphere_Aabb(),Bo1_Facet_Aabb()]),
> InteractionLoop(
>
> [Ig2_Sphere_Sphere_ScGeom(),Ig2_Facet_Sphere_ScGeom()],
> [Ip2_FrictMat_FrictMat_MindlinPhys()],
> [Law2_ScGeom_MindlinPhys_Mindlin()]
> ),NewtonIntegrator(gravity=(0,0,-9.81),
> label='newtonIntegrator'),
> PyRunner(iterPeriod=1, command="self.storeData()",
> label='storage')
> ]
> def run(self):
> O.run(10,True)
> print self.data
> def storeData(self):
> self.data.append(O.time)
> if __name__ == "__main__":
> mySim = MyYadeSim()
> mySim.run()
>
> ------
> [1]
> https://yade-dem.org/doc/user.html#importing-yade-in-other-python-applications
>
> --
> You received this question notification because you are a member of
> yade-users, which is an answer contact for Yade.
>
> _______________________________________________
> Mailing list: https://launchpad.net/~yade-users
> Post to : <email address hidden>
> Unsubscribe : https://launchpad.net/~yade-users
> More help : https://help.launchpad.net/ListHelp
>

Revision history for this message
Alexander Eulitz [Eugen] (kubeu) said :
#2

Thanks. this works.

   command="mySim.storeData()"
is sufficient if you create the object of the class in which yade is used in the file of the class as it is shown in my code above.

the second solution:
   import __builtin__
   __builtin__.mySim = mySim
is necessary if you work with diffrent files in your programm.
In my case there is a main program that handles an optimisation algorithm which passes simulation parameters to the MyYadeSim class. (the object of MyYadeSim in the main programm is named mySim)

Revision history for this message
Alexander Eulitz [Eugen] (kubeu) said :
#3

I could add an example script somewhere if you like

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

On 18/09/13 14:41, Alexander Eulitz [Eugen] wrote:
> Question #235866 on Yade changed:
> https://answers.launchpad.net/yade/+question/235866
>
> Alexander Eulitz [Eugen] posted a new comment:
> I could add an example script somewhere if you like
>
Would be nice!
Importing yade is sometimes tricky. We may add an example/importingYade
folder to collect a few examples. It would certainly help.

Bruno