How to write a python script for EnSight

If there is an analysis that you do quite often, then you probably want to create a python script to automatize the steps, in order to have a work flow less prone to errors and to speed up your analysis. Moreover, having a python scripts that guides EnSight allows you to run EnSight in batch mode, with all the advantages this brings.


How can you create your first python script for EnSight?

Open EnSight in interactive mode. While doing your analysis, you can see that in the File -> Commands window some commands are generated to register the analysis that are being performed. Copy those commands by simply selecting them -> right click: copy, in the Commands widget. 

Now, access the python API by clicking on the python Tab. Open a new file and copy the commands. Select them and use the Edit -> Convert selection to native python option. This will translate your commands into the equivalent EnSight python language. You can now use these new python commands into a normal python script to guide the analysis in EnSight.


A more sophisticated script

If you look at the python commands that you have generated in the previous section, you will notice that they start with ensight.*  Indeed the python API contains the library ensight, through which you can access all the commands of EnSight and query and set the properties of EnSight's objects.

Let's see for example the ensight.objs.core library. This library allows you to access the attributes to EnSight's objects such as parts, annotations, viewports. For example, suppose you have created some XYZ clips on your model, and want to access the values at which these clips were created. The following script will cycle over all the parts, find the parts that were generated as XYZ clips (p.PARTTYPE==1) and print the corresponding part name and value:


from ensight.objs import *

for p in core.PARTS:
   if p.PARTTYPE == 1:
      print p.DESCRIPTION
      print p.VALUE


You can find a list of all the attributes in the python objects API classes in the User Defined Tools -> Utilities -> Development -> Python Object API


Create your own User Defined Tool

EnSight's python API doesn't only allow you to create and ran your own scripts, but also to create your own User Defined Tool, that you can save and load in EnSight at start-up, in order to personalize the software to your analysis and needs. For a detailed description of how to create them, see the How To Manual, section How to produce customized access to tools & features. In this section, we will simply modify the above script into a User Defined Tool.

The first step is to add a header that will tell EnSight this is a UDT. Next, we need to create a new instance of the class tool_extension. To do this we need to: load the correct ensight libraries, define name, description, icon of the tool, which function to run when the tool is called. The script we have written above will be inside this latter function. Find in attachment the UDT script that transforms the above script in a UDT. 

Once all of this is done, you will need to load the script into your distribution of EnSight. To do this, place it in the $CEI_HOME/ensight101/site_preferences/extensions/user_defined/Tools directory, or in your local user specific EnSight Defaults Directory (see How To manual for more details). Then, open EnSight from command line with the  


flag to refresh the python cache. From the next time you open EnSight, you will see your new UDT.


The beauty of User Defined Tools

UDT are easy to design and are powerful tools in personalizing EnSight to the users' specific needs. Of course it is possible to create more complex tools, involving i/o, decision making conditions, loops. It is even possible to implement your own GUI pop-up windows, to allow you to set parameters before launching the UDT.


Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request