[Bf-python] thesis project

Daniel Monteiro Basso danielmbasso at gmail.com
Sat Jul 21 00:25:36 CEST 2012


Hello,

On Thu, Jul 19, 2012 at 11:49 PM, Maltzan, Christopher <
maltzanc at garnet.union.edu> wrote:

> I am writing to you now to ask specifically about advice or comments on on
> connecting my software (genetic algorithm and GUI) to blender which would
> then make relatively simple changes to a base model and then send renders
> back to my software. In my mind, the hardest part is teaching blender to
> make changes to the avatar. Do you have any advice for where I can start?


That depends on the level of integration you want. If you want loose
coupling, I would suggest to use a client/server architecture, keeping
Blender as the server. You can use my code as base for your server script:

#!/usr/bin/env python3
#-*- coding:utf-8 -*-

import socketserver
from ctypes import Structure, c_byte, c_float, c_int, sizeof

import bpy
from mathutils import Euler


HOST, PORT = "0.0.0.0", 9999


def get_list(s):
    llen = ord(s.recv(1))
    retv = []
    for i in range(llen):
        slen = ord(s.recv(1))
        retv.append(s.recv(slen).decode("ascii"))
    return retv


class EyeParameters(Structure):
     _fields_ = [
        ('eye', c_byte),
        ('retina', c_byte),
        ('yaw', c_float),
        ('pitch', c_float),
        ('aperture', c_float),
        ('distance', c_float),
        ('samples', c_int),
        ('format', c_int),
        ]


class EyeClientHandler(socketserver.BaseRequestHandler):
    def handle(self):
        print("New client {}.".format(self.client_address))
        socket = self.request.fileno()
        self.request.sendall(b"EyeServer")
        if self.request.recv(9) != b"EyeClient":
            print("Bad request, closed.")
            return
        cams, retinas = (get_list(self.request) for i in range(2))
        print(cams)
        print(retinas)
        e = EyeParameters()
        while True:
            if self.request.recv_into(e) != sizeof(e):
                print("Client closed the connection.")
                break
            if e.eye == -1 or e.eye == 0xff:
                print(e.eye)
                print("Client requested server termination.")
                EyeServer.quit = True
                break

            cam = bpy.data.objects[cams[e.eye]]
            bpy.context.scene.camera = cam
            bpy.context.scene.cycles.samples = e.samples
            bpy.context.scene.cycles.seed += 1
            cam.rotation_euler = Euler((e.yaw, e.pitch, 0))
            cam.data.dof_distance = e.distance
            cam.data.cycles.aperture_size = e.aperture
            cam.data.cycles.retina = retinas[e.retina]
            cam.data.cycles.retina_socket = socket | (e.format << 16)
            bpy.ops.render.render()


class EyeServer:
    quit = False
    server = socketserver.TCPServer((HOST, PORT), EyeClientHandler)
    @classmethod
    def serve(self):
        while not self.quit:
            self.server.handle_request()

try:
    EyeServer.serve()
except KeyboardInterrupt:
    print("EyeServer will shutdown now.")


Notes:

   - this is only an example, you will not be able to run it (it requires
   some modifications I made to Cycles to render mimicking the photoreceptors
   in the retina)
   - I'll not provide the client code because: 1 - it is easy to infer; 2 -
   it is currently a C++ spaghetti code; 3 - it is not using Boost, but some
   old networking code I made ages ago
   - the transmission is binary, ctypes does all the magic implicitly
   - I don't do anything after render() because my Cycles patch transmits
   the results of the render through the socket, but you could save the
   rendered image to a file and then open it and send its contents
   - sorry for the lack of comments, but I think it is pretty easy to
   understand this code... if you have any question, just ask



>
> My idea was that I would make a plugin which could select pre-determined
> vertices and move them on the xyz grid to specific pre-programmed
> coordinates or select a group of vertices and then scale them to be
> slightly larger (to make an avatar have the trait of thick arms for
> example). Obviously an over-simplification, but you get the idea. I will of
> course make the software open to the community :)
>

If that is the case, I would recommend a collaboration with the Make Human
folks... they have 90% of what you need already done.

Cheers,

Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.blender.org/pipermail/bf-python/attachments/20120720/c4690a96/attachment.html>


More information about the Bf-python mailing list