Running external Python file from Python Code object in Mechanical

arranosr71
arranosr71 Member Posts: 4
Name Dropper First Comment
**

Hello,

I am trying to run a post-processing script for my thermal results that uses Numpy and Scipy libraries.
However, those are not available in the ANSYS 2021 version, so I am trying to do that by using a Python Code obejct in the interface that calls my post-processing script via a command window.
Sadly, I am a bit lost as the script does not seem to be running even though no error message appears.

I'all appreciate your help.
Thank you

Answers

  • arranosr71
    arranosr71 Member Posts: 4
    Name Dropper First Comment
    **

    This would be the code I am using.

    import subprocess
    import os

    solver_files_directory = ExtAPI.DataModel.Project.Model.Analyses[0].WorkingDir
    anaconda_1 = "@echo OFF\n"
    anaconda_2 = 'call conda activate C:\ProgramData\Miniconda3\envs\py37_v1\n'
    anaconda_3 = 'call conda deactivate\n'
    anaconda_4 = 'pause'

    python_executable = "python"

    script_path = r'Y:\gen_path.py'

    arguments = [solver_files_directory, "Selection.txt", "Selection 4.txt", "Selection 2.txt", "Selection 3.txt"]

    command = "{} {} {} {} {} {} {}".format(anaconda_1, anaconda_2, python_executable, script_path, ' '.join(arguments), anaconda_3, anaconda_4)

    subprocess.Popen(["cmd", "/c", "start", "cmd", "/k", command])

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 295
    25 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited July 1

    There are lots of ways to do this, which you can explore on the internet. At Ansys we do not have access to Anaconda, so we cannot debug your methodology. Personally, I like to create and run a ".bat" file to make it easier to develop and debug my CPython script independent of Mechanical's IPython:

    import os
    release = Ansys.Utilities.ApplicationConfiguration.DefaultConfiguration.VersionInfo.VersionString
    pythonEXE = r'%AWP_ROOT{}%\commonfiles\CPython\3_7\winx64\Release\python\python.exe'.format(release)
    RunDir = r'D:\Test'
    BatFile = os.path.join(RunDir , "RunCP.bat")
    OutFile = os.path.join(RunDir , "log.txt")
    CPscript = os.path.join(RunDir , "myscript.py")
    with open(BatFile, "w") as f:
            cmd = '"{}" "{}"'.format(pythonEXE,CPscript)
            f.write(cmd)
    os.system(BatFile+" > "+OutFile)
    
  • arranosr71
    arranosr71 Member Posts: 4
    Name Dropper First Comment
    **

    Thank you Landon, it worked. My python script that is called by the bat file produces a couple of png outputs. Do you think it would be possible to show these in the ANSYS solution tree, as a branch of the Python Code obejct perhaps?

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 295
    25 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited July 2
    myfile = r"D:\Test\capture.png"
    solution = ExtAPI.DataModel.Project.Model.Analyses[0].Solution
    solution.AddImage(myfile)