/
io_import_BrushSet.py
158 lines (129 loc) · 5.13 KB
/
io_import_BrushSet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8-80 compliant>
#---------------------------------------------#
import bpy
import os
from bpy.props import (
CollectionProperty,
StringProperty,
BoolProperty,
)
from bpy.types import (
Operator,
OperatorFileListElement,
)
# Addon description
bl_info = {
"name": "Import BrushSet",
"author": "Daniel Grauer (kromar), CansecoGPC, Gorgious56",
"version": (1, 2, 3),
"blender": (2, 80, 0),
"location": "File > Import > BrushSet",
"description": "Imports selected image files or all the images in the folder if no file is selected",
"warning": '', # used for warning icon and text in addons panel
"doc_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/Scripts/Import-Export/BrushSet",
"tracker_url": "https://developer.blender.org/maniphest/task/edit/form/2/",
"category": "Import-Export",
}
#---------------------------------------------#
# Extension filter (alternative use mimetypes)
# TODO: rewrite so it tries to load image and if it fails we know its not a format blender can load
ext_list = ['.bmp',
'.png',
'.jpg',
'.jp2',
'.rgb',
'.dds',
'.hdr',
'.exr',
'.dpx',
'.cin',
'.tga',
'.tif'];
#---------------------------------------------#
def LoadBrushSet(directory, files, use_fake_user, verbose, overwrite):
imported_images = 0
if files[0].name == "":
# Selected a directory. Parse all its files
files = os.listdir(directory)
else:
# Selected at least one individual file. Get their name
files = [f.name for f in files]
print("\nBegin Importing Brush Set :")
for file in files:
path = directory + file
# Filter image files :
if any(file.lower().endswith(ext) for ext in ext_list):
# Use existing image and texture if overwrite is selected :
if overwrite and file in bpy.data.textures and file in bpy.data.images:
texture = bpy.data.textures.get(file)
image = bpy.data.images.get(file)
else:
texture = bpy.data.textures.new(file, 'IMAGE')
image = bpy.data.images.load(path)
texture.use_fake_user = use_fake_user
image.use_fake_user = use_fake_user
bpy.data.textures[texture.name].image = image
if verbose:
print(f" Imported {file}")
imported_images += 1
print(f"Import Ended : {imported_images} Textures were imported\n")
#---------------------------------------------#
class BrushSetImporter(Operator):
'''Load Brush Set'''
bl_idname = "import_image.brushset"
bl_label = "Import BrushSet"
use_fake_user: BoolProperty(name="Fake User", description="Import Textures and Images with Fake User")
verbose: BoolProperty(name="Verbose Output", description="Verbose output in the System Console")
overwrite: BoolProperty(name="Overwrite", description="Overwrite Image & Texture if already loaded in file")
files: CollectionProperty(
type=OperatorFileListElement,
options={'HIDDEN', 'SKIP_SAVE'},
)
directory: StringProperty(
name="Selected Directory",
)
def execute(self, context):
LoadBrushSet(self.directory, self.files, self.use_fake_user, self.verbose, self.overwrite)
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
wm.fileselect_add(self)
return {'RUNNING_MODAL'}
#---------------------------------------------#
def menu_func(self, context):
# Clear the default name for import
import_name = ""
self.layout.operator(BrushSetImporter.bl_idname, text = "Brush Set").directory = ""
#---------------------------------------------#
classes = (
BrushSetImporter,
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.types.TOPBAR_MT_file_import.append(menu_func)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
bpy.types.TOPBAR_MT_file_import.remove(menu_func)
if __name__ == "__main__":
register()