Search Unity

Struggling to control Alembic via script

Discussion in 'Asset Importing & Exporting' started by robrab2000-aa, Oct 8, 2019.

  1. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    117
    In the documentation is says that the Alembic can be controlled via Timeline, Animation or script:

    Because of the way that our timeline works (i.e. being stretched an contracted to accomodate the changes in duration) we don't want to be using Timeline or animation, we want to do it via script (https://bit.ly/30YFJym).

    - upload_2019-10-8_23-13-45.png

    You can see here that the link for the scripting page has been unlinked. The 'AlembicStreamPlayer' (the component used to control the alembic animation) has a 'Time' parameter which would allow us to move the animation along in time with everything else happening in the system quite easily if we were able to set it directly. Unfortunately when I tried to access this parameter I'm struggling to access it directly. From searching through the forums I found this response (https://bit.ly/2INPF7N) from vladala at Unity:

    - upload_2019-10-8_23-14-29.png

    The code that they refers to here provides a way to extract the current time out of the StreamPlayer but setting it doesn't seem to work.

    I can certainly try using the timeline and compensate for the duration etc but it will likely be a rather messy solution to what seems like it should be a relatively trivial problem.
     
  2. vladala

    vladala

    Unity Technologies

    Joined:
    Mar 3, 2017
    Posts:
    189
    To confirm, you tried doing prop.floatValue=12.0f and it does not work?
    If you look in the thread you mentioned the next reply is another user that created some code to control the alembicStreamPlayer by reflection?
     
  3. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    117
    Thanks, that worked great! It turns out that when i tried their solution before, i had put it in the wrong place. Now it works perfect:)
     
  4. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    117
    Hmm.. Whilst this is working great in the editor, I'm having trouble building it for WebGL. I'm controlling the AlembicStreamPlayer (via the AlembicStreamController script) with my own script (AlembicAnimatable.cs), which works fine in the editor but when I build for WebGL I get the following erros which prevent it fromm building:

    Assets\Scripts\Alembic\AlembicAnimatable.cs(5,19): error CS0234: The type or namespace name 'Formats' does not exist in the namespace 'UnityEngine' (are you missing an assembly reference?)


    and

    Assets\Scripts\Alembic\AlembicAnimatable.cs(14,17): error CS0246: The type or namespace name 'AlembicStreamController' could not be found (are you missing a using directive or an assembly reference?)


    it seems to be a problem finding the namespace for
    using UnityEngine.Formats.Alembic.Importer;


    Is there a way I can force it to recognise this?
     
    Last edited: Oct 14, 2019
  5. robrab2000-aa

    robrab2000-aa

    Joined:
    Feb 8, 2019
    Posts:
    117
    I've been doing a few experiments with a fresh project (so I can eliminate the possibility that it's a conflit with something else in our codebase. I've found that I am able to build for webgl in a fresh project (without my AlembicAnimatable.cs script), but although it builds, the alembic objects are invisible.

    Is the Alembic system maybe just not compatible with WebGL?

    This is my AlembicAnimatable script btw:
    (the #if !UNITY_WEBGL is just so I can get the rest of my codebase working as it is a primarily webgl based project)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6.     public class AlembicAnimatable : MonoBehaviour
    7.     {
    8.     //#if !UNITY_WEBGL
    9.         #region -- Variable Declaration --------------------------------------------------------------------------------
    10.  
    11.         private UnityEngine.Formats.Alembic.Importer.AlembicStreamController controller;
    12.         private float Duration;
    13.         public Project currentProject;
    14.         public FloatVariable MasterTimelinePlayhead;
    15.         public FloatVariable TargetDuration;
    16.  
    17.         #endregion -- End Variable Declaration -------------------------------------------------------------------------
    18.  
    19.         #region -- Initialisation --------------------------------------------------------------------------------------
    20.  
    21.         // Start is called before the first frame update
    22.         void Start()
    23.         {
    24.             controller = GetComponent<UnityEngine.Formats.Alembic.Importer.AlembicStreamController>();
    25.             Duration = controller.duration;
    26.         }
    27.    
    28.         #endregion -- End Initialisation -------------------------------------------------------------------------------
    29.    
    30.         #region -- Update & Animation ----------------------------------------------------------------------------------
    31.    
    32.         void Update()
    33.         {
    34.             if (currentProject.focusedAsset)
    35.             {
    36.                 SetPlayheadUpdate(MasterTimelinePlayhead.Value);
    37.             }
    38.         }
    39.  
    40.         // Function to set the 'Time' value of the alembic animation
    41.         private void SetPlayheadUpdate(float value)
    42.         {
    43.             // Scale the anim in point relative to the master timeline
    44.             float animIn = currentProject.buildAnimationSettingsLocator.GetAnimIn() * 10;
    45.        
    46.             if (MasterTimelinePlayhead.Value >= animIn)
    47.             {
    48.                 // Calculate the start point of the animation relative to the scaled time
    49.                 float startOnMaster = AA_Tools.RemapRangeClamped(animIn, 0, TargetDuration.Value, 0, 10);
    50.                 // Calculate the end point of the animation relative to the scaled time
    51.                 float endOnMaster = (Duration / TargetDuration.Value) * 10;
    52.            
    53.                 // Remap the timeline's value relative to the duration and offset by the start point so that animation plays at the correct speed
    54.                 controller.time = AA_Tools.RemapRangeClamped(MasterTimelinePlayhead.Value, startOnMaster,
    55.                     startOnMaster + endOnMaster, 0f, Duration);
    56.             }
    57.             else
    58.             {
    59.                 controller.time = 0f;
    60.             }
    61.         }
    62.    
    63.         #endregion -- End Update & Animation ---------------------------------------------------------------------------
    64. //#endif
    65.     }
    When I load the fresh project with the alembic in webgl I get these errors:
    upload_2019-10-15_15-34-50.png

    I'm wondering if its getting mixed up between the Editor and Runtime scripts perhaps?
     
    Last edited: Oct 15, 2019
  6. JeffreyStrate

    JeffreyStrate

    Joined:
    Jul 27, 2016
    Posts:
    32

    INSTALL Alembic from
    Install a package from a registry