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
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

The script I was working with above was the one I modified in an earlier post. I didn't post the whole script. Just the "Run" function. The prefs code is the same I didn't change it. The only thing I did was to change the run script and combine the other code into it.

Here is the whole script:


Code: Select all

--[[ Script name ]]

ScriptName = "Syn_Addons"


--[[ General info ]]

Syn_Addons = {}

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

function Syn_Addons:Version()
  return "1.0"
end

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

function Syn_Addons:Creator()
  return "SynthSin75"
end

function Syn_Addons:UILabel()
  return "Add-ons"
end


--[[ Recurring values ]]

Syn_Addons.tlpath = ""
Syn_Addons.bpath = ""

function Syn_Addons:AssignTLP()
  local path = LM.GUI.OpenFile("Select ...scripts\\tool\\_tool_list.txt")
end


function Syn_Addons:LoadPrefs(prefs)
  self.bpath = prefs:GetString("Syn_Addons.bpath", "")
end

function Syn_Addons:SavePrefs(prefs)
  prefs:SetString("Syn_Addons.bpath", self.bpath)
end


--[[ Main body ]]

function Syn_Addons:IsEnabled(moho)
    return true
end
  
function Syn_Addons:Run(moho) -- function start
    --initial path UI stuff
    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
User avatar
synthsin75
Posts: 10153
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Ohhhh. That's probably the problem. The example you gave me originally didn't have an empty string in the get.string function.

Code: Select all

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

function Syn_Addons:SavePrefs(prefs)
  prefs:SetString("Syn_Addons.bpath", self.bpath)
end
Should have been:

Code: Select all

function Syn_Addons:LoadPrefs(prefs) 
  self.bpath = prefs:GetString("Syn_Addons.bpath", "") 
end 

function Syn_Addons:SavePrefs(prefs) 
  prefs:SetString("Syn_Addons.bpath", self.bpath) 
end
I hadn't been able to find any scripts that used the get/set string pref, so I had no working example. Sorry to have been a pain, and thanks for clearing that up for me. :wink:


p.s. I'm sure that original goof was from my code not yours. :oops:
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Don't worry about mistakes or silly questions. I am learning this stuff as much as you. If I don't have the answer I go find it and learn something new as well.

-vern
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

I've modified your script to a cleared version and more bug checker. I have no time now to explain the changes. Please examine the script and ask if you don't understand.

You can restore to the original just changing "Syn_Addons2" to "Syn_Addons"

Code: Select all

--[[ Script name ]]

ScriptName = "Syn_Addons2"


--[[ General info ]]

Syn_Addons2 = {}

function Syn_Addons2:Name()
  return "Add-ons2"
end

function Syn_Addons2:Version()
  return "2.0"
end

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

function Syn_Addons2:Creator()
  return "SynthSin75"
end

function Syn_Addons2:UILabel()
  return "Add-ons2"
end


--[[ Recurring values ]]

Syn_Addons2.tlpath = ""
Syn_Addons2.bpath = ""

function Syn_Addons2:LoadPrefs(prefs)
  self.bpath = prefs:GetString("Syn_Addons.bpath", "")
end

function Syn_Addons2:SavePrefs(prefs)
  prefs:SetString("Syn_Addons.bpath", self.bpath)
end


--[[ Main body ]]

function Syn_Addons2:IsEnabled(moho)
    return true
end
 
function Syn_Addons2:Run(moho) -- function start
    --initial path UI stuff
    local path = ""
    local pathok = false
    local check1
    local check2 
    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)
    else
        check1 = LM.GUI.Alert(LM.GUI.ALERT_QUESTION, "Syn_Addons has a current paths to your tool list, Do you want to change them?: ",self.bpath, self.tlpath, "YES", "NO", nil)
    end
    if (check1 == 0)then -- user want to change path or the path is not valid
         repeat ----------------main loop
            path = LM.GUI.OpenFile("Select ...scripts\\tool\\_tool_list.txt")
            if (string.sub(path, -14) == "_tool_list.txt") then
                 self.tlpath = path
                 pathok = true
            end
            if (pathok == false) then 
               check2 = LM.GUI.Alert(LM.GUI.ALERT_INFO, "Wrong file.", "Please select again.", nil, "OK", "Cancel", nil)
            end
            if (check2 == 1) then 
                LM.GUI.Alert(LM.GUI.ALERT_WARNING, "Path to tool list could be wrong!", nil, nil, "OK", nil, nil)
                break
            end 
         until (pathok == true) ---main loop end
    end
    if (pathok == true) then
         local path = string.sub(self.tlpath, 1, - 28)
         if (path ~= "") then
             self.bpath = path
         end
    end

    if (self.tlpath ~= nil and self.bpath ~= nil) then 
       LM.GUI.Alert(LM.GUI.ALERT_INFO, "Syn_Addons has those paths", self.tlpath, self.bpath, "OK", nil, nil)
    end

end -- function end 
User avatar
synthsin75
Posts: 10153
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Thanks Genete. Some good ideas in there, but not really implemented how I want. I want this bit to never run again once the preference is set. I plan on providing an option later to reset the variable to nil.

Thanks for showing me an example of a 'repeat-until' statement. I hadn't tried using that yet. Also the boolean check will help. :wink:
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

Ok, I wrote that just because I don't like use so many times the fi.. then.. elseif...end conditional. It can produce some strange responses from the user and finally a bug.

You're welcome. I keep reading this thread. Welcome to the programming world :)
User avatar
synthsin75
Posts: 10153
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Thank you Genete! Your code really helped. That last dialog to check the path variables is just what I needed to understand that it was actually saving the preferences. For some reason my 'print' tests were confusing me about whether or not the pref was loading.

This first bit is now a done deal! :shock: Hopefully no other issues arrise out of it. So now I've just started playing with writing a backup default tool list. So far so good.

Code: Select all

--[[ Script name ]]

ScriptName = "Syn_Addons"

--[[ General info ]]

Syn_Addons = {}

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

function Syn_Addons:Version()
  return "1.0"
end

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

function Syn_Addons:Creator()
  return "SynthSin75"
end

function Syn_Addons:UILabel()
  return "Add-ons"
end


--[[ Recurring values ]]

Syn_Addons.bpath = ""
Syn_Addons.tlpath = ""

function Syn_Addons:LoadPrefs(prefs)
  self.bpath = prefs:GetString("Syn_Addons.bpath", "")
  self.tlpath = prefs:GetString("Syn_Addons.tlpath", "")
  end
  
function Syn_Addons:SavePrefs(prefs)
  self.bpath = prefs:SetString("Syn_Addons.bpath", self.bpath)
  self.tlpath = prefs:GetString("Syn_Addons.tlpath", self.tlpath)
  end

--[[ Main body ]]

function Syn_Addons:IsEnabled(moho)
  return true
end
function Syn_Addons:Run(moho)
  local path = ""
  local pathok = false
  local check1
  local check2
----------------------reset for testing--------------------
--[[test = LM.GUI.Alert(LM.GUI.ALERT_QUESTION, "Reset the test variables?", nil, nil, "OK", "NO", nil)
  if (test == 0) then
  Syn_Addons.bpath = ""
  Syn_Addons.tlpath = ""
    else
      return
  end]]
-----------------------set paths------------------------
if (self.bpath == "") or (self.bpath == nil) then
  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", nil, nil)
end
  if (check1 == 0) then
    repeat
      path = LM.GUI.OpenFile("Select '...scripts\\tool\\_tool_list.txt'")
    if (path == "") then
      return
    end
      if (string.sub(path, -14) == "_tool_list.txt") then
        if (path ~= "") then
        self.tlpath = path
        pathok = true
        end
      end
      if (pathok == false)then
        check2 = LM.GUI.Alert(LM.GUI.ALERT_INFO, "Wrong file.", "Please select again.", nil, "OK", "Cancel", nil)
        if (check2 == 1) then
          break
        end
      end
    until (pathok == true)
  end
  if (pathok == true) then
    local path = string.sub(self.tlpath, 1, - 28)    
    if (path ~= "") then
      self.bpath = path
    end
  end
--[[if (self.tlpath ~= nil and self.bpath ~= nil) then 
       LM.GUI.Alert(LM.GUI.ALERT_INFO, "Syn_Addons has those paths", self.tlpath, self.bpath, "OK", nil, nil)
end]]
--------------write backup default tool list----------------
if (self.bpath ~= nil) and (self.bpath ~= "") then
file = io.open(self.bpath .. "scripts\\tool\\_tool_list~.txt", "w")
file:write("group Draw\ncolor	204 220 232 255\ntool	lm_select_points	G\ntool	lm_translate_points	T\ntool	lm_scale_points		S\ntool	lm_rotate_points	R\ntool	lm_add_point		A\ntool	lm_delete_edge		D\ntool	lm_curvature		C\ntool	lm_magnet			X\ntool	lm_freehand			F\ntool	lm_rectangle		E\ntool	lm_oval				L\ntool	lm_arrow			...\ntool	lm_shear_points_x	...\ntool	lm_shear_points_y	...\ntool	lm_perspective_points_h	...\ntool	lm_perspective_points_v	...\ntool	lm_bend_points_h	...\ntool	lm_bend_points_v	...\ntool	lm_noise			N\nspacer\nbutton	lm_smooth			M\nbutton	lm_peak				P\nbutton lm_flip_points_h	...\nbutton	lm_flip_points_v	...\ngroup Fill\ncolor	232 220 204 255\ntool	lm_select_shape		Q\ntool	lm_create_shape		U\ntool	lm_paint_bucket		...\ntool	lm_delete_shape		...\ntool	lm_line_width		W\ntool	lm_hide_edge		H\nbutton	lm_lower_shape		...\nbutton	lm_raise_shape		...\ngroup Bone\ncolor	204 220 232 255\ntool	lm_select_bone		B\ntool	lm_translate_bone	T\ntool	lm_scale_bone		S\ntool	lm_rotate_bone		R\ntool	lm_add_bone			A\ntool	lm_reparent_bone	P\ntool	lm_bone_strength	...\ntool	lm_manipulate_bones	Z\ntool	lm_bind_layer		...\ntool	lm_bind_points		I\ntool	lm_offset_bone		...\ngroup Layer\ncolor	232 220 204 255\ntool	lm_translate_layer	1\ntool	lm_scale_layer		2\ntool	lm_rotate_layer		3\ntool	lm_set_origin		0\ntool	lm_rotate_layer_x	...\ntool	lm_rotate_layer_y	...\ntool	lm_shear_layer_x	...\ntool	lm_shear_layer_y	...\nbutton	lm_flip_layer_h		...\nbutton	lm_flip_layer_v		...\ntool	lm_switch_layer		...\ntool	lm_particle_layer	...\ngroup Camera\ncolor	233 201 201 255\ntool	lm_track_camera		4\ntool	lm_zoom_camera		5\ntool	lm_roll_camera		6\ntool	lm_pantilt_camera	7\ngroup Workspace\ncolor	212 238 212 255\ntool	lm_pan_workspace	...\ntool	lm_zoom_workspace	...\ntool	lm_rotate_workspace	8\ntool	lm_orbit_workspace      9")
file:close()
end
end
Although I may just end up having the user drop this backup default tool list in with this script and any png I may use. Otherwise this script will get bloated fast, as you can see. I'm not sure if that's a big problem though, unless it slows the script down terribly.

Please feel free to test this and give me any feedback you might have.

Thanks. :wink:
User avatar
synthsin75
Posts: 10153
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Damn, I wish even just LUA was fully documented, much less AS LUA. Hunting around I found a couple of LUA OS commands that were in none of the references I had seen. (Granted OS commands may be system dependant.)

Code: Select all

--os.execute("xcopy C:\\test\\one.txt C:\\test2\\one2.txt/e/s")
folder = "C:\\testingness\\testy"
  os.execute("mkdir " .. folder)
Here's a test I did with them. I had been wondering if there was a way to create folders and actually copy files rather than just move them with os.rename. Both of these tests work on Windows, so at least on that OS I'll be able to implement my idea fully.

Please let me know if anyone can test these on other OS. If so, let me know and I'll post the script with this test. I know you guys (Vern & Genete) use Mac and Linux.

Thanks. :wink:
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Most likely anything OS specific will need an "if then" test for the platform. I don't know specifically offhand how to do this but I'm sure it can be done.

Once you determine the OS it is a simple matter of using different separators for the paths. I am pretty sure "mkdir" is the same on any platform. I stayed away from that stuff due to fear of doing something "bad" by accident. ;)

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

Post by synthsin75 »

Okay, now I'm having trouble building my tables.

Code: Select all

local tl = {n}
n = {type, name, key}
n = 0

for line in io.lines(self.tlpath) do
  n = n + 1
  if (line == "spacer") then
    n.type = 'spacer'
    if (line ~= "") and (line ~= 'spacer') then
      __,__,n.type = string.find(line, "(.-)%s.+")
      __,__,n.name = string.find(line, ".+%s-(.-).+")
      __,__,n.key = string.find(line, ".+%s+.+%s+(.+)")
    end
  end
end
  for i, line in ipairs(tl) do
  print(line)
  --table.foreach(tl, print)
end
It may not be clear by this example, but what I want is a table 'tl = {}' that each of its indices is a table of indices 'type, name, & key'. So: tl = {{[type][name][key]}} :?:

I keep ending up with the error: 'attempt to index local 'i' (a number value)'.

I'm sure I'm missing something here. :? :roll:

Do I need to do my value assignments within the table construction? Seems I should be able to initialize all these indices before assigning their values.
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Look at your variables. You have used "n" twice. The second "n = 0" is overwriting the first declaration of n. Try it with a different variable name.

-vern
Post Reply