General Mechanical

General Mechanical

Automatically run a Python script when starting Mechanical

    • fabian.maier
      Subscriber
      Hello i wrote a Python script which basically imports a views.xml file and loads different model views from this file. This script already works fine but i would like to run it automatically each time a Mechanical Session is launched.
      I don't want to use the "Connect/Run Python Code Objects When Mechanical is Launched", because the solution is being deleted when adding a Python Script to the Model Tree.
      ACT would be an option, but i did not find a proper function to automatically call the function.
      Is there any way to automatically run a python script when a mechanical is started?
      Thanks in advance!
    • Aniket
      Ansys Employee
      Would you be able to create an extension and make it default, and then run this code using onload callback of this extension?
      -Aniket
      How to access Ansys help links
      Guidelines for Posting on Ansys Learning Forum
    • fabian.maier
      Subscriber
      Hello Aniket, thank you for your reply.
      I tried to implement this in my extension, unfortunately, this doesn't work for some reason. I tried the extension with OnInit, OnReady and OnLoad, but nothing worked for me.
      My xml file looks like this:



      AutoImportViews



      And the function AutoImportViews looks like this: ´╗┐
      def AutoImportViews():
      import os
      result_loc = Model.Analyses[0].AnalysisSettings.SolverFilesDirectory
      #dosomethinghere
      ´╗┐ #dosomethingmore
      Is this setup correct for my application?
      If i use the callback what function input do i use in def AutoImportViews(input):? According to the ACT Guide it should be:

      Called when a project is loaded.
      [function(currentFolder)]
      but i don't know how to set the correct input for OnLoad.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.