3

I need to send instructions to blender trough an external application.

for example: import model to current scene

Is this possible? Can someone point me in the right direction based on previous experience.

Thanks in advance

Ghus

Ghus
  • 41
  • 5

1 Answers1

1

You could poll a file with instructions or use sockets (for remote networks) or something else from pythons interprocess communication.

Using a file could be quite simple, the basic idea is to execute the statements in a loop:

Process A                           Process B(lender)
------------------------------------------------------
                                    File exists No -> sleep    
Create file with instructions       
                                    File exists Yes -> read and execute statements
Does file still exist Yes ->sleep
                                    After execution delete file

While Process-A just needs to print some lines in a text file like:

import,file1.obj
import,file2.obj
import,file3.obj
cancel,none

The Blender side could be timer operator (based on the build in template) it actually reads the commands from the file (the last command 'cancel' deletes the file and stops the timer):

import bpy
import os.path

class ModalTimerOperator(bpy.types.Operator):
    """Operator which runs its self from a timer"""
    bl_idname = "wm.modal_timer_operator"
    bl_label = "Modal Timer Operator"

    _timer = None
    filename='C:\\dev\\command.txt'

    def modal(self, context, event):
        if event.type == 'TIMER':
            if os.path.isfile(self.filename):
                for line in open(self.filename):
                    line=line.rstrip("\n")
                    cmd,arg = line.split(",")
                    if cmd == 'import':
                        print('import requested file=%s' % arg )
                    elif cmd == 'cancel':
                        self.cancel( context )
                    else:
                        print('unknown request=%s arg=%s' % (cmd,arg))
                os.remove( self.filename )
                print('command file removed')
            else:
                print('waiting for command file=%s' % self.filename )

        return {'PASS_THROUGH'}

    def execute(self, context):
        wm = context.window_manager
        self._timer = wm.event_timer_add(5, context.window)
        wm.modal_handler_add(self)
        return {'RUNNING_MODAL'}

    def cancel(self, context):
        wm = context.window_manager
        wm.event_timer_remove(self._timer)
        print('timer removed')

def register():
    bpy.utils.register_class(ModalTimerOperator)


def unregister():
    bpy.utils.unregister_class(ModalTimerOperator)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.wm.modal_timer_operator()
stacker
  • 38,549
  • 31
  • 141
  • 243
  • thanks stacker this is very useful

    i don't want to push it but do you have a tcp server integrated in blender =)

    – Ghus Sep 09 '14 at 17:38
  • @Ghus I hadn't a use for a direct network connection yet (consider sharing filesystems over network) But you could find a lot of examples by google: serversocket blender python 3 – stacker Sep 09 '14 at 20:10
  • how would this code look like with a tcp server instead of a file? – Ymmanuel Jul 07 '17 at 23:47