[Closed] Block colours
/*
sphere segments:128 isselected:on
*/
-- check the difference!
(
gc()
t1 = timestamp()
m1 = heapfree
for v=1 to $.mesh.numverts do meshop.getvert $.mesh v
format "time:% leak:%
" (timestamp() - t1) (m1 - heapfree)
)
-- and
(
gc()
t1 = timestamp()
m1 = heapfree
mesh = copy $.mesh
getv = meshop.getvert
for v=1 to mesh.numverts do getv mesh v
format "time:% leak:%
" (timestamp() - t1) (m1 - heapfree)
delete mesh
)
<node>.mesh creates a copy of node’s trimesh on every call.
:bowdown: :bowdown: :bowdown:
Awesome, thanks for the pointer.
On another issue, I’m trying to have it write the average color back to the vertices but am unable to find a method to do this without collapsing to a mesh first.
Am I missing something obvious?
you can assign vertex colors to the geometry if it’s Editable_Mesh or_Poly, Edit_Poly, or applying UVW Unwrap modifier.
Thanks, I used the unwrap UVW method.
It works fine when operating on one object only, but fails with more than one.
The cause of this is that the UVW unwrap modifier does not accept the .setVC true method for some reason. Is there an update method I need to use to make this stick?
try(destroydialog solidCols)catch()
fn SetAvgColor o chan =
(
local cols=#()
local theBMP=rendermap o.material.diffuseMap
local theMesh=copy o.mesh
local getMV=meshop.getMapVert
for i = 1 to (meshop.getnummapverts theMesh chan) do
(
local UVvert=getMV theMesh chan i
local thecoords=[(mod UVvert[1] 1)*(theBMP.width-1),(1-(mod UVvert[2] 1))*(theBMP.height-1)]
local thePix=getPixels theBMP thecoords 1
append cols thePix[1]
)
delete theMesh
local sum=black
for c = 1 to cols.count do sum+=cols[c]
sum=sum/cols.count
if (classof o.modifiers[1]==unwrap_UVW) and (o.modifiers[1].getVC()) then (local UVmod=o.modifiers[1]) else
(
local UVmod=unwrap_UVW()
addModifier o UVmod
)
UVmod.setVC true
local setVP=UVmod.setVertexPosition
for v = 1 to UVmod.numberVertices() do UVmod.setVertexPosition currentTime v ((sum as point3)/255)
UVmod
)
rollout solidCols "Block colors"
(
spinner mapChan "Map Channel" type:#integer range:[1,100,1]
button convertIt "Convert selected to solid"
on convertIt pressed do if selection.count>0 do undo "Convert to solid" on
(
t1 = timestamp()
m1 = heapfree
setWaitCursor()
local VCMat=standardmaterial diffusemap:(vertexColor())
for o in selection where (classof o.material==standardmaterial) and (classof o.material.diffusemap==bitmaptex) do
(
SetAvgColor o mapChan.value
o.material=VCMat
)
setArrowCursor()
format "time:% leak:%
" (timestamp() - t1) (m1 - heapfree)
)
)
createDialog solidCols
the Unwrap modifier works for multiple objects. You have to use setVertexPositionByNode.
.setVC also has to work. The modifier has to be open in Modpanel. If it doesn’t work for you for any reason you can use UIAccessor to press the radiobutton “Vertex Color Channel”.
Ah, my problem was that the modifier was not open in the modify panel.
Here is a working version of the script, it now uses the actual diffuse texture, not the bitmap (using the rendermap method as suggested). Actually it doesn’t have to use a bitmap, as long as it is using a 2d map.
Thanks for your help Denis.
try(destroydialog solidCols)catch()
fn SetAvgColor o=
(
local cols=#()
local theBMP=rendermap o.material.diffuseMap
local chan=o.material.diffuseMap.coords.mapChannel
local theMesh=copy o.mesh
local getMV=meshop.getMapVert
for i = 1 to (meshop.getnummapverts theMesh chan) do
(
local UVvert=getMV theMesh chan i
local thecoords=[(mod UVvert[1] 1)*(theBMP.width-1),(1-(mod UVvert[2] 1))*(theBMP.height-1)]
local thePix=getPixels theBMP thecoords 1
append cols thePix[1]
)
delete theMesh
local sum=black
for c = 1 to cols.count do sum+=cols[c]
sum=sum/cols.count
with redraw off
(
max modify mode
select o
if (classof o.modifiers[1]==unwrap_UVW) and (o.modifiers[1].getVC()) then (local UVmod=o.modifiers[1]) else
(
local UVmod=unwrap_UVW()
addModifier o UVmod
)
UVmod.setVC true
local setVP=UVmod.setVertexPosition
for v = 1 to UVmod.numberVertices() do UVmod.setVertexPosition currentTime v ((sum as point3)/255)
)
)
rollout solidCols "Block colors"
(
button convertIt "Convert selected to solid"
on convertIt pressed do if selection.count>0 do undo "Convert to solid" on
(
t1 = timestamp()
m1 = heapfree
setWaitCursor()
local VCMat=standardmaterial diffusemap:(vertexColor())
local sel = selection as array
for o in sel where (classof o.material==standardmaterial) do
(
SetAvgColor o
o.material=VCMat
)
setArrowCursor()
format "time:% leak:%
" (timestamp() - t1) (m1 - heapfree)
)
)
createDialog solidCols