Geomagic :: Knowledge Base Nuggets - Sharing values between scripts?

Sandham's Flying Circus

Ping-Fu-exec

Beyond the Box

The news and stories from Geomagic that you don't find anywhere else.

Ping-Fu-exec

Sandham's Flying Circus

The crazy world of customizing Geomagic products

Ping-Fu-exec

The World is 3D

Seeing the world in 3D from Ping Fu

April 09, 2012

Knowledge Base Nuggets - Sharing values between scripts?

When your scripting solutions become more complex...more powerful...you will most likely need to share values between functions and scripts.  You might even need to share information with programs outside of the GPSE.

The Geomagic Python Scripting Environment (GPSE) is just that...an environment...it can be thought of as a door or window to the current session or process of a running Geomagic industrial application...a programming interface to your data and our technology.  Each of our v2012 industrial applications: Geomagic Studio/Wrap/Qualify/Probe v2012 includes this programming interface.

Note: You can now find the GPSE Help through the standard Geomagic Help by pressing the Help Button in your open application, looking up "About Scripting" in the Search Tab, and opening the Scripting Documentation link to the html help system.

So, where am I going with all this...well, I am simply framing the nugget for this week.  The enviroment, the GPSE, is alive as long as the application is running...and it is in this enviroment that you can create and share objects: variables, classes, and functions (models, features, numbers, strings, code, etc.) in and between not only your functions in your script, but also with other scripts and even programs outside of the GPSE.  How do you share data between scripts?  Enjoy this weeks GPSE snack...

Q: How do I share values and functions between scripts using the Geomagic Python Scripting Environment (GPSE)?

The Geomagic Python Scripting Environment is a running instance of Python and employs the same heap and stack constructs that a Python console would have if it was started from a Windows command line console using "C:\Python27\python.exe".  This 'environment' is shared with any script that is executed during a running session of a Geomagic industrial application (Geomagic Studio/Wrap/Qualify/Probe v2012 and later.)


The key to the successful sharing of objects across scripts and functions is understanding scope.  Python is an indentation sensitive language and anything declared (or typed) at the far left of your script...against the left margin...is at the 'global' scope level.

Functions will have their own scope in Python.  Even try...except...finally, if, while, and for statements have a scope of sorts.  Python treats these differently than Classes or Functions, but that discussion is outside of this article's scope (pun intended.)  The point is that anything declared inside a function exists (lives and dies) within that function during the running of your script...not outside.  If a function you write creates a value you want to use elsewhere, you must pass it back to the calling script or use a global variable, otherwise the value is lost when the function exits and returns control to the calling script.

This example is a single script file and defines the function and passes the calculated value back to the variable in the assignment statement that calls the function. The script then creates two points, passes them to the function, and assigns the returned value to the variable used in the print statement.


import geoappall
for m in geoappall.execStrings: exec m in locals(), globals()

import geoapiall
for m in geoapiall.modules: exec "from %s import *" % m in locals(), globals()

def my_calculation( p1, p2 ):
   return( p1.dist( p2 ) )

point_one = Vector3D( -1, 0, 0 )
point_two = Vector3D(  0, 0, 1 )

myDist = my_calculation( point_one, point_two )

print "The distance between my points is:", myDist


This works whether my_calculation is called from this script or a completely different script (file).

This next example uses a global scope variable myDist, again all contained in a single script file.  The idea of the global variable is the next step in sharing values across script files...sharing objects within the GPSE:


import geoappall
for m in geoappall.execStrings: exec m in locals(), globals()

import geoapiall
for m in geoapiall.modules: exec "from %s import *" % m in locals(), globals()

def my_calculation( p1, p2 ):
   """
   my_calculation() - calculates the distance between two Vector3D (points)

   """
   global myDist
   MyDist = p1.dist( p2 ) )

myDist = 0.0

point_one = Vector3D( -1, 0, 0 )
point_two = Vector3D(  0, 0, 1 )

my_calculation( point_one, point_two )

print "The distance between my points is:", myDist


Note the differences in the function definition as well as the call to the function.  Not only do you need to declare the use of a global variable within the function, you no longer need to return a value.  Also, if the function is stored in another file you will need to parse or run that file prior to calling it.  Here is a multiple file example:

#
# file: functions.py
#

def my_calculation( p1, p2):
   """
   my_calculation() - calculates the distance between two Vector3D (points)

   """
   global myDist
   myDist = p1.dist( p2 ) )

#
# end of functions.py
#


...that is the function file...and then the script...

#
# file: myscript.py
#

import geoappall
for m in geoappall.execStrings: exec m in locals(), globals()

import geoapiall
for m in geoapiall.modules: exec "from %s import *" % m in locals(), globals()

default_macro_folder = Directories.getDirectory(Directories.DIRECTORY_MACROS) + "\\"

# global variable declaration
myDist = 0.0

point_one = Vector3D( -1, 0, 0 )
point_two = Vector3D(  0, 0, 1 )

# parse functions
execfile( default_macro_folder + "functions.py" )

# call my calculation
my_calculation( point_one, point_two )

print "The distance between my points is:", myDist

#
# end of myscript.py
#


This uses the execfile command from Python and simply executes the functions.py file, which in turn defines the my_calculation function at global scope.  That is it...sharing in functions, between files, between scripts within the GPSE. Sharing objects within a running session of a Geomagic industrial application.