[Closed] help with Name selection and random material script
Hey Guys, Im trying to write a script for work but I’m quite new to scripting. Wonder if anyone could help.
I’ll try to keep this simple…
What we have is sets of 4 people, i.e. Sandy, Ben, Sue and Jerry. The next set would be Sandy01, Ben01, Sue01, Jerry01 and so forth. THEN For each person we have 4 material variations, so lets say Sandy could have ONE material from the materials named Sandy, Sandy01, Sandy02, Sandy04 and the same for the other people. We need a script to add a random material from the ones that are associated with the characters name. I hope that makes sense, I tried to keep it simple and clear.
I’m having trouble selecting all the objects using characters name (although i think im close) I then am stuck a little on how to select a random material from the ones in the material editor with the characters name.
I’ve attached the code so far (with comments on my thought process), if anyone is able to help it will be much appreciated.
fn materialRandomPick basicNames: materials: =
(
-- use default names if the basic names are unsupplied:
if basicNames == unsupplied do basicNames = #("Sandy","Ben","Sue","Jerry")
-- use medit materials if the material list is unsupplied:
if materials == unsupplied do materials = for mat in meditmaterials where iskindof mat material collect mat
for name in basicNames do
(
-- collect all node with the basic name:
nodes = execute ("$'" + name + "'*")
-- if we have the nodes:
if nodes != undefined do
(
-- collect all materials with the basic name:
mats = for mat in materials where matchpattern mat.name pattern:(name + "*") collect mats
-- if we have so do random apply:
if mats.count > 0 do for node in nodes do node.material = mats[random 1 mats.count]
)
)
)
/*
[color=white]-- call it:[/color]
materialRandomPick basicNames:#("Sandy","Ben","Sue","Jerry") materials:meditmaterials
*/
Thankyou that helps alot. Just realised my script did not attach
My original code
rollout AssignRandomMaterial_rollout "Please Input A Name"
(
edittext Object_name ""
button assign_materials "ASSIGN MATERIALS..."
on assign_materials pressed do
--MAIN AIM: Clear selection so no objects are selected and then select all the objects beginning with the name the user input (Object_name)
(
clearselection()
--clear selection so nothing selected
Execute ("$'"+Object_name+"*'")
--select the objects beginning with the inputted name
)--end on
--MAIN AIM: Select all materials beginning with the name the user input (Object_name) and then select a random one
function getMaterials
(
--Object_materials = get all material slots using (Object_name+"*")
--select random Object_materials
)
--MAIN AIM: Loop around the selection adding a random material (hence the function getMaterials)
sel = selection as array
count = 1
undo on
(
if sel.count > 0 then
(
while count <= sel.count do
(
select sel[count]
count += 1
$.material = getMaterials()
)
)
)
)--end rollout
createDialog AssignRandomMaterial_rollout 250 50
I am having a little problem though, it happened when testing your script and parts of mine, it seems its having problems with the line :
nodes = execute "$'" + name + "'*"
from yours
The listener is saying:
-- No ""+"" function for undefined
I'm using 3ds Max 2011 x64 and also need it to work in 2010 x64 - am not sure if max is the problem though, gotta be my fault somewhere lol
edit: the listener reads
materialRandomPick()
-- Error occurred in name loop; filename: C:\Users\redwatty\Desktop
ewscript.ms; position: 472; line: 11
-- Frame:
-- mats: undefined
-- nodes: undefined
-- name: "Sandy"
-- called in materialRandomPick(); filename: C:\Users\redwatty\Desktop
ewscript.ms; position: 822; line: 20
-- Frame:
-- basicNames: #("Sandy", "Ben", "Sue", "Jerry")
-- materials: #meditMaterials()
-- No ""+"" function for undefined
thanks, so I called the function and it returned ok, no materials where applied though, am not too sure if they should be? I am now going to disect your script and work out everything you have done, its a little over my head as you can probably tell from my original script lol but this is all good for my learning and many many thanx
here is “your original” script with my correction:
rollout AssignRandomMaterial_rollout "Please Input A Name"
(
edittext object_name ""
button assign_materials "ASSIGN MATERIALS..."
function getMaterials name =
(
materials = for mat in meditmaterials where iskindof mat material and matchpattern mat.name pattern:(name + "*") collect mat
if materials.count > 0 do materials[random 1 materials.count]
)
on assign_materials pressed do undo "Assign Random Material" on
(
--MAIN AIM: collect all the objects beginning with the name the user input (object_name)
nodes = execute ("$'" + object_name.text + "'*")
--MAIN AIM: Loop around the node list and adding a random material (hence the function getMaterials)
if nodes != undefined do for node in (nodes as array) do node.material = getMaterials object_name.text
)
)--end rollout
createDialog AssignRandomMaterial_rollout 250 50
Thankyou Denis you just made my night, works perfectly, tested thoroughly. Thankyou so much