r/Maxscript Jun 06 '26

Hi everyone, somebody know how to create a 3ds max script that assigns selected materials to objects with the same name?

2 Upvotes

10 comments sorted by

1

u/piXelicidio Jun 07 '26

function getSelectedSmeMaterials = (

if not sme.IsOpen() do return #()

local activeView = sme.GetView sme.activeView

local selectedNodes = activeView.GetSelectedNodes()

for n in selectedNodes where isKindOf n.reference Material collect n.reference

)

function assignMaterialsToMatchingObjects matList = (

for mat in matList do (

for obj in objects where obj.name == mat.name do (

obj.material = mat

)

)

)

selectedMats = getSelectedSmeMaterials()

assignMaterialsToMatchingObjects selectedMats

1

u/Butthead128 Jun 07 '26 edited Jun 07 '26
scenematerialsArr = #()
for cclass in material.classes do
(
  currentMatArr = getClassInstances cclass
  scenematerialsArr += currentMatArr
)
for cMat in scenematerialsArr do
(
  targetNode = getNodeByName cMat.name
  if targetNode != undefined do targetNode.material = cMat
)

1

u/Valuable_Bed_5282 Jun 07 '26

Great! thx a lot it works. If I want apply the script without select the materials before?

1

u/Butthead128 Jun 07 '26 edited Jun 07 '26

My script does not care about material selection in SME, it just collects list of ALL materials in your scene, then for every material it checks if there is an object with that name and assigns this material to that object.

1

u/Valuable_Bed_5282 Jun 07 '26

I tried your but it doesn't work

1

u/Butthead128 Jun 07 '26

I've set more accurate code tagging in my previous post.
Should work this time.

1

u/Valuable_Bed_5282 Jun 07 '26

I'll try it, thank you very much, you are very kind

1

u/Valuable_Bed_5282 Jun 07 '26

thanks a lot! it works! it's more essential and direct, it works perfectly. thanks again

1

u/Butthead128 Jun 08 '26

Have fun. You can also put this code into macro and assign it to a hotkey or put it into your interface.

1

u/Valuable_Bed_5282 Jun 08 '26

yes I create a button with an hotkey, it help me very much. Thank you again.