Page 1 of 1
Loading image in a script
Posted: Fri Jun 03, 2022 8:31 pm
by ggoblin
Hi,
I want to ask the user for an image file, then load it as an image layer.
I tried:
Code: Select all
local imgFile = LM.GUI.OpenFile("Select image file")
if not imgFile then return end
local imglayer = nil
imglayer = moho:CreateNewLayer(LT_IMAGE)
imglayer:SetSourceImage(imgFile)
But it doesn't work, seems imglayer is NIL when imglayer:SetSourceImage is called
I've use CreateNewLayer(LT_VECTOR) to create vector layers with no problem so assumed same for image layers (with LT_IMAGE).
What am I missing?
Thank you.
Re: Loading image in a script
Posted: Fri Jun 03, 2022 8:41 pm
by ggoblin
Also tried to explicitly caste the layer as an image layer:
Code: Select all
imglayer = moho:LayerAsImage(moho:CreateNewLayer(LT_IMAGE))
But made no difference

Re: Loading image in a script
Posted: Fri Jun 03, 2022 9:17 pm
by hayasidist
Layer type need a MOHO prefix: MOHO.LT_IMAGE (
https://www.mohoscripting.com/index.php ... &name=MOHO)
If that still gives you problems, check (i.e. print out) that the pathname return from LM.GUI.OpenFile is what you'd expect.
hth
Re: Loading image in a script
Posted: Fri Jun 03, 2022 10:55 pm
by ggoblin
Thank you Hayasidist for such a swift response!
What a silly mistake, thank you.
With that fixed it is now creating the image layer. but it still fails at the same line with same message:
Code: Select all
attempt to call method 'SetSourceImage' (a nil value)
When I print imgFile it is fine - shows the full path including the image name and extension. But for some reason it is not setting the source image for the image layer.
In Moho the image is displayed like a document icon with a cross on it, and when I open the layer properties and go to image tab and select 'Reveal source image..' it just opens the desktop directory (the image is not there, its off the content directory). I need to click on 'set source image..' to set the image to make it look right.
Are scripts limited to where they can open files from (for security)? Although this shouldn't be an issue as my file is off a subdirectory in the content directory.
Re: Loading image in a script
Posted: Fri Jun 03, 2022 11:05 pm
by ggoblin
Is the layer setting panel a lua script? Perhaps I can have a peek to see how they do it?
Where are all the Moho UI scripts kept?
mohoscripting.com is an amazing resource, especially when you look up a function and it lists scripts in mohscripts.com that use it so you can go have a peek to see how to use that function. It would be great if this could be extended to the scripts that come with Moho - both those in the scripts menu and Mohos tool panels, etc.
Re: Loading image in a script
Posted: Fri Jun 03, 2022 11:22 pm
by synthsin75
Try this:
Code: Select all
local imgFile = LM.GUI.OpenFile("Select image file")
if not imgFile then return end
moho:CreateNewLayer(MOHO.LT_IMAGE)
local imglayer = moho:LayerAsImage(moho.layer) --cast the layer just created as an image layer
imglayer:SetSourceImage(imgFile)
Only tool and menu scripts are scripts. None of the UI is.
Re: Loading image in a script
Posted: Fri Jun 03, 2022 11:58 pm
by ggoblin
Thank you Wes, your suggestion of casting works!
It also solved another similar problem I had where I created a group layer using
Code: Select all
grp = moho:CreateNewLayer(MOHO.LT_GROUP)
and it seemed to be recognised as a group layer as I successfully moved layers into it using
Code: Select all
moho:PlaceLayerInGroup(veclayer, grp)
but then later when I tried to change its masking properties using
Code: Select all
grp:SetGroupMask(MOHO.GROUP_MASK_HIDE_ALL)
It gave me the same error message:
Code: Select all
attempted to call method 'SetGroupMask' (a nil value)
Following your example I also fixed that error by using:
Code: Select all
grp = moho:LayerAsGroup(moho:CreateNewLayer(MOHO.LT_GROUP))
Thank you!
Where are the tool and menu scripts located?
All I found were the cpp bindings in the Extra Files folder. BTW they surprisingly seemed to suggest that the Image Layer is inherited from the Audio Layer?
Re: Loading image in a script
Posted: Sat Jun 04, 2022 1:54 am
by synthsin75
ggoblin wrote: ↑Fri Jun 03, 2022 11:58 pm
Thank you!
Where are the tool and menu scripts located?
All I found were the cpp bindings in the Extra Files folder. BTW they surprisingly seemed to suggest that the Image Layer is inherited from the Audio Layer?
Happy to help.
The tool and menu scripts are in the installation directory:
C:\Program Files\Moho\Resources\Support\Scripts\Tool
C:\Program Files\Moho\Resources\Support\Scripts\Menu
You can find more details about the Moho API from mohoscripting, in my signature.
Technically, an image layer and a movie layer are the same thing, which is why an image layer also has audio methods.
Re: Loading image in a script
Posted: Sun Jun 05, 2022 2:45 pm
by ggoblin
Thank you Wes, those scripts will be an invaluable source for trying to understand how to use scripting.
Is there any chance those scripts could be integrated into mohoscripting.com so when we look up a method it will search these scripts in addition to the ones on mohoscripts.com as examples of how to use that method? It would be extremely useful as often there are is no description of what a method does other than its name, so we can only rely on examples of its use to understand how to use it.
I didn't see any timeline related scripts, am I correct in assuming that means its all hard coded in cpp and can't be modified by a lua script? For example, it would have been nice to add extra keyframe interpolation modes via lua.
Re: Loading image in a script
Posted: Sun Jun 05, 2022 6:00 pm
by hayasidist
ggoblin wrote: ↑Sun Jun 05, 2022 2:45 pm
...
I didn't see any timeline related scripts, am I correct in assuming that means its all hard coded in cpp and can't be modified by a lua script? For example, it would have been nice to add extra keyframe interpolation modes via lua.
nope - the interp modes are hard-wired but ...
with bezier interpolation you can get "any" (smooth curve) profile between keys.
You can set Bezier interp by script, but you'll need to convert between "standard" bezier control points {P0, P1} and the "out" component of the first keyframe; and between {P2,P3} and the "in" component of the following key.
Sorry, I don't have an example of this for keyframes; but
http://www.lostmarble.com/forum/viewtop ... 77#p195377 links to a script that manages the bezier handles for points -- that might help, but shout if you want more about the maths of Bezier curves.
Re: Loading image in a script
Posted: Sun Jun 05, 2022 9:09 pm
by ggoblin
Thank you, I will check it out.