forked from franMarz/TexTools-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_color_io_import.py
55 lines (40 loc) · 1.58 KB
/
op_color_io_import.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
import bpy
import string
from . import utilities_color
class op(bpy.types.Operator):
bl_idname = "uv.textools_color_io_import"
bl_label = "Import"
bl_description = "Import hex colors from the clipboard as current color palette"
@classmethod
def poll(cls, context):
if bpy.context.area.ui_type != 'UV':
return False
return True
def execute(self, context):
import_colors(self, context)
return {'FINISHED'}
def import_colors(self, context):
# Clipboard hex strings
hex_strings = bpy.context.window_manager.clipboard.split(',')
for i in range(len(hex_strings)):
hex_strings[i] = hex_strings[i].strip().strip('#')
if len(hex_strings[i]) != 6 or not all(c in string.hexdigits for c in hex_strings[i]):
# Incorrect format
self.report({'ERROR_INVALID_INPUT'}, "Incorrect hex format '{}' use a #RRGGBB pattern".format(hex_strings[i]))
return
else:
name = "color_ID_color_{}".format(i)
if hasattr(bpy.context.scene.texToolsSettings, name):
# Color Index exists
color = utilities_color.hex_to_color( hex_strings[i] )
setattr(bpy.context.scene.texToolsSettings, name, color)
else:
# More colors imported than supported
self.report({'ERROR_INVALID_INPUT'}, "Only {}x colors have been imported instead of {}x".format(
i,len(hex_strings)
))
return
# Set number of colors
bpy.context.scene.texToolsSettings.color_ID_count = len(hex_strings)
bpy.ops.ui.textools_popup('INVOKE_DEFAULT', message="{}x colors imported from clipboard".format( len(hex_strings) ))
bpy.utils.register_class(op)