ScriptSpot home

Forum topic · Scripts Wanted

Apply random material to element

By Claviateur · 2013-03-20

Description

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 (13)
Claviateur

It works thanks so much :)

Vojtech Cada

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

Reply to Vojtech Cada
Branko Živković

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

Branko Živković

:) 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

Reply to Branko Živković
titane357

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.

Reply to titane357
miauu

Try with this:


seed (timestamp())
randVal = random 0 100 -- or what you need
Reply to miauu
titane357

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

Reply to titane357
miauu

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.

Reply to miauu
titane357

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

Reply to Branko Živković
Claviateur

Thanks, I already tried this one however this technique creates as many IDs as we have sub objects in a mesh but what I need is to apply a limited amount of textures (let's say 25) randomly to the subobjects. The line of script I included does just that but for independant objects and not elements of a mesh

Thanks