我有以下代碼:自定義完一個模式操作攪拌機的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」}攪拌機崩潰或死機返回{「結束」},有什麼辦法來結束操作?
而我怎麼能做一個模式運算符執行代碼,而不需要按任何鍵或鼠標,像一個實時操作員? – Fabrizio
某些動作需要啓動運算符,鍵或按鈕,一旦啓動,它將繼續,而模態返回'RUNNING_MODAL'您不必在模態()中使用鍵或鼠標事件 - 但是您應該停止或取消某些事件。如果您不希望用戶輸入來啓動您的腳本,請查看[bpy.app.handlers](https://www.blender.org/api/blender_python_api_current/bpy.app.handlers.html) – sambler