LoadDocument returns nil and not a MohoDoc

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
mminsel
Posts: 9
Joined: Fri Oct 25, 2019 10:38 am

LoadDocument returns nil and not a MohoDoc

Post by mminsel »

Hi,
I have the following issue:
When I run the following code:

Code: Select all

print(moho.document:Path())
local newDocument = moho:LoadDocument("C:/path/to/file.moho")
print(moho.document:Path())
print(tostring(newDocument))
I'll get the following printouts:

Code: Select all

""
""
nil
Moho opened the document, but the moho object is now outdoated and in addition LoadDocument is not returning anything.
Actually I wanted to open a document, do stuff, save and close it again. But this doesn't seem to be possible.

Does anyone have had this issue before? Do you have an idea on how to work around this?
User avatar
hayasidist
Posts: 3526
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: LoadDocument returns nil and not a MohoDoc

Post by hayasidist »

if you have a new (unnamed) document as the active tab when you run the script, you'll get the empty string back.

also load returns a document object, so you'll need print(newDocument:Path()) not print(tostring(newDocument))
mminsel
Posts: 9
Joined: Fri Oct 25, 2019 10:38 am

Re: LoadDocument returns nil and not a MohoDoc

Post by mminsel »

hayasidist wrote: Fri Jun 16, 2023 10:15 am if you have a new (unnamed) document as the active tab when you run the script, you'll get the empty string back.

also load returns a document object, so you'll need print(newDocument:Path()) not print(tostring(newDocument))
Hi hayasidist,
Thanks for your reply. The thing is, that LoadDocument returns nil and not a new document.
Which is why print(newDocument:Path()) will raise an error. The first empty string in my example code was expected, while the second empty string looks wrong to me.
User avatar
hayasidist
Posts: 3526
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: LoadDocument returns nil and not a MohoDoc

Post by hayasidist »

do you actually have a file to load at the path you gave???

take a look at (e.g.) C:\Program Files\Moho\Resources\Support\Scripts\Tool\lm_scatterbrush.lua -- with focus on function LM_ScatterBrush:LoadDocument(moho, docPath) and the places from which it's called -- the script builds a string for the path and loads existing files with no problems.
User avatar
SimplSam
Posts: 1048
Joined: Thu Mar 13, 2014 5:09 pm
Location: London, UK
Contact:

Re: LoadDocument returns nil and not a MohoDoc

Post by SimplSam »

As already stated the only place I have seen moho:LoadDocument being used is in the Scatter Brush tool (lm_scatterbrush.lua).

I have tried the function in a bit of code, and it seems to work/load OK.

Code: Select all

local doc = moho:LoadDocument("W:/SqrCir.moho")
print(tostring(doc:Name()))
result: SqrCir
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
Post Reply