[Closed] Link camera to frame number
Can anyone suggest how to handle this task?
Here’s an example of what I want to do:
Say, I have multiple cameras. When I look through cam1, the slider goes to frame1, when I look through camera2, the slider goes to frame7. So, I want to have some link between camera and frame number.
I did a runtime script in MEL for something very similar long time back, but couldn’t figure out a way to handle in max.
Something along the lines of:
gac = getActiveCamera() --get the camera
gvn = gac.name as string --get the name of the camera
if gvn == "<name-of-camera>" then (
sliderTime = <frame number you want it to start on>
)
Assuming your camera names are numbered, this will change the slidertime to the camera number everytime the scene is redrawn.
I’m sure there are better ways to do this.
fn frameByCamName =
(
gac = getActiveCamera()
if gac != undefined do
(
camName = trimRight gac.name "0123456789"
camNum = trimLeft gac.name camName
)
slidertime = camNum as time
redrawViews()
)
registerRedrawViewsCallback frameByCamName
-- Uncomment the line below to remove this CallBack:
-- unregisterRedrawViewsCallback frameByCamName
I tried this but I am obviously doing something wrong. This will crash Max without warning.
DO NOT RUN THIS WITHOUT SAVING…
callbacks.removeScripts #viewportchange id:#setTimeByCam
fn frameByCamName =
(
gac = getActiveCamera()
if gac != undefined do
(
camName = trimRight gac.name "0123456789"
camNum = trimLeft gac.name camName
slidertime = camNum as time
)
)
callbacks.addScript #viewportchange "frameByCamName()" id:#setTimeByCam
Thanks for all your comments.
How can I run in it realtime? That’s the main problem I am facing. How can I change the frame number automatically as I see through a different camera?
That’s what callbacks do. As you make a viewport change, the function will fire and set the frame number according to the camera name. The first one I posted doesn’t crash max. I don’t know why the second one does. Dennis?
because! when you change the time (time frame) it fires #viewportchange event. you have to disable the event (remove script) before changing the time, and add script back after. also you have to check if time needs to be changed. changing of veiwport transform fires the event as well, but it doesn’t need change the time.
Ahha! (?) This seems to work… Do I get a passing grade today?
Edit: even renaming the camera fires the callback. Kinda cool.
(
callbacks.removeScripts #viewportchange id:#setTimeByCam
global frameByCamName
fn frameByCamName =
(
gac = getActiveCamera()
if gac != undefined do
(
callbacks.removeScripts #viewportchange id:#setTimeByCam
camName = trimRight gac.name "0123456789"
camNum = trimLeft gac.name camName
if slidertime != camNum as time do sliderTime = camNum as time
callbacks.addScript #viewportchange "frameByCamName()" id:#setTimeByCam
)
)
callbacks.addScript #viewportchange "frameByCamName()" id:#setTimeByCam
)
much better! but:
camName = trimRight gac.name "0123456789"
camNum = trimLeft gac.name camNam
it’s not right. what will happen if the camera’s name is “camera10_12”?
first line is write. second might be replace or substring
also i don’t like “as time” conversion. a time value has property “frame”. it’s better to compare it to integer. check slidertime.frame
Thanks for all your comments, I am still not very clear about the concept here.
Where do these scripts go? Are these somehow attached to the camera or I just run this script and that will be all?
Also, if a file is saved by one user, does the script go with the file as well?
There are many ways to distribute/implement scripts like this across your network. I can’t speak to the ‘best’ way.
This callback script, as it is written is only live during the max session in which it is run. With Max open, run the script and the callback will remain active, even after a New File or Open File etc. Restart Max and the callback goes away.
So, one very simple way (though probably not recommended by the pros here) would be to place this script in your /scripts/startup folder or in your /plugins folder. Max will run scripts in both locations. Then the callback will be active every time Max is started.
Dennis is right about the camera names though. This script needs some more error checking before it is foolproof.
callback script can be saved with file. see persistent parameter for addscript method.