problem attaching mesh

hi guys, i'm really new on 3DSMAX and scripting, so excuse my horrible mistakes, well my problem is attaching meshes in 3DS Max, what i need is a script that attach all the meshes in one, no matter if they have diffent materials, i have this script:

(
mtl = #() ; tmp = #() ; fin = #()
sel = selection as array
if sel.count == 0 then -- check selection
messageBox "Nothing selected!" title:"Attach Mesh by Material"
else
( -- filter selection
sel = for i in sel where canConvertTo i Editable_Mesh \
and i.material != undefined collect i

if sel.count <= 1 then -- 2nd check
messageBox "Procedure requered\nmore then 1 valid objects\nwith assigned materials!" title:"Attach Mesh by Material"
else
(
for i in sel do
(
if findItem mtl i.material == 0 do
append mtl i.material
)
for m in mtl do
(
tmp = for i in sel collect i
append fin tmp
)
undo on
(
clearSelection()
for i in fin do
(
if i.count > 1 do
(
trg = snapshot i[1]
selectMore trg
delete i[1] -- del source obj
deleteItem i 1 -- del item array
for j in i do
meshop.attach trg j attachMat:#IDToMat condenseMat:true
)
)
)
)
)
)

but when i run it, it displays this error: Runtime error: Attempt to access deleted scene object on this lines of the code: trg = snapshot i[1]

i don't know what to do!!! can anyone help me!!!
thank you so much!

Comments

Comment viewing options

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

Comment viewing options

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