Table of Contents
This document covers how to set up a Python script that can be both executed from the command line and from the within a MineSight® product.
That you are familiar with Python and have basic understanding of how MineSight® Grail integrates with the MineSight® System.
MineSight® provides you with a basic-boilerplate.py script located in the $(medexe)\scripts\samples directory. The steps to creating your script are,
- Copy and rename the basic-boilerplate.py script to your new script name.
- Open the new script in your favorite editor.
- Go to the run_code() function and add your Python script.
This boilerplate can be used to discuss a few points regarding scripts.
import grail
Any statement that uses the import grail statement is indicating that you wish to use features of the MineSight® Grail library.
Note
If your script imports a module from either the grail.ip or grail.ms3d sub-packages then your script will only execute within the context of MineSight®-IP or MineSight®-3D respectively. If you wish to create a script that executes within MineSight®-3D see HowTo: Create an Embedded Script.
What is gmain?
The gmain function is a standard entry point for all scripts. This is the location in the script that any MineSight® product can communicate through.
Of the numerous messages that can travel through a gmain function you are generally only concerned with the messages.gRUN message. All messages can be found in the grail.messages module.
if __name__=="__main___" idiom
If any script is executed from the command line like,
python.exe myscript.pyThen the __name__ attribute will be set to "__main__". Using this idiom will guarantee that anything below the if statement will be executed if and only if the script is executed from the command line. However, if the script is executed from within a MineSight® product, the script will start with the gmain function and ignore anything below the if statement.
If you use the gsys.GMAIN helper, you can do the following,
gmain = gsys.GMAIN(main, __name__)
gmain.run()