Critical setup requirements
Required imports
mech_dpf.setExtAPI(ExtAPI) before using DPF operators in Mechanical. This initializes DPF with Mechanical’s API context.
Important usage conventions
Named selections must be UPPERCASE
When using named selections in DPF, they must be specified in UPPERCASE, even if created with lowercase in Mechanical:DPF operators pattern
Use hierarchical namespace syntax for operators (not genericdpf.Operator("name")):
dpf.operators.result.*- Result extraction (stress, displacement, etc.)dpf.operators.math.*- Mathematical operations (add, scale, etc.)dpf.operators.scoping.*- Scoping operations (on_named_selection, etc.)dpf.operators.min_max.*- Min/max operationsdpf.operators.utility.*- Utility operations (html_doc, etc.)
Mechanical scripting window
The scripting window in Mechanical provides an interactive environment for DPF scripts:Accessing the scripting window
- Open Ansys Mechanical
- Go to View → Scripting
- The scripting window appears at the bottom of the Mechanical interface
Scripting window features
- Interactive Execution: Run Python code immediately
- DPF Pre-loaded: DPF assemblies are automatically available
- Mechanical API Access: Full access to ExtAPI and Mechanical objects
- Intellisense Support: Auto-completion for DPF and Mechanical APIs
Example scripting window usage
Python Result objects
Python Result objects allow you to create custom result types in Mechanical:Creating a Python Result
- In the Mechanical tree, right-click on Solution
- Insert → Result → Python Result
- Edit the Python code in the properties panel
Python Result template
Python Result context
- Use
define_dpf_workflow(analysis)function pattern analysis: Passed automatically by Mechanicalthis: Reference to current Python Result objectthis.Text: Display text in Mechanicalthis.WorkflowId: Set for visualization supportdpf.Workflow(): Required for contour plottingSetOutputContour(): Specifies what field to display- Access to all Mechanical and DPF APIs
- Create operators and process data
- Create
dpf.Workflow()object - Add operators to workflow
- Call
SetOutputContour()on final operator - Record workflow and set
this.WorkflowId
Common helper functions
Create reusable functions for common DPF operations in Mechanical:Best practices for Mechanical
IronPython syntax considerations
Mechanical uses IronPython, which has some differences from CPython:Error handling
Performance tips
- Reuse DPF model objects when possible
- Cache frequently accessed data
- Use operators for complex operations
- Minimize data transfers between .NET and Python
Next steps
- Hello DPF - Run your first Mechanical script
- User Guide - Learn DPF concepts
- Examples - See Mechanical-specific examples