Skip to main content
Learn how to effectively use DPF in different contexts within Ansys Mechanical.

Critical setup requirements

Required imports

Important: You must call 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 generic dpf.Operator("name")):
Common operator categories:
  • 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 operations
  • dpf.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

  1. Open Ansys Mechanical
  2. Go to View → Scripting
  3. 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

  1. In the Mechanical tree, right-click on Solution
  2. Insert → Result → Python Result
  3. 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 Mechanical
  • this: Reference to current Python Result object
  • this.Text: Display text in Mechanical
  • this.WorkflowId: Set for visualization support
  • dpf.Workflow(): Required for contour plotting
  • SetOutputContour(): Specifies what field to display
  • Access to all Mechanical and DPF APIs
For visualization in Python Result:
  1. Create operators and process data
  2. Create dpf.Workflow() object
  3. Add operators to workflow
  4. Call SetOutputContour() on final operator
  5. 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