Notifications
Clear all

[Closed] Scripted look at any viewport… help/hint

I’ve rewritten a script by Dave Stewart http://www.davestewart.co.uk . I wanted to see if I could write a script controller that looks at any current view instead of being linked to a specific camera. This way we could apply it to assets and just have them work when inserted.

It’s easy enough to get the controller assigned with a script that works. But what I attempted to add was a callback that updates the script/rotation on any viewport change.

There must be an elegant way to do this rather than rotating the objects on viewportChanged.

rollout ro_ScriptedLookAt "ScriptedLookAt" width:162 height:311
 (
 	---------------------------------------------------------------------------------------------------
 	-- local declarations
 	---------------------------------------------------------------------------------------------------
 
 	local thisObj, objArray, str
 
 	---------------------------------------------------------------------------------------------------
 	-- Interface
 	---------------------------------------------------------------------------------------------------
 
 	Group "Assign Look-At Controller"
 		(
 		button btnAssign "Assign to Sel" width:144 height:16 tooltip:"Assign scripted rotation controller (on chosen axis) to selection"
 		)
 	Group "Delete Controller(s)"
 		(
 		button btnDelete "Delete from Sel." width:88 height:16 across:2 align:#right offset:[10,0] tooltip:"Delete scripted rotation controller (from chosen axis) to selection"
 		checkButton chkReset "Reset to 0" align:#left width:62 height:16 offset:[10,0] checked:true tooltip:"Uncheck this to leave objects looking in the same direction after deleting controller"
 		)
 
 
 	---------------------------------------------------------------------------------------------------
 	-- Functions
 	---------------------------------------------------------------------------------------------------
 	fn makeScript thisObj=
 		(
 
 str= "c = (inverse(viewport.gettm())).row4
 thisObj=$" + thisObj.name + "
 p = thisObj.pos
 x=p.x - c.x
 y=p.y - c.y
 a=atan(y/x)
 if x < 0 then degToRad(a+90)
 else degToRad(a-90)"
 		)
 
 	fn assignControllers objArray=
 		(
 		undo "Assign Scripted LookAt" on
 			(
 			for thisObj in objArray do
 				(
 						thisObj.rotation.controller = Euler_XYZ()
 						thisObj.rotation.controller[3].controller = float_script()
 						thisObj.rotation.controller[3].controller.script = (makeScript thisObj)
 				)
 			)
 		)
 
 	fn deleteControllers objArray=
 		(
 		undo "Delete Scripted LookAt" on
 			(
 			for thisObj in objArray do
 				(
 				if thisObj != undefined then
 					(
 					try	(
 						thisObj.rotation.controller[3].controller = Bezier_Float()
 						setUserProp thisObj "hasLookAtController" false
 						if chkReset.checked then
 							(
 							thisObj.rotation.controller[3].controller.value = 0
 							)
 						)
 					catch(print thisObj)
 					)
 				)
 			)
 		)
 	---------------------------------------------------------------------------------------------------
 	-- Handlers
 	---------------------------------------------------------------------------------------------------
 
 
 	on btnAssign pressed do
 		(
 		if (selection.count > 0) then 
 			(
 				for i in selection do
 				(
 					assignControllers i
 					setUserProp i "hasLookAtController" true
 				)
 			)
 		else 
 			(
 			objArray = (selectByName title:"Select objects to assign Scripted Lookat Controller to:")
 			if objArray != undefined then assignControllers objArray
 				setUserProp i "hasLookAtController" true
 			)
 		
 			
 
 		objArray = #()
 		
 			
 		)
 
 	on btnDelete pressed do
 		(
 		if (selection.count > 0) then 
 			(
 				deleteControllers selection
 			)
 		else 
 			(
 			objArray = (selectByName title:"Select objects to delete Scripted Lookat Controller from:")
 			if objArray != undefined then 
 			(
 				deleteControllers objArray
 			)
 				
 			)
 		objArray = #()
 		)
 )
 try destroydialog ro_ScriptedLookAt catch()
 createdialog ro_ScriptedLookAt width:188 Height:100 pos:[0,370]
 
 
 -----------------------------------------
 --call back
 ----------------------------------------
 
 fn updateLookAtRotations =
 ( undo off
 	for o in objects where (getUserProp o "hasLookAtController") != undefined and (getUserProp o "hasLookAtController") do
 	(
 		rotate o (eulerangles 0 0 1)
 	)
 	undo on
 )
 
 callbacks.removeScripts id:#JonahVP
 callbacks.addScript #viewportChange "updateLookAtRotations()" id:#JonahVP
 
2 Replies

http://forums.cgsociety.org/showpost.php?p=6651872&postcount=2

Denis, Denis, Denis,

Simultaneously school me on scripting and scold me for not searching.

Thanks Dude.