Blender模态运算符无法插入关键帧并移动对象

时间:2018-11-15 16:40:42

标签: python modal-dialog operator-keyword blender

在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'}

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

2 个答案:

答案 0 :(得分:1)

简单的解决方案是启用搅拌机Auto Keyframing

如果您仍然想让运算符正常工作,我希望您不必调用其他运算符就可以直接使用数据,尤其是在模态运算符中。

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

答案 1 :(得分:0)

好,我解决了这个问题。

我正在为错误的对象(动画,而不是立方体本身)调用keyframe_insert

这是插入它的正确方法,例如当对象是相机时:

camera = bpy.context.scene.camera
camera.keyframe_insert(data_path='location', index=0)
camera.keyframe_insert(data_path='location', index=1)
camera.keyframe_insert(data_path='location', index=2)
camera.keyframe_insert('rotation_quaternion')

以这种方式在播放时插入关键帧。