Run AnyBody step by step through Python

Dear AnyBody community

I am experiencing problems with executing the inverse dynamic function of my AnyBody model step by step (F6 in AnyBody) through Python.
I tried the following macro command in which I reload the main.any and include every time one additional step:

macro = [
'load, "Main.any" '
]

for i in range(0,11):
macro.append(
'operation Main.RunAnalysis.InverseDynamics "step" ',
)
Knee_JRF = app.start_macro(
macro,
folderlist,
search_subdirs=r"\Main.any",
logfile="BatchProcessingLogs_JRF/RunAndSave.txt",
)

However the "step" is not executed correctly.
Additionally, I want to extract in every position (thus every step) the stl of femur and tibia. I tried to save these stl using the following macro command:

macro_save1 = [
'classoperation Main.HumanModel.BodyModel.Right.Leg.Seg.Thigh.Drw3 "Export surface" --file=Femur.stl" --scale=1 --reference=global ',
'classoperation Main.HumanModel.BodyModel.Right.Leg.Seg.Shank.DrwSurfTibia "Export surface" --file=Tibia.stl" --scale=1 --reference=global',
]

However, the saving is canceled.
I want to thank you in advance for helping me with this!
Aline Van Oevelen

Hi Aline

I don't think I have heard about anyone doing something like that :sweat_smile: I will try it myself, and get back to you.

There may be some work-arounds if the "step" operations can't be interleaved with the export. But let talk about that when I have taken a closer look.

Best wishes,
Morten

Hi Aline

It think it should work. I tried on small toy example and the following python code worked for me:

Hope it makes sense.

import anypytools

def export_macro(obj, filename):
    macrostr = f'classoperation {obj} "Export surface" --file="{filename}" --scale=1.0 --reference=global'
    return macrostr


macro = [
    'load "Demo.arm2d.any"',
    'operation Main.ArmModelStudy.InverseDynamics',
    'step','step','step', # These first 3 steps are initial conditions
    'step',
    export_macro("Main.ArmModel.Segs.LowerArm.PalmNode.DrwSTL", "out-0.stl"),
    'step',
    export_macro("Main.ArmModel.Segs.LowerArm.PalmNode.DrwSTL", "out-1.stl"),
    'step',
    export_macro("Main.ArmModel.Segs.LowerArm.PalmNode.DrwSTL", "out-2.stl"),
    'step',
    export_macro("Main.ArmModel.Segs.LowerArm.PalmNode.DrwSTL", "out-3.stl"),
    'step',
    'step',
    'step',
    'step',
    'step',
    'reset', # This final reset seems to be necessary to avoid an error message when AnyBody Shuts down
]

print(macro)

app = anypytools.AnyPyProcess()
app.start_macro(macro)

Hi Morten

Thank you for helping me out!
I had to adapt one line of your code in my script to make it work:

'operation Main.RunAnalysis.InverseDynamics',

Best wishes
Aline