Blender Python:如何取消Operator执行但不关闭props_dialog弹窗
解决方案
Blender Operator 针对invoke_props_dialog弹出的配置表单,原生提供了check方法钩子专门用于表单校验,不需要使用Modal算子即可实现校验失败时弹窗不关闭的效果:
- 点击表单「OK」按钮时,Blender会先执行
check方法 - 若
check返回False,弹窗将保持打开状态,用户已填写的参数不会丢失,同时你在check中上报的错误提示也会正常展示 - 若
check返回True,才会继续执行execute方法
修改后的代码
import bpy from bpy.types import Operator # DMX_Fixture_AddEdit contains prop declarations and the draw method. class DMX_OT_Fixture_Add(DMX_Fixture_AddEdit, Operator): bl_label = "DMX: Add Fixture" bl_idname = "dmx.add_fixture" bl_options = {'REGISTER','UNDO'} def check(self, context): # 所有校验逻辑移到check方法中 if self.name in bpy.data.collections: self.report({'ERROR'}, "已存在同名的集合,请修改名称") return False if not len(self.profile): self.report({'ERROR'}, "未选择GDTF配置文件") return False if not len(self.mode): self.report({'ERROR'}, "未选择DMX模式") return False # 校验全部通过 return True def execute(self, context): scene = context.scene dmx = scene.dmx # execute中仅保留业务逻辑 for i in range(self.units): dmx.addFixture(self.name+" "+str(i+1), self.profile, self.universe, self.address, self.mode, list(self.gel_color)) return {'FINISHED'} def invoke(self, context, event): self.name = "Fixture "+str(len(context.scene.dmx.fixtures)+1) self.units = 1 DMX_Fixture_AddEdit.profile_list_items = [] wm = context.window_manager return wm.invoke_props_dialog(self)
说明
你之前的方案行不通是因为execute方法被触发时,Blender已经默认要关闭弹窗了,无论你返回任何状态码都不会阻止弹窗关闭,所以校验逻辑必须放在check阶段完成。
内容的提问来源于stack exchange,提问作者Hugo Aboud
相关产品推荐
相关产品推荐

