You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Digital Micrograph中实现dm-script与Python的通信及数据传递?

How to Pass Data from DM-Script Dialog to Python in DigitalMicrograph

The core challenge here is that DM-Script and Python operate in separate execution contexts within DigitalMicrograph—you can’t directly access DM-Script variables from Python. Instead, you need to explicitly return the dialog’s input value from your DM-Script code, then capture that return value in Python using DM.ExecuteScriptString().

Here’s a step-by-step solution with modified code:

Step 1: Update the DM-Script Dialog to Preserve and Return the Input

First, adjust your DM-Script class to store the input as a class member variable (instead of a loose global string) so it stays accessible after the dialog closes. Then add a return statement at the end of the script to pass the value back to Python.

import DigitalMicrograph as DM

script = """
class ButtonDialog : UIFrame{
    // Store input as a class member to retain it after dialog closes
    string field_value;

    void field_changed(object self, TagGroup field){
        // Update the class member when the user changes the field
        self.field_value = field.DLGGetStringValue();
    }

    object init(object self){
        TagGroup dlg, dlg_items, field;
        dlg = DLGCreateDialog("Type in a number", dlg_items);
        dlg.DLGAddElement(DLGCreateLabel("Number:"));
        field = DLGCreateIntegerField(0, 10, "field_changed");
        dlg.DLGAddElement(field);
        self.super.init(dlg);
        return self;
    }
}

object dialog = alloc(ButtonDialog).init();
dialog.pose();
// Send the captured value back to Python
return dialog.field_value;
"""

Step 2: Capture the Return Value in Python

The DM.ExecuteScriptString() function returns the result of the final return statement from your DM-Script. Assign this result to your Python variable to access the dialog input:

# Execute the script and retrieve the dialog value
field_value = DM.ExecuteScriptString(script)

# Use the value in your Python code
print(f"Received number from DM-Script dialog: {field_value}")

Why This Works

  • Using a class member variable (self.field_value) ensures the input persists after the dialog closes—loose global variables might go out of scope or be inaccessible to the Python context.
  • The return dialog.field_value line explicitly passes the data from DM-Script to Python, which DM.ExecuteScriptString() captures directly.

Alternative: Persistent Tag Group for Shared Data

If you need to share data across multiple DM-Script and Python sessions, you can store the value in DigitalMicrograph’s persistent tag group:

DM-Script side:

// Save value to persistent storage
GetPersistentTagGroup().TagGroupSetTagAsString("dialog_input", dialog.field_value);

Python side:

// Retrieve value from persistent storage
persistent_tags = DM.GetPersistentTagGroup()
field_value = persistent_tags.TagGroupGetTagAsString("dialog_input")

This is useful for longer-lived data, but the direct return method is simpler for one-off dialog data transfer.

内容的提问来源于stack exchange,提问作者miile7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 20:37:57