How do I record solver data using Python?

How do I record solver data using Python?

Anonymous
Not applicable
1,413 Views
3 Replies
Message 1 of 4

How do I record solver data using Python?

Anonymous
Not applicable

We're trying to use MB in our pipeline to drive certain physics related animation.

 

For instance, an enemy drops their weapon at a particular frame when it's parented to a joint for every frame before that moment. In our custom tools, we mark the frame that the weapon should drop and we re-process the FBX in our MB pipeline. The goal is to switch from the parented bone animation to the simulated drop of the weapon at a particular frame. 

 

I feel like I'm jumping through hoops trying to trick MB into doing what we need. I must be doing something wrong. 

 

In order to make this work in our automated pipeline:

  1. I duplicate the weapon geometry at the drop frame
  2. I create a solver/body and connect all of the things
  3. I execute my_solver.evaluate_simulation(weapon_geo, start_fb_time, end_fb_time)

Of course that third step doesn't work because there is no such function on solvers. 

 

Instead, my third step looks something like this:

  1. FBSystem().Scene.Evaluate()
  2. player.DeleteAllGlobalTimeMarks()
  3. mark_index = self.player.AddGlobalTimeMark(FBTime( 0, 0, 0, self.end_frame))
  4. player.SetGlobalTimeMarkAction(mark_index, FBTimeMarkAction.kFBTimeMarkAction_Stop)
  5. select(self.weapon_dummy)
  6. player.LoopActive = True
  7. solver.Active = True'
  8. solver.Live = True
  9. player.Play(True)
  10. player.Record(True)
  11. FBSystem().Scene.Evaluate()
  12. FBSleep(1000)
  13. while player.IsPlaying:
  14.     FBSleep(500)
  15. FBSystem().Scene.Evaluate()
  16. solver.Active = False
  17. solver.Live = False
  18. player.DeleteAllGlobalTimeMarks()
  19. FBSystem().Scene.Evaluate()

While this does technically bake some keys to the default animation layer (not the selected or active one, for some reason), it completely breaks everything afterwards. I can't do anything else in Python because the main thread never evaluates anything until my Python script has terminated. 

 

I have tried an embarrassing number of hacks to try and get this to work. I even tried saving the file and opening it once the timeline stops play. But, alas, the baked keys don't exist in the saved file because MB never seems to re-evaluate after my Play() call. 

 

What am I doing wrong?

Certainly there is some better way to bake simulation data, right?

 

I am delirious, I assume this post isn't even clear. I'll provide more information if needed. I'll send gifts. Please help me. 

 

0 Likes
1,414 Views
3 Replies
Replies (3)
Message 2 of 4

jps
Advocate
Advocate

Not sure if this is something you could consider, but as simulation is unpredictable in time, why not give the user the ability to stop the simulation?

def plot(OverwriteTake=False):

    FBMessageBox("RECORD CALCULATION","Press OK to start recording\n\nUse STOP button from Transport Controls to stop recording","OK")

    goToStartingFrame()

    lPlayer = FBPlayerControl()

    lPlayer.Record(OverwriteTake,True)

    lPlayer.Play(False)

 

I've done that for a ragdaoll tool.

My problem was to properly initialize the start frame solving. If my rigid body "Active" param was switch on at a different frame than the first frame, solving was ignoring that start frame. I had do a trick for the initialization: Go to start, move to second 2 frame, move back to first frame. That way it was properly initialized.

 

def goToStartingFrame():

   takeStartTime = FBSystem().CurrentTake.LocalTimeSpan.GetStart()#.GetFrame()

   frameTwo = takeStartTime + FBTime(0,0,0,1)

   lPlayer = FBPlayerControl()

   lPlayer.Stop()

   lPlayer.Goto(frameTwo)#Force simulation to refresh calculation

   lPlayer.Goto(takeStartTime)

 

 

Not sure if it can help.

0 Likes
Message 3 of 4

jps
Advocate
Advocate

One other thing that was problematic was the starting of the solver.

It was either doing noting or noting was recorded. I had to activate and make live the solver in 2 pass from different functions while forcing the player to go to first frame and do the frame +1 frame -1 trick in-between each call.

 

def setSolverLive(Bool,lSolver):

lSolver.Live = Bool

 

def solverRestToStart(lSolver):

lSolver.PropertyList.Find("Reset to start",False)()

 

def setSolverOnline(Bool,lSolver):

lSolver.Active = Bool

 

--- the following functions were into the class that was activating the solver. goToStartingFrame() is the same function as on the previous reply---

 

def _ActivateSolving(self):

lSolver = createSolver(solverName)

goToStartingFrame()

setSolverOnline(True,lSolver)

setSolverLive(True,lSolver)

goToStartingFrame()

 

def _DisableSolving(self):

lSolver = getSolver(solverName)

setSolverLive(False,lSolver)

goToStartingFrame()

solverRestToStart(lSolver)

setSolverOnline(False,lSolver)

 

def _ResetSolving(self):

lSolver = getSolver(solverName)

goToStartingFrame()

solverRestToStart(lSolver)

0 Likes
Message 4 of 4

Anonymous
Not applicable

There is no user in my case, it's part of an automated mocap pipeline. I was able to solve the issue but it was more complicated than it should have been. 

 

The issue boiled down to a weird quirk in the way MotionBuilder manages threading from Python scripts, kinda.

 

It seems as though once Python controls the playbar, which is required to start a simulation (along with record), the state of the MotionBuilder scene never recovers cleanly to the point where continued modifications can be made as part of that same script. 

 

That's not entirely true.

 

It's not really about Python controlling "play" & "stop", it's knowing when the playhead stops and what was needed to figure that out. Controlling the playhead using Python does not hold up the main Python thread, so the rest of a script continues on. This is desirable only if the last thing you need your Python script to do is hit "play". 

 

That in itself isn't really an issue, except that when you run a sim, you need to know when the playhead stops, so you can continue processing that scene. 

 

So, I tried using FBSleep()

 

My solution was to set a timeline marker on the last frame of the timeline (set to "stop" the playhead). Then, set the player to "Play" and "Record" while setting my solver to "active". Then, calling FBSleep() in a loop, checking for the state of the player to go from "playing" to "stopped", then continuing on with my other scene management functions. 

 

Enter the actual problem: The scene never recovers from FBSleep in a way that you expect. Technically, everything I just mentioned worked fine. The sim did record. The playhead does stop. I retained control in my script and was able to continue doing stuff. 

 

Except that the scene was in some weird state for the duration of my script. Even calling FBSystem().Scene.Evaluate() like a maniac didn't do anything. It's hard to explain what was broken about the scene after calling FBSleep() and before the script stopped running, because that weird state was restored as soon as my script was finished. But it was weird, and things were missing, and it drove me crazy. 

 

All I wanted was some sort of loop to process each of my functions asynchronously. 

 

Enter MB UI & FBSystem().OnUIIdle.Add(self.update)

 

This was the key to my solution. 

 

I was able to use the UI system to call an update function as a sort of hacked together loop that watches the result of my Python scripts asynchronously.

 

The OnUIIdle() event is fired often. So often that I added a timer to check the state of everything every few seconds. 

 

There was no need to actually build a UI component, except for the fact that it utilizes a different MB thread than normal Python scripts, allowing me to have a clean update loop that could keep an eye on things like "is the playhead playing?" and "if so, when it stops, should we initiate another function?". 

 

This allowed me to start a sim, then wait for the playhead to reach the end of the timeline and stop. Then so other processing.

 

I'm explaining this just in case some future person is losing their mind with MotionBuilder's Python integration. 

 

 

 

 

0 Likes