-
Notifications
You must be signed in to change notification settings - Fork 260
Scripting Applications with Python
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 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:
SystemSystem.DrawingSystem.Collections.GenericSystem.Collections.ObjectModelSystem.Windows.FormsSystem.TextSystem.IOSystem.Xml.SchemaSystem.Xml.XPathSystem.Xml.SerializationSce.AtfSce.Atf.ApplicationsSce.Atf.VectorMathSce.Atf.AdaptationSce.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 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:
ScriptConsoleAtfScriptVariablesAutomationService
To use Python scripts with your application, do the following:
- Import the scripting components into your application. As previously noted, it is easiest to import all the scripting components described in Scripting Components.
- Import
ScriptingServicewherever you want to expose variables, as in this code from the ATF Simple DOM Editor Sample:
[Import(AllowDefault = true)]
private ScriptingService m_scriptingService = null;- Call the appropriate
ScriptingServicemethods to access application objects from scripts. For example, the ATF Simple DOM Editor Sample sample does this in itsEditorcomponent:
if (m_scriptingService != null)
{
// load this assembly into script domain.
m_scriptingService.LoadAssembly(GetType().Assembly);
m_scriptingService.ImportAllTypes("SimpleDomEditorSample");
m_scriptingService.SetVariable("editor", this);
}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.
- Write Python scripts using the available variables for the objects you want to access.
Development, Debugging, and Testing
-
Debugging the DOM with Visual Studio: Shows how to get
DomNodeinformation to help you debugging a DOM. - Visual Studio Debugger Display Attributes and Other Features: Learn about enhancing debugging in Visual Studio by using debugger display attributes and other facilities in C#.
- Using DomExplorer: Tells about a component you can use to visualize the contents of a DOM node tree.
-
Using the DomRecorder Component: Discusses the
DomRecorderand the DOM events it records and shows an example. - General ATF Scripting Support: Explains ATF's facilities to script applications, accessing C# objects in application classes.
-
Scripting Applications with Python: Shows how to use the
BasicPythonServiceandPythonServicecomponents to script an ATF application. - ATF Test Code: Discusses the classes to facilitate writing tests for ATF-based applications as well as ATF test code.
- Writing Python Scripts for ATF Applications: Shows how to write Python scripts for ATF applications, using existing examples.
- Home
- Getting Started
- Features & Benefits
- Requirements & Dependencies
- Gallery
- Technology & Samples
- Adoption
- News
- Release Notes
- ATF Community
- Searching Documentation
- Using Documentation
- Videos
- Tutorials
- How To
- Programmer's Guide
- Reference
- Code Samples
- Documentation Files
© 2014-2015, Sony Computer Entertainment America LLC