Script Speed
I have written a script which will do something like this:
Detach faces applied with material whose name ends with "_alpha" (material name) to new objects, and put those new objects inside a group called "Alpha_blending_mesh".
The script is running perfectly fine and doing exactly what I intend it to do. But the problem is it is running fast only the first time....from the next time it is taking too long (specially for complex scenes with too many objects).....again if I restart max it is running fast for the first time. I am not sure what is happening once I run the script for the first time....which is making it slow from the next attempt!! I have tried gc()....clearundobuffer()...nothing is working!! I have saved the files after I ran the script once....restarted max and ran it again...it again works fast for the first time....so the problem is not in the file...something is eating up the memory after the first run!!
I have written this by following this forum but since it is my first script it might look very clumpsy and funny :P.....here is the script:
(
undo off
(
-- delete previous groups named "Alpha_blending_mesh"
objgroup = #()
for o in helpers do
(
if o.name == "Alpha_blending_mesh" then
(
append objgroup o
)
)
(
if objgroup.count != 0 then
(
for i = 1 to objgroup.count do
(
ExplodeGroup $Alpha_blending_mesh
)
)
)
-- delete previous groups named "Alpha_blending_mesh"
--will merge all vertices with threshold 0.001 if the obj is not inside a group
fn weldverts obj =
(
result = isGroupMember obj
if result == false do
(
$.weldThreshold = 0.001
polyop.weldVertsByThreshold obj #All
)
)
--will merge all vertices with threshold 0.001 if the obj is not inside a group
-- Collect objects from scene in working list
workingObject = #()
for o in Geometry do
(
if classof o != bonegeometry do
(
append workingObject o
)
)
-- Collect objects from scene in working list
fn Singlemat obj =
(
mat = obj.material
TextureFileName = "notexture"
if (classof obj != bonegeometry) and (mat != undefined) and classof mat.diffuseMap == Bitmaptexture do
(
stringSS = mat.name
stringcount = stringSS.count
if stringcount > 6 then
(
TextureName = substring stringSS (stringcount - 5) ((stringcount - 5) + 6)
)
if TextureName == "_alpha" do
(
objname = obj.name
stringTT = objname as string
stringTTcount = stringTT.count
if stringTTcount > 6 then
(
ObjnameTT = substring stringTT (stringTTcount - 5) ((stringTTcount - 5) + 6)
)
if ObjnameTT != "_alpha" do
(
obj.name = objname +"_alpha"
)
)
)
)
fn detachFBI obj =
(
objname = obj.name
ids = obj.material.materialIDList
for id in ids do
(
obj.selectbymaterial id
_faceSel = polyOp.getFaceSelection obj
polyOp.detachFaces obj _faceSel delete:true asNode:true name: (objname +"_tempdelface")
)
select $*_tempdelface*
$.name = objname
)
-- Run though all objects in array and detach them
for o in workingObject do
(mat = o.material
if (mat != undefined) and (classof mat == Multimaterial) do
(
select o
detachFBI o
delete o
)
)
-- Run though all objects in array and detach them
--assigning single material to all object (removing unused multimaterials)
for o in geometry where isKindOf o.material multimaterial and (classof o != bonegeometry) do
(
-- Get face material ID from face 1
local id = getFaceMatID o.Mesh 1
-- Assign that material
o.material = o.material[id]
)
--assigning single material to all object (removing unused multimaterials)
-- Run though all objects and check for "_alpha" in diffuse map, rename objets as *"_alpha"
for g in geometry do
(
select g
Singlemat g
)
-- Run though all objects and check for "_alpha" in diffuse map, rename objets as *"_alpha"
--group as "Alpha_blending_mesh"
clearselection()
select $*_alpha
if $ != undefined do
(
group selection name:"Alpha_blending_mesh"
)
--group as "Alpha_blending_mesh"
--attaching same named objects and welding vertices by 0.001 thresold
newworking = #()
for o in geometry do
(
if classof o == editable_poly and classof o != bonegeometry do
(
append newworking o
)
)
i = 1
while i <= newworking.count do
(
b = newworking[i]
j = i + 1
while j<= newworking.count do
(
if (newworking[j].name == b.name) then
(
b.attach (newworking[j]) b
deleteItem newworking j
select b
weldverts b
)
else
(
j = j +1
)
)
i = i + 1
)
--attaching same named objects and welding vertices by 0.001 thresold
--set final selection to group "Alpha_blending mesh, tool to 'move' mode, panel to 'modify' mode"
groupName = "Alpha_blending_mesh"
grp = getNodeByName groupName
if isValidNode grp and isGroupHead grp do
(
select $Alpha_blending_mesh
)
toolmode.commandmode = #move
setCommandPanelTaskMode mode:#modify
--set final selection to group "Alpha_blending mesh, tool to 'move' mode, panel to 'modify' mode"
)
)
Comments
I not tried the script. Put
I not tried the script.
Put locals weldbythresh, detachF and getMatID inside "on btn pressed do" event.
bga
Try this code
I add some stuff in your code.Need to optimize more but not have
time right now.
bga
Its working fast now....but
Its working fast now....but something with the material assignment gone wrong.....lets see if I can debug that!! Thanks a lot!!
Only one question....do we always need to mention "local" for local variables....I thought it takes local automatically...unless we mention it as "global"??
To better undertand local and
To better undertand local and global variables read this
http://docs.autodesk.com/3DSMAX/15/ENU/MAXScript-Help/index.html?url=fil...
bga
I tried to optimize a bit by
I tried to optimize, I deleting the actions selection ... over the script of Barigazy ...
kimarotta.com.br
3d Artist
I don't know what is the
I don't know what is the problem, both barigazy and kimarotto's script is ruining my material assignments in multitextured objects.....but I got the point from your scripts, on how to make it fast....so i added the "local" before the variables and freed the arrays in my old script....it is now working fine and fast!!
try this
bga
No...its not working.....!!
No...its not working.....!!
see
see
kimarotta.com.br
3d Artist
Now looking fine :)