def send_operator(op): # Try to translate operator directly to armory if arm.utils.with_krom() and hasattr( bpy.context, 'object') and bpy.context.object != None: objname = bpy.context.object.name if op.name == 'Translate': vec = bpy.context.object.location js_source = 'var o = armory.Scene.active.getChild("' + objname + '"); o.transform.loc.set(' + str( vec[0]) + ', ' + str(vec[1]) + ', ' + str( vec[2]) + '); o.transform.dirty = true;' barmory.call_js(js_source) return True elif op.name == 'Resize': vec = bpy.context.object.scale js_source = 'var o = armory.Scene.active.getChild("' + objname + '"); o.transform.scale.set(' + str( vec[0]) + ', ' + str(vec[1]) + ', ' + str( vec[2]) + '); o.transform.dirty = true;' barmory.call_js(js_source) return True elif op.name == 'Rotate': vec = bpy.context.object.rotation_euler.to_quaternion() js_source = 'var o = armory.Scene.active.getChild("' + objname + '"); o.transform.rot.set(' + str( vec[1]) + ', ' + str(vec[2]) + ', ' + str(vec[3]) + ' ,' + str( vec[0]) + '); o.transform.dirty = true;' barmory.call_js(js_source) return True return False
def send_operator(op): # Try to translate operator directly to armory if armutils.with_krom() and bpy.context.object != None: objname = bpy.context.object.name if op.name == 'Translate': vec = bpy.context.object.location js_source = 'var o = armory.Scene.active.getChild("' + objname + '"); o.transform.loc.set(' + str(vec[0]) + ', ' + str(vec[1]) + ', ' + str(vec[2]) + '); o.transform.dirty = true;' barmory.call_js(js_source) return True elif op.name == 'Resize': vec = bpy.context.object.scale js_source = 'var o = armory.Scene.active.getChild("' + objname + '"); o.transform.scale.set(' + str(vec[0]) + ', ' + str(vec[1]) + ', ' + str(vec[2]) + '); o.transform.dirty = true;' barmory.call_js(js_source) return True elif op.name == 'Rotate': vec = bpy.context.object.rotation_euler.to_quaternion() js_source = 'var o = armory.Scene.active.getChild("' + objname + '"); o.transform.rot.set(' + str(vec[1]) + ', ' + str(vec[2]) + ', ' + str(vec[3]) + ' ,' + str(vec[0]) + '); o.transform.dirty = true;' barmory.call_js(js_source) return True return False
def on_scene_update_post(context): global last_time # global last_update_time global last_operator global redraw_ui global redraw_progress global first_update if first_update == True: # Skip first one, object reports is_update_data first_update = False return # 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 redraw_progress = False # New operator 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 # Undo was performed - Blender clears the complete operator stack, undo last known operator atleast # if len(ops) == 0 and last_operator != None: # if hasattr(bpy.context, 'object'): # op_changed(last_operator, bpy.context.object) # last_operator = None if state.is_render: import numpy # fp = arm.utils.get_fp_build() krom_location, krom_path = arm.utils.krom_paths() fp = krom_location resx, resy = arm.utils.get_render_resolution(arm.utils.get_active_scene()) wrd = bpy.data.worlds['Arm'] cformat = wrd.rp_rendercapture_format if cformat == '8bit': cbits = 4 ctype = numpy.uint8 elif cformat == '16bit': cbits = 8 ctype = numpy.float16 elif cformat == '32bit': cbits = 16 ctype = numpy.float32 if os.path.isfile(fp + '/render.bin') and os.path.getsize(fp + '/render.bin') == resx * resy * cbits: data = numpy.fromfile(fp + '/render.bin', dtype=ctype) data = data.astype(float) if cformat == '8bit': data = numpy.divide(data, 255) n = "Render Result" if n in bpy.data.images and bpy.data.images[n].size[0] == resx and bpy.data.images[n].size[1] == resy: bpy.data.images[n].pixels = data else: float_buffer = cformat != '8bit' image = bpy.data.images.new("Render Result", width=resx, height=resy, float_buffer=float_buffer) image.pixels = data state.is_render = False os.remove(fp + '/render.bin') print('Output image captured into Blender - UV/Image Editor - Render Result') # 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 # Auto patch on every operator change if not 'Arm' in bpy.data.worlds: props.create_wrd() wrd = bpy.data.worlds['Arm'] if state.krom_running and \ wrd.arm_play_live_patch and \ wrd.arm_play_auto_build and \ operators_changed: # Otherwise rebuild scene if bridge.send_operator(last_operator) == False: if state.compileproc == None: # state.is_paused = True # Speeds up recompile assets.invalidate_enabled = False make.play_project(in_viewport=True) assets.invalidate_enabled = True # Use frame rate for update frequency for now fps_mult = 2.0 if (state.krom_running and arm.utils.get_os() == 'win') else 1.0 # Handlers called less frequently on Windows? fps = 60 if time.time() - last_time >= (1 / (fps * fps_mult)): last_time = time.time() 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': area.tag_redraw() break # New output has been logged if log.tag_redraw and bpy.context.screen != None: log.tag_redraw = False redraw_progress = True # Player finished, redraw play buttons if state.playproc_finished and bpy.context.screen != None: state.playproc_finished = False redraw_ui = True # Compilation finished if state.compileproc_finished and bpy.context.screen != None: state.compileproc_finished = False redraw_ui = True # Compilation succesfull if state.compileproc_success: # Notify embedded player if state.krom_running: if state.recompiled and wrd.arm_play_live_patch: # state.is_paused = False barmory.parse_code() for s in state.mod_scripts: barmory.call_js('armory.Scene.patchTrait("' + s + '");') else: barmory.call_js('armory.Scene.patch();') # Or switch to armory space elif arm.utils.with_krom() and state.in_viewport: state.play_area.type = 'VIEW_ARMORY' # Prevent immediate operator patch if len(ops) > 0: last_operator = ops[-1] # No attribute when using multiple windows? if hasattr(bpy.context, 'active_object'): obj = bpy.context.active_object if obj != None: if obj.is_updated_data: # + data.is_updated recache(obj) if obj.type == 'ARMATURE': # Newly parented objects needs to be recached for c in obj.children: if c.is_updated_data: recache(c) if hasattr(bpy.context, 'sculpt_object') and bpy.context.sculpt_object != None: recache(bpy.context.sculpt_object) if hasattr(bpy.context, 'active_pose_bone') and bpy.context.active_pose_bone != None: recache(bpy.context.active_object) if hasattr(bpy.context, 'object'): obj = bpy.context.object if obj != None: if operators_changed: op_changed(ops[-1], obj) if obj.active_material != None and obj.active_material.is_updated: if obj.active_material.lock_cache == True: # is_cached was set to true obj.active_material.lock_cache = False else: obj.active_material.is_cached = False # Invalidate logic node tree cache if it is being edited.. space = arm.utils.logic_editor_space() if space != None: space.node_tree.is_cached = False
def on_scene_update_post(context): global last_time # global last_update_time global last_operator global redraw_ui global redraw_progress # 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 redraw_progress = False # New operator 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 # Undo was performed - Blender clears the complete operator stack, undo last known operator atleast # if len(ops) == 0 and last_operator != None: # if hasattr(bpy.context, 'object'): # op_changed(last_operator, bpy.context.object) # last_operator = None # 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 # Auto patch on every operator change wrd = bpy.data.worlds['Arm'] if state.krom_running and \ wrd.arm_play_live_patch and \ wrd.arm_play_auto_build and \ operators_changed: # Otherwise rebuild scene if bridge.send_operator(last_operator) == False: make.play_project(in_viewport=True) # Use frame rate for update frequency for now if time.time() - last_time >= (1 / bpy.context.scene.render.fps): last_time = time.time() 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': area.tag_redraw() break # New output has been logged if log.tag_redraw and bpy.context.screen != None: log.tag_redraw = False redraw_progress = True # Player finished, redraw play buttons if state.playproc_finished and bpy.context.screen != None: state.playproc_finished = False redraw_ui = True # Compilation finished if state.compileproc_finished and bpy.context.screen != None: state.compileproc_finished = False redraw_ui = True # Compilation succesfull if state.compileproc_success: # Notify embedded player if state.krom_running: barmory.call_js('armory.Scene.patch();') # Or switch to armory space elif armutils.with_krom() and state.in_viewport: state.play_area.type = 'VIEW_ARMORY' # Prevent immediate operator patch if len(ops) > 0: last_operator = ops[-1] # 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 if hasattr(bpy.context, 'object'): obj = bpy.context.object if obj != None: if operators_changed: op_changed(ops[-1], obj) if obj.active_material != None and obj.active_material.is_updated: if obj.active_material.lock_cache == True: # is_cached was set to true obj.active_material.lock_cache = False else: obj.active_material.is_cached = False
def on_scene_update_post(context): global last_time # global last_update_time global last_operator global redraw_ui global redraw_progress global first_update if first_update == True: # Skip first one, object reports is_update_data first_update = False return # 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 redraw_progress = False # New operator 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 # Undo was performed - Blender clears the complete operator stack, undo last known operator atleast # if len(ops) == 0 and last_operator != None: # if hasattr(bpy.context, 'object'): # op_changed(last_operator, bpy.context.object) # last_operator = None # 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 # Auto patch on every operator change wrd = bpy.data.worlds['Arm'] if state.krom_running and \ wrd.arm_play_live_patch and \ wrd.arm_play_auto_build and \ operators_changed: # Otherwise rebuild scene if bridge.send_operator(last_operator) == False: assets.invalidate_enabled = False make.play_project(in_viewport=True) assets.invalidate_enabled = True # Use frame rate for update frequency for now fps_mult = 2.0 if ( state.krom_running and arm.utils.get_os() == 'win') else 1.0 # Handlers called less frequently on Windows? if time.time() - last_time >= (1 / (bpy.context.scene.render.fps * fps_mult)): last_time = time.time() 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': area.tag_redraw() break # New output has been logged if log.tag_redraw and bpy.context.screen != None: log.tag_redraw = False redraw_progress = True # Player finished, redraw play buttons if state.playproc_finished and bpy.context.screen != None: state.playproc_finished = False redraw_ui = True # Compilation finished if state.compileproc_finished and bpy.context.screen != None: state.compileproc_finished = False redraw_ui = True # Compilation succesfull if state.compileproc_success: # Notify embedded player if state.krom_running: barmory.call_js('armory.Scene.patch();') # Or switch to armory space elif arm.utils.with_krom() and state.in_viewport: state.play_area.type = 'VIEW_ARMORY' # Prevent immediate operator patch if len(ops) > 0: last_operator = ops[-1] # No attribute when using multiple windows? if hasattr(bpy.context, 'active_object'): obj = bpy.context.active_object if obj != None: if obj.is_updated_data: # + data.is_updated recache(obj) if obj.type == 'ARMATURE': # Newly parented objects needs to be recached for c in obj.children: if c.is_updated_data: recache(c) if hasattr(bpy.context, 'sculpt_object') and bpy.context.sculpt_object != None: recache(bpy.context.sculpt_object) if hasattr(bpy.context, 'active_pose_bone') and bpy.context.active_pose_bone != None: recache(bpy.context.active_object) if hasattr(bpy.context, 'object'): obj = bpy.context.object if obj != None: if operators_changed: op_changed(ops[-1], obj) if obj.active_material != None and obj.active_material.is_updated: if obj.active_material.lock_cache == True: # is_cached was set to true obj.active_material.lock_cache = False else: obj.active_material.is_cached = False if hasattr(bpy.context, 'window') and bpy.context.window != None: # Invalidate logic node tree cache if it is being edited.. areas = bpy.context.window.screen.areas for area in areas: if area.type == 'NODE_EDITOR': for space in area.spaces: if space.type == 'NODE_EDITOR': if space.node_tree != None and space.node_tree.bl_idname == 'ArmLogicTreeType': # and space.node_tree.is_updated: space.node_tree.is_cached = False
def on_scene_update_post(context): global last_time # global last_update_time global last_operator global redraw_ui global redraw_progress # 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 redraw_progress = False # New operator 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 # 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 # 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() make.compile_project(target_name="krom", patch=True) # Use frame rate for update frequency for now if time.time() - last_time >= (1 / bpy.context.scene.render.fps): last_time = time.time() 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': area.tag_redraw() break # New output has been logged if log.tag_redraw and bpy.context.screen != None: log.tag_redraw = False redraw_progress = True # Player finished, redraw play buttons if state.playproc_finished and bpy.context.screen != None: state.playproc_finished = False redraw_ui = True # Compilation finished if state.compileproc_finished and bpy.context.screen != None: state.compileproc_finished = False redraw_ui = True # Compilation succesfull if state.compileproc_success: # Notify embedded player if state.krom_running: barmory.call_js('armory.Scene.patch();') # Or switch to armory space elif armutils.with_krom() and state.in_viewport: state.play_area.type = 'VIEW_ARMORY' # Prevent immediate operator patch if len(ops) > 0: last_operator = ops[-1] # 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 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 if obj.active_material != None and obj.active_material.is_updated: obj.active_material.is_cached = False