3
$\begingroup$

I wanted to append a Geometry nodes node group from a blend file named "test", and the node group name is also 'test' and I used the following code, but I am getting an error of the wrong path("test does not exist"). I am trying to install it as an addon, and a blend file with the same name and node group with the same name is present in the same folder where the __init__.py is What am I doing wrong?

import os
import bpy

from bpy.types import (
    Operator,
    Panel
)
from bpy.props import (
    StringProperty
)

bl_info = {
    "name": "Geonode tree",
    "description": "Imports Geometry Node tree",
    "author": "Rakesh",
    "version": (1, 0),
    "blender": (3, 0, 0),
    "location": "Node Editor > Properties Panel",
    "warning": "",  # used for warning icon and text in addons panel
    "tracker_url": "",
    "category": "Node"
}


# ------------------------------------------------------------------------
#    Append Operator
# ------------------------------------------------------------------------


class NG_OT_Append(Operator):
    bl_idname = "ng.append_node_tree"
    bl_label = "Append NG"
    bl_options = {'REGISTER'}

    blend : StringProperty(name="Library Blend File")
    ng_name : StringProperty(name="Node group Name")

    @classmethod
    def poll(cls, context):
        ob = context.active_object
        
        return ob is not None and ob.type == 'MESH'

    def execute(self, context):

        ob = context.active_object

        if ob is None:
            self.report({'ERROR'}, "No active object")
            return{"CANCELLED"}

        if ob.type != 'MESH':
            self.report({'ERROR'}, "Active object is not a mesh")
            return {"CANCELLED"}

        # os.path.abspath(__file__) returns path to the addon
        filepath = os.path.join(os.path.dirname(
            os.path.abspath(__file__)), self.blend)

        # -> Test whether the file exist
        if not os.path.isfile(filepath):
            self.report({'WARNING'}, "{} does not exist".format(self.blend))
            return {'CANCELLED'}

        
        with bpy.data.libraries.load(filepath, link=False) as (data_from, data_to):
            data_to.node_groups = [
                name for name in data_from.node_groups if name.startswith("test")]

        # -> Test whether the object exist
        if not data_to.node_groups:
            self.report({'WARNING'}, "{} not found in {}".format(
                self.ng_name, self.blend))
            return {'CANCELLED'}
        
        self.report({'INFO'}, "Test Node tree added")
        return {'FINISHED'}


# ------------------------------------------------------------------------
#    Panel in Object Mode
# ------------------------------------------------------------------------

class NG_PT_mainPanel(Panel):
    '''Adds nodetree to active object'''
    bl_label = "NG"
    bl_idname = "NG_PT_mainPanel"
    bl_space_type = 'NODE_EDITOR'
    bl_region_type = "UI"
    bl_category = "NG"

    def draw(self, context):
        layout = self.layout

        file_name = "test"
        ng_name = "test"
        
        row = layout.row()
        row = layout.row()
        row.scale_y = 1.5
        props = row.operator(NG_OT_Append.bl_idname,
                             text="Add Node tree", icon='IMPORT')
        props.blend = file_name
        props.ng_name = ng_name
        
        layout.separator()
        

# ------------------------------------------------------------------------
#    Registration
# ------------------------------------------------------------------------


classes = (
    NG_OT_Append,
    NG_PT_mainPanel,
)


def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)


def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)


if __name__ == "__main__":
    register()
$\endgroup$

1 Answer 1

4
+50
$\begingroup$

If I understand well the context you're describing,

you just need to replace:

filepath = os.path.join(os.path.dirname(
    os.path.abspath(__file__)), self.blend)

by:

filepath = os.path.join(os.path.dirname(
    os.path.abspath(__file__)), self.blend + ".blend")

The reason is the os.path.isfile won't recognize a file path if it does not includes the file extension.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .