-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDefine joint group.lua
74 lines (61 loc) · 2.66 KB
/
Define joint group.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
sim = require 'sim'
function sysCall_info()
return {autoStart = false, menu = 'Kinematics\nDefine joint group...'}
end
function sysCall_init()
simUI = require 'simUI'
if sim.getSimulationState() ~= sim.simulation_stopped then return {cmd = 'cleanup'} end
-- get selection, expand selected models:
local sel = {}
for _, handle in ipairs(sim.getObjectSel()) do
table.insert(sel, handle)
if sim.getModelProperty(handle) & sim.modelproperty_not_model == 0 then
for _, handle1 in ipairs(sim.getObjectsInTree(handle, sim.handle_all, 1)) do
table.insert(sel, handle1)
end
end
end
-- filter by joints:
local joints = {}
for _, handle in ipairs(sel) do
if sim.getObjectType(handle) == sim.sceneobject_joint then
if sim.getJointType(handle) ~= sim.joint_spherical then
table.insert(joints, handle)
end
end
end
-- at least one joint must be selected:
if #joints < 1 then
simUI.msgBox(
simUI.msgbox_type.critical, simUI.msgbox_buttons.ok, 'No joint selected',
'Error: no joint selected\n\nTo use this add-on, select some joints then execute the add-on.'
)
return {cmd = 'cleanup'}
end
local name = ''
while name == '' do
name = simUI.inputDialog(
'JointGroup',
'Enter a name for the joint group which will contain the following joints:\n\n' ..
table.join(map(function(h) return ' - ' .. sim.getObjectAlias(h, 7) end, table.slice(joints, 1, 10)), '\n') ..
(#joints > 10 and string.format('\n (and %d more)', #joints - 10) or '') .. '\n'
)
end
if name == nil then return {cmd = 'cleanup'} end
local modelHandle = sim.getObject(':', {proxy = joints[#joints]})
local scriptText = ''
local function appendLine(...)
scriptText = scriptText .. string.format(...) .. '\n'
end
appendLine("require 'models.jointGroup_customization'")
local jointGroupScript = sim.createScript(sim.scripttype_customization, scriptText)
sim.setReferencedHandles(jointGroupScript, joints)
sim.setObjectAlias(jointGroupScript, name)
sim.setObjectParent(jointGroupScript, modelHandle, false)
sim.setObjectPose(jointGroupScript, {0, 0, 0, 0, 0, 0, 1}, modelHandle)
sim.setObjectInt32Param(jointGroupScript, sim.objintparam_visibility_layer, 0)
sim.setObjectInt32Param(jointGroupScript, sim.objintparam_manipulation_permissions, 0)
sim.writeCustomBufferData(jointGroupScript, '__jointGroup__', sim.packInt32Table {1})
sim.announceSceneContentChange()
return {cmd = 'cleanup'}
end