Timeline + Cinemachine : create Shots by script

I'm using Timeline and Cinemachine and I want to create new CinemachineShots in a CinemachineTrack via scripting, but I have 2 issues :
- I don't know how to add a CinemachineShot in the track via script : the only way I found to do so is creating a default clip in the Cinemachine Track, but then I can only access it as a TimelineClip
- When I have my Cinemachine Shot, I want to set its VirtualCamera but since it is an ExposedReference I don't know how. I guess i have to use the CreatePlayable method but I don't know how

Thanks a lot

In order to get access to CinemachineShot, you will need to do:

var cinemachineShot = myTimelineClip.asset as CinemachineShot;

Here's why: Timeline is built on the Playable system to play animation, audio and custom scripts, which means that each TimelineClip contains a reference to a PlayableAsset, which contains the data need by the playable to operate at runtime. A TimelineClip is generic and is only concerned with timing (duration, start, end, etc.) and blending. If you want to get access to data specific only to a certain type of clip (like the Virtual Camera property of a CinemachineShot), you will need to through the *TimelineClip'*s asset.

To set the Virtual Camera:

myCinemachineShot.VirtualCamera.exposedName = UnityEditor.GUID.Generate().ToString();
myPlayableDirector.SetReferenceValue(myCinemachineShot.VirtualCamera.exposedName, myVirtualCamera);

Here's why: Since CinemachineShot is an asset, it cannot contain any reference to an object in a scene. So you need to ask the PlayableDirector to remember that VirtualCamera (an exposed reference) refers a given object in the scene. You don't have to do it inside the CreatePlayable method.

Hope this helps!

[EDIT] I forgot to initialize the exposed reference name.


Yeah, it helps a lot !
I'm having another issue though : when I add a second CinemaShot on my track, and I want to set its VirtualCamera, it modifies also the VirtualCamera for the first Shot.
I've tried to change the exposedname of the second shot but the problem remains...

I forgot one important step :face_with_spiral_eyes:

myCinemachineShot.VirtualCamera.exposedName = UnityEditor.GUID.Generate().ToString();

It's not obvious that you need to initialize exposedName. Sorry about that.;)

It works, thanks a lot.
I just have one last question : is it possible to "update" the CinemachineTrack in Run mode.
Indeed, the camera linked to my CinemachineTrack does not use the CinemachineShots I create in Run, like it doesn't detect them

Hello HugMat

Could you tell me how to creating a default clip in the Cinemachine Track, but then I can only access it as a TimelineClip?
I try a lot of time, still don't how to access.
Thank you~

Hello julienb:)

I try to use your method.
But "var cinemachineShot = myTimelineClip.asset as CinemachineShot;"
I just have "CinemachineClearShot" can be choose >_<

Hello leoleroy,

are you using Cinemachine.Timeline ? You have to to chose CinemachineShot


Is there a API in unityengeion to get a guid?

hi, can i ask how you do it? Like adding virtual Cinemachine cameras using c#. It will be a big help :)

exposed reference + asset bundle = lol

1 Like

it's me or api look very strange here

I created a script (with the code in this thread) which adds new CinemachineShots into timeline and connects them to a CinemachineVirtualCamera during runtime. After quitting run mode, the clip in the timeline is persisted but not the referenced VirtualCamera. Is there a way to persist this reference in CinemachineShot to the VirtualCamera?

TimelineClip clip = cinemachineTrack.CreateClip<CinemachineShot>();
var shot = clip.asset as CinemachineShot;
shot.VirtualCamera.exposedName = UnityEditor.GUID.Generate().ToString();        
playableDirector.SetReferenceValue(shot.VirtualCamera.exposedName, virtualCamera);

The problem is the playable director, which is what saves the binding, is part of the scene. The scene gets reverted when going out of playmode. There are some tools out there to save playmode scene state, one of which exists in the Cinemachine package.

Because Timeline is an asset, changes to it persist during playmode.