コード例 #1
0
def _consume_update_tree_code_queue():
    edit_tree = getattr(bpy.context.space_data, "edit_tree", None)
    if _generate_on_game_start not in bpy.app.handlers.game_pre:
        bpy.app.handlers.game_pre.append(_generate_on_game_start)
    if edit_tree:
        # edit_tree = bpy.context.space_data.edit_tree
        old_name = _tree_to_name_map.get(edit_tree)
        if not old_name:
            _tree_to_name_map[edit_tree] = edit_tree.name
        else:
            if old_name != edit_tree.name:
                update_tree_name(edit_tree, old_name)
    if not _update_queue:
        return
    now = time.time()
    last_event = _update_queue[-1]
    delta = now - last_event
    if delta > 0.25:
        _update_queue.clear()
        try:
            bpy.ops.bge_netlogic.generate_logicnetwork()
        except Exception:
            if getattr(bpy.context.scene.logic_node_settings,
                       'use_generate_all', False):
                utils.warn('Could not update tree, updating all...')
                bpy.ops.bge_netlogic.generate_logicnetwork_all()
            else:
                utils.error('Could not update tree, context incorrect!')
        return True
コード例 #2
0
 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"}
コード例 #3
0
 def cleanup(self, context):
     if self.socket.value == "Press a key...":
         self.socket.value = ""
     self.socket = None
     self.node = None
     try:
         context.region.tag_redraw()
     except Exception:
         utils.warn("Couldn't redraw panel, code updated.")
コード例 #4
0
 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"}
コード例 #5
0
    def invoke(self, context, event):
        self.socket = context.socket
        self.node = context.node

        if (not self.socket) and (not self.node):
            utils.error("No socket or Node")
            return {'FINISHED'}

        if (self.socket):
            self.socket.value = "Press a key..."

        else:
            self.node.value = "Press a key..."
        try:
            context.region.tag_redraw()
        except Exception:
            utils.warn("Couldn't redraw panel, code updated.")
        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}
コード例 #6
0
 def execute(self, context):
     if context.window is None:
         utils.warn('Working Window not found, hibernating...')
         bge_netlogic._tree_code_writer_started = False
         return {"FINISHED"}
     if context.window_manager is None:
         utils.warn('Window Manager not found, hibernating...')
         bge_netlogic._tree_code_writer_started = False
         return {"FINISHED"}
     if self.timer is not None:
         utils.warn('No Timer Set. Hibernating...')
         return {'FINISHED'}
     self.timer = context.window_manager.event_timer_add(
         1.0, window=context.window)
     context.window_manager.modal_handler_add(self)
     return {"RUNNING_MODAL"}
コード例 #7
0
    def group_make(self, group_name, add_nodes):
        node_tree = bpy.data.node_groups.new(group_name, 'BGELogicTree')
        group_name = node_tree.name

        nodes = node_tree.nodes
        new_nodes = {}
        parent_tree = bpy.context.space_data.edit_tree
        locs = []

        for node in add_nodes:
            added_node = nodes.new(node.bl_idname)
            added_node.location = node.location
            new_nodes[node] = added_node

        for old_node in new_nodes:
            new_node = new_nodes[old_node]
            for attr in dir(old_node):
                if attr in NODE_ATTRS:
                    setattr(new_node, attr, getattr(old_node, attr))
            for socket in old_node.outputs:
                for link in socket.links:
                    to_node = link.to_node
                    if to_node not in add_nodes:
                        msg = 'Some linked Nodes are not selected!'
                        self.report({"ERROR"}, msg)
                        utils.error(msg)
                        return None
            for socket in old_node.inputs:
                index = self._index_of(socket, old_node.inputs)
                for attr in dir(socket):
                    if attr in NODE_ATTRS or attr.startswith('slot_'):
                        try:
                            if attr != 'label':
                                setattr(new_node.inputs[index], attr,
                                        getattr(socket, attr))
                        except Exception:
                            utils.warn(
                                'Attribute {} not writable.'.format(attr))
                for link in socket.links:
                    try:
                        output_socket = link.from_socket
                        output_node = new_nodes[output_socket.node]
                        outdex = self._index_of(output_socket,
                                                output_socket.node.outputs)
                        node_tree.links.new(new_node.inputs[index],
                                            output_node.outputs[outdex])
                    except Exception:
                        bpy.data.node_groups.remove(node_tree)
                        msg = 'Some linked Nodes are not selected! Aborting...'
                        self.report({"ERROR"}, msg)
                        utils.error(msg)
                        return None
            locs.append(old_node.location)

        for old_node in new_nodes:
            parent_tree.nodes.remove(old_node)
        redir = parent_tree.nodes.new('NLActionExecuteNetwork')
        redir.inputs[0].value = True

        try:
            redir.inputs[1].value = bpy.context.object
        except Exception:
            msg = 'No Object was selected; Set Object in tree {} manually!'.format(
                parent_tree.name)
            self.report({"WARNING"}, msg)
            utils.warn(msg)
        redir.inputs[2].value = bpy.data.node_groups[group_name]
        redir.location = self.avg_location(locs)
        node_tree.use_fake_user = True
        utils.success(f'Created Node Tree {group_name}.')
        return node_tree
コード例 #8
0
    def group_make(self, group_name, add_nodes):
        node_tree = bpy.data.node_groups.new(group_name, 'BGELogicTree')
        group_name = node_tree.name
        attrs = [
            'value',
            'game_object',
            'default_value',
            'use_toggle',
            'true_label',
            'false_label',
            'value_type',
            'bool_editor',
            'int_editor',
            'float_editor',
            'string_editor',
            'radians',
            'filepath_value',
            'sound_value',
            'float_field',
            'expression_field',
            'input_type',
            'value_x',
            'value_y',
            'value_z',
            'title',
            'local',
            'operator',
            'formatted',
            'pulse',
            'hide',
            'label',
            'use_owner',
            'advanced'
        ]

        nodes = node_tree.nodes
        new_nodes = {}
        parent_tree = bpy.context.space_data.edit_tree
        locs = []

        for node in add_nodes:
            added_node = nodes.new(node.bl_idname)
            added_node.location = node.location
            new_nodes[node] = added_node

        for old_node in new_nodes:
            new_node = new_nodes[old_node]
            for attr in dir(old_node):
                if attr in attrs:
                    setattr(new_node, attr, getattr(old_node, attr))
            for socket in old_node.inputs:
                index = self._index_of(socket, old_node.inputs)
                for attr in dir(socket):
                    if attr in attrs:
                        try:
                            if attr != 'label':
                                setattr(new_node.inputs[index], attr, getattr(socket, attr))
                        except Exception:
                            utils.warn('Attribute {} not writable.'.format(attr))
                for link in socket.links:
                    try:
                        output_socket = link.from_socket
                        output_node = new_nodes[output_socket.node]
                        outdex = self._index_of(output_socket, output_socket.node.outputs)
                        node_tree.links.new(new_node.inputs[index], output_node.outputs[outdex])
                    except Exception:
                        bpy.data.node_groups.remove(node_tree)
                        msg = 'Some linked Nodes are not selected! Aborting...'
                        self.report({"ERROR"}, msg)
                        utils.error(msg)
                        return None
            locs.append(old_node.location)

        for old_node in new_nodes:
            parent_tree.nodes.remove(old_node)
        redir = parent_tree.nodes.new('NLActionExecuteNetwork')
        redir.inputs[0].value = True

        try:
            redir.inputs[1].value = bpy.context.object
        except Exception:
            msg = 'No Object was selected; Set Object in tree {} manually!'.format(parent_tree.name)
            self.report({"WARNING"}, msg)
            utils.warn(msg)
        redir.inputs[2].value = bpy.data.node_groups[group_name]
        redir.location = self.avg_location(locs)
        node_tree.use_fake_user = True
        utils.success(f'Created Node Tree {group_name}.')
        return node_tree