Open AnyBody GUI with Python and replay study

This is a short description how to automatically replay a previous study in the AnyBody GUI using Python and AnyPyTools.

Given: a saved study e.g. by right clicking Output and choosing “Save data”.

Add the following to the AnyBodyStudy, to make a replay available:

AnyBodyStudy Study = { 
    ...
    // reference to the replay to make it selectable in the Operation dropdown menu
    AnyOperationSequence ReplayStudy = {
      AnyOperation& rep = Main.Study.Replay;
    };
}; // End of study

In Python this was used to create the macrofile in AnyPyTools and automatically start the AnyBody GUI loading the calculated study and replaying:

import os
import subprocess

from resources.AnyPyTools.anypytools.macro_commands import (MacroCommand, Load, OperationRun)

class AnyPy:
   LOAD = 'load'
   LOAD_H5 = 'load_h5'
   REPLAY = 'replay'

   def __init__(self, main_filepath, h5_filepath):
      self.any_path, self.any_model = os.path.split(main_filepath)
      self.main_filepath = os.path.normpath(main_filepath)
      self.h5_filepath = os.path.normpath(h5_filepath)
   
   def replay(self):
     macro_output_path = 'classoperation Main.Study.Output "Load data" --file="{}"'.format(self.h5_filepath)

     # build the macrolist executed by AnyPyTools
     operation_cmd = {AnyPy.LOAD: Load(self.main_filepath),
                      AnyPy.LOAD_H5: MacroCommand(macro_output_path),
                      AnyPy.REPLAY: OperationRun("Main.Study.ReplayKinematics")}

     macrolist = []
     for operation in operation_cmd:
         macrolist.append(str(operation_cmd[operation]))

     print('Starting Anybody with the macros:\n{}'.format(self.macrolist))
     print('Executing "{}" in "{}"'.format(self.any_path, self.any_model))

     # save current working directory and change to AnyBody project folder
     cwd = os.getcwd()
     os.chdir(self.any_path)

     # write macro file to be opened by AnyBody GUI
     macro_replay_path = os.path.join(self.any_path, 'replay.anymcr')
     with open(macro_replay_path, 'wb') as macro_file:
         macro_file.write("\n".join(macrolist).encode("UTF-8"))
         macro_file.flush()
         anybodycmd = [os.path.realpath('C:/Program Files/AnyBody Technology/AnyBody.7.1/AnyBody.exe'),
                       "-m", macro_file.name]

     # execute AnyBody GUI with the command from anybodycmd
     subprocess.Popen(anybodycmd)

     # change back to original folder
     os.chdir(cwd)


AnyPy(
    r'C:\Program Files\AnyBody Technology\AnyBody.7.1\AMMR\Application\Examples\FreePosture\FreePostureFullBodyMove.Main.any',
    'output.anydata.h5').replay()

We assumed that the output.anydata.h5 file is in the same directory as the Main model file.
You might have to adapt the import path of the AnyPyTools.

Now in AnyBody you can select ReplayStudy from the Execute dropdown menu and use the slider to change the step.

I hope this is helpful. Thank you Morten for helping us on this.

Best regards
Sean

Another approach was suggested by the AnyBody support (Morten):

Create an AnyOperationSequence which loads and replays data. With that solution the replay bar should be active afterwards:

AnyOperationSequence LoadAndReplay = {
  AnyOperationMacroLoad = {
     MacroStr= {
       "classoperation Main.Study.Output "+ strquote("Loaddata")
       + " --file=" + strquote("C:\Users\mel\Desktop\test.anydata.h5")};
  };
  AnyOperation& rep  = Main.Study.Replay;
};

Is there a general option to call the AnyBody.exe with parameters (i.e. giving a path to a Main.any file)?

Yes. That is what AnyPyTools does for you, but for the console application. You could do the same things for the GUI application. AnyPyTools creates Macro files which are passed to the console application

anybodycon.exe -m "macrofile.anymcr"

The same approach is possible with AnyBody.exe (the GUI application).

This topic was automatically closed 125 days after the last reply. New replies are no longer allowed.