1
$\begingroup$

Im making a Boeing 787 model and I just finished rigging it however I thought that maybe I would go deeper and try make a custom panel to control the rig. I've got the panel itself done using one of the default templates but I'm a complete noob at scripting new text.
Basically what I want to do is use a code to create a button in the panel, and when pressed, it will rotate one of the bones used for rigging, say for example the rudder. I really need help with this. By the way, Ive never seen it done but what would be even better would be a slider. This would mean I have more control over the rotation.

$\endgroup$

1 Answer 1

1
$\begingroup$

One way might be a modal operator which tracks mouse movements and applies it to pose bone rotation. The operator modal template (Goto Scripting View -> Templates -> Python -> Operator modal) should be a good starting point.

Here is a small example:

import bpy
from bpy.props import IntProperty, FloatProperty

class ModalOperator(bpy.types.Operator):
    """Rotate a bone with the mouse, example"""
    bl_idname = "object.modal_operator"
    bl_label = "Simple Modal Operator"

    first_mouse_x = IntProperty()
    first_value = FloatProperty()

    def modal(self, context, event):
        if event.type == 'MOUSEMOVE':
            delta = self.first_mouse_x - event.mouse_x
            self.pbone.rotation_euler.x = self.first_value + delta * 0.1

        elif event.type == 'LEFTMOUSE':
            return {'FINISHED'}

        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        self.first_mouse_x = event.mouse_x
        self.pbone = context.selected_pose_bones[0] # <- put your bone here instead
        self.first_value = self.pbone.rotation_euler.x

        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}


def register():
    bpy.utils.register_class(ModalOperator)


def unregister():
    bpy.utils.unregister_class(ModalOperator)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.object.modal_operator('INVOKE_DEFAULT')

You can then add your operator to your panel

$\endgroup$
1
  • $\begingroup$ Thats good, however its too 'free'. I want a set number for it to rotate so that its more realistic. For example the most the flaps can move is I think 40 Degrees so I want to have a button that says: Flaps, 10,20,30,40. Then you would be able to press each one and the bone controlling the flap would rotate x about of degrees. $\endgroup$ Oct 29, 2014 at 5:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .