-- Custom Right Click Menu Scripts File
-- Created:  		March 4 1999
-- Last Updated: 	March 5 1999 11:30pm
--
-- Author:Frank DeLise
-- Version:3D Studio Max Release 3.0
--
-- Custom Richt Click Menu Script "QuickBox"
-- This script Allows you to add your own entrys to the Custom Right Click menu.
--***********************************************************************************************
-- MODIFY THIS AT YOUR OWN RISK
--***********************************************************************************************

-- Add you own Custom Controls here.

-- This CustomRCMenu consists of three things you can do
-- 1. Add a MenuItem, Example "MenuItem mi_Render "Render"
-- 2. MenuItem Picked, Example "On mi_render picked do Render ()"
-- 3. A filter, show only when filter is True, Example "MenuItem mi_Isolate "Isolate Tool" Filter:IsSomethingSelected
-- A filter is created like this, FN IsoSomethingSelected = (Selection.count == 1) -- This returns "True" if something is selected and enables the render button

-- Note:!! The Custom menu only appears when Control is pressed during a RightClick.

RcMenu CustomRCMenu -- Start of Custom Menu
(	
	    fn itype item = (return classof item as string) -- filter used in biped testing
	

	---************************************************************************************************
	-- Start Custom Filters
	--------------------------------------------------------------------------------------------------
	-- Filter Fucntion Defined
	FN Meshselected = (selection.count == 1 and classof selection[1] == Editable_mesh)
	FN Multimeshes = (meshlist = for items in objects where classof items == Editable_mesh collect items
					(meshlist.count > 1)
					)
	FN IsoSomethingSelected = (selection.count >= 1)
	
	FN IsBipedSelected = (
					if selection.count == 0 do return false
					biplist = for items in selection where classof items == biped_object collect items	
					if biplist.count == selection.count then return true
					return false
					)	
	Fn ListBipeds = 
		(
		for items in $objects/* as array where classof items == Biped_object collect items
		)
		
	fn FindByName namestring  =
	(targets = for items in objects where (items.name as name) == (namestring as name) collect items
	case targets.count of
		(	
		(0): return undefined
		(1): return targets[1]
		default: return targets
		)
	)
	
	FN IsBiped = 
		(
		(listbipeds()).count > 0
		)
	
	fn selectwhich bodypart = 
		(
		Bips = listbipeds()
		case selection.count of 
			(
			(0):(broot = if Bips.count >= 1 then Bips[1] else undefined)
			(1):(broot = ParentBipOf $)
			(default):( BipArray = #()
						for items in selection do 
							(eachbip = ParentBipOf items 
							if eachbip != undefined and \
								finditem BipArray eachbip == 0 do append BipArray eachbip
							broot = if biparray.count > 0 then BipArray[1] else undefined
							)
						)
			)
		if broot == undefined do (return undefined)
		target = if bodypart == "" then (broot.name) else ((broot.name as string) + " " + (bodypart))
		if target == undefined do return bodypart 
		select (FindByName  target)
		)
		

	
	-- Start Custom Menu Items
	--------------------------------------------------------------------------------------------------
	Submenu "Whole body" filter:IsBiped
		(
		MenuItem Bhoriz "Horizontal"
		MenuItem Bvert "Vertical"
		MenuItem Bturn "Turning"
		)
	MenuItem Bhead "Head" filter:IsBiped
	MenuItem BNeck "Neck" filter:IsBiped
	SubMenu "Right arm" filter:IsBiped
		(
	 	MenuItem BRclav "R Clavicle" 
		MenuItem  BRshould "R Shoulder" 
		MenuItem BRelbow "R Elbow" 
		MenuItem BRHand	"R Hand" 
		)
	SubMenu "Left Arm" filter:IsBiped
		(
	 	MenuItem BLclav "L Clavicle" 
		MenuItem  BLshould "L Shoulder" 
		MenuItem BLelbow "L Elbow" 
		MenuItem BLHand	"L Hand" 
		)
	
	MenuItem Bspine0 "Spine" filter:IsBiped
	MenuItem Bspine1 "Spine 1" filter:IsBiped
	MenuItem BSpine2 "Spine 2" filter:IsBiped
	MenuItem BSpine3 "Spine 3" filter:IsBiped

	MenuItem Bpelvis "Pelvis" filter:IsBiped
	SubMenu "Right Leg" filter:IsBiped
		(
		MenuItem BRHip "R Hip" 
		MenuItem BRknee "R Knee" 
		MenuItem BRfoot "R foot" 
		)
	SubMenu "Left Leg" filter:IsBiped
		(
		MenuItem BLHip "L Hip" 
		MenuItem BLknee "L Knee" 
		MenuItem BLfoot "L foot" 	
		)
	
	Separator Sep_010 filter:IsBipedSelected
	menuitem selectOp "Biped Select Opposite" filter:IsBipedSelected
	menuitem SelectSym "Biped Select Symmetrical" filter:IsBipedSelected	
		
	Separator Sep_001 filter:IsBiped
	menuitem Showbips "Show all Bipeds" filter:IsBiped
	menuitem HideBips "Hide all bipeds" filter:IsBiped
	menuitem HideSubs "Hide helper bones" filter:IsBiped enabled:false
	menuitem ShowtheGnomons "Show Gnomons" filter:IsBipedSelected
	menuitem HidetheGnomons "Hide Gnomons" filter:IsBipedSelected
	menuItem HideAlltheGnomons "Hide All Gnomons" filter:IsBiped
	Menuitem miMakeSingleAxisInterp "Make Interp Bone" filter:isBiped	
	Menuitem miMakeAllAxisInterp "Make All Interp Bones" filter:isBiped	
	Menuitem miSaveAllAxisInterp "Save Interp Bones" filter:isBiped	
	Menuitem miLoadAllAxisInterp "Load Interp Bones" filter:isBiped	
	Seperator Sep_000 filter:IsBiped
	Menuitem Quickrender1 "Small Test Animation"
	Menuitem Quickrender2 "Large Test Animation"
	Menuitem Showlast "Show Last Penciltest"
	
	Seperator Sep_bp001 filter:MeshSelected
	Menuitem ShowBPW "Show Bone Weights" filter:MeshSelected
	
	Seperator S01
	menuitem FaceCopy "Copy Mesh Animations" 
	menuitem FacePrep "Prepare Mesh animation" filter:MeshSelected

	
	Seperator Sep_111		-- Seperator
					
	
	MenuItem mi_render "Quick Render"
	MenuItem mi_renderSettings "Renderer Settings"
	
	MenuItem mi_Medit "Materials Editor"
	MenuItem mi_Isolate "Isolate Tool" Filter:IsoSomethingSelected
	Seperator Sep_221
	MenuItem mi_Customize "Customize Menu"			-- Menu Item
	Seperator Sep_222							-- Seperator
	
	--------------------------------
	on Bhoriz picked do (selectwhich ""; max move; max tool xy)
	on Bvert picked do (Selectwhich ""; max move; max tool Z)
	on Bturn picked do (selectwhich ""; max rotate)
	on BHead picked do (Selectwhich "head"; max rotate)
	on Bneck picked do (Selectwhich "neck"; max rotate)
	on BRclav picked do (Selectwhich "R clavicle"; max rotate)
	on BRSHould picked do (Selectwhich "R upperarm"; max rotate)
	on BRelbow picked do Selectwhich "R forearm"
	on BRhand picked do Selectwhich "R hand"
	on BLclav picked do (Selectwhich "L clavicle"; max rotate)
	on BLSHould picked do (Selectwhich "L upperarm"; max rotate)
	on BLelbow picked do Selectwhich "L forearm"
	on BLhand picked do Selectwhich "L hand"
	on Bspine3 picked do (Selectwhich "spine3"; max rotate)
	on Bspine2 picked do (Selectwhich "spine2"; max rotate)
	on Bspine1 picked do (Selectwhich "spine1"; max rotate)
	on Bspine0 picked do (Selectwhich "spine"; max rotate)
	on Bpelvis picked do Selectwhich "pelvis"
	on BRhip picked do (Selectwhich "R thigh"; max rotate)
	on BRKnee picked do Selectwhich "R calf"
	on BRfoot picked do Selectwhich "R foot"
	on BLhip picked do (Selectwhich "L thigh"; max rotate)
	on BLKnee picked do Selectwhich "L calf"
	on BLfoot picked do Selectwhich "L foot"
	
	on showbips picked do 
	(
		for item in objects do
	 	(
		pname = item.parent
		if pname == undefined then pname = ""
		if itype item == "Editable_mesh" and itype pname == "Biped_Object" do (unhide item; freeze item)
		if itype item == "Dummy" and itype pname == "Biped_Object" do (hide item)
		if itype item == "Biped_Object" do (unhide item)
		)
	)
	
	on hidebips picked do
	(
		for item in objects do
	 	(
		pname = item.parent
		if pname == undefined then pname = ""
		if itype item == "Editable_mesh" and itype pname == "Biped_Object" do (unhide item; unfreeze item)
		if itype item == "Dummy" and itype pname == "Biped_Object" do (hide item)
		if itype item == "Biped_Object" then hide item
		)
	)
	
	on SelectOP picked do
		(SelectOpposite selection[1]
		)
	on SelectSym picked do 
		(SelectBoth selection[1])
	-- Start Custom Button Picks
	
	on ShowtheGnomons picked do 
		(
		if showGnomon != undefined do (showGnomon selection)
		)
	on hidetheGnomons picked do 
		(
		if hideGnomon != undefined do (HideGnomon selection)
		)
	on hideAlltheGnomons picked do
		(
		if hideAllGnomons != undefined do hideAllGnomons()
		)

	on miMakeSingleAxisInterp picked do
		(
		if MakeSingleAxisInterp != undefined do MakeSingleAxisInterp()
		)		

	on miMakeAllAxisInterp picked do
		(
		if MakeAllAxisInterp!= undefined do MakeAllAxisInterp()
		)		
		
	on miSaveAllAxisInterp picked do
		(
		if SaveAllAxisInterp != undefined do SaveAllAxisInterp()
		)		
		
	on miLoadAllAxisInterp picked do
		(
		if LoadAllAxisInterp != undefined do LoadAllAxisInterp()
		)		
		
	on ShowBPW picked do
		(
		if showBpWeights != undefined do (showBpWeights $)
		)
		
	--------------------------------------------------------------------------------------------------
	on mi_render Picked do 
	(
		If RCRenderwin == undefined or RCRenderWin.Width != RenderWidth then 
		(
			If RCRenderWin != undefined then Close RCRenderWin
			BM = Render Rendertype:#Selection OutputSize:[RenderWidth,RenderHeight]  
			RCRenderWin = BM
			Display RCRenderWin
		)
		Else
		(
			 Render Rendertype:#Selection to:RCRenderWin 
		)	
	)

	on mi_RenderSettings picked do (Max Render Scene)
	on mi_Medit picked do (Max mtledit) 
	on mi_Isolate Picked do (Try (Macros.Run "Tools" "Isolate") Catch (MessageBox "Isolate Tool not installed." Title:"Custom RCMenu"))
	
	on mi_Customize picked do 
	(
		If queryBox "This will allow you to load and edit the CustomRCMenu script, Continue?" Beep:NO title:"Custom Menu" then
		(
			Try -- A Try and Catch allows you to try something, if it fails it goes to catch where you can place a messagebox
			(
				RCDir = GetDir #Maxroot 
				RCMenuFile = RCDir as string + "STDPLugs\\StdScripts\\CustomRCMenu.ms"
				Edit RCMenuFile
			)
			Catch (MessageBox "Custom Right Click Menu not available." title:"Custom Right Click Menu.")
		)
	)
	
	on QuickRender1 picked do
		(
		setwaitcursor()
		try (
			pencil = render\
			fromframe: Animationrange.start\
			toframe: animationrange.end\
			outputsize:  [160,120]\
			antialiasing: false\
			outputfile:(getdir #preview + "\\preview.avi")\
			vfb:false
			
			)
		catch (messagebox "Quickrender Failed")
		setarrowcursor()
		Ramplayer (getdir #preview + "\\preview.avi") ""
		
		)
		
		on QuickRender2 picked do
		(
		setwaitcursor()
		try (
			pencil = render\
			fromframe: Animationrange.start\
			toframe: animationrange.end\
			outputsize:  [400,300]\
			antialiasing: true\
			outputfile:(getdir #preview + "\\preview.avi")\
			vfb:false
		
			)
		catch (messagebox "Quickrender Failed")
		setarrowcursor()
		Ramplayer (getdir #preview + "\\preview.avi") ""
		
		)


	on FacePrep Picked do
		(
		if $ != undefined then
			(
			if subobjectlevel == 1 then
				(vlist = $.selectedverts)
			else
				(vlist = $.verts)
			converttomesh $
			max modify mode
			slidertime = animationrange.start
			animate on
			for ti = animationrange.start to animationrange.end do 
				(
				slidertime = ti
				move vlist [0,0,0]
				)
			animate off
			bob = trackview.open "Vertices"
			trackview.clearfilter "Vertices" #all
			trackview.setfilter "Vertices" #objects #animatedtracks #transforms #modifiedobjects #baseobjects
			trackview.zoomselected "Vertices"
			)
		else 
			(messagebox "select mesh first please")
		)	
		
	on FaceCopy picked do
		(
		MeshAnimCopy()
		)
	on Showlast picked do
		(Ramplayer (getdir #preview + "\\preview.avi") ""
		)
	on CustomRCMenu open do
	(
		Global RCRenderWin
	)

	
 ) -- End of Custom Menu


