1
$\begingroup$

I'am writing python script that uses data from IMU sensor and changes armature structure position.

To visualise movement I use bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1) from bpy library.

Recently something happend and I have following issue:

When I start program evething is ok. Then I make some changes, save it and run it again, visualisation doesn't work. When the program stops, it shows last position of sensor data. But during program it doesn't move at all.

I have noticed, that when I change directory of blender file and run it again it works for the first time. But when I save it, the problem occures again.

I see in System console that data is beeing recieved and whenever block of data comes it shows "Warning 1x Draw window and swap: [xx] ms, average [xx] ms". So it seems, that command works.

How to manage this problem? I've no idea what's going on, tried all I can :/

import serial
import time
import bpy
import mathutils
from mathutils import Euler
import math

ser = serial.Serial('COM3','115200')
time.sleep(3)

ob = bpy.data.objects['Armature']
armature = ob.data
bpy.ops.object.mode_set(mode='POSE')
rig = bpy.data.objects.get("Armature") 
upperArmL = rig.pose.bones['upper_arm.L']
foreArmL = rig.pose.bones['forearm.L']
upperArmR = rig.pose.bones['upper_arm.R']
foreArmR = rig.pose.bones['forearm.R']
chest = rig.pose.bones['spine.003']

flagaSTART = 0;
kalibracja = 0;
i = 0;

while (flagaSTART == 0):
    readBEGIN = str(ser.readline())
    readBEGIN = (readBEGIN[2:len(readBEGIN)-5])
    print(readBEGIN)
    if(readBEGIN == "BEGIN"):
        flagaSTART = 1
        print("poczatekOdpytywania")        
        
    
while (flagaSTART == 1):
        ser.write(b'g\n')
        read = str(ser.readline())           
        readSTOP = read
        readSTOP = (readSTOP[2:len(readBEGIN)-10])
        print(readSTOP)
        dlug=len(read)
        read = (read[2:dlug-4])
        read = read.split()
        read_floats= []
        while(kalibracja == 0):

            for i in range(11):
                ser.write(b'g\n')
                read = str(ser.readline())           
                dlug=len(read)
                read = (read[2:dlug-4])
                read = read.split()
                read_floats= []
                
                for element in read:
                       read_floats.append(float(element))
                       
                if(i==0):
                    avg_w=read_floats[0]
                    avg_x=read_floats[1]
                    avg_y=read_floats[2]
                    avg_z=read_floats[3]
                    
                avg_w=round(((avg_w+read_floats[0])/2),2)
                avg_x=round(((avg_x+read_floats[1])/2),2)
                avg_y=round(((avg_y+read_floats[2])/2),2)
                avg_z=round(((avg_z+read_floats[3])/2),2)
           
                print("avgW= ", avg_w)       
                print("avgX= ", avg_x)
                print("avgY= ", avg_y)
                print("avgZ= ", avg_z)

                if(i==10):
                    kalibracja=1

        
        if (readSTOP == "STOP"):
            kalibracja = 0
            break
        
        for element in read:
           read_floats.append(float(element))
           
           
        current_w = read_floats[0]
        current_x = read_floats[1]
        current_y = read_floats[2]
        current_z = read_floats[3]
        
        rad_w = current_w
        rad_x = current_x
        rad_y = current_y
        rad_z = current_z

        chest.rotation_quaternion = [rad_w, rad_x, rad_z, rad_y]

        bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)

ser.close()
$\endgroup$

1 Answer 1

0
$\begingroup$

Rather than calling bpy.ops.wm.redraw_timer(), I think you want bpy.context.view_layer.update()

If you're not running code from the console, you need to tell Blender when to redraw the scene. A timer will do that and I don't know why yours stopped working, but the update function is less heavy weight.

$\endgroup$

You must log in to answer this question.

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