-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMinimalistic importer.lua
224 lines (213 loc) · 9.79 KB
/
Minimalistic importer.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
-- this add-on function is a minimalistic scene content importer, meant as an example.
sim = require 'sim'
function sysCall_info()
return {autoStart = false, menu = 'Importers\nMinimalistic importer...'}
end
function sysCall_init()
simUI = require 'simUI'
if simUI.msgbox_result.yes == sim.msgBox(
simUI.msgbox_type.info, simUI.msgbox_buttons.yesno, "Minimalistic Importer",
"This add-on is a minimalistic importer, meant as an example. Content in folder 'exportedContent' will be imported. Do you want to proceed?"
) then
local directoryName = "exportedContent"
local fileName = "sceneObjects.txt"
idTag = 0
refTag = 1
parentTag = 2
typeTag = 3
fileTag = 4
colorTag = 5
positionTag = 6
limitsTag = 7
commentTag = 8
visibilityTag = 9
aliasTag = 10
local appPath = sim.getStringParam(sim.stringparam_application_path)
local importDir = appPath .. "/" .. directoryName
local file = io.open(importDir .. "/" .. fileName, "r")
local lines = {}
while true do
local line = file:read()
if line == nil then break end
lines[#lines + 1] = line
end
local l = 1
local newHandlesAndIds = {}
while l <= #lines do
local value = getValue(lines[l], commentTag)
if value == nil then
value = getValue(lines[l], idTag)
if value then
local objId = value
local objAlias = getValue(lines[l], aliasTag)
value = getValue(lines[l], refTag)
local objMatr = getNumberTable(value)
value = getValue(lines[l], parentTag)
local objParent = value
local visible = tonumber(getValue(lines[l], visibilityTag))
value = getValue(lines[l], typeTag)
local objType = value
if objType == 'object' then
objHandle = sim.createDummy(0.01)
sim.setObjectMatrix(objHandle, objMatr)
end
if objType == 'shape' then
l = l + 1
local filename = getValue(lines[l], fileTag)
local form = -1
if string.find(string.lower(filename), ".stl") then
form = 4
end
if string.find(string.lower(filename), ".dxf") then
form = 1
end
if string.find(string.lower(filename), ".obj") then
form = 0
end
local color = getNumberTable(getValue(lines[l], colorTag))
objHandle = sim.importShape(form, importDir .. "/" .. filename, 0, 0, 1)
local m = sim.getObjectMatrix(objHandle)
objMatr = sim.multiplyMatrices(objMatr, m)
sim.setObjectMatrix(objHandle, objMatr)
sim.setShapeColor(objHandle, nil, sim.colorcomponent_ambient_diffuse, color)
sim.setShapeColor(
objHandle, nil, sim.colorcomponent_diffuse,
{color[4], color[5], color[6]}
)
end
if objType == 'multishape' then
l = l + 1
local filename = getValue(lines[l], fileTag)
local subshapes = {}
while filename do
local form = -1
if string.find(string.lower(filename), ".stl") then
form = 4
end
if string.find(string.lower(filename), ".dxf") then
form = 1
end
if string.find(string.lower(filename), ".obj") then
form = 0
end
local color = getNumberTable(getValue(lines[l], colorTag))
objHandle = sim.importShape(form, importDir .. "/" .. filename, 0, 0, 1)
sim.setShapeColor(
objHandle, nil, sim.colorcomponent_ambient_diffuse, color
)
sim.setShapeColor(
objHandle, nil, sim.colorcomponent_specular,
{color[4], color[5], color[6]}
)
subshapes[#subshapes + 1] = objHandle
l = l + 1
if l <= #lines then
filename = getValue(lines[l], fileTag)
else
filename = nil
end
end
objHandle = sim.groupShapes(subshapes)
local m = sim.getObjectMatrix(objHandle)
objMatr = sim.multiplyMatrices(objMatr, m)
sim.setObjectMatrix(objHandle, objMatr)
l = l - 1
end
if objType == 'joint' then
l = l + 1
value = getValue(lines[l], typeTag)
local jointType = value
value = getValue(lines[l], positionTag)
local position = value
value = getValue(lines[l], limitsTag)
local limits = nil
if value ~= "none" and value ~= "cyclic" then
limits = getNumberTable(value)
limits[2] = limits[2] - limits[1]
end
if jointType == "prismatic" then
objHandle = sim.createJoint(
sim.joint_prismatic, sim.jointmode_kinematic, 0
)
sim.setJointInterval(objHandle, false, limits)
sim.setJointPosition(objHandle, tonumber(position))
end
if jointType == "revolute" then
objHandle = sim.createJoint(
sim.joint_revolute, sim.jointmode_kinematic, 0
)
if limits then
sim.setJointInterval(objHandle, false, limits)
else
sim.setJointInterval(objHandle, true, {math.pi, 2 * math.pi})
end
sim.setJointPosition(objHandle, tonumber(position))
end
if jointType == "spherical" then
objHandle = sim.createJoint(
sim.joint_spherical, sim.jointmode_kinematic, 0
)
sim.setJointInterval(objHandle, false, {math.pi, 2 * math.pi})
sim.setSphericalJointMatrix(objHandle, getNumberTable(position))
end
sim.setObjectMatrix(objHandle, objMatr)
end
if visible == 0 then
sim.setObjectInt32Param(objHandle, 10, 256)
end
sim.setObjectAlias(objHandle, objAlias)
newHandlesAndIds[#newHandlesAndIds + 1] = objHandle
newHandlesAndIds[#newHandlesAndIds + 1] = objId
newHandlesAndIds[#newHandlesAndIds + 1] = objParent
end
end
l = l + 1
end
for i = 1, #newHandlesAndIds / 3, 1 do
local objHandle = newHandlesAndIds[3 * (i - 1) + 1]
local parentName = newHandlesAndIds[3 * (i - 1) + 3]
for j = 1, #newHandlesAndIds / 3, 1 do
local objHandle2 = newHandlesAndIds[3 * (j - 1) + 1]
local objName2 = newHandlesAndIds[3 * (j - 1) + 2]
if objName2 == parentName then
sim.setObjectParent(objHandle, objHandle2, true)
break
end
end
end
local nsel = {}
for i = 1, #newHandlesAndIds / 3, 1 do
nsel[i] = newHandlesAndIds[3 * (i - 1) + 1]
end
sim.setObjectSel(nsel)
sim.announceSceneContentChange()
end
return {cmd = 'cleanup'}
end
getValue = function(str, tg)
local s = nil
if tg == idTag then s = "id" end
if tg == aliasTag then s = "alias" end
if tg == refTag then s = "ref" end
if tg == parentTag then s = "parent" end
if tg == typeTag then s = "type" end
if tg == fileTag then s = "file" end
if tg == colorTag then s = "color" end
if tg == positionTag then s = "position" end
if tg == limitsTag then s = "limits" end
if tg == visibilityTag then s = "visibility" end
if tg == commentTag then
local r = string.match(str, "%/%/.*")
return r
end
if s == nil then return nil end
local r = string.match(str, s .. "[ ]*{(.-)}")
if r == nil then return nil end
r = string.reverse(string.match(r, " *(.*)"))
return string.reverse(string.match(r, " *(.*)"))
end
getNumberTable = function(str)
local retTable = {}
for w in string.gmatch(str, "%S+") do table.insert(retTable, tonumber(w)) end
return retTable
end