Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to set relaxed state on first frame? (Sth. similar to prewarm?)
#1
Hey Everyone,

the team I work with recently bought obi rope and we are very happy with it however there is one thing we could not figure out at all and searching through the forum did not give us any results, maybe I am just missing the right keyword.

Is there a way to start the scene with a rope already in it's relaxed state?


Given the existing API I guess i could write this on my own, enter playmode, simulate idk 100 frames, capture current state of all particles, write them back into the blueprint or another asset and reapply all those values on start next time. 
However it seems like this kind of init / set initial state function should be so common it might already exist?


thanks for your advice
Reply
#2
Hi there,

You can use actor.SaveStateToBlueprint(blueprint). This will save current particle positions and velocities to a blueprint. Make sure you save to a copy of your current blueprint, though! (that is, var passThisOne = Instantiate<ObiActorBlueprint>(rope.blueprint); )

Then use it the next time you want to instantiate the actor.

Btw, this is the method Obi uses internally when you switch to a new solver at runtime, to ensure data from the current solver is passed on to the new one: before leaving the current solver, it calls SaveStateToBlueprint on a copy of its current blueprint, and after entering the new solver, it loads this copy instead of the original blueprint.

cheers!
Reply
#3
Thanks for your quick reply, sounds great, but I am not getting it to work exactly as expected.

This is the script and a video trying to execute it. I do the entire process twice, on the first try it does create a new blueprint with the correct name but after assigning it the inital state does not change, then I execute the function again writing to the current state to that same currently selected initializedbp blueprint and then it keeps that state while not in play mode but as soon as I hit play it resets back to the original state.



(note: the newly saved blueprint does not have definitions for attachment points, you still need the old blueprint too)
Code:
using UnityEngine;
using UnityEditor;
using Obi;

public class Test : MonoBehaviour
{
   public ObiActor actor;

   [ContextMenu("SaveBpState")]
   void SaveBpState()
   {
       var passThisOne = Instantiate<ObiActorBlueprint>(actor.blueprint);
       actor.SaveStateToBlueprint(passThisOne);
       AssetDatabase.CreateAsset(passThisOne, "Assets/initializedBp.asset");
   }
}

btw I use CreateAsset to save a new copy of the passThisOne blueprint, because I noticed that when I ctrl+d duplicate a blueprint asset it is not a straight copy, but turns into a container instead?
[Image: bWeNtrU.jpg]
Reply
#4
Woops, there's a small detail that prevents assigning the blueprint manually from the editor, only allowing to do it programmatically. In ObiRopeBlueprintBase.cs, lines 48-51:

protected void OnValidate()
       {
           GenerateImmediate();
       }

Blueprints are automatically re-generated from the rope path when they're validated (and they're validated when you drag them around in editor, so by dragging it the blueprint is being reset to match the rope path). This is already taken care of the rope editor itself, so comment out the GenerateImmediate(); line. Things will works as expected now.
Reply
#5
Works like a charm, thank you very much Gran sonrisa

SaveStateToBlueprint is not documented right? Is there another place I could have found that function?
http://obi.virtualmethodstudio.com/api.html



mmmhh it is still not entirely without any issues. For anyone else missing this feature:
if I comment out the GenerateImmediate(); line most things keep working as expected but when I change a property on a blueprint like resolution or thickness the ropes don't get updated with this new information.
You need to call GenerateImmediate from a custom inspector of/like ObiRopeBlueprintEditor. This still clears the initialized state, but that is okay since changing the blueprint changes amount of particles etc too.
Reply
#6
(27-04-2020, 05:05 PM)josemendez Wrote: Woops, there's a small detail that prevents assigning the blueprint manually from the editor, only allowing to do it programmatically. In ObiRopeBlueprintBase.cs, lines 48-51:

protected void OnValidate()
       {
           GenerateImmediate();
       }

Blueprints are automatically re-generated from the rope path when they're validated (and they're validated when you drag them around in editor, so by dragging it the blueprint is being reset to match the rope path). This is already taken care of the rope editor itself, so comment out the GenerateImmediate(); line. Things will works as expected now.

Hi, I can't find ObiRopeBlueprintBase.cs file for this tip. Did something chaange?
Reply
#7
(19-11-2021, 10:14 AM)greyhawk Wrote: Hi, I can't find ObiRopeBlueprintBase.cs file for this tip. Did something chaange?

Nope, this has been there from the start and it's still there. Using Unity's project search bar should point you to it. Just in case, it's located here: /Obi/Scripts/RopeAndRod/Blueprints/ObiRopeBlueprintBase.cs

However, the change mentioned in this thread was included in the asset a long time ago. There should be nothing to modify as of now.
Reply
#8
(19-11-2021, 10:18 AM)josemendez Wrote: Nope, this has been there from the start and it's still there. Using Unity's project search bar should point you to it. Just in case, it's located here: /Obi/Scripts/RopeAndRod/Blueprints/ObiRopeBlueprintBase.cs

However, the change mentioned in this thread was included in the asset a long time ago. There should be nothing to modify as of now.

My bad. I found it but there is no
"protected void OnValidate()
       {
           GenerateImmediate();
       }"
part.
Reply
#9
(19-11-2021, 10:35 AM)greyhawk Wrote: My bad. I found it but there is no
"protected void OnValidate()
       {
           GenerateImmediate();
       }"
part.

Yes, as I mentioned this was removed a long time ago. There's no need to change anything now, assigning a blueprint should work fine.
Reply