Skip to content

Scripting Applications with Python

Gary edited this page Aug 27, 2014 · 1 revision

Table of Contents

Python scripting support is furnished by the BasicPythonService and PythonService components. You can use either one, but PythonService derives from BasicPythonService and also offers a scripting console.

These components are used in ATF applications to make them scriptable. They allow you to run scripts from another process, such as a test application, to access the ATF application's objects. For details on how such test applications are built, see ATF Test Code.

BasicPythonService Component

BasicPythonService sets up the Python scripting engine and imports many common .NET and ATF types into the Python namespace. BasicPythonService is derived from ScriptingService, so it provides a scripting service for Python.

You can use BasicPythonService by itself. However, consider using ScriptConsole and AtfScriptVariables as additional MEF components to provide a console and set script variables for common ATF services. For information on these components, see General ATF Scripting Support.

BasicPythonService overrides ImportAllTypes() and ImportType() to perform these methods for Python.

BasicPythonService's constructor is:

[ImportingConstructor]
public BasicPythonService()
{
    ScriptEngine engine = CreateEngine();
    SetEngine(engine);
    Initialize();
}

CreateEngine() gets the ScriptEngine by calling IronPython.Hosting.Python.CreateEngine(). ScriptingService.SetEngine() then sets the ScriptEngine.

Initialize() initializes the Python engine with some common assembly imports. It does so by creating a string with a list of Python import statements, such as the following:

from Sce.Atf.Dom import *

It then calls ScriptingService.ExecuteStatements() to execute these Python statements that perform the import. The following namespaces have all their types imported:

  • System
  • System.Drawing
  • System.Collections.Generic
  • System.Collections.ObjectModel
  • System.Windows.Forms
  • System.Text
  • System.IO
  • System.Xml.Schema
  • System.Xml.XPath
  • System.Xml.Serialization
  • Sce.Atf
  • Sce.Atf.Applications
  • Sce.Atf.VectorMath
  • Sce.Atf.Adaptation
  • Sce.Atf.Dom
Initialize() also looks through the list of all assemblies loaded in the current AppDomain and conditionally imports types from additional namespaces based on that list. Initialize() examines the full assembly name and does the following:
If full assembly name begins with... Then...
"Atf." or "Scea." Load the assembly by calling ScriptingService.LoadAssembly() so all its namespaces are in the script domain
"Atf.Gui.WinForms" Import all types from the namespaces "Sce.Atf.Controls" and "Sce.Atf.Controls.Adaptable"
"Scea.Core" Import all types from the namespace "Scea.Editors.Host.Internal"
"Scea.Dom" Import all types from the namespace "Scea.Dom"

PythonService Component

PythonService provides a dockable command console for entering Python statements. If no command console is needed, use the BasicPythonService MEF component.

PythonService derives from BasicPythonService, so if you use PythonService, you don't need to import BasicPythonService, too. You can also use BasicPythonService without PythonService. However, all the samples import PythonService for the convenience of having a script command console.

PythonService tries to import ScriptConsole, and if none was imported, it instantiates a new ScriptConsole.

For greatest flexibility, you should also import:

  • ScriptConsole
  • AtfScriptVariables
  • AutomationService
All the samples that use MEF import all of these, as noted in Scripting Components.

Using Python Scripts

To use Python scripts with your application, do the following:

  1. Import the scripting components into your application. As previously noted, it is easiest to import all the scripting components described in Scripting Components.
  2. Import ScriptingService wherever you want to expose variables, as in this code from the ATF Simple DOM Editor Sample:
[Import(AllowDefault = true)]
private ScriptingService m_scriptingService = null;
  1. Call the appropriate ScriptingService methods to access application objects from scripts. For example, the ATF Simple DOM Editor Sample sample does this in its Editor component:
if (m_scriptingService != null)
{
    // load this assembly into script domain.
    m_scriptingService.LoadAssembly(GetType().Assembly);
    m_scriptingService.ImportAllTypes("SimpleDomEditorSample");
    m_scriptingService.SetVariable("editor", this);
}
Calling LoadAssembly() adds the assembly's namespaces to the script domain. One of these namespaces is SimpleDomEditorSample, and the next line imports all its types. Finally, the variable editor is set to correspond to this, the Editor class, so all its methods and properties are exposed to the script with this variable. You can also add commonly used variables by importing the AtfScriptVariables component; for details, see AtfScriptVariables Component.
  1. Write Python scripts using the available variables for the objects you want to access.
For information on how Python is used to test applications, see Python Functional Tests. For details on script creation, see Writing Python Scripts for ATF Applications.

Topics in this section

Clone this wiki locally