armory/blender/arm/handlers.py

112 lines
3.7 KiB
Python
Raw Normal View History

2017-02-09 22:52:47 +01:00
import os
import sys
2018-05-24 22:16:28 +02:00
import bpy
2018-09-13 17:26:26 +02:00
import importlib
2016-10-31 19:29:03 +01:00
from bpy.app.handlers import persistent
2017-03-15 12:30:14 +01:00
import arm.utils
import arm.props as props
import arm.make_state as state
2018-08-08 10:29:07 +02:00
import arm.api
2016-10-19 13:28:06 +02:00
2016-10-31 19:29:03 +01:00
@persistent
2018-12-19 20:10:34 +01:00
def on_depsgraph_update_post(self):
if state.proc_build != None:
return
2018-05-24 22:16:28 +02:00
2018-12-19 20:10:34 +01:00
depsgraph = bpy.context.depsgraph
for update in depsgraph.updates:
uid = update.id
if hasattr(uid, 'arm_cached'):
# uid.arm_cached = False # TODO: does not trigger update
if isinstance(uid, bpy.types.Mesh):
bpy.data.meshes[uid.name].arm_cached = False
elif isinstance(uid, bpy.types.Curve):
bpy.data.curves[uid.name].arm_cached = False
elif isinstance(uid, bpy.types.MetaBall):
bpy.data.metaballs[uid.name].arm_cached = False
elif isinstance(uid, bpy.types.Armature):
bpy.data.armatures[uid.name].arm_cached = False
elif isinstance(uid, bpy.types.NodeTree):
bpy.data.node_groups[uid.name].arm_cached = False
elif isinstance(uid, bpy.types.Material):
bpy.data.materials[uid.name].arm_cached = False
def always():
if state.redraw_ui and context_screen != None:
for area in context_screen.areas:
2018-06-19 23:21:53 +02:00
if area.type == 'VIEW_3D' or area.type == 'PROPERTIES':
area.tag_redraw()
state.redraw_ui = False
2018-12-19 20:10:34 +01:00
return 0.5
2017-02-09 22:52:47 +01:00
2017-03-06 02:29:03 +01:00
appended_py_paths = []
2018-12-19 20:10:34 +01:00
context_screen = None
2016-10-31 19:29:03 +01:00
@persistent
def on_load_post(context):
2017-03-06 02:29:03 +01:00
global appended_py_paths
2017-02-09 22:52:47 +01:00
2018-12-19 20:10:34 +01:00
global context_screen
context_screen = bpy.context.screen
2016-10-31 19:29:03 +01:00
props.init_properties_on_load()
2017-11-22 21:17:36 +01:00
reload_blend_data()
2016-10-31 19:29:03 +01:00
2017-10-17 13:39:50 +02:00
bpy.ops.arm.sync_proxy()
2017-03-06 02:29:03 +01:00
wrd = bpy.data.worlds['Arm']
2017-05-24 23:04:24 +02:00
wrd.arm_recompile = True
2018-08-08 10:29:07 +02:00
arm.api.drivers = dict()
2017-05-24 23:04:24 +02:00
2018-08-08 10:29:07 +02:00
# Load libraries
2017-09-05 00:36:16 +02:00
if os.path.exists(arm.utils.get_fp() + '/Libraries'):
libs = os.listdir(arm.utils.get_fp() + '/Libraries')
for lib in libs:
if os.path.isdir(arm.utils.get_fp() + '/Libraries/' + lib):
fp = arm.utils.get_fp() + '/Libraries/' + lib
if fp not in appended_py_paths and os.path.exists(fp + '/blender.py'):
appended_py_paths.append(fp)
2018-09-13 17:26:26 +02:00
sys.path.append(fp)
2017-09-05 00:36:16 +02:00
import blender
2018-09-13 17:26:26 +02:00
importlib.reload(blender)
2017-09-05 00:36:16 +02:00
blender.register()
2018-09-13 17:26:26 +02:00
sys.path.remove(fp)
2017-02-09 22:52:47 +01:00
2017-12-11 00:55:26 +01:00
arm.utils.update_trait_groups()
2017-11-22 21:17:36 +01:00
def reload_blend_data():
armory_pbr = bpy.data.node_groups.get('Armory PBR')
if armory_pbr == None:
load_library('Armory PBR')
def load_library(asset_name):
if bpy.data.filepath.endswith('arm_data.blend'): # Prevent load in library itself
return
sdk_path = arm.utils.get_sdk_path()
data_path = sdk_path + '/armory/blender/data/arm_data.blend'
data_names = [asset_name]
# Import
data_refs = data_names.copy()
with bpy.data.libraries.load(data_path, link=False) as (data_from, data_to):
data_to.node_groups = data_refs
for ref in data_refs:
ref.use_fake_user = True
2016-10-19 13:28:06 +02:00
def register():
2016-10-31 19:29:03 +01:00
bpy.app.handlers.load_post.append(on_load_post)
2018-12-19 20:10:34 +01:00
bpy.app.handlers.depsgraph_update_post.append(on_depsgraph_update_post)
# bpy.app.handlers.undo_post.append(on_undo_post)
bpy.app.timers.register(always, persistent=True)
2018-08-20 16:26:52 +02:00
# TODO: On windows, on_load_post is not called when opening .blend file from explorer
if arm.utils.get_os() == 'win' and arm.utils.get_fp() != '':
on_load_post(None)
2017-11-22 21:17:36 +01:00
reload_blend_data()
2016-10-19 13:28:06 +02:00
def unregister():
2017-01-13 15:11:30 +01:00
bpy.app.handlers.load_post.remove(on_load_post)
2018-12-19 20:10:34 +01:00
bpy.app.handlers.depsgraph_update_post.remove(on_depsgraph_update_post)
# bpy.app.handlers.undo_post.remove(on_undo_post)