How can I use require() in a script?

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
bbrraayyaann
Posts: 68
Joined: Thu Jan 24, 2019 3:52 am

How can I use require() in a script?

Post by bbrraayyaann »

Hello, I have seen many useful libraries, but I don't know if I could install them in a lua script and use the require() function.
Would anyone know how to install those libraries and then use them in a script ?

These libraries can be downloaded from github.com .
User avatar
SimplSam
Posts: 1048
Joined: Thu Mar 13, 2014 5:09 pm
Location: London, UK
Contact:

Re: How can I use require() in a script?

Post by SimplSam »

I have used this technique to load pure XML lua libraries in the SS SVG Import script, in a manner similar to that below:

Essentially - you need to tell Moho (Lua) where to look, by appending your path to the Lua package.path, but you also only want to do that once.

Below is an example for JSON - where I have the script file 'json.lua' in the custom ScriptResources folder, and then tell Moho (Lua) to look in there for *.lua files, after which I require 'json' which searches for json.lua. You can also use 'mysubfolder.json' or 'mysubfolder/json' to look in subfolders of the package.path.

Code: Select all

local json
function XX_My_Script:IsRelevant(moho)
    if (not _did_my_resources_package_path) then
        package.path = package.path .. ";" .. moho:UserAppDir() .. "/scripts/ScriptResources/?.lua;"
        _did_my_resources_package_path = true
    end
    json = json or require("json")
    return true
end
For ref:: https://gist.github.com/tylerneylon/59f ... be525b30ab -- a nice & compact pure Lua JSON lib.
Last edited by SimplSam on Thu Apr 06, 2023 2:07 pm, edited 1 time in total.
Moho 14.1 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.1 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam


Sam
bbrraayyaann
Posts: 68
Joined: Thu Jan 24, 2019 3:52 am

Re: How can I use require() in a script?

Post by bbrraayyaann »

SimplSam wrote: Wed Apr 05, 2023 6:26 am I have used this technique to load pure XML lua libraries in the SS SVG Import script, in a manner similar to that below:

Essentially - you need to tell Moho (Lua) where to look, by appending your path to the Lua package.path, but you also only want to do that once.

Below is an example for JSON - where I have the script file 'json.lua' in the custom ScriptResources folder, and then tell Moho (Lua) to look in there for *.lua files, after which I require 'json' which searches for json.lua. You can also use 'mysubfolder.json' or 'mysubfolder/json' to look in subfolders of the package.path.

Code: Select all

local json
function XX_My_Script:IsRelevant(moho)
    if (not _did_my_resources_package_path) then
        package.path = package.path .. ";" .. moho:UserAppDir() .. "/scripts/ScriptResources/?.lua;"
        _did_my_resources_package_path = true
    end
    json = json or require("json")
    return true
end
Thank you.
bbrraayyaann
Posts: 68
Joined: Thu Jan 24, 2019 3:52 am

Re: How can I use require() in a script?

Post by bbrraayyaann »

SimplSam wrote: Wed Apr 05, 2023 6:26 am I have used this technique to load pure XML lua libraries in the SS SVG Import script, in a manner similar to that below:

Essentially - you need to tell Moho (Lua) where to look, by appending your path to the Lua package.path, but you also only want to do that once.

Below is an example for JSON - where I have the script file 'json.lua' in the custom ScriptResources folder, and then tell Moho (Lua) to look in there for *.lua files, after which I require 'json' which searches for json.lua. You can also use 'mysubfolder.json' or 'mysubfolder/json' to look in subfolders of the package.path.

Code: Select all

local json
function XX_My_Script:IsRelevant(moho)
    if (not _did_my_resources_package_path) then
        package.path = package.path .. ";" .. moho:UserAppDir() .. "/scripts/ScriptResources/?.lua;"
        _did_my_resources_package_path = true
    end
    json = json or require("json")
    return true
end
For ref:: https://gist.github.com/tylerneylon/59f ... be525b30ab -- a nice & compact pure Lua JSON lib.
Hello, a question.
Could you tell me in json there is a function that is "decode" when I call require("json")
but when I call json.decode(result) there is an error, why is that?
User avatar
SimplSam
Posts: 1048
Joined: Thu Mar 13, 2014 5:09 pm
Location: London, UK
Contact:

Re: How can I use require() in a script?

Post by SimplSam »

bbrraayyaann wrote: Sun Apr 09, 2023 6:41 pm ... Could you tell me in json there is a function that is "decode" when I call require("json"), but when I call json.decode(result) there is an error, why is that?
It depends which library you were using, and also what the error was.

If it was the same library as the one I referenced, then it has 2 primary functions.

myTable = json.parse(myString) - to convert a JSON text string into a Lua table (encode)
myString = json.stringify(myTable) - to convert a Lua table into a JSON text string (decode)

* The Lua table acts as the JSON object.
Last edited by SimplSam on Tue Apr 18, 2023 5:14 am, edited 1 time in total.
Moho 14.1 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.1 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam


Sam
bbrraayyaann
Posts: 68
Joined: Thu Jan 24, 2019 3:52 am

Re: How can I use require() in a script?

Post by bbrraayyaann »

SimplSam wrote: Sun Apr 09, 2023 7:58 pm
bbrraayyaann wrote: Sun Apr 09, 2023 6:41 pm ... Could you tell me in json there is a function that is "decode" when I call require("json"), but when I call json.decode(result) there is an error, why is that?
It depends which library you were using, and also what the error was.

If it was the same library as the one I referenced, then it has 2 primary functions.

myTable = json.parse(myString) - to convert a JSON text string into a Lua table (encode)
myString = json.stringyfy(myTable) - to convert a Lua table into a JSON text string (decode)

* The Lua table acts as the JSON object.
Hi SimplSam, thanks for the answer.
Is there any way to install libraries with C using Lua?
For example I want to use this library :https://github.com/lunarmodules/luasocket
But using package.path doesn't work.
Do you think it is possible?
User avatar
SimplSam
Posts: 1048
Joined: Thu Mar 13, 2014 5:09 pm
Location: London, UK
Contact:

Re: How can I use require() in a script?

Post by SimplSam »

bbrraayyaann wrote: Tue Apr 11, 2023 3:23 pm ... Is there any way to install libraries with C using Lua?
For example I want to use this library :https://github.com/lunarmodules/luasocket
But using package.path doesn't work.
Do you think it is possible?
This has been tried before, and unfortunately the current conclusion is binary libraries do not work with Moho. There are 2 primary reasons:

1) Binary compatibility

Whilst most generic Lua script code can run on any version of Lua with few modifications; The ABI (Application Binary Interface) of Lua major.minor versions are not compatible. i.e. Lua 5.2.xx is not binary compatible with 5.1.xx nor 5.4.xx. etc. So a binary plugin built for 5.1 would not run with a 5.2 host.

You could potentially get around this issue by building your binary client library to the correct matching Lua version.


2) Duplicate virtual machines

Current Moho runs Lua version 5.2, with processes in a virtual machine of a non-shared binary library. Which means add-on client libraries would need to run in another Lua virtual machine instance. Unfortunately Lua 5.2 does not allow what it calls 'duplicate virtual machines', thus you cannot run the host & client binaries simultaneously - as parent & child.

It order to fix this, Moho would need to either use shared Lua libraries or Moho & Client upgrade to Lua 5.4+.
Moho 14.1 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.1 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam


Sam
bbrraayyaann
Posts: 68
Joined: Thu Jan 24, 2019 3:52 am

Re: How can I use require() in a script?

Post by bbrraayyaann »

SimplSam wrote: Thu Apr 13, 2023 4:43 am
bbrraayyaann wrote: Tue Apr 11, 2023 3:23 pm ... Is there any way to install libraries with C using Lua?
For example I want to use this library :https://github.com/lunarmodules/luasocket
But using package.path doesn't work.
Do you think it is possible?
...
I see, I have investigated and what can be done is to compile the library to be portable to pure Lua code.
And yes it is possible but you need advanced knowledge in C.
Another option I looked into and it worked for me, was to write a code similar to the library you want in Python language and then compile it into a .exe file.
Then run io.popen() in lua with the .exe file you created in Python.
And that works just pass it as an argument what you want in lua using io.popen and it will run as an external file using Python.
Post Reply