[Closed] Material ID / Object ID randomizer?
Hey guys im really a novice to maxscript
and it should be really simple to write some code for that purpose
but im not an expert for this stuff.
I need a little script in order to
randomize my objects MaterialId / ObjectID.
–
max select all
for i in selection do i.gbufferchannel = random 1 100″
Also i dont want that any object have the same MatId/ObjId number.
Any hints are appreciated.
NAIK
material ID is face property. If you want to apply some material ID to all faces of an object you can use for example Material Modifier…
so the code might be
for id=1 to objects.count while id < 65536 do
(
obj = objects[id]
append ids id
addmodifier obj (MaterialModifier materialID:id)
)
-- or if want to use random
ids = #{1..65535} as array
for k=1 to objects.count while k < 65536 do
(
obj = objects[k]
id = ids[random 1 ids.count]
deleteitem ids id
addmodifier obj (MaterialModifier materialID:id)
obj.gbufferChannel id
)
hey mate,
what is Obj ID I have no idea. Every node in scene has a unique handle <node>.INode.handle (see Node Handles in MXS help),
right click any object in the scene.
Under the Rendering Control you got
the slot G-Buffer – Object ID.
cheers
NAIK
My little tiny code is workign fine so far.
But i need to implement some decent code
for checking if one Object Id has already been
given to an object.
So that every object has an unique Object ID.
For Render Elements ( Object ID ) as an example…
cheers
NAIK
Thanks for the code mate,
but its not working 100%…
i get this error message
– Syntax error: at ), expected <factor>
– In line: )
Also, i think it would be better just to randomize the
ObjID instead of adding a modifier for the MaterialID.
best regards
NAIK
ids = #{}
needID = #()
for obj in objects do if (finditem ids (id = obj.gbufferChannel)) == 0 then append ids id else append needID obj
ids = (#{1..65535} - ids) as array
for k=1 to needID.count while k < (65536 - ids.count) do
(
obj = needID[k]
id = ids[1]
deleteitem ids 1
-- addmodifier obj (MaterialModifier materialID:id)
obj.gbufferChannel = id
)
Sorry mate,
when i copy your code to the Maxscript window
and select all line and evaluate it
i get this
#{}
#()
OK
#(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, …)
OK
OK
Seems cool, but when i select one object and check its Object Id its still
0 – and thats for all objects in the scene…
Also when i copy the code into the Maxscript Listener
i get again this error message
– Syntax error: at ), expected <factor>
– In line: )
Am i nuts?
we have to check for ZERO id:
ids = #{}
needID = #()
for obj in objects do if (id = obj.gbufferChannel) == 0 or (finditem ids id) != 0 then append needID obj else append ids id
ids = (#{1..65535} - ids) as array
for k=1 to needID.count while k < (65536 - ids.count) do
(
obj = needID[k]
id = ids[1]
deleteitem ids 1
-- addmodifier obj (MaterialModifier materialID:id)
obj.gbufferChannel = id
)
here is a clean version:
ids = #{}
needID = #()
for obj in objects do if (id = obj.gbufferChannel) == 0 or (finditem ids id) != 0 then append needID obj else append ids id
ids = (#{1..65535} - ids) as array
while ids.count > 0 and needID.count > 0 do
(
needID[1].gbufferChannel = ids[1]
deleteitem ids 1
deleteitem needID 1
)