Blender armature control in real time? I'm new to blender and python I need to control armature in blender game engine via a serial data comes from arduino. I'm useing this code but it does not work.

import GameLogic

arm = GameLogic.getCurrentController().owner

print(arm.channels['Upperleg.L'].rotation_euler)

arm.channels['Upperleg.L'].rotation_euler = [5, 5, 5]
arm.update()

print(arm.channels['Upperleg.L'].rotation_euler)


I'm using blender 2.71 and used a always sensor with the python controller(script). I Can't find whats wrong in this code. Does any one know how to change the euler angles of the armature in blender game engine mode?
You need to invoke your script from an actuator otherwise the scene will not be updated see image at the bottom of this post.
I searched my old sources and found this very messy one that still works in BGE (today tested with Blender (2.68.5):



import sys
import time

import os
import random
import select

from math import *
import mathutils

import bge
import bpy
import math
from mathutils import Vector
from mathutils import Quaternion


def applyKinect(rot,name):
    scene = bge.logic.getCurrentScene()
    source = scene.objects
    main_arm = source.get('Armature')
    #q = Quaternion((rot.w,-rot.x,-rot.z,rot.y))
    q = Quaternion((rot.w,rot.x,rot.y,rot.z))
    main_arm.channels[ name ].rotation_quaternion = q.normalized()
    main_arm.update()
    v = rot.to_euler("XYZ")
    if name.startswith('head'): 
        print("%s X:%r Y:%r Z:%r" % (name,math.degrees(v.x),math.degrees(v.y),math.degrees(v.z)))


def map():
    #print("map")
    for i in range(1, 11):
        user = bpy.nui.get_user(i)
        if not user:
            continue
        #print("Map Kinect Found User=" + str(i))
#        rot = user.hip_center[1]
#        applyKinect( rot, 'hip_center' )
#        print("hip_center %r" % rot )
        #rot = user.neck[2]
        #applyKinect( rot, 'neck' )
        rot = user.head[2]
        arm = applyKinect( rot, 'head' )
        rot = user.r_shoulder[2]
        applyKinect( rot, 'shoulder_R' )
        rot = user.r_elbow[2]
        applyKinect( rot, 'elbow_R' )
        rot = user.r_wrist[2]
        applyKinect( rot, 'wrist_R' )
        rot = user.r_hand[2]
        applyKinect( rot, 'hand_R' )

        rot = user.l_shoulder[1]
        applyKinect( rot, 'shoulder_L' )
        rot = user.l_elbow[1]
        applyKinect( rot, 'elbow_L' )
        rot = user.l_wrist[1]
        applyKinect( rot, 'wrist_L' )
        rot = user.l_hand[1]
        applyKinect( rot, 'hand_L' )


def dump(ctx, obj, level=0):
    for attr in dir(obj):
       print( "%s.%s = %s" % (ctx, attr, getattr(obj, attr)))

map()
#test()


I used quaternions (note that you can convert your euler angles to a quaternion using the methods from mathutils).

The data is provided by an addon (and polled by bpy.nui.get_user(i)), the script is executed with a logic block like this: