The path has to include everything, including the drive (like "c:\" in windows, or whatever would be an acceptable path to use for command line work in whatever OS you are using. If you want to pick the file, you can use this:
Code: Select all
local path = LM.GUI.OpenFile("Choose file")
if (path == "") then
return
end
local file = io.open(path, "r")
if (file == nil) then
print("file doesn't exist!:", path)
return
end
file:close()
Then you don't have to hard code the path, and it works the same on any OS. All the bits from "local file =" are just some basic error checking, to make sure theres actually a file there, maybe not necessary but idiot-proofing is good, especially if the idiot is your OS. But you're real problem is that it should be "layer:SetSourceImage(...". Note the colon. That means applying "SetSourceImage" to the object "layer". If you use the dot, then it means you have a global value of "layer", with a sub-value that's a function called "SetSourceImage".
Also If you want, I've been building up my
library (which goes in the Moho/scripts/utility directory, and you can make you're own for commonly used things) to do all this kind of stuff, just to simplify things. There are 2 functions that relate to this:
Code: Select all
---------------------------------------------------------------------
-- create a new image layer, with an optional name
-- use - newLayer = SF.newImageLayer(moho, "Absolutely Great Image")
-- the name can of course be a string variable
---------------------------------------------------------------------
function SF.newImageLayer(moho, name)
local anImageLayer = moho:LayerAsImage(moho:CreateNewLayer(MOHO.LT_IMAGE) )
if name then
anImageLayer:SetName(name)
end
return anImageLayer
end
---------------------------------------------------------------------
-- Import an image into a new image layer. The "name" parameter is
-- optional, if omitted the filename will be used. The "useExt"
-- parameter is either true or false, and determines, if you don't
-- provide the optional layer "name", if the file extension will be
-- stripped from the layer name.
---------------------------------------------------------------------
function SF.ImportImage(moho, path, useExt, name)
local layer = nil
if name then
layer = SF.newImageLayer(moho, name)
else
local filename = SF.filenameFromPath(path)
if not useExt then
filename = SF.removeExtFromFilename(filename)
end
layer = SF.newImageLayer(moho, filename)
end
layer:SetSourceImage(path)
return layer
end
There are a few bit's in there for common path manipulation:
Code: Select all
function SF.findFromEnd(s, pattern)
local lastChar = string.len(s)
local i = -1
position = nil
while (position == nil) and ((-i) <= string.len(s)) do
position = string.find(s, pattern, i)
if (position == lastChar) then position = nil end
i = i - 1
end
return position
end
function SF.filenameFromPath(path)
local found = SF.findFromEnd(path,"[\\/]")
if found then
return string.sub(path ,found + 1, string.len(path))
end
end
function SF.removeExtFromFilename(filename)
local found = SF.findFromEnd(filename,"[.]")
if found then
return string.sub(filename, 1, found - 1)
end
end
That's all stuff I'm doing in a lot of different places, so it's handy. Once you have a function debugged, it also helps save time from tracking down that minor misspelling or capital where it should be lowercase. Lua's awful picky with that stuff, so it saves a lot of hassle in the end.
Was that too much? Anyway, if you don't feel like dealing with some of the wierdness it takes a little while to get used to just yet, feel free to use anything in there, that's what it's for.
If you look my last tweak to Janimatics bulk importer, you can see