Update scripts to account for removal of the context override to bpy.ops
[blender-addons.git] / io_import_BrushSet.py
blob514d0faa1ff11579d38b0fbc877187cda4850c9d
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 #---------------------------------------------#
4 # todo
5 #---------------------------------------------#
6 '''
7 - add file selection for single and multiple files
8 - option to enable/disable fake users
9 '''
11 #---------------------------------------------#
12 import bpy
13 import os
14 from bpy.props import *
16 # addon description
17 bl_info = {
18 "name": "Import BrushSet",
19 "author": "Daniel Grauer (kromar), CansecoGPC",
20 "version": (1, 2, 2),
21 "blender": (2, 80, 0),
22 "location": "File > Import > BrushSet",
23 "description": "Imports all image files from a folder.",
24 "warning": '', # used for warning icon and text in addons panel
25 "doc_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/Scripts/Import-Export/BrushSet",
26 "tracker_url": "https://developer.blender.org/maniphest/task/edit/form/2/",
27 "category": "Import-Export",
30 #---------------------------------------------#
32 # extension filter (alternative use mimetypes)
33 # TODO: rewrite so it tries to load image and if it fails we know its not a format blender can load
34 ext_list = ['.bmp',
35 '.png',
36 '.jpg',
37 '.jp2',
38 '.rgb',
39 '.dds',
40 '.hdr',
41 '.exr',
42 '.dpx',
43 '.cin',
44 '.tga',
45 '.tif'];
47 #---------------------------------------------#
49 fakeUser = False
51 def LoadBrushSet(filepath, filename):
52 for file in os.listdir(filepath):
53 path = (filepath + file)
55 # get folder name
56 (f1, f2) = os.path.split(filepath)
57 (f3, foldername) = os.path.split(f1)
59 # filter files by extensions (filter images)
60 if any(file.lower().endswith(ext) for ext in ext_list):
61 print("file: ", file)
62 # create new texture
63 texture = bpy.data.textures.new(file, 'IMAGE')
64 texture.use_fake_user = fakeUser
65 print("texture: ", texture)
67 # now we need to load the image into data
68 image = bpy.data.images.load(path)
69 image.use_fake_user = fakeUser
70 # image.source = 'FILE' #default is FILE so can remove this
71 # image.filepath = path
72 print("image: ", image, " ", path)
73 print("texturename: ", texture.name)
75 # and assign the image to the texture
76 bpy.data.textures[texture.name].image = image
79 print("Brush Set imported!")
81 #---------------------------------------------#
83 class BrushSetImporter(bpy.types.Operator):
84 '''Load Brush Set'''
85 bl_idname = "import_image.brushset"
86 bl_label = "Import BrushSet"
88 filename: StringProperty(name = "File Name",
89 description = "filepath",
90 default = "",
91 maxlen = 1024,
92 options = {'ANIMATABLE'},
93 subtype = 'NONE')
95 filepath: StringProperty(name = "File Name",
96 description = "filepath",
97 default = "",
98 maxlen = 1024,
99 options = {'ANIMATABLE'},
100 subtype = 'NONE')
102 def execute(self, context):
103 LoadBrushSet(self.properties.filepath, self.properties.filename)
104 return {'FINISHED'}
106 def invoke(self, context, event):
107 wm = context.window_manager
108 wm.fileselect_add(self)
109 return {'RUNNING_MODAL'}
111 #---------------------------------------------#
113 def menu_func(self, context):
114 # clear the default name for import
115 import_name = ""
117 self.layout.operator(BrushSetImporter.bl_idname, text = "Brush Set").filename = import_name
120 #---------------------------------------------#
121 # GUI
122 #---------------------------------------------#
125 class Brush_set_UI(bpy.types.Panel):
127 bl_space_type ='USER_PREFERENCES'
128 bl_label = 'Brush_Path'
129 bl_region_type = 'WINDOW'
130 bl_options = {'HIDE_HEADER'}
132 def draw(self, context):
134 scn = context.scene
135 layout = self.layout
136 column = layout.column(align=True)
137 column.label(text='Brush Directory:')
138 column.prop(scn,'filepath')
141 #---------------------------------------------#
143 classes = (
144 BrushSetImporter,
148 def register():
149 from bpy.utils import register_class
150 for cls in classes:
151 register_class(cls)
152 bpy.types.TOPBAR_MT_file_import.append(menu_func)
155 def unregister():
156 from bpy.utils import unregister_class
157 for cls in reversed(classes):
158 unregister_class(cls)
159 bpy.types.TOPBAR_MT_file_import.remove(menu_func)
162 if __name__ == "__main__":
163 register()