-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMinimalistic exporter.lua
222 lines (217 loc) · 11.2 KB
/
Minimalistic exporter.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
-- this add-on function is a minimalistic scene content exporter, meant as an example.
sim = require 'sim'
lfs = require 'lfsx'
function sysCall_info()
return {autoStart = false, menu = 'Exporters\nMinimalistic exporter...'}
end
function sysCall_init()
simUI = require 'simUI'
if simUI.msgbox_result.yes == simUI.msgBox(
simUI.msgbox_type.info, simUI.msgbox_buttons.yesno, "Minimalistic Exporter",
"This add-on is a minimalistic exporter, meant as an example. The scene content will be exported to the 'exportedContent' folder, erasing its previous content. If a single object or model is selected, then only the selection will be exported. Do you want to proceed?"
) then
local directoryName = "exportedContent"
local fileName = "sceneObjects.txt"
local meshFormat = 4 -- 0=OBJ, 1=DXF, 4=binary STL
local exportIndividualShapeComponents = true
local appPath = sim.getStringParam(sim.stringparam_application_path)
local exportDir = appPath .. "/" .. directoryName
local extension
if meshFormat == 0 then extension = "obj" end
if meshFormat == 1 then extension = "dxf" end
if meshFormat == 4 then extension = "stl" end
lfs.rmdir_r(exportDir)
lfs.mkdir(exportDir)
local file = io.open(exportDir .. "/" .. fileName, "w")
file:write("// Data format:\n")
file:write("//\n")
file:write("// For each scene object:\n")
file:write(
"// id{<SCENE_OBJECT_ID>} alias{<SCENE_OBJECT_ALIAS>} ref{<ABS_OBJECT_REFERENCE_FRAME_MATRIX>} parent{PARENT_OBJECT_ID} visibility{<0_OR_1>} type{<OBJECT_TYPE>} \n"
)
file:write("//\n")
file:write("//\n")
file:write("// Where:\n")
file:write(
"// <ABS_OBJECT_REFERENCE_FRAME_MATRIX> is: Xx Yx Zx POSx Xy Yy Zy POSy Xz Yz Zz POSz 0 0 0 1\n"
)
file:write("// <PARENT_OBJECT_ID> is: * if the object has no parent\n")
file:write("// <OBJECT_TYPE> is: object, joint, shape or multishape\n")
file:write("//\n")
file:write("//\n")
file:write("// If <OBJECT_TYPE> is shape, then following line describes the shape mesh.\n")
file:write(
"// If <OBJECT_TYPE> is multishape, then following lines describe the shape mesh components.\n"
)
file:write("//\n")
file:write("//\n")
file:write("// A mesh is described with:\n")
file:write("// file{<FILE_NAME>} color{<MESH_COLOR>}\n")
file:write("//\n")
file:write("//\n")
file:write("// Where:\n")
file:write("// <MESH_COLOR> is: ambientR ambientG ambientB specularR specularG specularB\n")
file:write("//\n")
file:write("//\n")
file:write("// If <OBJECT_TYPE> is joint, then following line describes the joint.\n")
file:write("//\n")
file:write("//\n")
file:write("// A joint is described with:\n")
file:write("// type{<JOINT_TYPE>} position{<JOINT_POSITION>} limits{<JOINT_LIMITS>}\n")
file:write("//\n")
file:write("//\n")
file:write("// Where:\n")
file:write("// <JOINT_TYPE> is prismatic, revolute or spherical.\n")
file:write(
"// <JOINT_POSITION> is the linear or angular position, or the intrinsic transformation matrix of the spherical joint\n"
)
file:write(
"// <JOINT_LIMITS> is limitLow limitHigh (for prismatic or revolute joint), cyclic (for revolute joints), none (for spherical joints).\n"
)
file:write("//\n")
file:write("//\n")
local selectedObjects = sim.getObjectSel()
local allObjects = sim.getObjectsInTree(sim.handle_scene)
if #selectedObjects == 1 then
allObjects = sim.getObjectsInTree(selectedObjects[1])
end
local allIndividualShapesToRemove = {}
local visibleLayers = sim.getInt32Param(sim.intparam_visible_layers)
for obji = 1, #allObjects, 1 do
local objType = sim.getObjectType(allObjects[obji])
local objId = sim.getObjectAlias(allObjects[obji], 4)
local objAlias = sim.getObjectAlias(allObjects[obji])
local matr = sim.getObjectMatrix(allObjects[obji])
local parentId = "*"
local parentHandle = sim.getObjectParent(allObjects[obji])
if parentHandle ~= -1 then parentId = sim.getObjectAlias(parentHandle, 4) end
local layers = sim.getObjectInt32Param(allObjects[obji], 10)
file:write("id{" .. objId .. "}")
file:write(" alias{" .. objAlias .. "}")
file:write(" ref{")
for i = 1, 12, 1 do file:write(string.format("%e ", matr[i])) end
file:write(string.format("%e %e %e %e}", 0, 0, 0, 1))
file:write(" parent{" .. parentId .. "}")
file:write(" visibility{" .. sim.boolAnd32(visibleLayers, layers) .. "}")
file:write(" type{")
if objType == sim.sceneobject_shape then
local param = sim.getObjectInt32Param(allObjects[obji], 3016)
if exportIndividualShapeComponents and (param ~= 0) then
file:write("multishape}")
local tobj = sim.copyPasteObjects({allObjects[obji]}, 0)
local individualShapes = sim.ungroupShape(tobj[1])
for j = 1, #individualShapes, 1 do
allIndividualShapesToRemove[#allIndividualShapesToRemove + 1] =
individualShapes[j]
local indivName = sim.getObjectAlias(individualShapes[j], 4)
local indivMatr = sim.getObjectMatrix(individualShapes[j])
local totIndivMatrix = sim.getObjectMatrix(allObjects[obji])
sim.invertMatrix(totIndivMatrix)
totIndivMatrix = sim.multiplyMatrices(totIndivMatrix, indivMatr)
local vertices, indices = sim.getShapeMesh(individualShapes[j])
file:write("\n file{" .. indivName .. "." .. extension .. "}")
local result, col1 = sim.getShapeColor(
individualShapes[j], nil,
sim.colorcomponent_ambient_diffuse
)
local result, col2 = sim.getShapeColor(
individualShapes[j], nil,
sim.colorcomponent_specular
)
file:write(
string.format(
" color{%.2f %.2f %.2f %.2f %.2f %.2f}", col1[1], col1[2], col1[3],
col2[1], col2[2], col2[3]
)
)
if vertices then
for i = 1, #vertices / 3, 1 do
local v = {
vertices[3 * (i - 1) + 1], vertices[3 * (i - 1) + 2],
vertices[3 * (i - 1) + 3],
}
v = sim.multiplyVector(totIndivMatrix, v)
vertices[3 * (i - 1) + 1] = v[1]
vertices[3 * (i - 1) + 2] = v[2]
vertices[3 * (i - 1) + 3] = v[3]
end
sim.exportMesh(
meshFormat, exportDir .. "/" .. indivName .. "." .. extension, 0, 1,
{vertices}, {indices}, nil, {indivName}
)
end
end
else
file:write("shape}")
file:write("\n file{" .. objId .. "." .. extension .. "}")
local result, col1 = sim.getShapeColor(
allObjects[obji], nil,
sim.colorcomponent_ambient_diffuse
)
local result, col2 = sim.getShapeColor(
allObjects[obji], nil, sim.colorcomponent_specular
)
file:write(
string.format(
" color{%.2f %.2f %.2f %.2f %.2f %.2f}", col1[1], col1[2], col1[3],
col2[1], col2[2], col2[3]
)
)
local vertices, indices = sim.getShapeMesh(allObjects[obji])
if vertices then
sim.exportMesh(
meshFormat, exportDir .. "/" .. objId .. "." .. extension, 0, 1,
{vertices}, {indices}, nil, {objId}
)
end
end
else
if objType == sim.sceneobject_joint then
file:write("joint}")
local t = sim.getJointType(allObjects[obji])
local cyclic, interval = sim.getJointInterval(allObjects[obji])
if t == sim.joint_prismatic then
local pos = sim.getJointPosition(allObjects[obji])
file:write(
string.format(
"\n type{prismatic} position{%e} limits{%e %e}", pos,
interval[1], interval[1] + interval[2]
)
)
end
if t == sim.joint_revolute then
local pos = sim.getJointPosition(allObjects[obji])
if cyclic then
file:write(
string.format(
"\n type{revolute} position{%e} limits{cyclic}", pos
)
)
else
file:write(
string.format(
"\n type{revolute} position{%e} limits{%e %e}", pos,
interval[1], interval[1] + interval[2]
)
)
end
end
if t == sim.joint_spherical then
local jmatr = sim.getJointMatrix(allObjects[obji])
file:write("\n type{spherical} position{")
for i = 1, 12, 1 do
file:write(string.format("%e ", jmatr[i]))
end
file:write(string.format("%e %e %e %e} limits{none}", 0, 0, 0, 1))
end
else
file:write("object}")
end
end
file:write("\n")
end
file:close()
sim.removeObjects(allIndividualShapesToRemove)
end
return {cmd = 'cleanup'}
end