Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk 3ds® Max® / MaxScript / object merge by material
  RSS 2.0 ATOM  

object merge by material
Rate this thread
 
55981
 
Permlink of this thread  
avatar
  • nbac
  • Posted: 18 May 2011 10:17 PM
  • Total Posts: 5
  • Joined: 25 October 2007 08:23 PM

hi,

i got 21000 objects in my scene and there are 1500 scenematerials.
i want to attach all objects with the same material applied.
in the end there should be 1500 objects.
i think it is not possible to do it manually for this quantity of objects (pick material - select objects by material - attach ...)
i think there should be a better maxscript solution ... i am really new to maxscript, so any help would be appreciated.

one way could be. replace objectname with materialname (they are unique) >> check if objectname is assigned multiple times >> if so >> select objects by this name >> attach >> if not it is already processed ... end of loop

thank you

regards sebastian



Replies: 0
avatar
  • Location: West Midlands, England, UK
  • Total Posts: 14445
  • Joined: 06 August 2007 11:06 PM
  • Permlink of this post

Things to note…

If you managed to get 2 or more materials with the same name, this will fail. It assumes that the materials are Instanced to the objects.

Attaching that many objects is slow and fills up the Undo stack rapidly, which uses a lot of memory. The code attempt to mitigate those problems, but it means there is no going back once you have run the script. You MUST ensure your scene is saved before running it.

(
suspendEditing()
disableSceneRedraw()
with undo off
   
(
   for 
m in sceneMaterials do
      (
      
newObj Editable_Mesh()
      
convertTo newObj Editable_Poly
      newObj
.name "Object_" m.name
      
for o in geometry where (o.material == m) do
         
polyOp.attach newObj o
      centerPivot newObj
      
)
   )
enableSceneRedraw()
resumeEditing()
)


Max 4.2 through 2013.
XP-64 (SP2)
NVidia 9800GTX-512 (Driver 266.58).
Core 2 Quad Q6600 2.4GHz, 8Gb Ram, DX9.0c.

Replies: 0
avatar
  • nbac
  • Posted: 19 May 2011 12:15 AM

hi,

wow thank you for your fast response.
i think in principle this exactly what i need.
i run this on a simple test scene and getting an unknown system exception here.

polyOp.attach newObj o

any idea?

using max 2010
my test scene consits of 5 primitives and 2 scenematerials applied to those
so in the end there should be to editable polys

thank you!



Replies: 0
avatar
  • Location: West Midlands, England, UK
  • Total Posts: 14445
  • Joined: 06 August 2007 11:06 PM
  • Permlink of this post

I can only assume that you somehow have a scene material which is not actually applied to any geometric objects.

You should end up with an Editable Poly object for each material in the scene. It should attach objects which are not already Editable Polys because the Attach method is supposed to automatically convert the object to a Polymesh, so a Box primitive or even an Editable Mesh object should attach just fine. The code does work here, so if the modified version below doesn’t, then it will be difficult to work out what is wrong without having the actual scene to look at.

(
suspendEditing()
disableSceneRedraw()
with undo off
   
(
   for 
m in sceneMaterials do
      (
      
newObj Editable_Mesh()
      
convertTo newObj Editable_Poly
      newObj
.name "Object_" m.name
      objs 
= for o in geometry where (o.material == mcollect o
      
if objs.count 0 then
         
(
         for 
o in objs do
            (
            
polyOp.attach newObj o
            centerPivot newObj
            
)
         )
      )
   )  
enableSceneRedraw()
resumeEditing()
)


Max 4.2 through 2013.
XP-64 (SP2)
NVidia 9800GTX-512 (Driver 266.58).
Core 2 Quad Q6600 2.4GHz, 8Gb Ram, DX9.0c.

Replies: 0
avatar
  • nbac
  • Posted: 19 May 2011 01:16 AM

thank you very very very much!!!
you saved my day :) it is working ...

regards sebastian



Replies: 0
avatar
  • Location: West Midlands, England, UK
  • Total Posts: 14445
  • Joined: 06 August 2007 11:06 PM
  • Permlink of this post

Cool - glad it helped. :)



Max 4.2 through 2013.
XP-64 (SP2)
NVidia 9800GTX-512 (Driver 266.58).
Core 2 Quad Q6600 2.4GHz, 8Gb Ram, DX9.0c.

Replies: 0