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

Blender Text Editor脚本如何实现询问用户确认并等待回答?

在Blender脚本中实现用户确认弹窗

可以实现这类等待用户确认的功能,Blender的Python API提供了两种常见实现方式:

1. 使用自带的确认对话框

直接调用Blender内置的wm.confirm操作,快速生成带确认/取消按钮的弹窗,通过返回结果判断用户选择:

import bpy

def some_processing():
    print("执行处理步骤...")

while True:
    some_processing()
    # 弹出确认弹窗,自定义标题和提示内容
    result = bpy.ops.wm.confirm(
        'INVOKE_DEFAULT',
        title="是否继续?",
        message="确认执行下一轮处理吗?"
    )
    # 用户点击「取消」时返回{'CANCELLED'},终止循环
    if result == {'CANCELLED'}:
        break

2. 自定义Yes/No弹窗Operator

如果需要更定制化的弹窗(比如按钮文字明确显示Yes/No),可以自定义Operator类:

import bpy

class WM_OT_YesNoPopup(bpy.types.Operator):
    bl_idname = "wm.yes_no_popup"
    bl_label = "确认继续?"

    def execute(self, context):
        # 用户点击Yes时的逻辑
        self.report({'INFO'}, "用户选择了Yes")
        return {'FINISHED'}

    def invoke(self, context, event):
        # 触发弹窗显示
        return context.window_manager.invoke_confirm(self, event)

    def cancel(self, context):
        # 用户点击No时的逻辑
        self.report({'INFO'}, "用户选择了No")
        return {'CANCELLED'}

# 注册自定义Operator
bpy.utils.register_class(WM_OT_YesNoPopup)

# 业务逻辑示例
def some_processing():
    print("执行处理任务...")

while True:
    some_processing()
    # 调用自定义弹窗
    result = bpy.ops.wm.yes_no_popup('INVOKE_DEFAULT')
    if result == {'CANCELLED'}:
        break

# 可选:脚本结束后注销Operator(重复运行脚本建议添加)
# bpy.utils.unregister_class(WM_OT_YesNoPopup)

注意事项

  • 上述代码可直接在Blender的Text Editor中运行,弹窗会正常显示并等待用户操作
  • 'INVOKE_DEFAULT'参数是关键,确保以交互模式触发弹窗,而非直接执行

内容的提问来源于stack exchange,提问作者Тютюник Валентина

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 09:21:08