How to export Animations from Mechanical in batch from WB script?

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 242
50 Answers 100 Comments Second Anniversary 25 Likes
✭✭✭✭

I have a Modal analysis system with 100 deformations. I want to export all the animations in batch via WB scripting. All my deformations are grouped in a folder whose id can be used as an input to locate the result objects.

Answers

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 242
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    Here is an example script. You can run in from WB --> File --> Scripting --> Run Script File. You can track the time taken for each animation in the log file (WB --> Extensions --> View Log)

    #Example script to export animations
    mechScriptCmds = """
    ###User Inputs###
    import os
    import time 
    
    #Get the right analysis to store the data
    UserAnimPath = DataModel.AnalysisList[0].WorkingDir 
    GroupId=49
    
    ##################
    Graphics.ResultAnimationOptions.NumberOfFrames=30
    Graphics.ResultAnimationOptions.Duration=Quantity(2,'sec')
    settings = Ansys.Mechanical.Graphics.AnimationExportSettings(width = 1920, height =1080)
    expFormat=GraphicsAnimationExportFormat.MP4
    Group=DataModel.GetObjectById(GroupId)
    
    for i in range(0,len(Group.Children)):
        begin = time.time() 
        myResult=DataModel.GetObjectById(Group.Children[i].ObjectId)
        AnimPath=os.path.join(UserAnimPath,myResult.Name + ".mp4")
        ExtAPI.Log.WriteMessage(AnimPath)
        myResult.ExportAnimation(AnimPath,expFormat,settings)
        timetaken = time.time() - begin
        ExtAPI.Log.WriteMessage("Time taken for animation " + str(i+1) + " is :" + str(timetaken) + " s ")
    ############################
    """
    
    #Assuming we are working on 1st system in WB Project page
    #Get the right system to send the Mechanical script commands.
    mySys = GetAllSystems()[0]
    model = mySys.GetContainer(ComponentName="Model")
    model.Edit(Interactive=False)
    model.SendCommand(Language="Python", Command=mechScriptCmds)
    
    
  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 242
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    One can also do this export directly within Mechanical after the model solve is completed using Python Code object, with "After Post" callback. The script can be found below.

    def after_post(this, solution):# Do not edit this line
        """
        Called after post processing.
        Keyword Arguments : 
            this -- the datamodel object instance of the python code object you are currently editing in the tree
            solution -- Solution
        """
    
    
        # To access properties created using the Property Provider, please use the following command.
        # this.GetCustomPropertyByPath("your_property_group_name/your_property_name")
    
        # To access scoping properties use the following to access geometry scoping and named selection respectively:
        # this.GetCustomPropertyByPath("your_property_group_name/your_property_name/Geometry Selection")
        # this.GetCustomPropertyByPath("your_property_group_name/your_property_name/Named Selection")
    
        ###User Inputs###
        import os
        import time 
    
        UserAnimPath="D:\\animation_data\\"
    
        GroupId=49
    
        ##################
        Graphics.ResultAnimationOptions.NumberOfFrames=30
        Graphics.ResultAnimationOptions.Duration=Quantity(2,'sec')
        settings = Ansys.Mechanical.Graphics.AnimationExportSettings(width = 1920, height =1080)
        expFormat=GraphicsAnimationExportFormat.MP4
        Group=DataModel.GetObjectById(GroupId)
    
        for i in range(0,len(Group.Children)):
            begin = time.time() 
            myResult=DataModel.GetObjectById(Group.Children[i].ObjectId)
            AnimPath=UserAnimPath+str(myResult.Name)+".mp4"
            ExtAPI.Log.WriteMessage(AnimPath)
            myResult.ExportAnimation(AnimPath,expFormat,settings)
            timetaken = time.time() - begin
            ExtAPI.Log.WriteMessage("Time taken for animation " + str(i+1) + " is :" + str(timetaken) + " s ")
        ############################