Page 2 of 2
Re: Import a custom python library
Posted: 05 Nov 2024, 17:38
by coppelia
Code: Select all
#luaExec sim = require('sim')
#luaExec additionalPaths = {sim.getStringProperty(sim.handle_app, 'scenePath')}
from myModule import myFunction
def sysCall_init():
sim = require('sim')
print('starting')
myFunction(sim)
In your myModule.py, e.g.:
Code: Select all
import datetime
def myFunction(sim):
sim.addLog(sim.verbosity_scriptinfos, str(datetime.datetime.now()) + ' : Hello')
i.e. you need to pass the
sim object as function argument.
Or maybe better like that:
Code: Select all
#luaExec sim = require('sim')
#luaExec additionalPaths = {sim.getStringProperty(sim.handle_app, 'scenePath')}
import myModule
def sysCall_init():
sim = require('sim')
print('starting')
myModule.init(sim)
myModule.myFunction()
and:
Code: Select all
import datetime
def init(a):
global sim
sim = a
def myFunction():
global sim
sim.addLog(sim.verbosity_scriptinfos, str(datetime.datetime.now()) + ' : Hello')
Cheers
Re: Import a custom python library
Posted: 06 Nov 2024, 10:38
by javl0p
Ok, many thanks, it worked the following way
Code: Select all
#python
import sys
modulePath = 'C/:path/to/my/module/folder'
sys.path.append(modulePath)
and then calling the init function after declaring sim and simIK variables
Code: Select all
sim = require('sim')
simIK = require('simIK')
myModule.init(sim, simIK)
which is a bit cumbersome, but I guess is the only way to do it.
However, what I dont understand is why 'C:/path/to/my/module/folder' is not directly added by default, since I explicitely wrote that path in the 'usrset.txt' file
additionalPythonPath = 'C:/path/to/my/module/folder'
Shouldn't that option allow Coppelia to find all the .py script files in that folder?
Why do I have to add it manually in every child script?
Re: Import a custom python library
Posted: 06 Nov 2024, 11:37
by coppelia
Indeed...
you can pull
lua and that should be resolved.
Cheers
Re: Import a custom python library
Posted: 06 Nov 2024, 13:06
by javl0p
Ok, thanks for that commit.
But my company is stuck with CoppeliaPro v4.6 so I guess we can't have access to those last code changes...
Re: Import a custom python library
Posted: 06 Nov 2024, 15:29
by coppelia
Have a look at
the last commit, you should be able to figure out the changes to make.
Cheers
Re: Import a custom python library
Posted: 06 Nov 2024, 16:30
by javl0p
Ok, thank you!