metaforce/hecl/blender/blendershell.py

123 lines
3.6 KiB
Python
Raw Normal View History

import bpy, sys, os, re, code
2015-07-28 23:54:54 +00:00
ARGS_PATTERN = re.compile(r'''(?:"([^"]+)"|'([^']+)'|(\S+))''')
# Extract pipe file descriptors from arguments
2015-05-24 04:51:16 +00:00
print(sys.argv)
if '--' not in sys.argv:
bpy.ops.wm.quit_blender()
args = sys.argv[sys.argv.index('--')+1:]
readfd = int(args[0])
writefd = int(args[1])
def readpipeline():
retval = bytearray()
while True:
ch = os.read(readfd, 1)
if ch == b'\n' or ch == b'':
return retval
retval += ch
def writepipeline(linebytes):
2015-05-24 22:19:28 +00:00
os.write(writefd, linebytes + b'\n')
def writepipebuf(linebytes):
writepipeline(b'BUF')
os.write(writefd, struct.pack('I', len(linebytes)) + linebytes)
def quitblender():
writepipeline(b'QUITTING')
bpy.ops.wm.quit_blender()
2015-05-24 04:51:16 +00:00
# Check that HECL addon is installed/enabled
if 'hecl' not in bpy.context.user_preferences.addons:
if 'FINISHED' not in bpy.ops.wm.addon_enable(module='hecl'):
writepipeline(b'NOADDON')
bpy.ops.wm.quit_blender()
2015-05-24 22:19:28 +00:00
# Make addon available to commands
import hecl
# Intro handshake
writepipeline(b'READY')
ackbytes = readpipeline()
if ackbytes != b'ACK':
quitblender()
# Command loop
while True:
2015-07-28 23:54:54 +00:00
cmdline = readpipeline()
if cmdline == b'':
print('HECL connection lost')
bpy.ops.wm.quit_blender()
cmdargs = []
for match in ARGS_PATTERN.finditer(cmdline.decode()):
cmdargs.append(match.group(match.lastindex))
print(cmdargs)
2015-07-28 23:54:54 +00:00
if cmdargs[0] == 'QUIT':
quitblender()
2015-07-28 23:54:54 +00:00
elif cmdargs[0] == 'OPEN':
if 'FINISHED' in bpy.ops.wm.open_mainfile(filepath=cmdargs[1]):
2015-05-24 22:19:28 +00:00
writepipeline(b'FINISHED')
else:
writepipeline(b'CANCELLED')
2015-05-24 04:51:16 +00:00
2015-07-28 23:54:54 +00:00
elif cmdargs[0] == 'CREATE':
2015-07-28 02:25:33 +00:00
bpy.context.user_preferences.filepaths.save_version = 0
2015-07-28 23:54:54 +00:00
if 'FINISHED' in bpy.ops.wm.save_as_mainfile(filepath=cmdargs[1]):
2015-07-28 02:25:33 +00:00
writepipeline(b'FINISHED')
else:
writepipeline(b'CANCELLED')
elif cmdargs[0] == 'SAVE':
bpy.context.user_preferences.filepaths.save_version = 0
if 'FINISHED' in bpy.ops.wm.save_mainfile(check_existing=False):
writepipeline(b'FINISHED')
else:
writepipeline(b'CANCELLED')
2015-07-28 23:54:54 +00:00
elif cmdargs[0] == 'PYBEGIN':
2015-07-28 02:25:33 +00:00
writepipeline(b'READY')
globals = dict()
compbuf = str()
prev_leading_spaces = 0
2015-07-28 02:25:33 +00:00
while True:
try:
line = readpipeline()
if line == b'PYEND':
writepipeline(b'DONE')
break
linestr = line.decode()
if linestr.isspace() or not len(linestr):
writepipeline(b'OK')
continue
leading_spaces = len(linestr) - len(linestr.lstrip())
if prev_leading_spaces and not leading_spaces:
compbuf += '\n'
co = code.compile_command(compbuf, filename='<HECL>')
if co is not None:
exec(co, globals)
compbuf = str()
prev_leading_spaces = leading_spaces
if len(compbuf):
compbuf += '\n'
compbuf += linestr
co = code.compile_command(compbuf, filename='<HECL>')
if co is not None:
exec(co, globals)
compbuf = str()
2015-07-28 02:25:33 +00:00
except Exception as e:
writepipeline(b'EXCEPTION')
2015-07-28 23:54:54 +00:00
raise
2015-07-28 02:25:33 +00:00
break
writepipeline(b'OK')
2015-07-28 23:54:54 +00:00
elif cmdargs[0] == 'PYEND':
2015-07-28 02:25:33 +00:00
writepipeline(b'ERROR')
2015-05-24 04:51:16 +00:00
else:
2015-07-28 23:54:54 +00:00
hecl.command(cmdargs, writepipeline, writepipebuf)