自定义结束模态运算符Blender Python

时间:2016-09-20 14:15:38

标签: python modal-dialog operator-keyword blender

我有以下代码:

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”}搅拌机崩溃或冻结,有没有办法结束操作员?

1 个答案:

答案 0 :(得分:0)

首先,您展示的示例不会受益于作为模态运算符。模态运算符是允许在用户输入改变运算符所做的事情时更新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都会执行部分任务,更新视口并在每次调用之间收集用户输入。您可以向运算符类添加属性,以在每个模态调用之间保存状态信息。

一个简单的模态示例,可在移动鼠标时添加多维数据集 -

modal()

此示例中的缺陷 - 每次调用TaskScheduler.Current时,for循环会在每个位置创建一个多维数据集,从而导致在每个位置创建多个多维数据集。