I am wondering, is there a way to call Blender functions from a regular Python shell outside Blender. We need to control Blender for a different application - can this be done in 2.5?
(For example, with Houdini, you can import the hou module into a regular Python shell, so that you can easily integrate Houdini into your existing Python-based scripts.)
Actually there is a way. Blender can be run in background with -b option and there is also -P filename.py option, so it is possible to make blender start, do some script and hopefully render to file. There would have to be a Python program that would prepare the filename.py and then execute blender. I have not enough experience with programming but it is doable.
For piping a simple command line, although it needs work;
Try running it with “Blender -P <file>”
it works well but i cannot get the tabbing done right.
you can change stdin and stdout to anything else or buffer the code object ahead of time and pass it through a pipe. Which is what i think your after so you dont have to rewrite the interface.import sys
import threading #python2.6
import builtin #python3.1 #import builtins
class _thread(threading.Thread):
import sys,time,bpy,Blender
def quit(self):
self.pout(“Killing Session”)
thread._Thread__stop()
thread.notdone=0
class WrapExceptions():
def init(self,msg,type=None):
if (type==None):
self.type=self.str()
else:
self.type=type
self.msg=[":’’’",self.type,msg,"’’’"].join(’ ‘)
self._init()
def str(self):
return("Exception: "+self.msg.str())
def repr(self):
return(repr(self.msg))
def pout(self,arg):
self.stdout.write(arg.str())
def pin(self):
return(self.stdin.readline())
def _init(self):
self.notdone=1
self.stdin=sys.stdin
self.stdout=sys.stderr
self.init(target=self._slave_thread,args=[])
self.start()
def build_globals(self,objects):
ret={‘all’:[]}
for anObj in objects:
dl=self.builtins.dir(anObj)
ret[‘all’].append(dl)
for Objs in dl:
ret[Objs.str()]=anObj.getattribute(Objs.str())
return(ret)
def _slave_thread(self):
cmdStr=’’;ind=0
self.local=self.build_globals([self.builtins,self])
self.pout(’#For import; use setattr(builtins,‘name’,import(‘name’))
‘)
while(self.notdone):
if (cmdStr==’’):
self.pout(’
>>> ‘)
else:
self.pout(’
… ‘+self.builtins.chr(9).zfill(ind))
cmdStr+=self.pin()
p=cmdStr.len()
while (p>0):
if (cmdStr[p-1]==’:’):
ind+=1
break
elif(cmdStr[p-1].eq(" “) or cmdStr[p-1].eq(”
")):
p-=1
else:
try:
code=self.builtins.compile(cmdStr,’’,‘eval’)
self.pout(dir(code))
self.builtins.eval(code,self.local)
except(self.WrapExceptions,cmdStr):
pass
cmdStr=’’
break
def Gc_Thread(self):
while(self.notdone):
time.sleep(1)
if (not thread.isAlive()):
thread._init()
thread=_thread()
thread.builtins=builtin
thread._init()