Orientation Update Loop And Socket Module In BPY/BGE

[UPDATE]

Hi, I’ve been playing around with some python loops in BGE that read data from a UDP socket over a WIFI connection, it seems relatively straight forward in the game logic scripting (using the globalDict variables and such) but doesnt seem to be the same in the BPY (editing type viewport).

I will be adding some example scripts I’ve written for BGE and some videos to give more detail but long story short: in BPY (edit mode) I need a script that updates a vert’s rotation/position according to some data received from a remote application.
I can not seem to get the hang of initializing a loop in BPY as the embedded interpreter needs to loop blender’s main code for every time the viewport is redrawn. if i create a socket object like i would normally in python and store that object in a variable then try to run it in blender (BPY or BGE) it resets many things (including the variables containing the object’s id and its settings). I’m not aware of any global variable in BPY as the ones similar to the BGE’s GameLogic.globalDict[“SOMEVARIABLE”]

Any assistance here would be extremely helpful.
Thank you,
Grant.

[Update]

Here is a video of what i currently have up and running:

Here are the scripts from the video

UDP SERVER:

##Server
import socket, time, serial
 
UDP_IP="192.168.1.6"
UDP_PORT=8778
Xvalue,Yvalue,Zvalue = 0,0,0
ServoSer = serial.Serial()
ServoSer.port = 5
ServoSer.baudrate = 115200
ServoSer.timeout = 0.05
time.sleep(0.1)
ServoSer.open()
 
fromblender = "nothing set"
print('Encoder Server Started!!!')
try:
    sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) # UDP
    for n in range(500000):
 
        ServoSer.flushInput()
        ServoSer.write("X")
        try:
            Xvalue = float(ServoSer.readline()) / (-600/9)
        except:
            pass
        ServoSer.flushInput()
        ServoSer.write("Y")
        try:
            Yvalue = float(ServoSer.readline()) / (-600/9)
        except:
            pass        
        MESSAGE = str(str(Xvalue) + ' ' + str(Yvalue) + ' ' + str(Zvalue))
        #print MESSAGE
        sock.sendto(MESSAGE,(UDP_IP, UDP_PORT) )
 
except(KeyboardInterrupt):
    ServoSer.close()
    DCUSer.close()
    print('Forced Closed Ok')
 
print('Closed Ok')
ServoSer.close()
DCUSer.close()

UDP CLIENT:

import bge, socket, GameLogic
 
def main():
 
    cont = bge.logic.getCurrentController()
    own = cont.owner
 
    if not 'init' in own:
        print('Encoder Multiplex Init')
 
        own['init'] = 1
        own['UDP_IP'] = ""
        own['UDP_PORT'] = 8778
        own['Timeout'] = 0.01
        own['UDPdata'] = 0
        own['UDPPreamble'] = 0
        GameLogic.globalDict["SerialData"] = ('0 0 0')
        GameLogic.globalDict['sock'] = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
        GameLogic.globalDict['sock'].bind((own['UDP_IP'],own['UDP_PORT']))
        GameLogic.globalDict['sock'].settimeout(0.01)
        GameLogic.globalDict["EncoderReceiverState"] = 1
 
 
    GameLogic.globalDict["SerialData"], addr = GameLogic.globalDict['sock'].recvfrom(1024)
 
main()

SERVO 1:

import bge, GameLogic, mathutils
from math import radians
 
def main():
 
    cont = bge.logic.getCurrentController()
    own = cont.owner
 
    if int(GameLogic.globalDict["EncoderReceiverState"]) == 1:       
        own.orientation = mathutils.Matrix.Rotation(radians(float(str(GameLogic.globalDict["SerialData"].decode()).split()[0])), 3,'Z')
 
    else:
        print('TopArmAxisEncoder 0 FeedBack Down')
main()

SERVO 2:

import bge, GameLogic, mathutils
from math import radians
 
def main():
 
    cont = bge.logic.getCurrentController()
    own = cont.owner
 
    if int(GameLogic.globalDict["EncoderReceiverState"]) == 1:       
        own.orientation = mathutils.Matrix.Rotation(radians(float(str(GameLogic.globalDict["SerialData"].decode()).split()[1])), 3,'Z')
 
    else:
        print('TopArmAxisEncoder 1 FeedBack Down')
main()

Hi Blended_Blue ,

It is a very interesting demo. Can I share your blender file also? If so, email it to me at [email protected]? I would like to play it with some virtual data (without that controller connected at serial port) .

Thanks a lot.

very interesting and thanks for sharing the code I am sure many people will find it useful

Is it working? Have anybody tried out? Will it work for BI or Cycles? And can it stream data from Android app to blender?