Set ExposedReference StreamPlayer of Alembic Shot Asset

Hi,

I am creating some Alembic tracks with C# from alembic prefabs instantiated in the scene.

When doing it in the interface by drag and dropping the objects in the track, it works and it creates an Alembic Shot Asset with the Stream Player set to the Alembic Stream Player script of the prefab so the animation is referenced.

Now I need to do this with code so I currently have this :

// Create an alembic track
AlembicTrack alembicTrack = playableTimeline.CreateTrack<AlembicTrack>();

// Create alembic clip
TimelineClip alembicClip = alembicTrack.CreateClip<AlembicShotAsset>();

// Get alembic player script in prefab
AlembicStreamPlayer streamPlayerScript = selectedPrefabInstance.GetComponent<AlembicStreamPlayer>();

// Get Alembic shot asset
AlembicShotAsset alembicShotAsset = (alembicClip.asset as AlembicShotAsset);

// Set the exposed reference
ExposedReference<AlembicStreamPlayer> streamPlayerReference = alembicShotAsset.StreamPlayer;
streamPlayerReference.exposedName = UnityEditor.GUID.Generate().ToString();
playableDirector.SetReferenceValue(streamPlayerReference.exposedName, streamPlayerScript);

(selectedPrefabInstance is a prefab instance in the scene)

So I need to set the StreamPlayer reference of the Alembic shot asset but it’s an exposed reference and I found this thread :

I tried the solution above but it doesn’t seem to work…

Is it the right way to do this?

PS:
I am using
Unity 2020.3.9f1
Timeline 1.5.5
Alembic 2.1.3

Hey,

I checked the following code that it creates the correct thing:

using UnityEditor;
using UnityEngine;
using UnityEngine.Formats.Alembic.Importer;
using UnityEngine.Formats.Alembic.Timeline;
using UnityEngine.Playables;
using UnityEngine.Timeline;

namespace NUnit.Framework.Internal
{
    public class Class
    {
        [MenuItem("Window/DoIt")]
        public static void DoIt()
        {
            var tl = ScriptableObject.CreateInstance<TimelineAsset>();
            var track = tl.CreateTrack<AlembicTrack>();
            var clip = track.CreateDefaultClip();
            var asset = clip.asset as AlembicShotAsset;
            var go = new GameObject();
            var director = go.AddComponent<PlayableDirector>();
            director.playableAsset = tl;

            var asp = UnityEngine.Object.FindObjectOfType<AlembicStreamPlayer>();
            var expo = asset.StreamPlayer;
            expo.exposedName = UnityEditor.GUID.Generate().ToString();
            asset.StreamPlayer = expo;

            director.SetReferenceValue(asset.StreamPlayer.exposedName, asp);
        }
    }
}

I think your bug is around lines 15-17: ExposedReference is a struct and you are setting the exposed name on a local copy.

Please tell me how it goes.

1 Like

Hi @vladala

Thank you so much, the thing I was missing was to reassign the StreamPlayer local copy to the original one!

asset.StreamPlayer = expo;

It works now :wink: