Lua basics

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

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Latest issues with pref saving.

Okay now I can't seem to get the pref to load, or perhaps save. I don't know how to test which. Please take a look when you get a chance, and see if you can tell my problem.

(I'm not bothering to rename any of this testing. I figure, until I have something useful, you can just keep overwriting any of this you might already have.)

The script only asks for the path until you have choosen it. It should remember this in the pref and not ask for it in any following session.


:roll: Quite a bit of code to only be run once. :wink:
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Well I'm still having no luck with getting the script preference to load. :(

If all else fails, I guess I can always fall back on writing the info somewhere. Like I said, the end of the tool list is free for the writing. I'd rather avoid it, but I can't figure out what I'm doing wrong with the tool pref.

:roll:

:: I've been assuming that I could assign the pref through a variable, is this the problem?
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I haven't had a chance to go through the code but... how do you know it isn't saving and loading the prefs? when you open AS the prefs are loaded. Nothing happens unless you click on the button right? If you click on the button it is going through the process again regardless if the info already exists in the prefs.

I will try to look it over tomorrow... after jury duty.... <sigh> Jury duty. I hope they pick me for a good case. ;)

-vern
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I noticed too that the IsEnabled function just says "return"... I think it should be "return true". Also you have it set to only work on vector layers. was this intentional? You could just leave that bit of code out where it looks for a mesh. What happens is it won't work on any type of layer but a vector layer. That had me confused at first.

Also for some reason these scripts run twice on the PC. It's not just you it happens with my scripts too. I don't know why. On the mac they only run once but all my button scripts run twice after hitting the OK button only on Windows. Very strange.

-vern
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

I haven't been using it as a button so far, but I've seen a one of Genete's scripts run twice as a button, so I'll look into that.

I don't think that the enable bit will give me trouble. That and the mesh check I just took from whatever script I used as a template. I just commented out that vector layer necessity.

Let me know when or if you find where I've goofed the prefs.
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I made a few changes in the modified version. Basically I eliminated the extra variables.

http://www.lowrestv.com/anime_studio/sc ... hv-mod.lua

You have variables "tlpath" and "bpath" that are assigned to two other "global" variables. this is unnecessary. I initialized the variable:

Code: Select all

Syn_Addons.tlpath = ""
Syn_Addons.bpath = ""
then assigned the path from the dialog directly to:

Code: Select all

self.tlpath
I added a print statement to the load prefs function so you can see that the prefs ARE being saved and loaded:

Code: Select all

function Syn_Addons:LoadPrefs(prefs)
  self.bpath = prefs:GetString("Syn_Addons.bpath", self.bpath)
  print(self.bpath)
end
When you launch AS after a session where you chose the path this path is printed in the lua console. this indicates the path is being saved. I think what is happening is that the check for the variable isn't quite right somewhere. I think the variable is getting overwritten. I will have to look at it some more.

One more thing... the string.sub to find the sub path does not work on Mac. Not sure why. This means the sub path is not saved at all on the Mac. I will have to look into it when I have a chance.

-vern
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Thanks Vern. I'll look at this tonight and see what else I can figure out.

Yeah I've been getting afraid that the variable assignments from the dialogs is too complex. I probably need to see if I can simplify that some.

And I haven't been sure how to properly initialize the variables. Thanks.
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

I noticed that the button scripts that were running twice had dialogs, and none of the original buttons do. So I made lm_star a button, and it runs twice as well. So it seems even LM had trouble with that. For some reason, clicking the button on a dialog also 're-clicks' the scripts button in the tool palette.



I've rewritten everything from scratch to get rid of any extraneous code. Hopefully it'll still work. I'll let you know. :wink:
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

synthsin75 wrote:I noticed that the button scripts that were running twice had dialogs, and none of the original buttons do. So I made lm_star a button, and it runs twice as well. So it seems even LM had trouble with that. For some reason, clicking the button on a dialog also 're-clicks' the scripts button in the tool palette.

I've rewritten everything from scratch to get rid of any extraneous code. Hopefully it'll still work. I'll let you know. :wink:
It's strange that it runs twice on Windows and not on Mac. I can always create some simple function for running "just once" like I did with some of my layer scripts. It is probably a very similar problem.

-vern
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Okay, I've tested the pref every way I could think of, and the only thing that works it an explicit (literal) string directly in the set.string pref. It doesn't seem that it will accept a variable that has the string assigned to it, only the string itself.

This means that the only way I'll be able to utilize the pref for this, is if I want to try something crazy like writing the path back into the same script. :shock:

So I might as well just write it elsewhere and read it in. That sucks that the pref can't set a string from a variable. :roll:
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I have to disagree with you on this. When I did that "print" variable thing after reading the prefs it loaded the path that was saved using the variable. It has to be something else.

A variable with a string is EXACTLY THE SAME as the string itself.

I wish I had more time to spend on poking through the code and experimenting but I'm in the middle of something else right now and hate to break my enthusiasm for it or lose track.

-vern
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Code: Select all

--[[ Script name ]]

ScriptName = "Syn_pref_test"


--[[ General info ]]

Syn_pref_test = {}

function Syn_pref_test:Name()
  return "Add-ons"
end

function Syn_pref_test:Version()
  return "1.0"
end

function Syn_pref_test:Description()
  return "Add-on management"
end

function Syn_pref_test:Creator()
  return "SynthSin75"
end

function Syn_pref_test:UILabel()
  return "pref test"
end


--[[ Recurring values ]]

Syn_pref_test.tlpath = ""
Syn_pref_test.bpath = ""

function Syn_pref_test:LoadPrefs(prefs)
  self.bpath = prefs:GetString("Syn_pref_test.bpath", self.bpath)
  self.tlpath = prefs:GetString("Syn_pref_test.tlpath", self.tlpath)
  print(Syn_pref_test.bpath)
  end

function Syn_pref_test:SavePrefs(prefs)
  prefs:SetString("Syn_pref_test.bpath", Syn_pref_test.bpath)
  prefs:SetString("Syn_pref_test.tlpath", Syn_pref_test.tlpath)
  end

--[[ Main body ]]

function Syn_pref_test:IsEnabled(moho)
  return true
  end
  
function Syn_pref_test:Run(moho)
  end

Syn_pref_test.bpath = 'base path'

--print(Syn_pref_test.bpath .. 'test')
I've tried to simplifiy this bit just to test what's going wrong. Just trying to get the pref to work. The only thing I can figure is that that variable being initialized is somehow happenning after any pref load so that all I get is the empty string (""). :(
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I've got your original script sort of working. It is reading the prefs but it's not perfect yet.

EDIT: Through away that code above. I don't think it works the way you think it should. ;)

The problem is too many nested ifs that were not closed properly. You MUST keep the indenting proper. If I don't do that all the time in some of my scripts... ignore me. Do what I say not what I do. ;)

Indenting properly helps you see if the structure of the script is correct. The reason the variables weren't coming up correctly was that some parts of the script were in the wrong spot.

An if/then statement must be indented properly to read it. It doesn't change how the script works it's just for a visual reference. I choose NOT to indent "else" and "elseif". It's just easier for me to read the code. Most lua references say to indent the else and else if.

You may have an overly complicated if/then set up here that could be simplified. It is so easy to get lost in the code I need to study it to see how to make it simpler. One of the lines may extend longer than this forum window can display so it might run to another line.

This is a modified version of your "Run" function. there is nothing else after this. I removed all the other code below the run function. With this code the saved prefs are loaded but the path cuts of at the Anime Studio folder instead of the scripts folder... is that correct? Anyway, remember to indent properly. It will make life so much easier.

I've put in comments to match up the start and end of each if and while statement. You should get a program like jEdit that will automatically indent or at least make it easier to indent. Plus you can set it up to "collapse" the lines based on the indent allowing you to "hide" huge chunks of code and also check for proper structure.

Code: Select all

function Syn_Addons:Run(moho) -- function start
    if (self.bpath == nil or self.bpath == "") then -- first if start
        check1 = LM.GUI.Alert(LM.GUI.ALERT_INFO, "Syn_Addons needs to know where your tool list is located.", "Please navigate to '...Anime Studio\\scripts\\tool\\_tool_list.txt'", nil, "OK", "Cancel", nil)
        if (check1 == 1)then -- second if start
            return
        else -- part of second if needs no end
            local path = LM.GUI.OpenFile("Select ...scripts\\tool\\_tool_list.txt")
            if (path == "")then -- third if start
                return
            elseif (string.sub(path, -14) == "_tool_list.txt") then
                    self.tlpath = path
            elseif (path ~= "" or nil) then  
                while (string.sub(path, -14) ~= "_tool_list.txt") do -- while start
                    check2 = LM.GUI.Alert(LM.GUI.ALERT_INFO, "Wrong file.", "Please select again.", nil, "OK", "Cancel", nil)
                    if (check2 == 1) then -- fourth if start
                        break
                    elseif (check2 == 0) then
                        local path = LM.GUI.OpenFile("Select ...scripts\\tool\\_tool_list.txt")
                
                        if (path == "") then -- fifth if start
                            return
                
                        elseif (string.sub(path, -14) == "_tool_list.txt") then
                            self.tlpath = path
                            break
                        end -- fifth if end
                    end -- fourth if end
                end -- while end
            end -- third if end
        end -- second if end
    end -- first if end
    if (self.tlpath ~= nil) then
        if (string.sub(self.tlpath, -14) == "_tool_list.txt") then
            local path = string.sub(self.tlpath, 1, - 28)
            if (path ~= "") then
                self.bpath = path
            end
        end
    end

    if (self.tlpath ~= nil) then  
        print(self.tlpath)
    end 
    
    if (self.bpath ~= nil) then  
        print(self.bpath) 
    end

end -- function end

Last edited by heyvern on Fri Aug 08, 2008 9:14 pm, edited 1 time in total.
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

For testing purposes you may want to put in some kind of "reset" button. I don't know exactly how to do it right off the bat. Some way to change the preference saved so you can test the code.

Right now I got it to work as you intended but since the prefs are saved I never get the dialog box when clicking the button. Maybe modify that box so it always comes up and have a "reset" button to set the variable to "empty" or nil.

Haven't thought it through.

-vern
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

This is a modified version of your "Run" function. there is nothing else after this. I removed all the other code below the run function. With this code the saved prefs are loaded but the path cuts of at the Anime Studio folder instead of the scripts folder... is that correct? Anyway, remember to indent properly.
First, I don't see the pref lines in that code you posted. You mention that pref code not working how I think, but you didn't provide any alternative. And yes, the bpath is intended to end at the Anime Studio folder (just in case I ever want to access the strings file).

I have a newer version that I think is indented how you show. I'll have to check, but I did what you did there. Indent ifs but not elses or elseifs. I'll see how closely my already cleaned up code matches your example.


I'm guessing I'm still missing something about the prefs.


I didn't know jEdit could do all that, I'll definitely have to pick that up soon. I've just been using Notepad2. It shows the lines and bracket pairs, but that's about it.

For resetting, I've just been assigning the variable to nil to clear it.
Post Reply