def execute(self, context): # ensure that the local "bgelogic" folder exists local_bgelogic_folder = bpy.path.abspath("//bgelogic") if not os.path.exists(local_bgelogic_folder): try: os.mkdir(local_bgelogic_folder) except PermissionError: self.report( {"ERROR"}, "Cannot generate the code because the blender file has " "not been saved or the user has no write permission for " "the containing folder.") utils.set_compile_status(utils.TREE_FAILED) return {"FINISHED"} for tree in bpy.data.node_groups: if tree.bl_idname == bge_netlogic.ui.BGELogicTree.bl_idname: try: tree_code_generator.TreeCodeGenerator( ).write_code_for_tree(tree) except Exception: utils.error(f"Couldn't compile tree {tree.name}!") utils.set_compile_status(utils.TREE_COMPILED_ALL) try: context.region.tag_redraw() except Exception: utils.warn("Couldn't redraw panel, code updated.") return {"FINISHED"}
def _update_all_logic_tree_code(): now = time.time() _update_queue.append(now) now = time.time() last_event = _update_queue[-1] utils.set_compile_status(utils.TREE_MODIFIED) try: bpy.ops.bge_netlogic.generate_logicnetwork_all() except Exception: utils.error("Unknown Error, abort generating Network code")
def execute(self, context): # ensure that the local "bgelogic" folder exists local_bgelogic_folder = bpy.path.abspath("//bgelogic") if not os.path.exists(local_bgelogic_folder): try: os.mkdir(local_bgelogic_folder) except PermissionError: self.report( {"ERROR"}, "Cannot generate the code because the blender file has " "not been saved or the user has no write permission for " "the containing folder." ) utils.set_compile_status(utils.TREE_FAILED) return {"FINISHED"} # write the current tree in a python module, # in the directory of the current blender file context = bpy.context try: tree = context.space_data.edit_tree tree_code_generator.TreeCodeGenerator().write_code_for_tree(tree) except Exception as e: utils.error(e) utils.warn('Automatic Update failed, attempting hard generation...') if getattr(bpy.context.scene.logic_node_settings, 'use_generate_all', False): self.report( {'ERROR'}, 'Tree to edit not found! Updating All Trees.' ) for tree in bpy.data.node_groups: if tree.bl_idname == bge_netlogic.ui.BGELogicTree.bl_idname: tree_code_generator.TreeCodeGenerator().write_code_for_tree(tree) utils.set_compile_status(utils.TREE_FAILED) return {"FINISHED"} else: self.report( {'ERROR'}, 'Tree to edit not found! Aborting.' ) utils.error('Tree to edit not found! Aborting.') utils.set_compile_status(utils.TREE_FAILED) return {"FINISHED"} utils.set_compile_status(utils.TREE_COMPILED) try: context.region.tag_redraw() except Exception: utils.warn("Couldn't redraw panel, code updated.") return {"FINISHED"}
def update_tree_name(tree, old_name): utils.set_compile_status(utils.TREE_MODIFIED) new_name = tree.name _tree_to_name_map[tree] = new_name old_name_code = utilities.strip_tree_name(old_name) new_name_code = utilities.strip_tree_name(new_name) new_pymodule_name = utilities.py_module_name_for_tree(tree) # old_pymodule_name = ( # utilities.py_module_name_for_stripped_tree_name(old_name_code)) new_py_controller_module_string = ( utilities.py_controller_module_string(new_pymodule_name)) for ob in bpy.data.objects: old_status = None is_tree_applied_to_object = False for tree_item in ob.bgelogic_treelist: if tree_item.tree_name == new_name: st = tree_item.tree_initial_status utils.remove_tree_item_from_object(ob, tree_item.tree_name) new_entry = ob.bgelogic_treelist.add() new_entry.tree_name = tree.name new_entry.tree = tree # this will set both new_entry.tree_initial_status and add a # game property that makes the status usable at runtime utils.set_network_initial_status_key(ob, tree.name, st) tree_item.tree_name = new_name if old_status is not None: raise RuntimeError( "We have two trees with the same name in {}".format( ob.name)) if is_tree_applied_to_object: utilities.rename_initial_status_game_object_property( ob, old_name, new_name) gs = ob.game idx = 0 check_name = utils.make_valid_name(old_name) comp_name = f'nl_{check_name.lower()}' clsname = utils.make_valid_name(new_name) new_comp_name = f'nl_{clsname.lower()}.{clsname}' for c in gs.components: if c.module == comp_name: try: ops.tree_code_generator.TreeCodeGenerator( ).write_code_for_tree(tree) except Exception: utils.error(f"Couldn't compile tree {tree.name}!") text = bpy.data.texts.get(f'{comp_name}.py') if text: bpy.data.texts.remove(text) active_object = bpy.context.object bpy.context.view_layer.objects.active = ob bpy.ops.logic.python_component_remove(index=idx) bpy.ops.logic.python_component_register( component_name=new_comp_name) bpy.context.view_layer.objects.active = active_object idx += 1 for sensor in gs.sensors: if old_name_code in sensor.name: sensor.name = sensor.name.replace(old_name_code, new_name_code) for controller in gs.controllers: if old_name_code in controller.name: controller.name = controller.name.replace( old_name_code, new_name_code) if isinstance(controller, bpy.types.PythonController): controller.module = new_py_controller_module_string for actuator in gs.actuators: if old_name_code in actuator.name: actuator.name = actuator.name.replace( old_name_code, new_name_code) utils.success(f'Renamed Tree {old_name_code} to {new_name_code}')