ScriptSpot

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 · 2013-03-21

It works thanks so much :)

Swordslayer · 2013-03-21

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

barigazy · 2013-03-21

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

:) I know.

barigazy · 2013-03-21

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

Claviateur · 2013-03-21

It works thanks so much :)

titane357 · 2013-04-01

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.

Try with this:

miauu · 2013-04-01


seed (timestamp())
randVal = random 0 100 -- or what you need

titane357 · 2013-04-02

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

miauu · 2013-04-02

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 · 2013-04-02

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 · 2013-04-02

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

Claviateur · 2013-03-20

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