armory/blender/handlers.py

157 lines
5.3 KiB
Python
Raw Normal View History

2016-10-19 13:28:06 +02:00
import bpy
2016-10-27 01:11:11 +02:00
import armutils
2016-10-19 13:28:06 +02:00
import make
import make_state as state
import space_armory
2016-11-24 23:24:55 +01:00
import nodes_renderpath
2016-10-19 13:28:06 +02:00
import time
import bridge
import log
2016-10-31 19:29:03 +01:00
import props
from bpy.app.handlers import persistent
2016-10-19 13:28:06 +02:00
try:
import barmory
except ImportError:
pass
last_time = time.time()
2016-11-25 11:29:26 +01:00
# last_update_time = time.time()
2016-10-19 13:28:06 +02:00
last_operator = None
2016-11-23 15:34:59 +01:00
redraw_ui = False
redraw_progress = False
2016-10-19 13:28:06 +02:00
2016-10-31 19:29:03 +01:00
@persistent
2016-10-19 13:28:06 +02:00
def on_scene_update_post(context):
global last_time
2016-11-25 11:29:26 +01:00
# global last_update_time
2016-10-19 13:28:06 +02:00
global last_operator
2016-11-23 15:34:59 +01:00
global redraw_ui
global redraw_progress
2016-10-19 13:28:06 +02:00
2016-11-23 15:34:59 +01:00
# Redraw at the start of 'next' frame
if redraw_ui and bpy.context.screen != None:
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D' or area.type == 'PROPERTIES':
area.tag_redraw()
redraw_ui = False
if redraw_progress and bpy.context.screen != None:
for area in bpy.context.screen.areas:
if area.type == 'INFO':
area.tag_redraw()
break
2016-11-25 11:29:26 +01:00
redraw_progress = False
2016-11-23 15:34:59 +01:00
# New operator
2016-11-07 00:31:48 +01:00
ops = bpy.context.window_manager.operators
operators_changed = False
if len(ops) > 0 and last_operator != ops[-1]:
last_operator = ops[-1]
operators_changed = True
2016-11-23 15:34:59 +01:00
# Player running
state.krom_running = False
if not state.is_paused and bpy.context.screen != None:
for area in bpy.context.screen.areas:
if area.type == 'VIEW_ARMORY':
state.krom_running = True
break
2016-10-19 13:28:06 +02:00
2016-11-23 15:34:59 +01:00
# Auto patch on every operator change
if state.krom_running and \
bpy.data.worlds['Arm'].arm_play_live_patch and \
bpy.data.worlds['Arm'].arm_play_auto_build and \
operators_changed:
# Otherwise rebuild scene
if bridge.send_operator(last_operator) == False:
make.patch_project()
2016-11-26 23:05:26 +01:00
make.compile_project(target_name="krom", patch=True)
2016-10-19 13:28:06 +02:00
2016-11-23 15:34:59 +01:00
# Use frame rate for update frequency for now
if time.time() - last_time >= (1 / bpy.context.scene.render.fps):
last_time = time.time()
2016-10-19 13:28:06 +02:00
2016-11-23 15:34:59 +01:00
if state.krom_running:
# Read krom console
if barmory.get_console_updated() == 1:
log.print_player(barmory.get_console())
# Read operator console
if barmory.get_operator_updated() == 1:
bridge.parse_operator(barmory.get_operator())
# Tag redraw
if bpy.context.screen != None:
for area in bpy.context.screen.areas:
if area.type == 'VIEW_ARMORY':
2016-10-19 13:28:06 +02:00
area.tag_redraw()
2016-11-23 15:34:59 +01:00
break
2016-10-19 13:28:06 +02:00
# New output has been logged
2016-11-13 11:46:54 +01:00
if log.tag_redraw and bpy.context.screen != None:
2016-10-19 13:28:06 +02:00
log.tag_redraw = False
2016-11-23 15:34:59 +01:00
redraw_progress = True
2016-10-19 13:28:06 +02:00
# Player finished, redraw play buttons
2016-11-13 11:46:54 +01:00
if state.playproc_finished and bpy.context.screen != None:
2016-10-19 13:28:06 +02:00
state.playproc_finished = False
2016-11-23 15:34:59 +01:00
redraw_ui = True
2016-10-19 13:28:06 +02:00
# Compilation finished
2016-11-13 11:46:54 +01:00
if state.compileproc_finished and bpy.context.screen != None:
2016-10-19 13:28:06 +02:00
state.compileproc_finished = False
2016-11-23 15:34:59 +01:00
redraw_ui = True
2016-10-19 13:28:06 +02:00
# Compilation succesfull
if state.compileproc_success:
# Notify embedded player
2016-11-23 15:34:59 +01:00
if state.krom_running:
2016-10-19 13:28:06 +02:00
barmory.call_js('armory.Scene.patch();')
# Or switch to armory space
2016-11-23 15:34:59 +01:00
elif armutils.with_krom() and state.in_viewport:
2016-10-19 13:28:06 +02:00
state.play_area.type = 'VIEW_ARMORY'
# Prevent immediate operator patch
if len(ops) > 0:
last_operator = ops[-1]
2016-11-12 18:30:39 +01:00
# No attribute when using multiple windows?
if hasattr(bpy.context, 'edit_object'):
edit_obj = bpy.context.edit_object
if edit_obj != None and edit_obj.is_updated_data:
if edit_obj.type == 'MESH':
edit_obj.data.mesh_cached = False
elif edit_obj.type == 'ARMATURE':
edit_obj.data.data_cached = False
2016-10-19 13:28:06 +02:00
2017-01-03 00:16:54 +01:00
if hasattr(bpy.context, 'object'):
obj = bpy.context.object
if obj != None:
if operators_changed:
# Modifier was added/removed, recache mesh
if ops[-1].bl_idname == 'OBJECT_OT_modifier_add' or ops[-1].bl_idname == 'OBJECT_OT_modifier_remove':
obj.data.mesh_cached = False
2017-01-03 13:20:33 +01:00
# Apply transform
elif ops[-1].bl_idname == 'OBJECT_OT_transform_apply':
obj.data.mesh_cached = False
2017-01-03 00:16:54 +01:00
if obj.active_material != None and obj.active_material.is_updated:
obj.active_material.is_cached = False
2016-11-07 00:31:48 +01:00
2016-10-31 19:29:03 +01:00
@persistent
def on_load_post(context):
props.init_properties_on_load()
2016-12-09 02:10:45 +01:00
nodes_renderpath.reload_blend_data()
2016-10-31 19:29:03 +01:00
@persistent
def on_save_pre(context):
props.init_properties_on_save()
2016-10-19 13:28:06 +02:00
def register():
bpy.app.handlers.scene_update_post.append(on_scene_update_post)
2016-10-31 19:29:03 +01:00
bpy.app.handlers.save_pre.append(on_save_pre)
bpy.app.handlers.load_post.append(on_load_post)
2017-01-13 15:09:23 +01:00
# On windows, on_load_post is not called when opening .blend file from explorer
if armutils.get_os() == 'win' and armutils.get_fp() != '':
on_load_post(None)
2016-10-19 13:28:06 +02:00
def unregister():
bpy.app.handlers.scene_update_post.remove(on_scene_update_post)
2016-10-31 19:29:03 +01:00
bpy.app.handlers.save_pre.remove(on_save_pre)
2017-01-13 15:11:30 +01:00
bpy.app.handlers.load_post.remove(on_load_post)