Search Unity

AnimationCurve.Evaluate can only be called from main thread

Discussion in '2018.2 Beta' started by snacktime, May 15, 2018.

  1. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    This is a new restriction in 2018.2. Was it intentional? Animation curves are used for a lot more then just animation and not being able to use them in jobs kind of sucks. My case I was using it for utility ai and now i have to go back to using a more complex process of exporting the data and using my own evaluate method on it.
     
  2. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    286
    Could you give us more info on this?
    I looked at the code, and there don't seem to be any explicit main thread checks in the AnimationCurve.

    That said, the AnimationCurve has never been thread safe, as its evaluation uses a cache. If you evaluate the curve on two threads at the same time, you are not going to get the right results
     
    richardkettlewell likes this.
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    I had to go back to 2018.1 for other reasons but there were two error lines. The first simply said Evaluate can only be called from the main thread. The second one was sort of odd it gave the message I think you get if you try to create a monobehaviour via new.

    The code path is a class referenced from a shared dll. Basically there is a ScriptableObject that contains that class, which has a field for an AnimationCurve. The field name is AnimationCurve if that matters at all.

    The job was an IJob, only one running at any given time.
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,639
    That's fine and no real reason to disbar it from threading accessibility (and not implying you meant that). I did like the extra information though - had no idea it was actually cached. Would be useful to put in manual as well, now jobs are the new normal.
     
  5. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    286
    We looked at it a bit more, and this seems to be a side effect of migrating to a new C# to C++ binding system. The new binding code generator seems to be defaulting to non-thread safe by default.

    We're going to force it back to what it was before, because barring non-main thread access just in case is pretty sad. Just be aware that modifying a curve on one thread and accessing it on another is dicey. It's not limited to evaluation.
     
    karl_jones and hippocoder like this.
  6. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    286
    If anyone has wants to file a bug with a repro scene, it's always appeciated.
     
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,639
    Yep can totally understand that :)

    But I am fairly sure the docs team will need to become jobs-aware as people will probably be excited by the whole "oh threading is perfectly safe now" thing and file bug reports on various parts of Unity, but this is just a general Unity problem.
     
  8. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    I hit another case where access is being blocked that I wonder if it's related. It throws an error trying to set keys saying SetKeys can't be called in serialization callbacks. MapMagic is effected by this, it can't save/load it's node editor data.
     
  9. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    286
    rizu and karl_jones like this.
  10. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    286
    @snacktime I am not 100% certain, but this is probably the same issue. It should affect all methods on animation curve.
     
  11. rizu

    rizu

    Joined:
    Oct 8, 2013
    Posts:
    1,177
    @DavidGeoffroy issue tracker indicated that this is fixed in future release. Do you have more specific target version for the fix? Like, is it planned to be fixed in 2018.2 release or later? I still get this on 2018.2.0b8.
     
  12. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,407
    Hmm I know they fixed at least part of this as I have a test project with some third party assets that were affected. And in beta 8 they started working. Guessing since the issue didn't mention all the api's affected that they missed some stuff?
     
  13. rizu

    rizu

    Joined:
    Oct 8, 2013
    Posts:
    1,177
    Could be. Asset affected by this is Particle Playground. It's already deprecated so the original author is not going to update it anymore, just noticed that it still worked on 2018.1 but fails on 2018.2 with following error message:
     
  14. rizu

    rizu

    Joined:
    Oct 8, 2013
    Posts:
    1,177
  15. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    1,191
    I’ve opened a bug for the gradient. We will get it fixed. Thanks for reporting it!
     
    rizu likes this.
  16. rizu

    rizu

    Joined:
    Oct 8, 2013
    Posts:
    1,177