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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kimarotta's picture

=]

=]

kimarotta.com.br
3d Artist  

barigazy's picture

:) :). Yea Instead of using

:) :). Yea
Instead of using "code" you can use also "mxs"

bga

kimarotta's picture

please put your code betwen

please put your code betwen <code>

kimarotta.com.br
3d Artist  

Goonda's picture

Sorry its not working....what

Sorry its not working....what am I doing wrong??

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.