2016-09-20 28 views
0

我有以下代碼:自定義完一個模式操作攪拌機的Python

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"} 

但是如果我把,而不是返回{「RUNNING_MODAL」}攪拌機崩潰或死機返回{「結束」},有什麼辦法來結束操作?

回答

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()被稱爲for循環產生在每個位置的立方體,從而導致多個立方體在每個位置創建。

+0

而我怎麼能做一個模式運算符執行代碼,而不需要按任何鍵或鼠標,像一個實時操作員? – Fabrizio

+0

某些動作需要啓動運算符,鍵或按鈕,一旦啓動,它將繼續,而模態返回'RUNNING_MODAL'您不必在模態()中使用鍵或鼠標事件 - 但是您應該停止或取消某些事件。如果您不希望用戶輸入來啓動您的腳本,請查看[bpy.app.handlers](https://www.blender.org/api/blender_python_api_current/bpy.app.handlers.html) – sambler