Notifications
Clear all

[Closed] Multiple Object LookAt Constraint

Okay, first off I’m a total novice at scripting so I’m really out of my element with this. What I’m trying to do is to create a script that will allow me to take a selection of multiple objects and apply a LookAt constraint to them with the same target. I want to select my objects, apply the constraint, and use a rubberband selection to pick the look at object (and if possible, I want to be able to change the LookAt target for all objects at the same time after it’s initially set up).

So,
1; Can the LookAt target be instanced (and if so, how)?
2; How can I apply a rubberband select to my script?
3; How can I change the ‘Keep Initial Offest’ option to checked (default is un-checked)
4; Where am I going wrong on this script?

What I have so far is the simple addition of the constraint to multiple objects (I can’t figure out how to get the appendTarget to work without specifying a particular target):

$.rotation.controller = lookat_constraint ()
$[1].rotation.controller.appendtarget $

I can put something like $Camera01 100 after it to tell the objects to all look at a particular camera, but I don’t know how to make a manual selection with the script. I could really use a push in the right direction here (if you provide a script, could you please explain why it does what it does… I’m not exactly fluent in ‘script’).

4 Replies
macroScript LookAtRelative category:"CGTalk"
(
	on isEnabled return selection.count > 0
	on execute do
	(	
		theTarget = pickObject rubberband:selection.center --pick a target with rubberband from the sel. center
		if isValidNode theTarget do --if the picked object is valid,
		(
			theCtrl = LookAt_Constraint relative:true --create a lookAt Constraint with Keep Initial Offset on
			theCtrl.appendTarget theTarget 100 --append the picked target to it with Weight of 100
			selection.rotation.controller = theCtrl --and instance the controller to all selected objects
		)
	)
)

You cannot instance the target, but you can instance the controller among the objects. Changing the target properties of any of the object’s controllers will automatically change all others.

You could now try writing another script which is enabled only if one object is selected and it has a LookAt constraint as the rotation controller. It would then let you pick another target and replace the existing one by removing the old one and adding the new one.

Wow… well, first, thank you. Second, I think I bit off more than I could chew. I’m not sure if I’m just misunderstanding the MaxScript documentation or what, but I think I was going about it wrong. Looks like I have a long way to go with learning scripting.

2 Replies
(@bobo)
Joined: 11 months ago

Posts: 0

No, you are doing it more or less right (although using $ was surely not entirely correct).
In the second line, you had $[1] and $ in the same function call, which would have caused troubles. In general, don’t use $ unless you are typing quick command line-like expressions in the Listener to manipulate the current scene. Inside of complex scripts, always use ‘selection’ and even snapshot it to an array (selection as array) if you expect the selection to change later so you can keep the original objects around.

I know I went a bit too far, but you wanted all the bells and whistless like rubberbands etc., so I decided to package it as a macroScript button you can place on a toolbar.

And yes, MAXScript is so deep I am still learning it, 10 years later…

(@graeme)
Joined: 11 months ago

Posts: 0

that might be my fault there. i suggested

$.rotation.controller = lookat_constraint ()
$[1].rotation.controller.appendtarget $camera01 100

for the listener in this thread on 3dbuzz
jessup3d had posted some more requests and, as i was thinking about it, he edited his post to say you had solved his problem. (beaten by the maXster )

anyway, when i realised what was wanted was a “select and lookat” button i seached scriptspot and found that one of the many useful scripts in Paul Hormis’ TiM-Scripts pack is “Add_LookAt_Controller.ms” which is worth looking at .