Insert results and export image and animation

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 715
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
edited June 2023 in Structures
  • In Mechanical scripting, I would like to add a total deformation result and an equivalent stress result, evaluate those results and export an image (.png) and an animation (.avi) for each result.

Best Answer

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 715
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓

    Here is an example:

    import os
    export_folder = r'D:\Data'
    
    analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
    solution = analysis.Solution
    solution.AddTotalDeformation()
    solution.AddEquivalentStress()
    solution.EvaluateAllResults()
    
    all_results = solution.GetChildren(DataModelObjectCategory.Result,True)
    for result in all_results:
        export_name = os.path.join(export_folder,result.Name)
        ExtAPI.Graphics.ExportImage(export_name +'.png')
        result.ExportAnimation(export_name + '.avi')
    

Answers

  • Erik Kostson
    Erik Kostson Member, Employee Posts: 87
    First Answer Name Dropper First Anniversary Ansys Employee
    edited November 2023

    Thank you - this is really helpful. If we already have a set of existing results and we want to save an image for every result in a separate file then the for loop really helps and with the additional line (Tree.Activate):

    import os
    export_folder = r'D:\Data'
    analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
    solution = analysis.Solution
    
    all_results = solution.GetChildren(DataModelObjectCategory.Result,True)
    for result in all_results:
        Tree.Activate(result)
        export_name = os.path.join(export_folder,result.Name)
        ExtAPI.Graphics.ExportImage(export_name +'.png')
    
  • sombodyfromtheworld
    sombodyfromtheworld Member Posts: 26
    First Comment Name Dropper
    edited December 2023

    Hi
    I would like to AppendGraph to my image as well. How can I do that?

    GRAPHICS = ExtAPI.Graphics
    GRAPHICS.ExportViewports('C:/.../image.png', GraphicsImageExportFormat.PNG, GraphicsViewportsExportSettings().AppendGraph = True)
    

    like this:

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 715
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member

    Hi @sombodyfromtheworld , exporting the graph view cannot be done through the supported APIs. I'd recommend grabbing the values of the plot (result.PlotData) and reconstructing the graph elsewhere if needed. There are some unsupported/undocumented methods to export the graph, but there is no guarantee they'll work: https://discuss.ansys.com/discussion/1214/how-to-take-a-screenshot-of-your-mechanical-application

  • sombodyfromtheworld
    sombodyfromtheworld Member Posts: 26
    First Comment Name Dropper
    edited December 2023

    @Pernelle Marone-Hitz you mean that I can access X,Y coordinates of the plot and build the custom plot for instance in matplotlib?

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 715
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member

    You can't access the values of the graph directly, but you can access the values of the 3D plot, and rebuild the graph (I acknowledge it is not straightforward).

  • @Pernelle Marone-Hitz could you please post the command to access the values of the 3D plot.

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 715
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member

    @sombodyfromtheworld There are already a couple of examples of using PlotData on the forum. For example, this one: https://discuss.ansys.com/discussion/comment/1511#Comment_1511

  • sombodyfromtheworld
    sombodyfromtheworld Member Posts: 26
    First Comment Name Dropper
    edited December 2023

    @Pernelle Marone-Hitz I see. PlotData returs only last substep values (at 1s in my case). In order for me to build the plot I need to EvaluateAllResults() at each substep and retrive max value from PlotData.

    something laike this, right?

    time_X_coord = []
    values_Y_coord = []
    for substep in np.arange(0, 1.1, 0.1):        
        new_stress.DisplayTime = (Quantity, substep)
        new_stress.EvaluateAllResults()
        max_stress_value = max(new_stress.PlotData['Values'])
        time_X_coord.append(substep)
        values_Y_coord.append(max_stress_value)
    

    or can I just return the TabularData from this table?

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 715
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member

    Indeed, for PlotData, you would need to change the time step and re-evaluate again. You can also grab the tabular data, but it also uses unsupported/undocumented commands: https://discuss.ansys.com/discussion/255/how-to-access-tabular-data-using-act

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 239
    First Anniversary First Comment 5 Likes Ansys Employee
    edited December 2023

    @sombodyfromtheworld ,
    If you would like time-history, data from the results, most often the best way to get this is through the data processing framework (DPF). Create your own python code, using DPF to extract the results for all-time points.
    There are a number of examples on this forum of how to extract different results using DPF. It is a bit more work, but provides much more control on what data you want, and how to export it. Also, once you have set up the code for one type of result, it is often just a couple lines to change for a different result. For example, changing from equivalent stress to principal, stress, or a stress tensor to a strain tensor.