Calling a Lua script from Python

Typically: "How do I... ", "How can I... " questions
Post Reply
aven
Posts: 8
Joined: 27 Feb 2020, 11:35

Calling a Lua script from Python

Post by aven » 25 Mar 2020, 15:47

Hello,
I have created the following piece of code, as suggested by another user:

Code: Select all

a={}
    for t=0,2,0.2
        do 
            a[t]=math.fmod(t,1)
            print(t..'\t'..a[t])
        end
    return {},{a},{},''
When I call that function from Python (I 've already used this function call with float values, but never with arrays) this way

Code: Select all

res, retInts, retFloats, retStrings, retBuffer = sim.simxCallScriptFunction(clientID, 'bubbleRob',
                                                                                    sim.sim_scripttype_childscript,
                                                                                    'sysCall_actuation', inputInts,
                                                                                    inputFloats,
                                                                                    inputStrings, inputBuffer,
                                                                                    sim.simx_opmode_blocking)
if res == sim.simx_return_ok:
         print(retFloats)
I get this error: Function didn't produce expected return values, i.e. an int table, a float table, a string table and a buffer string.
Why?
My idea is that there's something wrong inside the function, related to array management. I read everything here: https://www.lua.org/pil/contents.html#P1

Thank you.
Cheers
Last edited by aven on 26 Mar 2020, 10:43, edited 1 time in total.

coppelia
Site Admin
Posts: 7706
Joined: 14 Dec 2012, 00:25

Re: Calling a Lua script from Python

Post by coppelia » 26 Mar 2020, 10:19

So you are using the legacy remote API to call a script function in CoppeliaSim.

The called script function should return two tables of numbers, a table of strings, and a string.
In your case the function returns an empty table (ok), a table containing an empty table (not ok), an empty table (ok) and an empty string (ok).

Cheers

aven
Posts: 8
Joined: 27 Feb 2020, 11:35

Re: Calling a Lua script from Python

Post by aven » 26 Mar 2020, 10:26

coppelia wrote:
26 Mar 2020, 10:19
So you are using the legacy remote API to call a script function in CoppeliaSim.

The called script function should return two tables of numbers, a table of strings, and a string.
In your case the function returns an empty table (ok), a table containing an empty table (not ok), an empty table (ok) and an empty string (ok).

Cheers
Why is it an empty table? I would like to return the array a.

coppelia
Site Admin
Posts: 7706
Joined: 14 Dec 2012, 00:25

Re: Calling a Lua script from Python

Post by coppelia » 26 Mar 2020, 10:37

Sorry, not empty, but return value 2 is a table of a table. YOu should return:

Code: Select all

return {},a,{},''
not

Code: Select all

return {},{a},{},''
Cheers

aven
Posts: 8
Joined: 27 Feb 2020, 11:35

Re: Calling a Lua script from Python

Post by aven » 26 Mar 2020, 10:39

coppelia wrote:
26 Mar 2020, 10:37
Sorry, not empty, but return value 2 is a table of a table. YOu should return:

Code: Select all

return {},a,{},''
not

Code: Select all

return {},{a},{},''
Cheers
I've already tried to return just a but I got the same error reported before.

aven
Posts: 8
Joined: 27 Feb 2020, 11:35

Re: Calling a Lua script from Python

Post by aven » 29 Mar 2020, 09:43

coppelia wrote:
26 Mar 2020, 10:37
Sorry, not empty, but return value 2 is a table of a table. YOu should return:

Code: Select all

return {},a,{},''
not

Code: Select all

return {},{a},{},''
Cheers
Good morning,
I tried to create a vector a={1,2,3}, return it this way

Code: Select all

return {},a,{},''
and that's ok.
Therefore, could the problem be inside the for loop? Maybe, the instruction return does not like a table built in the loop...

fferri
Posts: 556
Joined: 09 Sep 2013, 19:28

Re: Calling a Lua script from Python

Post by fferri » 30 Mar 2020, 08:47

You fill a with:

Code: Select all

a[t]=math.fmod(t,1)
but t is not an integer positive index. That creates a map-like table.

To append an element to a use:

Code: Select all

table.insert(a, math.fmod(t,1))
That creates an array-like table.

aven
Posts: 8
Joined: 27 Feb 2020, 11:35

Re: Calling a Lua script from Python

Post by aven » 30 Mar 2020, 10:29

fferri wrote:
30 Mar 2020, 08:47
You fill a with:

Code: Select all

a[t]=math.fmod(t,1)
but t is not an integer positive index. That creates a map-like table.

To append an element to a use:

Code: Select all

table.insert(a, math.fmod(t,1))
That creates an array-like table.
Thank you very much, I fixed my problem.

Cheers

Post Reply