Apply random material to element

Hi,

I found on this forum the following script that works very well when applying a random amount of materials to selected objects

Now if I have a single mesh with many sub objects in it (cubes for example) that each can be selected with the mesh / element selection, how shall I modify the script to make it apply the random materials to the elements of the mesh?

I presume the "obj" should be swtiched to become refering to the element instead?

Script:
for obj in selection do obj.material = meditMaterials[random 1 4]

thanks

Comments

Comment viewing options

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

It works thanks so much :)

It works thanks so much :)

Swordslayer's picture

I might be missing something

I might be missing something obvious but why not just use the MaterialByElement modifier?

barigazy's picture

Yep. For E-Mesh objects are

Yep. For E-Mesh objects are better to use the MaterialByElement modifier

barigazy

barigazy's picture

:) I know.

Then try this fn. NOTE: Works only with E-Poly object without assigned modifiers.
Mesh objects are calculates slower because num of triangle faces.

fn randomMID idRange:#(1,10) = if selection.count != 0 do
(
	local pogeuf = polyOp.getElementsUsingFace, posfmid = polyop.setFaceMatID
	with redraw off 
	(
		for sel in selection where isKindOf sel Editable_Poly and sel.modifiers.count == 0 do
		(
			local fNumList = #{}, EuF  = #{}
			for fp = 1 to sel.numfaces where fNumList[fp] == false do
			(
				EuF = pogeuf sel fp
				posfmid sel EuF (random idRange[1] idRange[2])
				for f in EuF do fNumList[f] = true
			)
		)
	)
)
randomMID idRange:#(1,6) -- change mtlID range array

barigazy

titane357's picture

Thanks Barygazy, very

Thanks Barygazy, very usefull...
One problem is that the max's random function don't "really" return random value.
Is there a way to make more random values ?
Thanks.

miauu's picture

Try with this:

seed (timestamp())
randVal = random 0 100 -- or what you need
titane357's picture

hello miauu, I don't

hello miauu,
I don't understand what "seed (timestamp())" is used to ?

miauu's picture

Open the maxscreipt help file

Open the maxscreipt help file and search for:
Mathematical Operations
Then read at the bottom of the page:
For reasons beyond the scope of this tutorial, the random command will generate the same "random" numbers each time a script is run. This happens if you restart the software and run the script, but not if you run the script over and over. If you want the values created by the random function to change each time you start the software, you can use the seed command:

seed where is any float or integer. Each time you change the seed value, MAXScript generates a new set of "random" numbers.

About timestamp()
The function returns the number of milliseconds since 00:00 hours that day. This is useful for timing operations (presuming you are not timing across midnight.)

So, using seed timestamp() you will have unique seed values every time when you wants to generates random values.

titane357's picture

oooookkkkkeeeyy Thanks for

oooookkkkkeeeyy
Thanks for explanation miauu. The seed wasn't obvious, and I didn't found it in maxscript help...
But in result I don't find that random fn give a "real" randomization,...
cheers

barigazy's picture

For "seed" look in "Array

For "seed" look in "Array Value" topic in mxsHelp

barigazy

Comment viewing options

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