-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathScreenshot tool.lua
372 lines (342 loc) · 12.8 KB
/
Screenshot tool.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
sim = require 'sim'
function sysCall_info()
return {autoStart = false, menu = 'Tools\nScreenshot tool'}
end
function sysCall_init()
simUI = require 'simUI'
sim.addLog(
sim.verbosity_scriptinfos,
"Select the camera or vision sensor you wish to generate a high resolution screenshot for. This model is courtesy of Ulrich Schwesinger."
)
config = {}
config.res = {4096, 2304}
config.viewAngle = 60 * math.pi / 180
config.viewSize = 4
config.perspective = true
config.transparent = false
config.fileDlg = true
config.renderMode = 'opengl'
config.povray = {}
config.povray.available = false
config.povray.focalBlur = false
config.povray.focalDistance = 2
config.povray.aperture = 0.05
config.povray.blurSamples = 10
config.nearClipping = 0.01
config.farClipping = 250
pcall(
function()
simPovRay = require 'simPovRay'
config.povray.available = true
end
)
end
function sysCall_addOnScriptSuspend()
return {cmd = 'cleanup'}
end
function sysCall_cleanup()
hideDlg()
end
function sysCall_beforeInstanceSwitch()
hideDlg()
end
function sysCall_nonSimulation()
if leaveNow then return {cmd = 'cleanup'} end
end
function sysCall_sensing()
if leaveNow then return {cmd = 'cleanup'} end
end
function sysCall_selChange(inData)
local s = inData.sel
if #s == 1 and (sim.getObjectType(s[1]) == sim.sceneobject_camera or sim.getObjectType(s[1]) ==
sim.sceneobject_visionsensor) then
cam = s[1]
showDlg()
else
cam = nil
hideDlg()
end
end
function showImage()
local xml =
[[<ui title="Screenshot" closeable="true" on-close="imClose_callback" placement="center" modal="true">
<image id="1" keep-aspect-ratio="true" scaled-contents="true" style="* {background-color: #555555}" />
<button text="save screenshot" on-click="save_callback"/>
</ui>]]
imUi = simUI.create(xml)
end
function imClose_callback()
simUI.destroy(imUi)
end
function showDlg()
if not ui then
local pos = 'position="-50,50" placement="relative"'
if uiPos then
pos = 'position="' .. uiPos[1] .. ',' .. uiPos[2] .. '" placement="absolute"'
end
local xml =
'<ui title="Screenshot tool" activate="false" closeable="true" on-close="close_callback" ' ..
pos .. [[>
<group layout="form" flat="true">
<checkbox text="empty areas are transparent" on-change="transparent_callback" id="4" />
<label text=""/>
<checkbox text="show file dialog" on-change="dlg_callback" id="5" />
<label text=""/>
<checkbox text="perspective mode" on-change="perspective_callback" id="6" />
<label text=""/>
<label text="resolution X"/>
<edit on-editing-finished="res_callback" id="1" />
<label text="resolution Y"/>
<edit on-editing-finished="res_callback" id="2" />
<label text="view angle"/>
<edit on-editing-finished="viewAngle_callback" id="7" />
<label text="view size"/>
<edit on-editing-finished="viewSize_callback" id="8" />
<radiobutton text="OpenGL rendering" on-click="rendering_callback" id="21" />
<label text=""/>
<radiobutton text="OpenGL3 rendering" on-click="rendering_callback" id="22" />
<label text=""/>
<radiobutton text="POV-Ray rendering" on-click="rendering_callback" id="23" />
<label text=""/>
</group>
<group layout="form" flat="true" id="30">
<label text="POV-Ray rendering:"/>
<label text=""/>
<checkbox text="focal blur (slow)" on-change="povItems_callback" id="31" />
<label text=""/>
<label text="focal distance"/>
<edit on-editing-finished="povItems_callback" id="32" />
<label text="aperture"/>
<edit on-editing-finished="povItems_callback" id="33" />
<label text="blur samples"/>
<edit on-editing-finished="povItems_callback" id="34" />
</group>
<button text="render screenshot" on-click="render_callback"/>
</ui>]]
ui = simUI.create(xml)
updateDlg()
end
end
function updateDlg()
local sel = simUI.getCurrentEditWidget(ui)
simUI.setEditValue(ui, 1, tostring(math.floor(config.res[1])))
simUI.setEditValue(ui, 2, tostring(math.floor(config.res[2])))
simUI.setEditValue(ui, 7, tostring(math.floor(0.5 + config.viewAngle * 180 / math.pi)))
simUI.setEditValue(ui, 8, string.format('%.2f', config.viewSize))
simUI.setEnabled(ui, 7, config.perspective)
simUI.setEnabled(ui, 8, not config.perspective)
simUI.setCheckboxValue(ui, 4, config.transparent and 2 or 0)
simUI.setCheckboxValue(ui, 5, config.fileDlg and 2 or 0)
simUI.setCheckboxValue(ui, 6, config.perspective and 2 or 0)
simUI.setRadiobuttonValue(ui, 21, config.renderMode == 'opengl' and 1 or 0)
simUI.setRadiobuttonValue(ui, 22, config.renderMode == 'opengl3' and 1 or 0)
simUI.setRadiobuttonValue(ui, 23, config.renderMode == 'povray' and 1 or 0)
simUI.setEnabled(ui, 23, config.povray.available)
simUI.setEnabled(ui, 30, config.povray.available and config.renderMode == 'povray')
simUI.setCheckboxValue(ui, 31, config.povray.focalBlur and 2 or 0)
simUI.setEditValue(ui, 32, string.format('%.2f', config.povray.focalDistance))
simUI.setEnabled(
ui, 32,
config.povray.available and config.renderMode == 'povray' and config.povray.focalBlur
)
simUI.setEditValue(ui, 33, string.format('%.2f', config.povray.aperture))
simUI.setEnabled(
ui, 33,
config.povray.available and config.renderMode == 'povray' and config.povray.focalBlur
)
simUI.setEditValue(ui, 34, tostring(math.floor(config.povray.blurSamples + 0.5)))
simUI.setEnabled(
ui, 34,
config.povray.available and config.renderMode == 'povray' and config.povray.focalBlur
)
simUI.setCurrentEditWidget(ui, sel)
end
function hideDlg()
if ui then
uiPos = {}
uiPos[1], uiPos[2] = simUI.getPosition(ui)
simUI.destroy(ui)
ui = nil
end
end
function transparent_callback(ui, id, v)
config.transparent = not config.transparent
updateDlg()
end
function dlg_callback(ui, id, v)
config.fileDlg = not config.fileDlg
updateDlg()
end
function perspective_callback(ui, id, v)
config.perspective = not config.perspective
updateDlg()
end
function res_callback(ui, id, v)
v = tonumber(v)
if v then
v = math.floor(math.abs(v) + 0.5)
if v > 4096 then v = 4096 end
if v < 2 then v = 2 end
config.res[id] = v
end
updateDlg()
end
function viewAngle_callback(ui, id, v)
v = tonumber(v)
if v then
if v > 90 then v = 90 end
if v < 5 then v = 5 end
v = v + 0.5
config.viewAngle = v * math.pi / 180
end
updateDlg()
end
function viewSize_callback(ui, id, v)
v = tonumber(v)
if v then
if v > 100 then v = 100 end
if v < 0.05 then v = 0.05 end
config.viewSize = v
end
updateDlg()
end
function rendering_callback(ui, id, v)
if id == 21 then config.renderMode = 'opengl' end
if id == 22 then config.renderMode = 'opengl3' end
if id == 23 then config.renderMode = 'povray' end
updateDlg()
end
function povItems_callback(ui, id, v)
if id == 31 then
config.povray.focalBlur = not config.povray.focalBlur
else
v = tonumber(v)
if v then
if id == 32 then
if v > 100 then v = 100 end
if v < 0.01 then v = 0.01 end
config.povray.focalDistance = v
end
if id == 33 then
if v > 1 then v = 1 end
if v < 0.01 then v = 0.01 end
config.povray.aperture = v
end
if id == 34 then
if v > 50 then v = 50 end
if v < 1 then v = 1 end
config.povray.blurSamples = math.floor(v + 0.5)
end
end
end
updateDlg()
end
function render_callback(ui, id, v)
local pose = sim.getObjectPose(cam)
local opt = 1
local a = config.viewSize
if config.perspective then
opt = opt | 2
a = config.viewAngle
end
local visSens = sim.createVisionSensor(
opt, {config.res[1], config.res[2], 0, 0},
{config.nearClipping, config.farClipping, a, 0.1, 0.1, 0.1, 0, 0, 0, 0, 0}
)
sim.setObjectPose(visSens, pose)
-- sim.setObjectInt32Param(visSens,sim.visionintparam_resolution_x,config.res[1])
-- sim.setObjectInt32Param(visSens,sim.visionintparam_resolution_y,config.res[2])
-- sim.setObjectFloatParam(visSens,sim.visionfloatparam_perspective_angle,config.viewAngle)
-- sim.setObjectFloatParam(visSens,sim.visionfloatparam_ortho_size,config.viewSize)
-- sim.setObjectInt32Param(visSens,sim.visionintparam_perspective_operation,config.perspective and 1 or 0)
sim.setObjectInt32Param(
visSens, sim.visionintparam_pov_focal_blur, config.povray.focalBlur and 1 or 0
)
sim.setObjectFloatParam(
visSens, sim.visionfloatparam_pov_blur_distance, config.povray.focalDistance
)
sim.setObjectFloatParam(visSens, sim.visionfloatparam_pov_aperture, config.povray.aperture)
sim.setObjectInt32Param(visSens, sim.visionintparam_pov_blur_sampled, config.povray.blurSamples)
if config.renderMode == 'opengl' then
sim.setObjectInt32Param(visSens, sim.visionintparam_render_mode, 0)
end
if config.renderMode == 'opengl3' then
sim.setObjectInt32Param(visSens, sim.visionintparam_render_mode, 7)
end
if config.renderMode == 'povray' then
sim.setObjectInt32Param(visSens, sim.visionintparam_render_mode, 3)
end
local savedVisibilityMask = 0
local savedVisibilityMask = sim.getObjectInt32Param(cam, sim.objintparam_visibility_layer)
sim.setObjectInt32Param(cam, sim.objintparam_visibility_layer, 0)
local newAttr = sim.displayattribute_renderpass
newAttr = newAttr + sim.displayattribute_forvisionsensor
newAttr = newAttr + sim.displayattribute_ignorerenderableflag
sim.setObjectInt32Param(visSens, sim.visionintparam_rendering_attributes, newAttr)
sim.handleVisionSensor(visSens)
sim.setObjectInt32Param(cam, sim.objintparam_visibility_layer, savedVisibilityMask)
showImage()
local img, resolution = sim.getVisionSensorImg(visSens)
local rxy = {}
img, rxy = sim.getScaledImage(img, resolution, {800, 800}, 4)
simUI.setImageData(imUi, 1, img, rxy[1], rxy[2])
local cutOff = 0
if config.transparent then cutOff = 0.99 end
local opt = 0
if config.transparent then
opt = 2
end
image, res = sim.getVisionSensorImg(visSens, opt, cutOff)
sim.removeObjects({visSens})
end
function save_callback(ui, id, v)
local options = 0
if config.transparent then options = 1 end
local fileNames
if config.fileDlg then
fileNames = simUI.fileDialog(
simUI.filedialog_type.save, 'title', '', 'screenshot.png', 'image file',
'*'
)
else
local fileName = 'coppeliaSim_screenshot_' .. os.date("%Y_%m_%d-%H_%M_%S", os.time()) .. '.png'
if sim.getInt32Param(sim.intparam_platform) == 1 then
-- MacOS, special: executable is inside of a bundle:
fileName = '../../../' .. fileName
end
fileNames = {fileName}
end
if #fileNames > 0 then
local fileName = fileNames[1]
if sim.saveImage(image, res, options, fileName, -1) ~= -1 then
sim.addLog(sim.verbosity_msgs, "Screenshot was saved to " .. fileName)
simUI.msgBox(
simUI.msgbox_type.info, simUI.msgbox_buttons.ok, 'Screenshot',
"Screenshot was saved to " .. fileName
)
else
sim.addLog(
sim.verbosity_scripterrors,
"Failed saving the screenshot. Did you specify a supported image extension?"
)
simUI.msgBox(
simUI.msgbox_type.warning, simUI.msgbox_buttons.ok, 'Screenshot',
"Failed saving the screenshot. Did you specify a supported image extension?"
)
end
else
sim.addLog(
sim.verbosity_scripterrors,
"Failed saving the screenshot. Bad filename or action canceled."
)
simUI.msgBox(
simUI.msgbox_type.warning, simUI.msgbox_buttons.ok, 'Screenshot',
"Failed saving the screenshot. Bad filename or action canceled."
)
end
simUI.destroy(imUi)
imUi = nil
end
function close_callback()
leaveNow = true
end