python help add-on

I don’t know what is wrong with this code. Cane somebody please help me?:spin:


import bpy
import os


class LegoCreator(bpy.types.Operator):
    bl_idname = "object.legocreator"
    bl_label = "Lego Creator"
    
             
    def execute():
        dir_path = os.path.join("D:\\","blender","progetti","Lego_creator_addon","Pieces")
        list_file = os.listdir(dir_path)
        Brick_list = [brick for brick in list_file if brick[-3:] == "obj"]
        for brick in Brick_list:
            p_path = os.path.join(dir_path, brick)
            bpy.ops.import_scene.obj(filepath = p_path)
            bpy.ops.transform.resize(value=(10,10,10))
            value = Brick_list.index(brick) * 0.8
            bpy.ops.transform.translate(value=(-value,0,0))
        return {"Added main pieces"}
 
def register():
    bpy.utils.register_class(LegoCreator)
    
if __name__ == "__main__":  
    register()  
    

You could hardcode dir_path by hand. Right now D:\ likely confuses os


# Save this .blend file before execute script.

import bpy, os

blendfilepath = bpy.data.filepath

# Swap commented line to see different results
directory = os.path.dirname(blendfilepath)
#directory = os.path.dirname(blendfilepath) + '/garbage'

try:
    list_file = os.listdir(directory)
except IOError:
    print("Could not make listing from ", directory)
else:    
    Brick_list = [brick for brick in list_file if brick[-5:] == "blend"]
    print(Brick_list)
    for brick in Brick_list:
        p_path = os.path.join(directory, brick)
        # ...
    
    print('
DONE')


Hope helps.