【问题标题】:Custom End a Modal operator Blender Python自定义结束模态运算符 Blender Python
【发布时间】:2017-01-28 12:31:25
【问题描述】:

我有以下代码:

class audio_visualizer_create(bpy.types.Operator):
    bl_idname = "myop.audio_visualizer_create"
    bl_label = "Audio Visualizer Create"
    bl_description = ""
    bl_options = {"REGISTER"}


    @classmethod
    def poll(cls, context):
        return True

    def invoke(self, context, event):
        context.window_manager.modal_handler_add(self)
        scene = bpy.context.scene




        ##VARIABLES
        type = scene.audio_visualizer_type
        subtype = scene.audio_visualizer_subtype
        axis = scene.audio_visualizer_axis
        object = scene.audio_visualizer_other_sample_object
        scalex = scene.audio_visualizer_sample_object_scale[0]
        scaley = scene.audio_visualizer_sample_object_scale[1]
        scalez = scene.audio_visualizer_sample_object_scale[2]
        object = scene.audio_visualizer_other_sample_object
        bars = scene.audio_visualizer_bars_number

        print(bars)
        print(scaley)
        print(scene.audio_visualizer_bars_distance_weight)

        ##GETTING THE OBJECT
        if object == "OTHER":
            object = scene.audio_visualizer_other_sample_object

        ##Setting Up the bars
        total_lenght = (scaley*bars) + (scene.audio_visualizer_bars_distance_weight/100*(bars-1))

        for i in range(0, bars):
            bpy.ops.mesh.primitive_cube_add(radius=1, view_align=False, enter_editmode=False, location=(0, 0, 0), layers=bpy.context.scene.layers)
            bpy.context.object.scale = (scalex,scaley,scalez)
            bpy.context.object.location.y = total_lenght/bars*i


        is_finished = True

此时我想完成模态运算符。

        return {"RUNNING_MODAL"}

    def modal(self, context, event):


        if event.type in {"ESC"}:
            print("You've Cancelled The Operation.")

            return {"CANCELLED"}

        if event.type in {"MIDDLEMOUSE", "RIGHTMOUSE", "LEFTMOUSE"}:
            return {"FINISHED"}

        return {"FINISHED"}

但是如果我把 return {"FINISHED"} 而不是 return {"RUNNING_MODAL"} 搅拌机崩溃或冻结,有什么办法可以结束操作员?

【问题讨论】:

    标签: python modal-dialog operator-keyword blender


    【解决方案1】:

    首先,您展示的示例并不能从模态运算符中受益。模态运算符是一种允许在用户输入改变运算符所做的操作时更新 3DView 的运算符。模态运算符的一个示例是刀工具,一旦启动,它会在运行时根据用户输入更改最终结果。

    您的示例存在的问题是您在调用和模态中执行了错误的任务。 invoke() 应调用 modal_handler_add() 并返回 {"RUNNING_MODAL"} 以表示应在运算符仍在运行时调用 modal()modal() 应该执行数据更改,在它仍在工作时返回 {"RUNNING_MODAL"},完成后返回 {"FINISHED"}{"CANCELLED"}

    对于模态运算符,modal() 就像一个循环,对模态的每次调用都会执行部分任务,同时更新视口并在每次调用之间收集用户输入。您向操作符类添加属性以保存每个模式调用之间的状态信息。

    一个简单的模态示例,在您移动鼠标时添加立方体 -

    class audio_visualizer_create(bpy.types.Operator):
        bl_idname = "myop.audio_visualizer_create"
        bl_label = "Audio Visualizer Create"
        bl_options = {"REGISTER"}
    
        first_mouse_x = bpy.props.IntProperty()
        first_value = bpy.props.FloatProperty()
    
        def modal(self, context, event):
            delta = 0
            if event.type == 'MOUSEMOVE':
                delta = event.mouse_x - self.first_mouse_x
            elif event.type in ['LEFTMOUSE','RIGHTMOUSE','ESC']:
                return {'FINISHED'}
    
            for i in range(delta//5):
                bpy.ops.mesh.primitive_cube_add(radius=1)
                s = i*0.1
                bpy.context.object.scale = (s,s,s)
                bpy.context.object.location.y = i
    
            return {"RUNNING_MODAL"}
    
        def invoke(self, context, event):
            self.first_mouse_x = event.mouse_x
            self.first_value = 0.0
            context.window_manager.modal_handler_add(self)
    
            return {"RUNNING_MODAL"}
    

    此示例中的缺陷 - 每次调用 modal() 时,for 循环都会在每个位置创建一个立方体,这会导致在每个位置创建多个立方体。

    【讨论】:

    • 我如何制作一个模态运算符,无需按任何键或鼠标即可执行代码,类似于实时运算符?
    • 某些动作需要启动操作符、键或按钮,一旦启动它将继续,而模态返回 RUNNING_MODAL 您不必在 modal() 中使用键或鼠标事件 - 但您应该停止或取消某些事件。如果您不希望用户输入来启动您的脚本,请查看bpy.app.handlers
    猜你喜欢
    • 2020-12-30
    • 2011-04-10
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 2018-09-14
    • 2019-01-04
    相关资源
    最近更新 更多