[Closed] How to optimize this code
Hi,
This code is used to fold the selected border (hard) edges of Editable_Poly object.
You can set chamfer amount, connection segment and randomness.
But the problem arises when the edges of different length.
I need optimization for this function.
Any help or guidelines how to achieve this, is greatly appreciated.
Original concept by Alex Bluesummers “Chew Edge utility”
(
global alreadyWrinkled
if selection.count == 1 and classof selection[1] == editable_poly and (polyOp.getEdgeSelection selection[1]).numberSet != 0 do
(
if alreadyWrinkled == undefined then (alreadyWrinkled = false) else (max undo)
local ges = polyOp.getEdgeSelection, ses = polyOp.setEdgeSelection ; smg = polyop.setFaceSmoothGroup
local chemEdg = polyOp.chamferEdges, gev = polyOp.getEdgeVerts ; rtg = polyop.retriangulate
local gv = polyOp.getVert, sv = polyOp.setVert ; gnf = polyop.getNumFaces
fn Wrinkle obj chamferAmount:1 connectSeg:2 Rand:0.08 subD:on inter:2 =
(
undo "WrinkledEdges" on
(
local selEdg = ges obj -- collect selected edges
obj.connectEdgeSegments = connectSeg --adjust segments
if heapSize < 204800000 do heapSize = 204800000
with redraw off max create mode
for edg in selEdg do
(
ses obj edg -- set selection for each edge separately
chemEdg obj #{edg} chamferAmount -- chamfer edge with given a value
obj.ConnectEdges() -- connect edges
for v in selEdg do
(
local eVert = (gev obj v) -- collect edge vertex
sv obj #{eVert[1]} ((gv obj eVert[1])+(random -Rand Rand)) --set position for first vert
sv obj #{eVert[2]} ((gv obj eVert[2])+(random -Rand Rand)) --set position for second vert
)
)
local fBitArr = #{1..(gnf obj)} -- collect all faces
rtg obj fBitArr -- retriangulate faces
smg obj fBitArr 0 -- clear SmoothGroup
-- turn on "Use NURMS Subdivision"
if subD == on then (obj.surfSubdivide = on ; obj.iterations = 2) else obj.surfSubdivide = off
) ; alreadyWrinkled = true ; max views redraw
)
Wrinkle selection[1]
)
)
here is your code after optimization:
(
fn wrinkleEdges obj edges: chamferAmount:1 connectSeg:2 rand:[0.1,0.1,0.1] seedValue: subdivide:on = if iskindof obj editable_poly do
(
if edges == unsupplied then edges = obj.selectededges as bitarray else edges = edges as bitarray
if edges.numberSet > 0 do
(
if seedValue != unsupplied do seed seedValue
chamferedges = polyOp.chamferEdges
connectedges = obj.ConnectEdges
getedgeverts = polyOp.getEdgeVerts
movevert = polyOp.moveVert
obj.connectEdgeSegments = connectSeg --adjust segments
for edge in edges do
(
obj.selectededges = #{edge} -- set selection for each edge separately
chamferedges obj edge chamferAmount -- chamfer edge with given a value
connectedges() -- connect edges
for e in obj.selectededges as bitarray do
(
vv = getedgeverts obj e -- collect edge vertex
movevert obj vv #(random -rand rand, random -rand rand) --set position for first vert
)
)
faces = obj.faces as bitarray -- collect all faces
polyop.retriangulate obj faces -- retriangulate faces
polyop.setFaceSmoothGroup obj faces 0 -- clear SmoothGroup
if (obj.surfSubdivide = subdivide) do obj.iterations = 2
)
)
undo "WrinkledEdges" on
(
if (mode = getCommandPanelTaskMode()) != #create do setCommandPanelTaskMode mode:#create
t1 = timestamp()
m1 = heapfree
wrinkleEdges selection[1] seed:0
format "time:% memory:%
" (timestamp() - t1) (m1 - heapfree)
if (getCommandPanelTaskMode()) != mode do setCommandPanelTaskMode mode:mode
)
)
it’s 6 times faster and uses 4 times less memory now.
i marked with red most important changes, with yellow useful, and with green recommended.
ps. don’t use abbreviations for function names or variables. a week later you will forget what they mean for sure.
some another recommendation…
every coding language has its own coding style… that’s a good practice. i tried to give your code a mxs style.
Denis thank you very much.
This is outstanding examle from the outstanding man.
In any case, your code is not only six time faster, but a hundred times.
I really appreciate all your efforts in this and any recommendations that you gave.
One question:
I noticed that the “UNDO” outside the function.
Is that the main reason why the code is running slow?
it depends on a number of edges you proceed …
haven’t got it. could you clear the question?
Sorry for above question. I fix it.
Originally Posted by DenisT
it depends on a number of edges you proceed ….
And yes, i’ve used last night a lot of edges in one test and block my pc.
But now is different, it’s works smoothly, thanks to you.
my point can be wrong… i got a comment from a big gun… but i always check how much my function eats the memory.
and i say it again… i’m probably paranoiac but i always looking a way to make my functions as less as possible memory used.
my point can be wrong… i got a comment from a big gun… but i always check how much my function eats the memory.
and i’m saying it again… i’m probably paranoiac but i’m always looking for a way to make my functions as less as possible memory used.
I could not access the CGTalk forum for ten minutes.
I don’t know what happened.
to continue
Obviously it’s easier to just use modifiers for this examle, then go through all the edges
(one by one) and doing chamfering, connecting and moving verts at the end.
But this is the “one” click operations.
The only thing that should be added to the function is to check longest edge in selection,
then add “connectEdgeSegments” and for all other edges, depending on the length,
automatically calculate different (lover) number of segs or if it’s to short don’t do connection.