3
$\begingroup$

I've written a small script as follows:

Requirements

Manual Part: I manually select a specific Action in Dopesheet -> Action Editor. (Note that these actions will have only two keyframes, at 1 and at 50, with identical poses).

After this I manually select specific bones to which the script below should apply.

Script Part: Then run the script to do the following:

  1. Loop through all keyframes and add small random rotations to all the bones I've selected in the manual part.
  2. Insert new keyframes which contain these random rotations

Actual Script:

import bpy
import random
import math

# User Settings
keyframe_insert_interval = 5
rand_rotation_deg_max = 0.25

# Get scene stuff
c = bpy.context
s = c.scene

# Get frames start and end
frame_start = s.frame_start
frame_end = s.frame_end
bones_to_rotate = c.selected_pose_bones

for i in range(frame_start + keyframe_insert_interval, frame_end, keyframe_insert_interval):
s.frame_current = i

    # Iterate through all bones
    for j in range(0, len(bones_to_rotate)):
        # Current Bone
        bone_curr = bones_to_rotate[j]

        # Set rotation mode
        bone_curr.rotation_mode = 'XYZ'

        # Get next random rotation add
        new_rand_x = random.uniform(-1.0 * rand_rotation_deg_max, rand_rotation_deg_max)
        new_rand_y = random.uniform(-1.0 * rand_rotation_deg_max, rand_rotation_deg_max)
        new_rand_z = random.uniform(-1.0 * rand_rotation_deg_max, rand_rotation_deg_max)

        # Placeholder - might want to add some other randomness ,so creating vars
        new_x = new_rand_x
        new_y = new_rand_y
        new_z = new_rand_z

        # Rotate   
        bone_curr.rotation_euler.rotate_axis('X', math.radians(new_x))
        bone_curr.rotation_euler.rotate_axis('Y', math.radians(new_y))
        bone_curr.rotation_euler.rotate_axis('Z', math.radians(new_z))

        # Tried all of these, none seem to work correctly           
        #bpy.data.actions[6].keyframe_insert(data_path="rotation_euler",frame=i)
        bone_curr.keyframe_insert(data_path="rotation_euler",frame=i)
        #bpy.ops.anim.keyframe_insert(type='LocRotScale')

Problem

The script works perfectly on the currently selected Action, but it messes up all the other actions. For some reason it applies the random rotations to all the other (unselected) Actions as well.

I'm sure I'm doing something systematically wrong, but cannot figure out what it is, and some help would be greatly appreciated!

Thanks very much, staticvoidlol

$\endgroup$
  • $\begingroup$ this is working fine for me ,i have armature with multiple actions (empty) any other steps i'm messing ? $\endgroup$ – Chebhou Apr 23 '15 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.