Notifications
Clear all

[Closed] Animatable Spinners

Hi,
I wrote a little script to make a path constraint where u can control only the constrained’ objects speed no matter how long the path is or if it changes.

Here’s the code:


macroScript X_PC category:"pp_utilities" tooltip:"Extended path constraint"
(
	clearListener()
	global flMain
	try(closeRolloutFloater flMain)catch("ERROR: Main rollout floater not existing! Cannot close!")
	
	local rollMain
	
	--FUNCTIONS--
	function spline_filt obj = superClassOf obj == Shape
		
	function calcKeys = (
		local sysUnits = units.systemType
		local fLength = curveLength rollMain.pkbPickPath.object
		local fSpeed = rollMain.spnSpeed.value
		local fKeyPos
		
		if sysUnits == #millimeters then (
			fKeyPos = (fSpeed*1000)/(fLength/100)
		)
		else if sysUnits == #centimeters then (
			if rollMain.ddlUnits.selection == 2 then ( --When m/s are selected
				fKeyPos = (fSpeed*100)/(fLength/100)
			) else if rollMain.ddlUnits.selection == 1 then ( --When km/h are selected
				fKeyPos = ((fSpeed*100)/3.6)/(fLength/100)
			) else if rollMain.ddlUnits.selection == 3 then (
				fKeyPos = ((fSpeed*100)/2.2369)/(fLength/100) --When mph are selected
			) else if rollMain.ddlUnits.selection == 4 then (
				fKeyPos = ((fSpeed*100)/39.37)/(fLength/100) --When "/s are selected
			)
		)
		else if sysUnits == #meters then (
			fKeyPos = fSpeed/(fLength/100)
		) 
		else if sysUnits == #kilometers then (
			fKeyPos = (fSpeed/1000)/(fLength/100)
		) 
		else (
			messageBox "Currently only the metric unit system is supported. Please set your system units accordingly first."
		)
		--fKeyPos
	)
	
	function updatePathCon = (
		local arrKeys = rollMain.pkbPickObj.object.position.controller.percent.keys
		local fPercent = calcKeys()
		deleteKey arrKeys 2
		with animate on (
			at time 1s rollMain.pkbPickObj.object.position.controller.percent = fPercent
		)
		setAfterORT rollMain.pkbPickObj.object.position.controller #relativeRepeat --Exact same speed along the whole path, no matter how long it is since we're using Out-of-range-Types for the last Key
	)
	

	
	function createPathCon = (
		local pathCon = path_constraint()
		rollMain.pkbPickObj.object.position.controller = pathCon
		rollMain.pkbPickObj.object.position.controller.path= rollMain.pkbPickPath.object
		rollMain.pkbPickObj.object.position.controller.follow = true
		updatePathCon()
	)
	

	
	function CallbackFn1 ev nd = (
		--format "Event Detected: Event %, Nodes %
" ev nd --PRINT
		try (updatePathCon()) catch("ERROR: Cannot Update. GeometryChanged Callback failed.")
	)
	--END OF FUNCTIONS--

	
	
	--ROLLOUTS--
	rollout rollMain "Main" (
		group "Objects" (
			pickbutton pkbPickPath "Pick Path" align:#left width:75 filter:spline_filt across:2
			pickbutton pkbPickObj "Pick Object" width:75
			label lblPath "<empty>" align:#left across:2 width:75
			label lblObj "<empty>" width:75
		)
		group "Properties" (
			spinner spnSpeed "Speed:" range:[0,999999,1] align:#left offset:[0,3]across:2
			dropdownlist ddlUnits "" items:#("km/h","m/s","mph","inches/s") selection:2
		)
		checkbox chkInteractive "Interactive mode" checked:true
		button btnCreate "Create" width:180 height:30 offset:[0,10]
		button btnUpdate "Update" width:180 height:30

		
		--VARS--
		local varPath
		local varObj
		--END OF VARS--
		
		--EVENT HANDLERS--
		on btnCreate pressed do (
			createPathCon()
		)
		
		on btnUpdate pressed do (
			updatePathCon()
		)
		
		on pkbPickPath picked obj do (
			lblPath.caption = obj.name
			varPath = obj
		)
		
		on pkbPickObj picked obj do (
			if superClassOf obj == shape then (
				if queryBox "You selected a shape. Are you sure that you want a shape to follow another shape?" title:"Selection warning!" then (
					lblObj.caption = obj.name
					pkbPickObj.object = obj
				)
				else
				(
					varObj = undefined
					lblObj.caption = "<empty>"
				)
			) 
			else 
			(
				varObj = obj	
				lblObj.caption = obj.name
			)
		)
		
		on spnSpeed changed value do (
			if chkInteractive.state == true then (
				if varPath != undefined and varObj != undefined then (
					updatePathCon()
				) 
				else
				(
					messageBox "You have to select a path and an object first!"
				)
			) else ()
		)
		
		on ddlUnits selected item do (
			if chkInteractive.state == true then (
				if varPath != undefined and varObj != undefined then (
					updatePathCon()
				) else (messageBox "You have to select a path and an object first!")
			) else ()
		)
		--END OF EVENT HANDLERS--
	)
	
	rollout rollAbout "About" rolledUp:true (
			label lbl1 "Author: Patrick Probst"
			label lbl2 "(c) 2011"
		
		--EVENT HANDLERS--
		
		--END OF EVENT HANDLERS--
	)
	--END OF ROLLOUTS--


	
	--CALLBACKS--
	callbackGeoChanged = NodeEventCallback mouseUp:true geometryChanged:callbackFn1
	--END OF CALLBACKS--
	
	flMain = newRolloutFloater "eXtended PathConstraint" 200 500
	addRollout rollMain flMain
	addRollout rollAbout flMain

)

SO I got two problems here:

  1. I want the Speed Parameter to be animatable (And obviously I got no Idea of how to do it)
  2. If the Parameter is animated am I right when say that then I have to change my updatePatchCon function to not renew the objects position but to somehow take the animation into account? How can I approach that? I’m no superScriptGuy or so ^^
3 Replies
 PEN

There are two ways to deal with the spinner update. Use callBacks or add the controller property to the spinner and provide the path to the controller that you are driving. The later is a more direct solution but requires you to either hard code in to the script or sort out how to dynamically change it. I have never tried dynamically changing that parameter so I don’t know if it can be done.

Where are you storing the keys, I hope it is on the track that is being animated. It is not the spinner that gets animated it is the parameter on the controller. A spinner is just a way to display the value of a track.

If you are interested I’m going to be offering several classes on Max Script.

Hi,

Thank you for your answer!
As of now I guess I’m not storing the keys in a var yet or so since at first I didnt think of animating that parameter. But now it is required.
I’m animating with two keyframes as of now: one at beginning of the path (start) and on after 1 second. And that one after 1 second I calculate by taking the speed lets say in meters per second and set that distance at the 2nd keyframe.
I guess I have to add the controller of the path percent to the spinner then.

Thanks for your offer on maxScript I am definitely interested but I’m a poor studentand can’t afford such courses :-/

 PEN

I’m going to suggest a script controller on the path constraint percent track as the best solution.