3
$\begingroup$

I am sending object loc rot and scale every keyframe to http address using urllib but the frame rate drops significantly.
Thats the code I am using:

import bpy
import urllib
import mathutils 
from bpy import context
from urllib import request, parse
from math import sin, cos, radians
from mathutils import Matrix


def RunPerFrame(scene):
    for obj in bpy.data.objects:
        mat = bpy.context.object.matrix_world
        loc = mat.to_translation()
        rot = mat.to_3x3().to_quaternion()
        scale = obj.dimensions
        qtX = round(rot.x,3)
        qtY = round(rot.y,3)
        qtZ = round(rot.z,3)
        qtW = round(rot.w,3)
        loX = round(loc.x,3)
        loY = round(loc.y,3)
        loZ = round(loc.z,3)
        scX = round(scale.x,3)
        scY = round(scale.y,3)
        scZ = round(scale.z,3)

    url = ''
    values = { 'msgInstruction=' : 'MOVE_OBJ',
        'rotQuat=' : str(qtX) + ',' + str(qtY) + ',' + str(qtZ) + ',' + str(qtW),
        'objLoc=' : str(loX) + ',' + str(loY) + ',' + str(loZ),
        'objScale=' : str(scX) + ',' + str(scY) + ',' + str(scZ) }


    data = urllib.parse.urlencode(values)
    encoded_data = data.encode('ascii')
    req = urllib.request.Request(url, encoded_data)
    response = urllib.request.urlopen(req)
    the_page = response.read()


bpy.app.handlers.frame_change_pre.append(RunPerFrame)

Currently testing with one object and the frame rate drops below 2 fps Is there a way to make this faster with this code?

$\endgroup$
  • 1
    $\begingroup$ Are you sure this is a Blender issue? $\endgroup$ – VRM Feb 25 '15 at 20:36
  • $\begingroup$ Im not really sure, Im very new with scripting, but the animation slows down in blender and not in external animation. $\endgroup$ – Denis Feb 25 '15 at 20:38
  • 3
    $\begingroup$ I don't know if urllib is threaded. That means whenever it's sending data over the network, it locks up the rest of Blender. Look into threading: blender.stackexchange.com/questions/2428/… $\endgroup$ – Mike Pan Feb 25 '15 at 20:47
  • $\begingroup$ @MikePan Yes the urllib is not threaded, so I assume that is the issue. Now need to figure out how to add that. $\endgroup$ – Denis Feb 25 '15 at 21:15
  • $\begingroup$ @MikePan can you give a suggestion how to put that code in a thread? $\endgroup$ – Denis Feb 28 '15 at 18:50
4
$\begingroup$

using Mike Pan answer i have tried to split the code and put the networking part on a different thread see if this helps with the speed

import bpy
import urllib
import mathutils 
from bpy import context
from urllib import request, parse
from math import sin, cos, radians
from mathutils import Matrix

import time
import threading


class Worker(threading.Thread):

    def __init__(self, values):
        self.values = values
        threading.Thread.__init__(self)

    def run(self):
        url = ''
        data = urllib.parse.urlencode(self.values)
        encoded_data = data.encode('ascii')
        req = urllib.request.Request(url, encoded_data)
        response = urllib.request.urlopen(req)
        the_page = response.read()


def RunPerFrame(scene):
    for obj in bpy.data.objects:
        mat = bpy.context.object.matrix_world
        loc = mat.to_translation()
        rot = mat.to_3x3().to_quaternion()
        scale = obj.dimensions
        qtX = round(rot.x,3)
        qtY = round(rot.y,3)
        qtZ = round(rot.z,3)
        qtW = round(rot.w,3)
        loX = round(loc.x,3)
        loY = round(loc.y,3)
        loZ = round(loc.z,3)
        scX = round(scale.x,3)
        scY = round(scale.y,3)
        scZ = round(scale.z,3)

    values = { 'msgInstruction=' : 'MOVE_OBJ','rotQuat=' : str(qtX) + ',' + str(qtY) + ',' + str(qtZ) + ',' + str(qtW),'objLoc=' : str(loX) + ',' + str(loY) + ',' + str(loZ),'objScale=' : str(scX) + ',' + str(scY) + ',' + str(scZ) }  
    thread = Worker(values)
    thread.start()




bpy.app.handlers.frame_change_pre.append(RunPerFrame)
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.