Grimm's take on the turntable anim script

Forums: Grimm's take on the turntable anim script
Forum for OctaneRender Lua scripting examples, discussion and support.

Re: Grimm's take on the turntable anim script

Postby stratified » Sun Feb 02, 2014 5:14 am

stratified Sun Feb 02, 2014 5:14 am
Can you describe what's broken with the saving? On my machine turntableG_defaults is written and loaded just fine.

cheers,
Thomas
User avatar
stratified
OctaneRender Team
OctaneRender Team
 
Posts: 945
Joined: Wed Aug 15, 2012 6:32 am
Location: Auckland, New Zealand

Re: Grimm's take on the turntable anim script

Postby grimm » Sun Feb 02, 2014 5:58 am

grimm Sun Feb 02, 2014 5:58 am
Yes, sorry, that code I gave you is still using the old method. I can set up the code for the new method for you.

Jason
Linux Mint 21.3 x64 | Nvidia GTX 980 4GB (displays) RTX 2070 8GB| Intel I7 5820K 3.8 Ghz | 32Gb Memory | Nvidia Driver 535.171
User avatar
grimm
Licensed Customer
Licensed Customer
 
Posts: 1324
Joined: Wed Jan 27, 2010 8:11 pm
Location: Spokane, Washington, USA

Re: Grimm's take on the turntable anim script

Postby grimm » Sun Feb 02, 2014 6:03 am

grimm Sun Feb 02, 2014 6:03 am
Here you go:

turntableG-V09T.zip
(21.42 KiB) Downloaded 246 times


And here is the error I'm getting:

Code: Select all
...graphics/scripts/turntableG_files/turntableG_helpers.lua:93: attempt to call field 'insert' (a nil value)


Thanks Thomas,

Jason
Linux Mint 21.3 x64 | Nvidia GTX 980 4GB (displays) RTX 2070 8GB| Intel I7 5820K 3.8 Ghz | 32Gb Memory | Nvidia Driver 535.171
User avatar
grimm
Licensed Customer
Licensed Customer
 
Posts: 1324
Joined: Wed Jan 27, 2010 8:11 pm
Location: Spokane, Washington, USA

Re: Grimm's take on the turntable anim script

Postby stratified » Sun Feb 02, 2014 8:01 pm

stratified Sun Feb 02, 2014 8:01 pm
Hi Jason,

The table parameter of saveTable clashes with table.insert. The code is shadowing the table module. The code is equivalent to this:

Code: Select all
table = {} -- bad idea
table.insert(table, 4) -- above table doesn't have an insert function, we shadowed the table module


Did the code have this parameter name originally? If yes I'm baffled how it could ever work.

cheers,
Thomas
User avatar
stratified
OctaneRender Team
OctaneRender Team
 
Posts: 945
Joined: Wed Aug 15, 2012 6:32 am
Location: Auckland, New Zealand

Re: Grimm's take on the turntable anim script

Postby grimm » Sun Feb 02, 2014 9:01 pm

grimm Sun Feb 02, 2014 9:01 pm
Hi Thomas,

Thanks for looking into it. I haven't changed anything with that code, I don't understand it enough to mess with it. :) If you comment out the binds in turntableG_gui.lua and comment out the createPropertytable line in turntableG.lua while also uncommenting the regular table init line below it, you can see that it works fine with a normal table.

I got the code from the lua users wiki site:

http://lua-users.org/wiki/SaveTableToFile

Jason
Linux Mint 21.3 x64 | Nvidia GTX 980 4GB (displays) RTX 2070 8GB| Intel I7 5820K 3.8 Ghz | 32Gb Memory | Nvidia Driver 535.171
User avatar
grimm
Licensed Customer
Licensed Customer
 
Posts: 1324
Joined: Wed Jan 27, 2010 8:11 pm
Location: Spokane, Washington, USA

Re: Grimm's take on the turntable anim script

Postby stratified » Sun Feb 02, 2014 9:23 pm

stratified Sun Feb 02, 2014 9:23 pm
Weird, your function signature is function saveTable(table, filename) and theirs is function table.save( tbl,filename ). They hook up their code directly in the table module. Maybe you fetched it from somewhere else.

Anyway, I'll have a look what changes when using regular tables.

BTW, your packing your .swp files. If your using vim for coding you should use set nobackup and set noswapfile in your .vimrc. Then vim won't generate them at all.

cheers,
Thomas
User avatar
stratified
OctaneRender Team
OctaneRender Team
 
Posts: 945
Joined: Wed Aug 15, 2012 6:32 am
Location: Auckland, New Zealand

Re: Grimm's take on the turntable anim script

Postby grimm » Mon Feb 03, 2014 7:13 am

grimm Mon Feb 03, 2014 7:13 am
Oh, I see now. Sorry Thomas, yes I did change the function names and the table var. Which I probable shouldn't have done, as I don't understand the code. :oops: I did change the functions back to how they were originally, but it didn't help. The header notes do state that the code doesn't support "Does not save Userdata, Metatables, Functions and indices of these", that is probably causing the issue. I should look for a more complete table load and save functions. Thanks,

Jason
Linux Mint 21.3 x64 | Nvidia GTX 980 4GB (displays) RTX 2070 8GB| Intel I7 5820K 3.8 Ghz | 32Gb Memory | Nvidia Driver 535.171
User avatar
grimm
Licensed Customer
Licensed Customer
 
Posts: 1324
Joined: Wed Jan 27, 2010 8:11 pm
Location: Spokane, Washington, USA

Re: Grimm's take on the turntable anim script

Postby stratified » Mon Feb 03, 2014 7:39 am

stratified Mon Feb 03, 2014 7:39 am
grimm wrote:Oh, I see now. Sorry Thomas, yes I did change the function names and the table var. Which I probable shouldn't have done, as I don't understand the code. :oops: I did change the functions back to how they were originally, but it didn't help. The header notes do state that the code doesn't support "Does not save Userdata, Metatables, Functions and indices of these", that is probably causing the issue. I should look for a more complete table load and save functions. Thanks,

Jason


No worries, it happens to all of us. It's part of the job :D

I poked around a bit and merged my bind example with your save code and it works fine in my case:

Code: Select all
--
-- Bind example script
--

-- Create the table that has the coupled value. It won't work with a regular
-- table. We need to do a bit of magic in the API.
local props = octane.gui.createPropertyTable()

--// exportstring( string )
--// returns a "Lua" portable version of the string
function exportstring( s )
   return string.format("%q", s)
end

--Save a table
function saveTable(tbl, filename)
    local charS,charE = "   ","\n"
    local file,err = io.open( filename, "wb" )
    if err then return err end

    -- initiate variables for save procedure
    local tables,lookup = { tbl},{ [tbl] = 1 }
    file:write( "return {"..charE )

    for idx,t in ipairs( tables ) do
       file:write( "-- Table: {"..idx.."}"..charE )
       file:write( "{"..charE )
       local thandled = {}

       for i,v in ipairs( t ) do
          thandled[i] = true
          local stype = type( v )
          -- only handle value
          if stype == "table" then
             if not lookup[v] then
                table.insert( tables, v )
                lookup[v] = #tables
             end
             file:write( charS.."{"..lookup[v].."},"..charE )
          elseif stype == "string" then
             file:write(  charS..exportstring( v )..","..charE )
          elseif stype == "number" then
             file:write(  charS..tostring( v )..","..charE )
          end
       end

       for i,v in pairs( t ) do
          -- escape handled values
          if (not thandled[i]) then
         
             local str = ""
             local stype = type( i )
             -- handle index
             if stype == "table" then
                if not lookup[i] then
                   table.insert( tables,i )
                   lookup[i] = #tables
                end
                str = charS.."[{"..lookup[i].."}]="
             elseif stype == "string" then
                str = charS.."["..exportstring( i ).."]="
             elseif stype == "number" then
                str = charS.."["..tostring( i ).."]="
             end
         
             if str ~= "" then
                stype = type( v )
                -- handle value
                if stype == "table" then
                   if not lookup[v] then
                      table.insert( tables,v )
                      lookup[v] = #tables
                   end
                   file:write( str.."{"..lookup[v].."},"..charE )
                elseif stype == "string" then
                   file:write( str..exportstring( v )..","..charE )
                elseif stype == "number" then
                   file:write( str..tostring( v )..","..charE )
                end
             end
          end
       end
       file:write( "},"..charE )
    end
    file:write( "}" )
    file:close()
end

local redSlider = octane.gui.create
{
    type     = octane.gui.componentType.SLIDER,
    width    = 200,
    height   = 24,
    minValue = 0,
    maxValue = 255,
}
-- bind the value and enable properties of the slider
redSlider:bind("value" , props, "red", function (key, data) print(key, data) end)
redSlider:bind("enable", props, "enableWidth")
props.red = 255

local swatch = octane.gui.create
{
    type     = octane.gui.componentType.COLOUR_SWATCH,
    width    = 100,
    height   = 100,
}
-- bind the colour and enable properties of the swatch
swatch:bind("colour", props, "myColour", function (key, data) props.red = data[1] end)
swatch:bind("enable", props, "allowColourPick")

local button = octane.gui.create
{
    type     = octane.gui.componentType.BUTTON,
    width    = 100,
    height   = 24,
    text     = "Enable/Disable",
    callback =
        -- callback that toggles the enabled state of the ui components
        function()
            props.enableWidth     = not props.enableWidth
            props.allowColourPick = not props.allowColourPick
        end
}

local layoutGrp = octane.gui.create
{
    type     = octane.gui.componentType.GROUP,
    children = { redSlider, swatch, button },
    rows     = 3,
    cols     = 1,
    padding  = { 20 },
    border   = false,
}

saveTable(props, "/tmp/test.txt")

for k,v in ipairs(props) do print(k, v) end

local window = octane.gui.create
{
    type     = octane.gui.componentType.WINDOW,
    width    = layoutGrp:getProperties().width,
    height   = layoutGrp:getProperties().height,
    children = { layoutGrp },
    text     = "Bind Example",
}

window:showWindow()


It saves it out just fine:

Code: Select all
return {
-- Table: {1}
{
   ["__octane.shadow"]={2},
},
-- Table: {2}
{
   ["red"]=255,
   ["myColour"]={3},
},
-- Table: {3}
{
   255,
   0,
   0,
   255,
},
}


If it doesn't work, you might try a hack. The actual values are nested in a nested table called __octane.shadow. You could feed this one to your code. This table doesn't have any special meta methods. You just need to make sure you fetch it via rawget to prevent triggering some meta methods:

Code: Select all
for k,v in ipairs(rawget(props, "__octane.shadow")) do print(k, v) end


I hope this points you in the right direction.

cheers,
Thomas
User avatar
stratified
OctaneRender Team
OctaneRender Team
 
Posts: 945
Joined: Wed Aug 15, 2012 6:32 am
Location: Auckland, New Zealand

Re: Grimm's take on the turntable anim script

Postby grimm » Mon Feb 03, 2014 8:15 am

grimm Mon Feb 03, 2014 8:15 am
Excellent! Thanks Thomas! :D I have the save table code working now. Now the problem child is the load table function. I think the problem is that the function creates a new table and then that replaces the special properties table. I'm not sure how to handle that as I think the binds are lost in the process. I'm thinking that I will have to read in the new table and then move the data, one component at a time, over to the binded properties table?

Jason
Linux Mint 21.3 x64 | Nvidia GTX 980 4GB (displays) RTX 2070 8GB| Intel I7 5820K 3.8 Ghz | 32Gb Memory | Nvidia Driver 535.171
User avatar
grimm
Licensed Customer
Licensed Customer
 
Posts: 1324
Joined: Wed Jan 27, 2010 8:11 pm
Location: Spokane, Washington, USA

Re: Grimm's take on the turntable anim script

Postby stratified » Mon Feb 03, 2014 7:14 pm

stratified Mon Feb 03, 2014 7:14 pm
The bindings should have already happened when you create your user interface components. Indeed, you should only assign each component it's new value from the loaded table. Blindly overwriting the property table wont work.

cheers,
Thomas
User avatar
stratified
OctaneRender Team
OctaneRender Team
 
Posts: 945
Joined: Wed Aug 15, 2012 6:32 am
Location: Auckland, New Zealand
PreviousNext

Return to Lua Scripting


Who is online

Users browsing this forum: No registered users and 3 guests

Wed May 22, 2024 12:54 am [ UTC ]