User config callback functions

A customization script that includes a user config callback function (which is one of many system callback functions), will display a configuration dialog icon, in the scene hierarchy:

[Configuration dialog icon]


When double-cliked, the user config callback function is triggered. This can be used as a convenient way of displaying a custom user interface to the user, that is specific to the object/model the customization script is attached to. User data can be read and written to objects with sim.readCustomDataBlock/sim.writeCustomDataBlock for instance:

[Custom configuration dialog]


#python def sysCall_init(): sim = require('sim') simUI = require('simUI') self.modelHandle = sim.getObject('.') def sysCall_userConfig(): xml = '''<ui title="Robot" closeable="true" modal="true" layout="form" on-close="customUiClosed"> <label text="Max. velocity:" /> <edit id="1" value="-" on-editing-finished="velocityChanged"/> <label text="Max. acceleration:" /> <edit id="2" value="-" on-editing-finished="accelerationChanged"/> </ui>''' ui = simUI.create(xml) data = readData() simUI.setEditValue(ui, 1, str(data['maxVel'])) simUI.setEditValue(ui, 2, str(data['maxAccel'])) def customUiClosed(ui): simUI.destroy(ui) def velocityChanged(ui, id, val): data = readData() val = float(val) if val: if val < 0.1: val = 0.1 if val > 0.5: val = 0.5 data['maxVel'] = val simUI.setEditValue(ui, id, str(data['maxVel'])) writeData(data) def accelerationChanged(ui, id, val): data = readData() val = float(val) if val: if val < 0.01: val = 0.01 if val > 0.2: val = 0.2 data['maxAccel'] = val simUI.setEditValue(ui, id, str(data['maxAccel'])) writeData(data) def readData(): data = sim.readCustomDataBlock(self.modelHandle, 'RobotParams') if data: data = sim.unpackTable(data) else: data = {} data['maxVel'] = 0.2 data['maxAccel'] = 0.05 return data def writeData(data): sim.writeCustomDataBlock(self.modelHandle, 'RobotParams', sim.packTable(data)) --lua function sysCall_init() sim = require('sim') simUI = require('simUI') modelHandle = sim.getObject('.') end function sysCall_userConfig() local xml = [[<ui title="Robot" closeable="true" modal="true" layout="form" on-close="customUiClosed"> <label text="Max. velocity:" /> <edit id="1" value="-" on-editing-finished="velocityChanged"/> <label text="Max. acceleration:" /> <edit id="2" value="-" on-editing-finished="accelerationChanged"/> </ui>]] local ui = simUI.create(xml) local data = readData() simUI.setEditValue(ui, 1, tostring(data.maxVel)) simUI.setEditValue(ui, 2, tostring(data.maxAccel)) end function customUiClosed(ui) simUI.destroy(ui) end function velocityChanged(ui, id, val) local data = readData() val = tonumber(val) if val then if val < 0.1 then val = 0.1 end if val > 0.5 then val = 0.5 end data.maxVel = val end simUI.setEditValue(ui, id, tostring(data.maxVel)) writeData(data) end function accelerationChanged(ui, id, val) local data = readData() val = tonumber(val) if val then if val < 0.01 then val = 0.01 end if val > 0.2 then val = 0.2 end data.maxAccel = val end simUI.setEditValue(ui, id, tostring(data.maxAccel)) writeData(data) end function readData() local data = sim.readCustomDataBlock(modelHandle, 'RobotParams') if data then data = sim.unpackTable(data) else data = {} data.maxVel = 0.2 data.maxAccel = 0.05 end return data end function writeData(data) sim.writeCustomDataBlock(modelHandle, 'RobotParams', sim.packTable(data)) end

You may also use the configUI module for quicker set-up of custom configuration dialog, that will also automatically write data to custom data blocks:

[Custom configuration dialog based on configUI module]


--lua require('configUi') function sysCall_init() local schema = { floatInput = { default = 0.1, maximum = 4, minimum = 0.05, name = "float input", type = "float", ui = { control = "spinbox", tab = "Tab 1", order = 1, col = 1 }, }, intInput = { default = 1, maximum = 10, minimum = 0, name = "int input", type = "int", ui = { control = "spinbox", tab = "Tab 1", order = 2, col = 2 }, }, checkbox1 = { type = 'bool', name = 'checkbox1', default = true, ui = {tab = "Tab 1", order = 3, col = 1}, }, checkbox2 = { type = 'bool', name = 'checkbox2', default = false, ui = {tab = "Tab 1", order = 4, col = 2}, }, colorselect = { type = 'color', name = 'color select', default = {1, 0, 1}, ui = {tab = "Tab 2", order = 5, col = 1}, }, stringInput = { default = "Hello", name = "string input", type = "string", ui = {tab = "Tab 2", order = 6, col = 2}, }, floatSlider = { default = 0.5, maximum = 1, minimum = 0, name = "float slider", type = "float", ui = { control = "slider", tab = "Tab 2", order = 7, col = 1 }, }, intSlider = { default = 1, maximum = 10, minimum = 0, name = "int slider", type = "int", ui = { control = "slider", tab = "Tab 2", order = 8, col = 2 }, }, choices1 = { default = 2, choices = {"option 1", "option 2", "option 3"}, name = "combo choices", type = "choices", ui = { control = "combo", tab = "Tab 3", order = 9, col = 1 }, }, choices2 = { default = 1, choices = {"option 1", "option 2", "option 3"}, name = "radio choices", type = "choices", ui = { control = "radio", tab = "Tab 3", order = 10, col = 1 }, }, } sim.writeCustomDataBlock(sim.getObject('.'), '__schema__', sim.packTable(schema)) configUi = ConfigUI('myModelType', nil, modificationCallback) end function modificationCallback(config) local objectHandle = sim.getObject('.') local txt = "Object '"..sim.getObjectAlias(objectHandle,5).."' just changed.\nNew parameters are:" print(txt) print(config) end