armory/blender/arm/handlers.py

179 lines
6.9 KiB
Python
Raw Normal View History

2020-09-27 17:12:22 +02:00
import importlib
2017-02-09 22:52:47 +01:00
import os
import sys
2020-09-27 17:12:22 +02:00
2018-05-24 22:16:28 +02:00
import bpy
2016-10-31 19:29:03 +01:00
from bpy.app.handlers import persistent
2020-09-27 17:12:22 +02:00
import arm.api
import arm.nodes_logic
2019-02-10 11:47:42 +01:00
import arm.make as make
2017-03-15 12:30:14 +01:00
import arm.make_state as state
2020-09-27 17:12:22 +02:00
import arm.props as props
import arm.utils
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
2019-02-10 11:47:42 +01:00
# Recache
2019-08-24 11:50:27 +02:00
depsgraph = bpy.context.evaluated_depsgraph_get()
2019-05-23 11:57:00 +02:00
2018-12-19 20:10:34 +01:00
for update in depsgraph.updates:
uid = update.id
if hasattr(uid, 'arm_cached'):
# uid.arm_cached = False # TODO: does not trigger update
2018-12-24 13:42:30 +01:00
if isinstance(uid, bpy.types.Mesh) and uid.name in bpy.data.meshes:
2018-12-19 20:10:34 +01:00
bpy.data.meshes[uid.name].arm_cached = False
2018-12-24 13:42:30 +01:00
elif isinstance(uid, bpy.types.Curve) and uid.name in bpy.data.curves:
2018-12-19 20:10:34 +01:00
bpy.data.curves[uid.name].arm_cached = False
2018-12-24 13:42:30 +01:00
elif isinstance(uid, bpy.types.MetaBall) and uid.name in bpy.data.metaballs:
2018-12-19 20:10:34 +01:00
bpy.data.metaballs[uid.name].arm_cached = False
2018-12-24 13:42:30 +01:00
elif isinstance(uid, bpy.types.Armature) and uid.name in bpy.data.armatures:
2018-12-19 20:10:34 +01:00
bpy.data.armatures[uid.name].arm_cached = False
2018-12-24 13:42:30 +01:00
elif isinstance(uid, bpy.types.NodeTree) and uid.name in bpy.data.node_groups:
2018-12-19 20:10:34 +01:00
bpy.data.node_groups[uid.name].arm_cached = False
2018-12-24 13:42:30 +01:00
elif isinstance(uid, bpy.types.Material) and uid.name in bpy.data.materials:
2018-12-19 20:10:34 +01:00
bpy.data.materials[uid.name].arm_cached = False
2019-02-10 11:47:42 +01:00
# Send last operator to Krom
wrd = bpy.data.worlds['Arm']
if state.proc_play != None and \
state.target == 'krom' and \
wrd.arm_live_patch:
ops = bpy.context.window_manager.operators
if len(ops) > 0 and ops[-1] != None:
send_operator(ops[-1])
# Hacky solution to update armory props after operator executions
last_operator = bpy.context.active_operator
if last_operator is not None:
on_operator_post(last_operator.bl_idname)
def on_operator_post(operator_id: str) -> None:
"""Called after operator execution. Does not work for operators
executed in another context. Warning: this function is also called
when the operator execution raised an exception!"""
# 3D View > Object > Rigid Body > Copy from Active
if operator_id == "RIGIDBODY_OT_object_settings_copy":
# Copy armory rigid body settings
source_obj = bpy.context.active_object
for target_obj in bpy.context.selected_objects:
target_obj.arm_rb_linear_factor = source_obj.arm_rb_linear_factor
target_obj.arm_rb_angular_factor = source_obj.arm_rb_angular_factor
target_obj.arm_rb_trigger = source_obj.arm_rb_trigger
target_obj.arm_rb_force_deactivation = source_obj.arm_rb_force_deactivation
target_obj.arm_rb_deactivation_time = source_obj.arm_rb_deactivation_time
target_obj.arm_rb_ccd = source_obj.arm_rb_ccd
target_obj.arm_rb_collision_filter_mask = source_obj.arm_rb_collision_filter_mask
2019-02-10 11:47:42 +01:00
def send_operator(op):
if hasattr(bpy.context, 'object') and bpy.context.object != None:
obj = bpy.context.object.name
if op.name == 'Move':
vec = bpy.context.object.location
js = 'var o = iron.Scene.active.getChild("' + obj + '"); o.transform.loc.set(' + str(vec[0]) + ', ' + str(vec[1]) + ', ' + str(vec[2]) + '); o.transform.dirty = true;'
make.write_patch(js)
elif op.name == 'Resize':
vec = bpy.context.object.scale
js = 'var o = iron.Scene.active.getChild("' + obj + '"); o.transform.scale.set(' + str(vec[0]) + ', ' + str(vec[1]) + ', ' + str(vec[2]) + '); o.transform.dirty = true;'
make.write_patch(js)
elif op.name == 'Rotate':
vec = bpy.context.object.rotation_euler.to_quaternion()
js = 'var o = iron.Scene.active.getChild("' + obj + '"); o.transform.rot.set(' + str(vec[1]) + ', ' + str(vec[2]) + ', ' + str(vec[3]) + ' ,' + str(vec[0]) + '); o.transform.dirty = true;'
make.write_patch(js)
else: # Rebuild
make.patch()
2018-12-19 20:10:34 +01:00
def always():
2019-01-07 11:19:04 +01:00
# Force ui redraw
2018-12-19 20:10:34 +01:00
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
2019-01-07 11:19:04 +01:00
# TODO: depsgraph.updates only triggers material trees
space = arm.utils.logic_editor_space(context_screen)
if space != None:
space.node_tree.arm_cached = 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
# Register newly added nodes and node categories
arm.nodes_logic.register_nodes()
2019-02-05 12:59:34 +01:00
# Show trait users as collections
arm.utils.update_trait_collections()
props.update_armory_world()
2017-12-11 00:55:26 +01:00
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)