Maxscript help

I’m trying to make a script that sets up a few simple things for me. I could do it as Maxstart or use a few of several free scripts out there, but i’d rather do it myself and learn a bit. All i’m trying to do is set some values and create some things on script run. My current questions are:

- Howcome i’m getting (below)?


-- Error occurred in anonymous codeblock; filename: C:\Documents and Settings\George\Local Settings\Application Data\Autodesk\3dsMax\2010 - 64bit\enu\UI\usermacros\Scene Presets-ScenePresetDayExterior.mcr; position: 3445; line: 117
-- Syntax error: at if, expected <rollout clause>
--  In line: 				if c

Here’s the icons if anyone wants em. Pop them in 3dsmax/ui/icons and you can use them anywhere.

http://www.dukecg.net/Preset\_Icons.zip


macroScript ScenePresetDayExterior
	category:"Scene Presets"

	(
		rollout scenePresetRollout "ScenePreset" width:160 height:128 
		(
			checkbox chk_RenderSettings "Checkbox" pos:[8,8] width:16 height:16 checked:true
			checkbox chk_SunSettings "Checkbox" pos:[8,32] width:16 height:16 checked:true
			checkbox chk_CameraSettings "Checkbox" pos:[8,56] width:16 height:16 checked:true
			checkbox chk_SkySettings "Checkbox" pos:[8,80] width:16 height:16 checked:true

			label lbl_RenderSettings "Render Settings" pos:[32,8] width:120 height:16
			label lbl_SunSettings "Sun Settings" pos:[32,32] width:120 height:16
			label lbl_CameraSettings "Camera Settings" pos:[32,56] width:120 height:16
			label lbl_SkySettings "Sky Settings" pos:[32,80] width:120 height:16

			button btn_Load "Load" pos:[8,104] width:144 height:16

			on btn_Load pressed do
				fn RenderSettings = 
				(
					vr = renderers.current

					vr.output_on = true

					vr.options_hiddenLights = false
					vr.options_defaultLights = 2
					vr.options_limitDepth = true
					vr.options_maxDepth = 6
					vr.options_ray_bias = 0.005

					vr.imageSampler_type = 0
					vr.filter_on = false
					vr.fixedRate_subdivs = 2
					vr.twoLevel_baseSubdivs = 1
					vr.twoLevel_fineSubdivs = 16
					vr.twoLevel_threshold = 0.01
					vr.twoLevel_useDMCSamplerThresh = false

					vr.colorMapping_type = 6
					vr.colorMapping_gamma = 2.2
					vr.colorMapping_affectBackground = true
					vr.colorMapping_subpixel = true

					vr.gi_on = true
					vr.gi_refractCaustics = false
					vr.gi_reflectCaustics = false
					vr.gi_primary_type = 0
					vr.gi_secondary_type = 2

					vr.gi_irradmap_minRate = -5
					vr.gi_irradmap_maxRate = -2
					vr.gi_irradmap_colorThreshold = 1.0
					vr.gi_irradmap_normalThreshold = 0.3
					vr.gi_irradmap_distThreshold = 0.2
					vr.gi_irradmap_showCalcPhase = true
					vr.gi_irradmap_showDirectLight = true

					vr.dmcgi_subdivs = 8
					vr.dmcgi_depth = 3

					vr.dmc_earlyTermination_amount = 0.95
					vr.dmc_earlyTermination_threshold = 0.008

					vr.system_region_x = 32
					vr.system_region_y = 32
					vr.system_lowThreadPriority = true
				)

				fn SunSettings = 
				(
					fn GetSun = 
					(
						vrSuns = for item in lights where ( (classof item == VRaySun) ) collect item
						if vrSuns.count == 0 then vrSun = VRaySun()
						else vrSun = vrSuns[1]
					)

					vrSun = GetSun()

					vrSun.enabled = true
					vrSun.invisible = true
					vrSun.turbidity = 2.0
					vrSun.shadow_subdivs = 6
					vrSun.shadow_bias = 0.0

					if vrSun.pos == [0,0,0] then vrSun.pos = [-100, -100, 100] 
					vrSun.target = targetObject pos:[0,0,0]
				)

				fn CamSettings = 
				(
					fn GetCam = 
					(
						vrCams = for item in cameras where ( (classof item == VRayPhysicalCamera) ) collect item
						if vrCams.count == 0 then vrCam = VRayPhysicalCamera()
						else vrCam = vrCams[1]
					)

					vrCam = GetCam()

					vrCam.targeted = false
					vrCam.f_number = 5.6
					vrCam.vignetting = false
					vrCam.shutter_speed = 320
					vrCam.ISO = 65
					vrCam.whiteBalance = [230, 246, 255]

					if vrCam.pos == [0,0,0] then vrCam.pos = [0, -10, 1.7]
				)

				fn SkySettings =
				(
					if environmentMap == undefined then environmentMap = VRaySky()
				)

				if chk_RenderSettings.checked then RenderSettings()
				if chk_SunSettings.checked then SunSettings()
				if chk_CameraSettings.checked then CamSettings()
				if chk_SkySettings.checked then SkySettings()
			)
		)

a few notes:

-) you can’t call a function before it is declared.

-) you must not use the names of fixed constants (“VRayPhysicalCamera”) as variable names for function calls.

i removed the cam stuff, because I’m not sure what you are trying to achieve…like what happens if there are multiple cams.

with the sun, usually there is 1 sun only, so the script only recognizes the first found sun. can be expanded easily.

fixed version:


(
	On Execute Do
	(

		fn RenderSettings = 
		(
			vr = renderers.current

			vr.output_on = true

			vr.options_hiddenLights = false
			vr.options_defaultLights = 2
			vr.options_limitDepth = true
			vr.options_maxDepth = 6
			vr.options_ray_bias = 0.005

			vr.imageSampler_type = 0
			vr.filter_on = false
			vr.fixedRate_subdivs = 2
			vr.twoLevel_baseSubdivs = 1
			vr.twoLevel_fineSubdivs = 16
			vr.twoLevel_threshold = 0.01
			vr.twoLevel_useDMCSamplerThresh = false

			vr.colorMapping_type = 6
			vr.colorMapping_gamma = 2.2
			vr.colorMapping_affectBackground = true
			vr.colorMapping_subpixel = true

			vr.gi_on = true
			vr.gi_refractCaustics = false
			vr.gi_reflectCaustics = false
			vr.gi_primary_type = 0
			vr.gi_secondary_type = 2

			vr.gi_irradmap_minRate = -5
			vr.gi_irradmap_maxRate = -2
			vr.gi_irradmap_colorThreshold = 1.0
			vr.gi_irradmap_normalThreshold = 0.3
			vr.gi_irradmap_distThreshold = 0.2
			vr.gi_irradmap_showCalcPhase = true
			vr.gi_irradmap_showDirectLight = true

			vr.dmcgi_subdivs = 8
			vr.dmcgi_depth = 3

			vr.dmc_earlyTermination_amount = 0.95
			vr.dmc_earlyTermination_threshold = 0.008

			vr.system_region_x = 32
			vr.system_region_y = 32
			vr.system_lowThreadPriority = true
		)

		fn SunSettings = 
		(		
			fn GetSun = 
			(
				scene_vray_suns = for item in lights where ( (classOf item == VRaySun) ) collect item  --scan all lights for vraylights, collect in array
				if scene_vray_suns.count==0 then vrSun = VRaySun()  --create new sun, if array is empty (no suns found)
				else vrSun = scene_vray_suns[1] --if array is not empty (suns in scene), use first sun in array

				return vrSun --return the sun, otherwise fuctions returns nothing
			)
			vrSun = GetSun() --call function

			vrSun.enabled = true
			vrSun.invisible = true
			vrSun.turbidity = 2.0
			vrSun.shadow_subdivs = 6
			vrSun.shadow_bias = 0.0

			vrSun.pos = [100, 100, 100]
			vrSun.target =targetObject pos:[0,0,0]
		)

		RenderSettings()
		SunSettings()
	)
)

It’s not a great script, just a fixed version of yours :wink:
right now, the functions are more or less useless, because they are only called 1 time. It gets more interesting when you use function in loops, to manipulate multiple objects, etc.

another note: right now it’s a bit confusing because the “vrSun” variable is called the same from outside and inside the function, but it’s important to know that they are totally different variables. that’s why the return call is needed. a variable defined in a function is “local”, it’s invisible from the outside, even if it has the same name as an outside variable.

Vray Sky is easy:


environmentMap = VRaySky ()

you can often find out how things work in maxscript by looking at the macro recorder.

Thanks plastic! I’ve updated my script in the first post :slight_smile:

watch the brackets…some cleanup:


macroScript ScenePresetDayExterior
category:"Scene Presets"
(
	rollout scenePresetRollout "ScenePreset" width:160
	(
		checkbox chk_RenderSettings "Render Settings" checked:true
		checkbox chk_SunSettings "Sun Settings" checked:true
		checkbox chk_CameraSettings "Camera Settings" checked:true
		checkbox chk_SkySettings "Sky Settings" checked:true

		button btn_Load "Load" width:144

		fn RenderSettings = 
		(
			vr = renderers.current

			vr.output_on = true

			vr.options_hiddenLights = false
			vr.options_defaultLights = 2
			vr.options_limitDepth = true
			vr.options_maxDepth = 6
			vr.options_ray_bias = 0.005

			vr.imageSampler_type = 0
			vr.filter_on = false
			vr.fixedRate_subdivs = 2
			vr.twoLevel_baseSubdivs = 1
			vr.twoLevel_fineSubdivs = 16
			vr.twoLevel_threshold = 0.01
			vr.twoLevel_useDMCSamplerThresh = false

			vr.colorMapping_type = 6
			vr.colorMapping_gamma = 2.2
			vr.colorMapping_affectBackground = true
			vr.colorMapping_subpixel = true

			vr.gi_on = true
			vr.gi_refractCaustics = false
			vr.gi_reflectCaustics = false
			vr.gi_primary_type = 0
			vr.gi_secondary_type = 2

			vr.gi_irradmap_minRate = -5
			vr.gi_irradmap_maxRate = -2
			vr.gi_irradmap_colorThreshold = 1.0
			vr.gi_irradmap_normalThreshold = 0.3
			vr.gi_irradmap_distThreshold = 0.2
			vr.gi_irradmap_showCalcPhase = true
			vr.gi_irradmap_showDirectLight = true

			vr.dmcgi_subdivs = 8
			vr.dmcgi_depth = 3

			vr.dmc_earlyTermination_amount = 0.95
			vr.dmc_earlyTermination_threshold = 0.008

			vr.system_region_x = 32
			vr.system_region_y = 32
			vr.system_lowThreadPriority = true
		)

		fn SunSettings = 
		(
			fn GetSun = 
			(
				local vrSun
				local vrSuns = for item in lights where ( (classof item == VRaySun) ) collect item
				if vrSuns.count == 0 then vrSun = VRaySun()
				else vrSun = vrSuns[1]
				return vrSun
			)

			vrSun = GetSun()

			vrSun.enabled = true
			vrSun.invisible = true
			vrSun.turbidity = 2.0
			vrSun.shadow_subdivs = 6
			vrSun.shadow_bias = 0.0

			if vrSun.pos == [0,0,0] then vrSun.pos = [-100, -100, 100] 
			vrSun.target = targetObject pos:[0,0,0]
		)

		fn CamSettings = 
		(
			fn GetCam = 
			(
				local vrCam
				local vrCams = for item in cameras where ( (classof item == VRayPhysicalCamera) ) collect item
				if vrCams.count == 0 then vrCam = VRayPhysicalCamera()
				else vrCam = vrCams[1]
				return vrCam
			)

			vrCam = GetCam()

			vrCam.targeted = false
			vrCam.f_number = 5.6
			vrCam.vignetting = false
			vrCam.shutter_speed = 320
			vrCam.ISO = 65
			vrCam.whiteBalance = [230, 246, 255]

			if vrCam.pos == [0,0,0] then vrCam.pos = [0, -10, 1.7]
		)

		fn SkySettings =
		(
			if environmentMap == undefined then environmentMap = VRaySky()
		)

		on btn_Load pressed do
		(
			if chk_RenderSettings.checked then RenderSettings()
			if chk_SunSettings.checked then SunSettings()
			if chk_CameraSettings.checked then CamSettings()
			if chk_SkySettings.checked then SkySettings()
		)
	)
	createDialog scenePresetRollout
)