五路拖拉慢 2024-03-20 19:24 采纳率: 75%
浏览 36
已结题

blender插件开发:显示文件夹自定义图标

我希望开发一个blender插件,能在文件视图显示缩略图模式看到以自定义图标作为缩略图的文件夹,(我设置很多自定义图标),但是GPT给我的代码运行后无法达成我的需要。

GPT:

bl_info = {
    "name": "Folder Icon",
    "author": "五路拖拉慢",
    "version": (1, 0),
    "blender": (3, 6, 0),
    "category": "UI",
    "location": "File View > Thumbnail View",
    "description": "Allows custom icons to be displayed as thumbnails for folders in Blender's file view.",
    "warning": "Experimental feature, use at your own risk.",
}

import bpy
import os
import win32con
import win32api
import win32gui


def get_folder_icon(folder_path):
    try:
        # 获取文件夹的系统属性
        attributes = win32api.GetFileAttributes(folder_path)
        # 判断是否是文件夹
        if attributes & win32con.FILE_ATTRIBUTE_DIRECTORY:
            # 使用 SHGetFileInfo 获取文件夹图标信息
            SHGFI_ICON = 0x000000100  # 获取图标
            SHGFI_SMALLICON = 0x000000001  # 小图标
            SHGFI_LARGEICON = 0x000000000  # 大图标
            SHGFI_USEFILEATTRIBUTES = 0x000000010  # 使用文件属性
            info = win32gui.SHGetFileInfo(folder_path, 0, SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES)
            hicon, iicon, dwAttr, name, typeName = info
            icon = win32gui.ExtractIconEx(name, iicon)
            if icon:
                return icon[0]
    except Exception as e:
        print("Error while getting folder icon:", e)
    return None


class FOLDER_ICON_OT_update_thumbnail(bpy.types.Operator):
    """更新所选文件夹的缩略图"""
    bl_idname = "folder_icon.update_thumbnail"
    bl_label = "Update Folder Thumbnail"

    def execute(self, context):
        # 获取所选文件夹路径
        selected = context.space_data.params.directory
        if os.path.isdir(selected):
            # 获取文件夹的自定义图标
            icon_handle = get_folder_icon(selected)
            if icon_handle:
                # 设置自定义缩略图
                bpy.data.images.remove(context.space_data.thumbnail_icon, do_unlink=True)
                thumbnail_icon = bpy.data.images.new(name="FolderThumbnail", width=32, height=32)
                thumbnail_icon.pixels[:] = win32gui.GetIconInfo(icon_handle)[4]
                context.space_data.thumbnail_icon = thumbnail_icon
                win32gui.DestroyIcon(icon_handle)
                self.report({'INFO'}, f"Thumbnail updated for folder: {selected}")
            else:
                self.report({'ERROR'}, f"No custom thumbnail found for folder: {selected}")
        else:
            self.report({'ERROR'}, "Selected path is not a folder")
        return {'FINISHED'}


class FOLDER_ICON_PT_thumbnail_panel(bpy.types.Panel):
    """用于显示更新缩略图按钮的面板"""
    bl_label = "Folder Thumbnail"
    bl_idname = "FOLDER_ICON_PT_thumbnail_panel"
    bl_space_type = 'FILE_BROWSER'
    bl_region_type = 'UI'
    bl_category = 'Thumbnail'

    @classmethod
    def poll(cls, context):
        return context.space_data.params.is_property_editable

    def draw(self, context):
        layout = self.layout
        layout.operator("folder_icon.update_thumbnail", text="Update Thumbnail")


classes = [
    FOLDER_ICON_OT_update_thumbnail,
    FOLDER_ICON_PT_thumbnail_panel,
]


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


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


if __name__ == "__main__":
    register()

插件可以正常启用

img

检查Blender 具有读取文件夹权限:

import win32api

def has_permission(folder_path):
    try:
        # 获取文件夹的系统属性
        attributes = win32api.GetFileAttributes(folder_path)
        # 检查是否是文件夹并且可读
        if attributes & 0x10 and not attributes & 0x01:
            return True
        else:
            return False
    except Exception as e:
        # 如果出现异常,打印详细的错误信息
        print("无法读取文件夹内容:", e)
        return False

# 检查权限
folder_path = r"E:\BDK\3D模型"
if has_permission(folder_path):
    print("Blender 具有读取文件夹权限")
else:
    print("Blender 无法读取文件夹权限")

C:\Users\五路拖拉慢\AppData\Local\Programs\Python\Python310\python.exe C:\Users\五路拖拉慢\Desktop\python项目\Blender\permission.py
Blender 具有读取文件夹权限

进程已结束,退出代码0
这段GPT写的代码运行后告诉我Blender 具有读取文件夹权限。我不知道是不是显示缩略图的那部分代码出了问题。

  • 写回答

21条回答 默认 最新

  • Jackyin0720 2024-03-20 22:06
    关注
    获得0.45元问题酬金

    根据题意理解,解题思路如下:
    解析同步参考了大模型和相关实例方法:
    下面是修改后的代码,已增加批注,期望可以帮助到你

    bl_info = {
        "name": "Folder Icon",
        "author": "五路拖拉慢",
        "version": (1, 0),
        "blender": (3, 6, 0),
        "category": "UI",
        "location": "File View > Thumbnail View",
        "description": "Allows custom icons to be displayed as thumbnails for folders in Blender's file view.",
        "warning": "Experimental feature, use at your own risk.",
    }
    
    import bpy
    import os
    import win32con
    import win32api
    import win32gui
    
    
    def get_folder_icon(folder_path):
        try:
            # 获取文件夹的系统属性
            attributes = win32api.GetFileAttributes(folder_path)
            # 判断是否是文件夹
            if attributes & win32con.FILE_ATTRIBUTE_DIRECTORY:
                # 使用 SHGetFileInfo 获取文件夹图标信息
                SHGFI_ICON = 0x000000100  # 获取图标
                SHGFI_SMALLICON = 0x000000001  # 小图标
                SHGFI_LARGEICON = 0x000000000  # 大图标
                SHGFI_USEFILEATTRIBUTES = 0x000000010  # 使用文件属性
                info = win32gui.SHGetFileInfo(folder_path, 0, SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES)
                hicon, iicon, dwAttr, name, typeName = info
                icon = win32gui.ExtractIconEx(name, iicon)
                if icon:
                    return icon[0]
        except Exception as e:
            print("Error while getting folder icon:", e)
        return None
    
    
    # 定义一个操作类,用于更新文件夹的缩略图
    class FOLDER_ICON_OT_update_thumbnail(bpy.types.Operator):
        bl_idname = "folder_icon.update_thumbnail"
        bl_label = "Update Folder Thumbnail"
        #下面是新增部分
        #在FOLDER_ICON_OT_update_thumbnail操作类的execute方法中,你需要添加自定义图标加载逻辑。
        #例如,从文件名或文件内容中提取图标信息,然后使用bpy.data.images.new()创建一个新图像,并将其设置为文件夹的缩略图
        def execute(self, context):
        selected = context.space_data.params.directory
        if os.path.isdir(selected):
            icon_handle = get_folder_icon(selected)
            if icon_handle:
                # 删除旧的缩略图
                bpy.data.images.remove(context.space_data.thumbnail_icon, do_unlink=True)
                # 创建一个新的图像对象,并将图标的像素数据复制到该图像对象中
                thumbnail_icon = bpy.data.images.new(name="FolderThumbnail", width=32, height=32)
                thumbnail_icon.pixels[:] = win32gui.GetIconInfo(icon_handle)[4]
                # 将新的图像对象设置为所选文件夹的缩略图
                context.space_data.thumbnail_icon = thumbnail_icon
                win32gui.DestroyIcon(icon_handle)
                self.report({'INFO'}, f"Thumbnail updated for folder: {selected}")
            else:
                self.report({'ERROR'}, f"No custom thumbnail found for folder: {selected}")
        else:
            self.report({'ERROR'}, "Selected path is not a folder")
        return {'FINISHED'}
    
    # 定义一个面板类,用于显示文件夹缩略图的设置界面
    class FOLDER_ICON_PT_thumbnail_panel(bpy.types.Panel):
        bl_label = "Folder Thumbnail"
        bl_idname = "FOLDER_ICON_PT_thumbnail_panel"
        bl_space_type = 'FILE_BROWSER'
        bl_region_type = 'UI'
        bl_category = 'Thumbnail'
        
        @classmethod
        def poll(cls, context):
            return context.space_data.params.is_property_editable
        
        def draw(self, context):
            layout = self.layout
            layout.operator("folder_icon.update_thumbnail", text="Update Thumbnail")
    
    # 将操作类和面板类添加到列表中
    classes = [
        FOLDER_ICON_OT_update_thumbnail,
        FOLDER_ICON_PT_thumbnail_panel,
    ]
    
    # 注册插件
    def register():
        for cls in classes:
            bpy.utils.register_class(cls)
    
    # 注销插件
    def unregister():
        for cls in reversed(classes):
            bpy.utils.unregister_class(cls)
    
    # 如果作为主程序运行,则注册插件
    if __name__ == "__main__":
        register()
    

    (如有疑问,随时追问;如有帮助,恭请采纳;有不对之处,也请包涵)

    评论

报告相同问题?

问题事件

  • 系统已结题 3月28日
  • 创建了问题 3月20日

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳