我希望开发一个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()
插件可以正常启用
检查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 具有读取文件夹权限。我不知道是不是显示缩略图的那部分代码出了问题。