looking for a script !!! Please help...

Hello Guys,

I’m looking for a script…

I want to select objects and create a multisub material (without attaching the objects…).

Best regards…

myobjs = $
combined_material = Multimaterial count:myobjs.count name:"combined_material"
for i = 1 to myobjs.count do
(
mymodifier = (Materialmodifier ())
mymodifier.materialID = i
addmodifier myobjs[i] mymodifier
combined_material.mapenabled[i] = on
combined_material.materialIDList[i] = i
combined_material.names[i] = myobjs[i].material.name
combined_material.material[i] = myobjs[i].material
myobjs[i].material = combined_material
)

better save your scene first… it adds a material modifier to your selected objects (no errorchecking if anything else than geometry is selected) and creates a multisub with instances of the original material in it. did not put that to the medit, gotta pick it yourself :wink:

regards,
michael

Ok, thank you very much but I’m looking for a script able to do this enevn if there is an existing multi/sub on selected object…

I don’t know if I’m very clear…

Best regards…

Ok here is a quick one. No checking for invalid selection:


(
	sel = selection as array
	mat = multimaterial numsubs:(sel.count)
	for i=1 to sel.count do
	(
		m = materialmodifier()
		m.materialID = i
		addmodifier sel[i] m
		mat.names[i] = sel[i].name
		sel[i].material = mat
	)
)

Just select your objects and it will create the multisub and apply it.

Best regards,
Daniel

I’m assuming you want to create a multi-sub material that contains all of the materials in your selection AND changes the face ID of your objects to match.

As a warning - I am rubbish at scripting, so I simply don’t know how to do what you want without collapsing the objects to a mesh (to change the ID). It could probably be done by adding an edit mesh modifier & making the changes to the face ID’s in that, but again… I am rubbish.

So, here’s my quick script that *might* do what you want… (But will most likely just crash).

Hope it’s of any help!


matstore = #()
matnum = selection.count
mm = multimaterial numsubs:matnum
for i in selection do (append matstore i.material)
converttomesh selection
for i = 1 to matnum do 
	(
	for s = 1 to selection[i].numfaces do 
		(
		setfacematID selection[i] s i
		)
	mm[i] = matstore[i]
	)
selection.material = mm

Hihi,

this makes things more complicated :slight_smile:
But let’s accelerate development a little bit…
I bet Sushidelic can’t do this in under 30 minutes… :smile:
Time runs…

Best regards,

Dieter

gimme some more minutes, gotta output a fusion comp first :wink:

Sorry it doesn’t work, wath I want to do is creating an only one multi/sub with selected object… And some of this object have a Multi/Sub assigned…

Sorry it doen’t work… He create a multisub with multisub inside…

Best regards…

try this


(
	maxindex = 0
	sel = selection as array
	mat = multimaterial numsubs:(sel.count) 
	-- first pass
	for i=1 to sel.count do
	(
		m = edit_mesh()
		m.name = "TmpModifi"
		addmodifier sel[i] m
		for j=1 to sel[i].numfaces  do
		(
			mtindex = getFaceMatID sel[i] j
			if mtindex > maxindex then maxindex = mtindex
		)
	)
	-- second pass
   for i=1 to sel.count do
   (
      m = materialmodifier()
      m.materialID = maxindex + i
      addmodifier sel[i] m
      mat.names[i] = sel[i].name
	  mat.materialIDList[i] = maxindex + i
	  mat.materialList[i] = sel[i].material
      sel[i].material = mat
   ) 
)

Hello Thanks…

But I’m sorry the script put all the multisud of each object into a new multisub…

On each ID of the new multisub I found others multiSub…

Best Regards…

Yes only after putting the last script did i had an idea on how to make this work. it will take a while to make the necessary changes.

best regards,
daniel

Ok i’ll wait…

For now I’ll do it with my hands… but I hope you can find the answer…

Best regards and thank you to all of you guys…

PS : Sorry my english sucks !!!

Sorry for the delay, but this one should work. The only side effect is collapsing the modifier stack for objects with multisubs.


( 
   mtlindex = 0
   mtllist = #()
   mtlname = #()
   mtlcount = #()
   sel = selection as array
   mat = multimaterial numsubs:10
   -- first pass 
   for i=1 to sel.count do 
   ( 
   		if (classof sel[i].material) == multimaterial then
   		(
	   		selmtlcount = 0
			selindex = #()
	   		tindex = mtlindex
	   		md = edit_mesh() 
		    md.name = "MultMat" 
		    addmodifier sel[i] md
			addmodifier sel[i] (mesh_select())
			collapseStack sel[i]
		    for j=1 to sel[i].numfaces  do 
		    ( 
		       facindex = getFaceMatID sel[i] j
		       found = finditem selindex facindex
		       if found != 0 then (setFaceMatID sel[i] j (tindex + found))
		       else
		       (
		       		mtlindex += 1
		       		append selindex facindex
		       		setFaceMatID sel[i] j mtlindex
		       )
		    )
		    append mtlcount (selindex.count)
		    for j=1 to selindex.count do
		    (
		    	found = finditem (sel[i].material.materialIDList) selindex[j]
				if found == 0 then append mtllist (NoMaterial())
		      	else append mtllist (sel[i].material.materialList[found])
				append mtlname (sel[i].name + "_" + (j as string))
		    )
   		)
   		else
   		(
   			mtlindex += 1
   			append mtllist (sel[i].material)
   			append mtlcount 1
	      	md = materialmodifier() 
	      	md.name = "MultMat"
	      	md.materialID = mtlindex
	      	addmodifier sel[i] m
			append mtlname (sel[i].name)
    	)
    	sel[i].material = mat
   )
   mat.numsubs = (mtllist.count)
   -- second pass 
   for i=1 to mtllist.count do 
   (
        mat.names[i] = mtlname[i] 
     	mat.materialIDList[i] = i 
    	mat.materialList[i] = mtllist[i]
   ) 
) 

Best regards,
Daniel

YYYYEEEEEEAAAAAAAAA !!!

It’s working !!!
Very nice…

Many many thanks…
I put your name on my “wonderful guy’s list” !!!

Just one more think… Is it possible to keep instances ???

Best regards and a big thank you !!!

Instances of materials, or object instances?

Best regards,
Daniel

Sorry Object Instances…

By the way you’ve done an amazing work…

Best regards…

It will take some time but it’s possible.

Best regards,
Daniel

Ok i’ll wait…

Now I can work with the existing one..

It is allready awsome

Ok here it is one with support for instance.


[size=9]
( 
   mtlindex = 0
   mtllist = #()
   mtlname = #()
   mtlcount = #()
   allsel = selection as array
   mat = multimaterial numsubs:10
   -- scan for unique objects and create an array with them
   for obj in allsel do
   (
      instances = #()
      instanceObjs = #()
      inst = false
      instancemgr.getinstances obj &instances
      if instances.count == 1 then inst = false
      else
      (
        --append sel instances[1]
        obj = instances[1]
        inst = true
        for j = 2 to instances.count do
        (
          found = finditem allsel instances[j]
          if found != 0 then
          (
          	deleteItem allsel found
          	h = point pos:(instances[j].pos) scale:(instances[j].scale) rotation:(instances[j].rotation) name:("point_"+instances[j].name)
          	append instanceObjs h
          	delete instances[j]
          )
        )
      )
      if (classof obj.material) == multimaterial then
      (
        selmtlcount = 0
        selindex = #()
        tindex = mtlindex
        md = edit_mesh() 
        md.name = "MultMat"
        addmodifier obj md
        collapsestack obj
        --maxOps.CollapseNodeTo sel[i] (sel[i].modifiers.count) off
        --if sel[i].modifiers.count > 0 then (format "Cannot handle modifiers.\n";break)
        --if (classof sel[i])==Editable_Mesh then (format "Not editable mesh.\n";break)
        for j=1 to obj.numfaces  do 
        ( 
           facindex = getFaceMatID obj j
           found = finditem selindex facindex
           if found != 0 then (setFaceMatID obj j (tindex + found))
           else
           (
              mtlindex += 1
              append selindex facindex
              setFaceMatID obj j mtlindex
           )
        )
        append mtlcount (selindex.count)
        for j=1 to selindex.count do
        (
          found = finditem (obj.material.materialIDList) selindex[j]
          if found == 0 then append mtllist (NoMaterial())
          else append mtllist (obj.material.materialList[found])
          append mtlname (obj.name + "_" + (j as string))
        )
      )
      else
      (
        mtlindex += 1
        append mtllist (obj.material)
        append mtlcount 1
        md = materialmodifier() 
        md.name = "MultMat"
        md.materialID = mtlindex
        addmodifier obj md
        append mtlname (obj.name)
      )
      obj.material = mat
      if inst then
      (
      	for i in instanceObjs do
      	(
      		o = instance obj
      		o.name = substring (i.name) 7 (i.name.count-7)
					o.rotation = i.rotation
					o.scale = i.scale
					o.pos = i.pos
					delete i
      	)
      )

   )
   mat.numsubs = (mtllist.count)
   -- second pass 
   for i=1 to mtllist.count do 
   (
      mat.names[i] = mtlname[i] 
      mat.materialIDList[i] = i 
      mat.materialList[i] = mtllist[i]
   ) 
) 
[/size]

I glade that i could help.
P.S. Use it at your one risk, i cannot be help responsible if something goes really wrong.

Best regards,
Daniel