Notifications
Clear all

[Closed] Look at to viewport camera

How can i look at constraint a simple object, let say a plane, to the perspective viewport camera?
It is possible?

Thanks!
-M.

3 Replies

Possible, but it updates too often.

unregisterRedrawViewsCallback OnViewportChanged

fn OnViewportChanged =
(
	if isKindOf ::NodesAlignedToView Array and viewport.IsPerspView() do
	(
		for n in ::NodesAlignedToView where isValidNode n do
		(
			n.dir = normalize ( (inverse (viewport.GetTM())).position - n.pos)
			format "updated %\n" (timestamp())
		)
		
	)

	true
	
)

delete objects
gc()
Teapot isselected:true
global NodesAlignedToView = #( selection[1] )
registerRedrawViewsCallback OnViewportChanged

maybe this will do a bit better

unregisterRedrawViewsCallback OnViewportChanged
   
fn OnViewportChanged =
(
	if isKindOf ::NodesAlignedToView Array and viewport.IsPerspView() do
	(
		local viewTM = inverse (viewport.GetTM())
		
		areEqual = (dot viewTM.row3 lastViewTM.row3) > 0.9999 and distance viewTM.pos lastViewTM.pos < 0.001
			
		if not areEqual do
		(
			for n in ::NodesAlignedToView where isValidNode n do
			(
				n.dir = normalize (viewTM.position - n.pos)
				format "updated %\n" (timestamp())
			)
			
			lastViewTM = viewTM
		)
		
	)

	true
	
)

delete objects
gc()
Teapot isselected:true
global NodesAlignedToView = #( selection[1] )
global lastViewTM = inverse (viewport.GetTM())
registerRedrawViewsCallback OnViewportChanged

another option is you can create an object thats constrained to the viewport and then use that as the target…

plugin Helper ViewportPoint
name:"ViewportPoint" 
classID:#(0x47db14fb, 0x4e9b5f95) 
category:"Scripted" 
extends:point 
( 
	local tnode;
	
	fn vpconst = if tnode != undefined and IsValidNode tnode then tnode.transform = inverse(getViewTM());

	on attachedToNode n do	
	(
		tnode = n;	
		registerRedrawViewsCallback vpconst;
	)	
	on deleted do 
	(	
		tnode = undefined;	
		UnregisterRedrawViewsCallback vpconst;
	)
	on create do completeRedraw();
	
	on load do
	(	
		tnode = refs.dependentNodes this firstOnly:true baseObjectOnly:false;
		registerRedrawViewsCallback vpconst;
	)	
)

The best method though is a custom plugin xform position constraint locked to the viewport

if you want it to work in orthographic views then change viewport constraint function to

 fn vpconst = if tnode != undefined and IsValidNode tnode then 
(
    tnode.transform = inverse(getViewTM());
    if not viewport.IsPerspView() then  tnode.transform[4] =  tnode.transform[4] +  tnode.transform[3] * 999999.0;
)

The constraining to a view is not a right idea. registerRedrawViewsCallback works only as a scene event and can’t be used in animation (rendering).
So the right solution is to constraint to a real camera node (transform) and to wire camera’s property controllers (if needed). This solution is trivial I hope