assign default vray material


#1

i am looking for a script where i can quickly assign a material to a selected object . i would make a shortcut for the script

  1. default vray material with 50% grey diffuse
  2. chrome vray material with black diffuse and 90% reflections.

what would be the basic for assigning a material?

http://www.scriptspot.com/forums/3ds-max/general-scripting/create-and-assign-material-mr-why-its-duplicating
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 ) )


#2

Default vray material with 50% grey diffuse

(
 	-- Create a test object
 	obj = Sphere()
 	
 	-- Create a new VRay material
 	mat = VRayMtl()
 	
 	-- Change the Diffuse color to Gray (50%)
 	mat.Diffuse = [128,128,128]
 
 	-- Assign the VRay material to the Object
 	obj.Material = mat
 )

Chrome vray material with black diffuse and 90% reflections

(
 	-- Create a test object
 	obj = Sphere()
 	
 	-- Create a new VRay material
 	mat = VRayMtl()
 	
 	-- Change the Diffuse color to Black
 	mat.Diffuse = [0,0,0]
 	
 	-- Change the Reflection value to 90%
 	mat.Reflection = [255,255,255] * 0.9
 
 	-- Assign the VRay material to the Object
 	obj.Material = mat
 )

#3

i can insert this in the script in the first post?


#4

I’ve modified the function to assign “default” or “chrome” VRay material to the selection.
It accepts a parameter “type” to define which material you want (#chrome or #default).

fn AssignMat asCopy:false type:#default =
 (
 	if selection.count > 0 do
 	(
 		case type of
 		(
 			 #chrome: mat = VRayMtl Diffuse:[0,0,0] Reflection:[229,229,229]
 			#default: mat = VRayMtl Diffuse:[128,128,128]
 		)
 		for o in selection do o.Material = if asCopy then copy mat else mat
 	)
 )

#5

i wanted to add also a white 220/220/220 material. like this?

fn AssignMat asCopy:false type:#default =
(
if selection.count > 0 do
(
case type of
(
#chrome: mat = VRayMtl Diffuse:[0,0,0] Reflection:[229,229,229]
#default: mat = VRayMtl Diffuse:[128,128,128]
#white: mat = VRayMtl Diffuse:[220,220,220]
)
for o in selection do o.Material = if asCopy then copy mat else mat
)
)

can i now add this as a shortcut to a menu or toolbar?


#6

The #white option you added is correct. Now to make a shortcut for this is not as simple, as this function has 6 options, so you would need 6 different Macros (asCopy true/false and 3 materials) or 1 Macro with a popup window where you can select the options you want.

Here is a MacroScript that will open a popup dialog. You will find it in the Customize menu under Category “Medit Tools”. The Action name is “Assign VRay Material”

macroScript AssignVRayMaterial
 category:"Medit Tools"
 tooltip:"Assign VRay Material"
 (
 	
 	rollout ro_assign_material "Assign VRay Material" width:120 height:112
 	(
 		button bt_default "Default" pos:[8,8] width:104 height:24
 		button bt_chrome "Chrome" pos:[8,32] width:104 height:24
 		button bt_white "White" pos:[8,56] width:104 height:24
 		checkbox chk_asCopy "As Copy" pos:[8,88] width:104 height:16
 		
 		local AssignMat
 
 		fn AssignMat asCopy:false type:#default =
 		(
 			if selection.count > 0 do
 			(
 				case type of
 				(
 					 #chrome: mat = VRayMtl Diffuse:[0,0,0] Reflection:[229,229,229]
 					#default: mat = VRayMtl Diffuse:[128,128,128]
 					  #white: mat = VRayMtl Diffuse:[220,220,220]
 				)
 				for o in selection do o.Material = if asCopy then copy mat else mat
 			)
 		)		
 		
 		on bt_default pressed do AssignMat asCopy:(chk_asCopy.checked) type:#default
 		on bt_chrome pressed do AssignMat asCopy:(chk_asCopy.checked) type:#chrome
 		on bt_white pressed do AssignMat asCopy:(chk_asCopy.checked) type:#white
 	)
 		
 	createdialog ro_assign_material style:#(#style_titlebar, #style_toolwindow, #style_sysmenu)
 	
 )

#7

works. thank you


#8

the created materials have the same name ‘‘VRayMtl’’. what would i need to add in to the script so that the name is ‘‘material#number’’ like with other new crreated materials?


#9

i am trying to turn off IOR fresnel reflections on the chrome material

now its
#chrome: mat = VRayMtl Diffuse:[0,0,0] Reflection:[229,229,229]

i am trying like this
#chrome: mat = VRayMtl Diffuse:[0,0,0] Reflection:[229,229,229] Reflection_ior : boolean

but fresnel reflections are not unchecked.


#10

For that you would need to keep a global variable to track the number of materials created. Otherwise there could be a field in the UI where you can enter the name you want.
What would you like to do and how would you want the names be?


#11

Try some of the following:

-- Set the Fresnel Reflection IOR value to 0.0
  reflection_ior:0.0
  
 -- Turn Fresnel Reflection ON (checked)
 reflection_fresnel:on
  
 -- Turn Fresnel Reflection OFF (unchecked)
 reflection_fresnel:off

#12

Vraychrome
Vraygray
Vraywhite

for the IOR ''reflection_fresnel:off ‘’ worked. thank you.


#13

This will add the names with a 3 digits number, ie: “Vraywhite_001”. The numbers are stored in a global variable that will last until you close max or until you “reset” the variable. To reset the counter you can type this in the Listener:

vrnames=()

You can also add a button to do it if you would like.

macroScript AssignVRayMaterial
 category:"Medit Tools"
 tooltip:"Assign VRay Material"
 (
 	 
 	 rollout ro_assign_material "Assign VRay Material" width:120 height:112
 	 (
 		 button bt_default "Default" pos:[8,8] width:104 height:24
 		 button bt_chrome "Chrome" pos:[8,32] width:104 height:24
 		 button bt_white "White" pos:[8,56] width:104 height:24
 		 checkbox chk_asCopy "As Copy" pos:[8,88] width:104 height:16
 		 
 		 local AssignMat
 		global vrnames
  
 		fn IntToString pInt = formattedPrint pInt format:"03d"
 		
 		 fn AssignMat asCopy:false type:#default =
 		 (
 			if vrnames == undefined do vrnames = #(1,1,1)
 			
 			 if selection.count > 0 do
 			 (
 				 case type of
 				 (
 					 #chrome:
 					(
 						mat = VRayMtl Diffuse:[0,0,0] Reflection:[229,229,229] name:("Vraychrome_" + IntToString vrnames[1])
 						vrnames[1] += 1
 					)
 					 #default:
 					(
 						mat = VRayMtl Diffuse:[128,128,128] name:("Vraygray_" + IntToString vrnames[2])
 						vrnames[2] += 1
 					)
 					#white:
 					(
 						mat = VRayMtl Diffuse:[220,220,220] name:("Vraywhite_" + IntToString vrnames[3])
 						vrnames[3] += 1
 					)
 				 )
 				 for o in selection do o.Material = if asCopy then copy mat else mat
 			 )
 		 )
 		 
 		 on bt_default pressed do AssignMat asCopy:(chk_asCopy.checked) type:#default
 		 on bt_chrome pressed do AssignMat asCopy:(chk_asCopy.checked) type:#chrome
 		 on bt_white pressed do AssignMat asCopy:(chk_asCopy.checked) type:#white
 	 )
 		 
 	 createdialog ro_assign_material style:#(#style_titlebar, #style_toolwindow, #style_sysmenu)
 	 
 )

#14

the names work perfectly. thank you


#15

Hi PolyTools3D, Glossiness 0.5 how can i add? thank you


#16

I don’t have the answer off the top of my head (it’ll be something like glossinessAmount) but a quick way of finding out is you turn on the macrorecorder and then make the change you need manually in material editor, you’ll see the code you need


#17

Is it possible? thank you for answering


#18

Yes, turn on the macro recorder in the maxscript listener, do the change manually in material editor, then look at the maxscript listener.

It will show you the line of code you need to change glossiness, then you can add it to the script


#19

I didn’t understand what you said because unfortunately i can’t write code, so sorry :slight_smile: :cry:


#20

No probs, but I can’t help much I’m afraid - I don’t have Vray installed on my machine at the moment. If you can find out what the maxscript listener says, I can tell you where to put it in @PolyTools3D 's code.

You just have to go to Scripting->MacroRecorder to turn the recorder on.

Then open material editor, make a vray material, change the glossiness value or whatever it is you want to change.

Then open the maxscript listener (Scripting->Maxscript Listener) and look for any code that says something about glossiness. Copy and paste that here