Skip to main content
 首页 » 编程设计

python之Blender 模态运算符(operator)无法插入关键帧和移动对象

2024年12月31日10webabcd

在 Blender 中,我使用模态运算符模板来移动对象并将其位置记录为关键帧。

我正在做这样的事情:

import bpy 
from bpy.props import IntProperty, FloatProperty 
 
class ModalOperator(bpy.types.Operator): 
    """Move an object with the mouse, example""" 
    bl_idname = "object.modal_operator" 
    bl_label = "Simple Modal Operator" 
 
    first_mouse_x = IntProperty() 
    first_value = FloatProperty() 
    current_frame = 1 
    endframe = bpy.data.scenes["Scene"].frame_end 
 
    def modal(self, context, event): 
        if event.type == 'MOUSEMOVE': 
            if self.current_frame < self.endframe: 
                delta = self.first_mouse_x - event.mouse_x 
                context.object.location.x = self.first_value + delta * 0.01 
                context.scene.frame_set(self.current_frame) 
                bpy.ops.anim.keyframe_insert_menu(type="Rotation") 
                bpy.ops.anim.keyframe_insert_menu(type="Location") 
                self.current_frame+=1 
 
        elif event.type == 'LEFTMOUSE': 
            return {'FINISHED'} 
 
        elif event.type in {'RIGHTMOUSE', 'ESC'}: 
            context.object.location.x = self.first_value 
            return {'CANCELLED'} 
 
        return {'RUNNING_MODAL'} 

发生在它插入所有关键帧之前,并且仅在您可以使用鼠标移动立方体之后发生,例如。我想移动立方体并同时“记录”它的移动。有解决方案吗?

请您参考如下方法:

简单的解决方案是启用 blender Auto Keyframing .

如果您仍然想让您的运算符工作,我希望您不需要调用其他运算符并直接处理数据,尤其是在模态运算符中。

context.object.keyframe_insert('location') 
context.object.keyframe_insert('rotation_euler')