[Closed] Unwrap random move polygons speed
Hello.
I need your help to make my code work faster. I know it’s possible to make it ~2x faster because one popular script can do that
I need to randomize uv shells, not uv polygons, but this code is just an example to understand my issue. It creates plane 70×70, breaks all vertices to get 4900 uv elements and then randomizes their position in Unwrap.
For now I have 3 methods:
-
Method 1.
Works fine. But as I said, I’m sure it’s possible to randomize 4900 uv polys 2 times faster. -
Method 2.
I think it could be faster because it doesn’t need face>vert conversion, but “moveSelected” function eats ~2.2GB of RAM (3dsmax process, not script itself) maybe because of TheHold.Put inside C++ method. Also I suppose this method could be faster without this undo issue but I don’t understand how to disable TheHold.Put. I tried SuperBegin-SuperAccept and “undo off” with no luck. -
Method 3.
Here I’m trying to check if selecting vertices is faster than selecting polys and converting to vertices (Method 1). So I selected #{1…4} verts just to test it, and this method is surprisingly slower than Method 1.
I will appreciative any help.
(
-- create test object
local obj = Plane width:1000 length:1000 lengthsegs:70 widthsegs:70
convertToPoly obj
polyop.breakVerts obj #{1..(polyop.getNumVerts obj)}
local numfaces = polyop.getNumFaces obj
local allUvPolys = #{1..numfaces}
local unwMod = Unwrap_UVW()
addmodifier obj unwMod
select obj
-- cache functions
c_selectPolygonsUpdate = unwMod.selectPolygonsUpdate
c_selectVertices = unwMod.selectVertices
c_moveSelectedVertices = unwMod.moveSelectedVertices
c_moveSelected = unwMod.moveSelected
c_getSelectionFromFace = unwMod.GetSelectionFromFace
clearlistener()
ts1 = timestamp()
-- move uv elements method 1
subobjectlevel = 1
for p in allUvPolys do (
c_selectPolygonsUpdate #{p} false
c_getSelectionFromFace()
theOffset = [random -0.1 0.1 , random -0.1 0.1 , 0]
c_moveSelectedVertices theOffset
)
subobjectlevel = 0
format "uv move METHOD 1: % ms\n" (timestamp() - ts1)
ts2 = timestamp()
-- move uv elements method 2
for p in allUvPolys do (
c_selectPolygonsUpdate #{p} false
theOffset = [random -0.1 0.1 , random -0.1 0.1 , 0]
c_moveSelected theOffset
)
format "uv move METHOD 2: % ms\n" (timestamp() - ts2)
ts3 = timestamp()
-- move uv elements method 3
for p in allUvPolys do (
c_selectVertices #{1..4} -- selecting any vertices
theOffset = [random -0.1 0.1 , random -0.1 0.1 , 0]
c_moveSelectedVertices theOffset
)
format "uv move METHOD 3: % ms\n" (timestamp() - ts3)
)
My time:
uv move METHOD 1: 2663 ms
uv move METHOD 2: 2701 ms
uv move METHOD 3: 3467 ms
(
delete objects
gc()
p = Plane width:1000 length:1000 lengthsegs:70 widthsegs:70
convertToPoly p
polyop.breakVerts p #all
t = timestamp()
h = heapfree
seed 0
for k=1 to p.numfaces do
(
vv = polyop.getmapface p 1 k
offset = random -[0.1,0.1,0] [0.1,0.1,0]
for v in vv do polyop.setmapvert p 1 v (polyop.getmapvert p 1 v + offset)
)
format "time:% heap:%\n" (timestamp() - t) (h - heapfree)
)
you don’t need Upwrap UVW to do it
Thanks.
My Unwrap tools script already have ~4k lines of code Randomizer is one of them. That is why I need Unwrap solution.
here is the best i see:
numberPointsInFace = unwMod.numberPointsInFace
getVertexIndexFromFace = unwMod.getVertexIndexFromFace
getVertexPosition = unwMod.getVertexPosition
setVertexPosition = unwMod.setVertexPosition2
t = currenttime
t0 = timestamp()
for p in allUvPolys do
(
offset = random -[0.1, 0.1, 0] [0.1, 0.1, 0]
for k=1 to numberPointsInFace p do
(
v = getVertexIndexFromFace p k
setVertexPosition t v (getVertexPosition t v + offset) off off
)
)
format "uv move METHOD 0: % ms\n" (timestamp() - t0)
– uv move METHOD 0: 42 ms
Wow. How did I miss SetVertexPosition, shame on me.
But is this method usable for rotation and scale UV Shells as well? I think it needs some transorm matrix magic, but I’m not sure. TM of each vertex needs to be modified based on UV Shell center?
you have to calculate transformation matrix… ‘center’ of transformation might be #world, or #cluster (#selection, #face, etc.) center
PS. Don’t forget to set ‘likes’
i guess that you want to randomize UV elements… So the bottle neck in you case would be the finding all uv elements.
there is (or was) a thread on this forum about how to find all mesh elements quick and safe. Try to apply the best algorithm from that thread to the mesh-map data
I know what are you talking about. Thanks to Jorge Rodríguez (PolyTools3D) I have this algorythm
And thanks to you I have a better idea to set vertices positions.