Notifications
Clear all

[Closed] Anyone know a script to Seperate all SUBObject IDs into new objects?

Hi there…

Dose anyone know of any script around tha place that can read a meshin max and see all the material ids, then detach each ID into a new mesh file?

Thanks in Advance.

–The Jynks

3 Replies

i wrote this in a few minutes, haven’t tried it with complex objects but it works for the time being. i may alter the code later, or include a gui maybe.

--name the object you want to detach using material IDs
OBJ = $Box01

--count materialIDs...
materialIDs = #()
totalfaces = polyop.getNumFaces OBJ
for thisFace = 1 to totalfaces do (
		facemapID = polyop.getFaceMatID OBJ thisFace
		appendIfUnique materialIDs facemapID
)

--for each material id...
for thismapID = 0 to materialIDs.count do
(
	totalfaces = polyop.getNumFaces OBJ 
	faceArray = #()
	for thisFace = 1 to totalfaces do (
		facemapID = polyop.getFaceMatID OBJ thisFace
		if facemapID == thismapID then (
			appendIfUnique faceArray thisFace
		)
	)
	
	polyop.detachFaces OBJ faceArray delete:true asNode:true name:("Detached"+thismapID as String)
)

-- as no face remained, we are deleting the empty object
delete OBJ
-- disable this if you have hidden faces

just change the first line “OBJ = $Box01” with your objects name…

i don’t know if there is a poly function to find the materialID count (not material count). so, script looks at each face and finds unique matIDs. so if you are working with high poly counts, this script might take some time to process.

also, the object has to be a poly

ed: this just detaches the faces as new polys. did you wanted to save them as max files ?

i found a few errors in my logic on that script. for once, i was keeping the number of materialIDs, but not the matID’s themselves, so an object may have matIDs {3,4,5}, but the script will think those as {0,1,2}.

to solve the problem, i used the array instead of the count.

plus, i added a simple gui. the code is a little dirty and still this is for poly only, not enough free time
maybe someone can modify it for editable mesh or patch…

materialIDs = #()
SelectedOBJ = ""

function DetachMatIDOne obj vminid vmaxid = (
  undo on
  (
    select obj
    for thismapID = vminid to vmaxid do
    (
      totalfaces = polyop.getNumFaces obj
      faceArray = #()
      for thisFace = 1 to totalfaces do
      (
        facemapID = polyop.getFaceMatID obj thisFace
        if facemapID == thismapID then
        (
          appendIfUnique faceArray thisFace
        )
      )
      polyop.detachFaces obj faceArray delete:true asNode:true name:(obj.name + "_ID_"+thismapID as String)
      obj.wirecolor = [random 0 254,random 0 254,random 0 254]
    )
    deselect obj
    
    if vminid == 1 then
    (
      if vmaxid == (amax materialIDs) then (
        delete obj
      )
    )
  )
)

rollout DetachMatIDs "Detach MaterialIDs" width:200 height:340
(
  pickButton picker "Pick" pos:[10,15] width:35 height:15
  label picked "<-- pick an object" pos:[52,16] width:110 height:15
  label idcount "MaterialID Count: 0" pos:[10,40] width:145 height:15
  spinner minid "Min ID :" pos:[20,70] width:85 height:16 type:#integer fieldWidth:45 range:[0,0,0]
  spinner maxid "Max ID :" pos:[20,90] width:85 height:16 type:#integer fieldWidth:45 range:[0,0,0]
  button detachsub "Detach using MatIDs" pos:[10,120] width:125 height:25
  
  on picker picked obj do
  (
    if (classof obj) == Editable_Poly then
    (
      materialIDs = #()
      picked.text = obj.name
      SelectedOBJ = obj
      select obj
      totalfaces = polyop.getNumFaces obj
      for thisFace = 1 to totalfaces do
      (
          facemapID = polyop.getFaceMatID obj thisFace
          appendIfUnique materialIDs facemapID
      )
      sort materialIDs
      deselect obj
      
      idcount.text = "MaterialID Count: "+((amax materialIDs) as string)
      minid.range = [1,(amax materialIDs),1]
      maxid.range = [1,(amax materialIDs),(amax materialIDs)]
    ) else (
      messagebox "You must select an editable poly object." title:"Detach MatIDs"
    )
  )
  
  on minid changed newValue do ( if newValue >= maxid.value then maxid.value = newValue )
  on maxid changed newValue do ( if newValue <= minid.value then minid.value = newValue )
  
  on detachsub pressed do DetachMatIDOne SelectedOBJ minid.value maxid.value
)

DMIDFloater = newRolloutFloater "emrahgunduz.com" 160 200
addRollOut DetachMatIDs DMIDFloater

wrote the script in max 2008, some methods may not be supported in earlier versions.

thank you… I havn’t really had many problems with your thing so far. Cheers.