1
$\begingroup$

I'm learning to program in Python for Blender and I'm trying to create my first addon, it works well, I made it inspired by EasyHDRI, however when I import an image into blender using python/my addon it is sent with copy as shown in the attached image. When I choose an Environment Texture manually this does not happen as it is also shown in the image. The problem that this causes is that I cannot delete images that are not in use. Would anyone have a solution on how to solve this problem, just solve this problem to finish my first addon :D

Code to open image:

#OPEN IMAGES
class OBJECT_OT_custompath(bpy.types.Operator):
bl_idname = "view3d.custom_path"
bl_label = "Select HDR file"
bl_description = "Open Dome Light File"

filename_ext = ".hdr;.exr;.png"
filter_glob = StringProperty(default="*.hdr;*.exr;*.png", options={'HIDDEN'})    

filepath = bpy.props.StringProperty(subtype="FILE_PATH")
files = CollectionProperty(
    name="File Path",
    type=bpy.types.OperatorFileListElement,
    )
def execute(self, context):

    #change image
    bpy.ops.image.open(filepath=self.properties.filepath, relative_path=True)
    print(self.properties.filepath)
    limit = 2
    index = 1
    for file in self.files:
        bpy.data.worlds["World"].node_tree.nodes["Environment"].image = bpy.data.images[file.name]

        index += 1
        if index == limit:
            break

    return {'FINISHED'}

def draw(self, context):
    self.layout.operator('file.select_all_toggle') 
def invoke(self, context, event):
    context.window_manager.fileselect_add(self)

    return {'RUNNING_MODAL'}

Code to clear unused images (it works, but only delete images that are not linked or duplicated, in short, only delete images that I send manually):

# REMOVE UNUSED IMAGES
def remove_images():
images = bpy.data.images
for img in bpy.data.images:
    if not img.users or (img.users == 1 and img.use_fake_user):
        bpy.data.images.remove(img)

class WORLD_OT_remove_unused_images(Operator):
bl_idname = "world.remove_images"
bl_label = "Remove unused images"
bl_description = "Remove unused user images"

def execute(self, context):
    remove_images()
    return {'FINISHED'}

Thank you very much

Problem when open image with addon

$\endgroup$

1 Answer 1

1
$\begingroup$

PROBLEM FIX

I just changed an line, I'm noob haha

bpy.ops.image.open(filepath=self.properties.filepath, relative_path=True)

TO:

bpy.data.images.load(filepath=self.properties.filepath, check_existing=True)
$\endgroup$

You must log in to answer this question.

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