-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathColor changer.lua
337 lines (315 loc) · 11.7 KB
/
Color changer.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
sim = require 'sim'
function sysCall_info()
return {autoStart = false, menu = 'Tools\nColor changer'}
end
function sysCall_init()
simUI = require 'simUI'
sim.addLog(
sim.verbosity_scriptinfos,
"This tool allows you to change the color of shapes, when those colors are named. Simply select individual shapes or models."
)
previousSelectedObjects = {}
colorNameIndex = -1
end
function sysCall_addOnScriptSuspend()
return {cmd = 'cleanup'}
end
function getSameColor(allCols, col)
for name, coll in pairs(allCols) do
local same = true
for i = 1, 3, 1 do
for j = 1, 3, 1 do
if coll[i][j] ~= col[i][j] then
same = false
break
end
end
if not same then break end
end
if same and name:sub(1, 1) == '*' then return name end
end
end
function sysCall_nonSimulation()
if leaveNow then return {cmd = 'cleanup'} end
end
function sysCall_selChange(inData)
selectedObjects = inData.sel
selectedObjects = getAlsoModelObjectsAndOnlyVisibleShapes(selectedObjects)
if not table.eq(selectedObjects, previousSelectedObjects) then
hideDlg2()
hideDlg1()
previousSelectedObjects = table.slice(selectedObjects)
if #selectedObjects > 0 then
allCols = {}
allShapesAndCols = {}
for i = 1, #selectedObjects, 1 do
local s = sim.getObjectStringParam(
selectedObjects[i], sim.shapestringparam_colorname
)
local r, a_cols = sim.getShapeColor(
selectedObjects[i], '@compound',
sim.colorcomponent_ambient_diffuse
)
local r, b_cols = sim.getShapeColor(
selectedObjects[i], '@compound', sim.colorcomponent_specular
)
local r, c_cols = sim.getShapeColor(
selectedObjects[i], '@compound', sim.colorcomponent_emission
)
local scolnms = {}
if s and s ~= '' then
local i = 0
for token in string.gmatch(s, "[^%s]+") do
local col = {
{a_cols[3 * i + 1], a_cols[3 * i + 2], a_cols[3 * i + 3]},
{b_cols[3 * i + 1], b_cols[3 * i + 2], b_cols[3 * i + 3]},
{c_cols[3 * i + 1], c_cols[3 * i + 2], c_cols[3 * i + 3]},
}
if token ~= '*' then
colName = token
allCols[colName] = col
else
colName = getSameColor(allCols, col)
if not colName then
local j = 1
while allCols['*' .. j] ~= nil do
j = j + 1
end
colName = '*' .. j
allCols[colName] = col
end
end
scolnms[i + 1] = colName
i = i + 1
end
end
allShapesAndCols[#allShapesAndCols + 1] = scolnms
end
colorNameTable = {}
for name, coll in pairs(allCols) do
colorNameTable[#colorNameTable + 1] = name
end
table.sort(colorNameTable)
-- print(allCols)
-- print(allShapesAndCols)
-- print(colorNameTable)
--[[
colorNames={}
for i=1,#selectedObjects,1 do
local s=sim.getObjectStringParam(selectedObjects[i],sim.shapestringparam_color_name)
if s and s~='' then
for token in string.gmatch(s,"[^%s]+") do
colorNames[token]=token
end
end
end
colorNameTable={}
for k, v in pairs(colorNames) do
colorNameTable[#colorNameTable+1]=k
end
--]]
showDlg1(colorNameTable)
end
end
end
function sysCall_cleanup()
hideDlg2()
hideDlg1()
end
function sysCall_beforeSimulation()
hideDlg2()
hideDlg1()
previousSelectedObjects = {}
end
function sysCall_beforeInstanceSwitch()
hideDlg2()
hideDlg1()
previousSelectedObjects = {}
end
function showDlg1()
if not ui and #colorNameTable > 0 then
local pos = 'position="-50,50" placement="relative"'
if uiPos then
pos = 'position="' .. uiPos[1] .. ',' .. uiPos[2] .. '" placement="absolute"'
end
local xml =
'<ui title="Color names" activate="false" closeable="true" on-close="close_callback" layout="vbox" ' ..
pos .. '>'
for i = 1, #colorNameTable, 1 do
local cn = colorNameTable[i]
cn = cn:gsub("*", "Unnamed color ")
xml = xml .. '<button text="' .. cn .. '" on-click="colorClick_callback" id="' .. i ..
'" />'
end
xml = xml .. '</ui>'
ui = simUI.create(xml)
end
end
function hideDlg1()
if ui then
uiPos = {}
uiPos[1], uiPos[2] = simUI.getPosition(ui)
simUI.destroy(ui)
ui = nil
end
end
function close_callback()
leaveNow = true
end
function colorClick_callback(ui, id, v)
hideDlg1()
colorNameIndex = id
showDlg2()
end
function showDlg2()
local pos = 'position="-50,50" placement="relative"'
if ui2Pos then
pos = 'position="' .. ui2Pos[1] .. ',' .. ui2Pos[2] .. '" placement="absolute"'
end
local cn = colorNameTable[colorNameIndex]
cn = cn:gsub("*", "Unnamed color ")
local xml = '<ui title="Color [' .. cn ..
']" activate="false" closeable="true" on-close="close2_callback" layout="vbox" ' ..
pos .. '>'
xml = xml .. [[
<label text="Ambient+diffuse" style="* {font-weight: bold;}"/>
<group layout="form" flat="true">
<label text="red"/>
<hslider id="1" on-change="sliderMoved" minimum="0" maximum="100"/>
<label text="green"/>
<hslider id="2" on-change="sliderMoved" minimum="0" maximum="100"/>
<label text="blue"/>
<hslider id="3" on-change="sliderMoved" minimum="0" maximum="100"/>
</group>
<label text="Specular" style="* {font-weight: bold;}"/>
<group layout="form" flat="true">
<label text="red"/>
<hslider id="4" on-change="sliderMoved" minimum="0" maximum="100"/>
<label text="green"/>
<hslider id="5" on-change="sliderMoved" minimum="0" maximum="100"/>
<label text="blue"/>
<hslider id="6" on-change="sliderMoved" minimum="0" maximum="100"/>
</group>
<label text="Emission" style="* {font-weight: bold;}"/>
<group layout="form" flat="true">
<label text="red"/>
<hslider id="7" on-change="sliderMoved" minimum="0" maximum="100"/>
<label text="green"/>
<hslider id="8" on-change="sliderMoved" minimum="0" maximum="100"/>
<label text="blue"/>
<hslider id="9" on-change="sliderMoved" minimum="0" maximum="100"/>
</group>
</ui>]]
ui2 = simUI.create(xml)
ambientDiffuse = allCols[colorNameTable[colorNameIndex]][1]
specular = allCols[colorNameTable[colorNameIndex]][2]
emission = allCols[colorNameTable[colorNameIndex]][3]
for i = 1, 3, 1 do
simUI.setSliderValue(ui2, i + 0, ambientDiffuse[i] * 100)
simUI.setSliderValue(ui2, i + 3, specular[i] * 100)
simUI.setSliderValue(ui2, i + 6, emission[i] * 100)
end
end
function hideDlg2()
if ui2 then
ui2Pos = {}
ui2Pos[1], ui2Pos[2] = simUI.getPosition(ui2)
simUI.destroy(ui2)
ui2 = nil
colorNameIndex = -1
previousSelectedObjects = {}
end
end
function sliderMoved(ui, id, v)
local s = v / 100
if id <= 3 then ambientDiffuse[id] = s end
if id > 3 and id <= 6 then
id = id - 3
specular[id] = s
end
if id > 6 then
id = id - 6
emission[id] = s
end
local colName = colorNameTable[colorNameIndex]
for i = 1, #selectedObjects, 1 do
local r, a_cols = sim.getShapeColor(
selectedObjects[i], "@compound", sim.colorcomponent_ambient_diffuse
)
local r, b_cols = sim.getShapeColor(
selectedObjects[i], "@compound", sim.colorcomponent_specular
)
local r, c_cols = sim.getShapeColor(
selectedObjects[i], "@compound", sim.colorcomponent_emission
)
for j = 1, #allShapesAndCols[i], 1 do
if allShapesAndCols[i][j] == colName then
for k = 1, 3, 1 do
a_cols[3 * (j - 1) + k] = ambientDiffuse[k]
b_cols[3 * (j - 1) + k] = specular[k]
c_cols[3 * (j - 1) + k] = emission[k]
end
end
end
sim.setShapeColor(
selectedObjects[i], "@compound", sim.colorcomponent_ambient_diffuse, a_cols
)
sim.setShapeColor(selectedObjects[i], "@compound", sim.colorcomponent_specular, b_cols)
sim.setShapeColor(selectedObjects[i], "@compound", sim.colorcomponent_emission, c_cols)
end
sim.announceSceneContentChange()
end
function close2_callback()
hideDlg2()
end
function getAlsoModelObjectsAndOnlyVisibleShapes(sel)
local tsel = {}
for i = 1, #sel, 1 do
local p = sim.getModelProperty(sel[i])
if (p & sim.modelproperty_not_model) == 0 then
-- We have a model
local modObjs = sim.getObjectsInTree(sel[i], sim.sceneobject_shape)
for k = 1, #modObjs, 1 do
local addIt = true
for j = 1, #tsel, 1 do
if tsel[j] == modObjs[k] then
addIt = false
break
end
end
if addIt then tsel[#tsel + 1] = modObjs[k] end
end
else
-- We do not have a model
if sim.getObjectType(sel[i]) == sim.sceneobject_shape then
-- We have a shape
local addIt = true
for j = 1, #tsel, 1 do
if tsel[j] == sel[i] then
addIt = false
break
end
end
if addIt then tsel[#tsel + 1] = sel[i] end
end
end
end
sel = {}
for i = 1, #tsel, 1 do
if sim.getObjectInt32Param(tsel[i], sim.objintparam_visible) ~= 0 then
sel[#sel + 1] = tsel[i]
end
end
return sel
end
function getColorValuesForColorName(colName, selObjects)
for i = 1, #selObjects, 1 do
local r, v0 = sim.getShapeColor(selObjects[i], colName, sim.colorcomponent_ambient_diffuse)
if r > 0 then
local r, v1 = sim.getShapeColor(selObjects[i], colName, sim.colorcomponent_specular)
local r, v2 = sim.getShapeColor(selObjects[i], colName, sim.colorcomponent_emission)
return v0, v1, v2
end
end
return nil, nil, nil
end