Lesson 1: Hello World
To familiarize yourself with the basic use of the Quick Python Scripts panel, here is an interactive tutorial using the sample KeyFrame.py (also contained in your examples/python/ directory).Now, we will write the ubiquitous Hello World. Here is the code:
import TecUtil
import TecVals
def TP_hello_world():
TecUtil.DialogMessageBox("Hello, world!",TecVals.MessageBox_Information)
Save the file into your examples/python directory. Load and run it via quick macro panel as shown on the right.
You will see this:

Now for some explanation. These lines:
import TecUtilimport TecVals
include the set of Tecplot constant definitions and functions documented in the ADK reference manual. We've tried to make these function calls as python-like as we could. This next line:
def TP_hello_world():
defines the function to call. As a convention, any function beginning with TP_ will be visible from the Quick Python Scripts panel for that module (with the TP_ removed). We did this to give users flexibility in what functions were displayed in the list. For example, without this filtering, importing a library like NumPy or SciPy would show a long, unwieldy list of functions.Finally, this line:
TecUtil.DialogMessageBox("Hello, world!",TecVals.MessageBox_Information)calls the Tecplot function to post a message box. The function's parameters are a message to post and the type of message. If I were a real programmer, I would check the return value of the function.
Let's make this slightly more useful and show another feature of the QuickPythonScripts panel, the ability to pass parameters. Here's the source, hello_ymh.py.
When loaded, you'll notice a slight change when the function is selected:def TP_hello_world(your_message_here):
return TecUtil.DialogMessageBox("Hello, " + your_message_here + "!",TecVals.MessageBox_Information)
We can now send a parameter to the script. For example, I've added my name. When the function's run, the obvious happens:
Multiple parameters are separated by a comma. For example, the debugv function takes a message and a value. (And, illustrating the earlier point about this dialog not showing functions that are not prefaced by TP_, TP_debugv calls another function, debugm.
For those who have used Tecplot's macro language, you can record Tecplot macros that load and call functions in the Quick Python Scripts. This might be useful if you're doing a batch set of operations. For example:
$!EXTENDEDCOMMAND COMMANDPROCESSORID = 'Python Utility'
COMMAND = 'RUNPYFUNCTION MODULE="hello_world" FUNCTION="hello_ymh" ARGUMENTS="Jim"'
0 TrackBacks
Listed below are links to blogs that reference this entry: Lesson 1: Hello World.
TrackBack URL for this entry: http://www.tecplottalk.com/b/mt-tb.cgi/3

Leave a comment