ScriptSpot

Forum topic · General Scripting

Create and assign material - mr - why its duplicating ?

By Dariusz · 2015-03-05

Description

Heya I want to create and assign material to selected object. But If I'm assigning the same material to 2 objects I'm creating 2 materials instead of assignign 1 mat to 2 objects. Can some1 look at my errors? 

[CODE]

fn mats = 

(

(

for o in getCurrentSelection() do (

try (

o.material = Arch___Design__mi ()

o.material.name ="TestMat"

o.material.diff_weight = 0.02

o.material.refl_weight = 1

o.material.refr_weight = 0.9

o.material.refl_func_fresnel = on

o.material.opts_1sided = on

o.material.opts_propagate_alpha = on

o.material.opts_refl_depth = 10

o.material.opts_refr_depth = 10

  ) catch()

)

)

)

 

mats()

[\CODE]

Comments (7)

Dariusz · 2015-03-05

Yeah

I thought I would script in python, but looking how little info of it is on internet I decided to move back to maxscript :(

barigazy · 2015-03-05

Python scripting is very similar to mxs. But I not found any use in max for now.
.NET is very well integrated in max. I hope that AD will add Python IDE to work inside max side by side with mxs editor

Dariusz · 2015-03-05

Wow that was simple... damn thanks ! Ure the best! :- ))))

barigazy · 2015-03-05


fn matAssign matName diffWeight = if selection.count != 0 do
(
material = if (sm = sceneMaterials[matName]) != undefined then sm else
(
Arch___Design__mi name:matName diff_weight:diffWeight refl_weight:1 refr_weight:.9 \
refl_func_fresnel:on opts_1sided:on opts_propagate_alpha:on opts_refl_depth:10 opts_refr_depth:10
)
for o in selection do o.mat = material
)
matAssign "test" 0.7

Dariusz · 2015-03-05

Hey

Thanks!

So this "almost" (:-))works great if I select all objects and assign a material. But if I select 3 objects, assign and then another 2 objects and assign I'm still getting duplicated material.

I was thinking that I need an if statement so




function matAssign matName diffWeight = 
(
if sceneMaterials[matName]  != undefinied then
(
	for o in getCurrentSelection() do (
	o.material = sceneMaterials [matName]
	)
)
else
	for o in getCurrentSelection() do (
	(
		material = Arch___Design__mi name:matName diff_weight:diffWeight refl_weight:1 refr_weight:.9 \
		refl_func_fresnel:on opts_1sided:on opts_propagate_alpha:on opts_refl_depth:10 opts_refr_depth:10
		-- "asCopy" argument in the function mean that you have choise to assigne material as instance or copy
		for o in selection do o.mat = if asCopy then (copy material) else material
	)
	)
)


matAssign "test" 0.7

That works if material exists but the else function fails to create material if it dont exists.

Also this test :


if sceneMaterials["aaab"]  != undefinied then
(
	print "mat exists"
)

Also this test function seems to have a weird problem.

If I DONT have the material in scene I get undefined. But when I have the material in scene on 1st execution I get undefined and on second execution I get "mat exists" printed. Is that normal that I need to run it 2x so that I get accurate value? :-(

I come from python scripting pipeline. I did MS for max 5 years ago and the code I posted originally worked for Vray just fine. Its frustrating how MR works :(

Can you hint me out how to fix the if statement? I want to avoid duplication of materials.

barigazy · 2015-03-05


fn assignMat asCopy:off = 
(
	if selection.count > 0 do
	(
		material = Arch___Design__mi name:"TestMat" diff_weight:.02 refl_weight:1 refr_weight:.9 \
		refl_func_fresnel:on opts_1sided:on opts_propagate_alpha:on opts_refl_depth:10 opts_refr_depth:10
		-- "asCopy" argument in the function mean that you have choise to assigne material as instance or copy
		for o in selection do o.mat = if asCopy then (copy material) else material
	)
)

assign material as copy to all selected objects


assignMat asCopy:on


assign material as instance to all selected objects


assignMat()