Loading image 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
ggoblin
Posts: 266
Joined: Wed Jan 19, 2022 2:09 pm

Loading image in a script

Post 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.
ggoblin
Posts: 266
Joined: Wed Jan 19, 2022 2:09 pm

Re: Loading image in a script

Post 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 :(
User avatar
hayasidist
Posts: 3557
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Loading image in a script

Post 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
ggoblin
Posts: 266
Joined: Wed Jan 19, 2022 2:09 pm

Re: Loading image in a script

Post 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.
ggoblin
Posts: 266
Joined: Wed Jan 19, 2022 2:09 pm

Re: Loading image in a script

Post 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.
User avatar
synthsin75
Posts: 10007
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Loading image in a script

Post 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.
ggoblin
Posts: 266
Joined: Wed Jan 19, 2022 2:09 pm

Re: Loading image in a script

Post 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?
User avatar
synthsin75
Posts: 10007
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Loading image in a script

Post 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.
ggoblin
Posts: 266
Joined: Wed Jan 19, 2022 2:09 pm

Re: Loading image in a script

Post 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.
User avatar
hayasidist
Posts: 3557
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Loading image in a script

Post 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.
ggoblin
Posts: 266
Joined: Wed Jan 19, 2022 2:09 pm

Re: Loading image in a script

Post by ggoblin »

Thank you, I will check it out.
Post Reply