Re: Grimm's take on the turntable anim script
Posted: 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
cheers,
Thomas
Community Forums for OTOY Customers
https://render.otoy.com/forum/
...graphics/scripts/turntableG_files/turntableG_helpers.lua:93: attempt to call field 'insert' (a nil value)
table
parameter of saveTable
clashes with table.insert
. The code is shadowing the table module. The code is equivalent to this:table = {} -- bad idea
table.insert(table, 4) -- above table doesn't have an insert function, we shadowed the table module
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.set nobackup
and set noswapfile
in your .vimrc. Then vim won't generate them at all.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. 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
--
-- 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()
return {
-- Table: {1}
{
["__octane.shadow"]={2},
},
-- Table: {2}
{
["red"]=255,
["myColour"]={3},
},
-- Table: {3}
{
255,
0,
0,
255,
},
}
__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:for k,v in ipairs(rawget(props, "__octane.shadow")) do print(k, v) end