-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.py
201 lines (159 loc) · 4.37 KB
/
utils.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import bpy
from bpy_types import bpy_types
import mathutils
from enum import Enum, auto
import keyword
import re
from typing import NamedTuple
class ST(Enum):
"""
Settings Types
"""
# Primitives
ENUM = auto()
ENUM_SET = auto()
STRING = auto()
BOOL = auto()
INT = auto()
FLOAT = auto()
VEC1 = auto()
VEC2 = auto()
VEC3 = auto()
VEC4 = auto()
COLOR = auto()
# Special settings
COLOR_RAMP = auto()
CURVE_MAPPING = auto()
NODE_TREE = auto()
SIM_OUTPUT_ITEMS = auto()
REPEAT_OUTPUT_ITEMS = auto()
ENUM_DEFINITION = auto()
INDEX_SWITCH_ITEMS = auto()
BAKE_ITEMS = auto()
# Asset Library
MATERIAL = auto() # Handle with asset library
OBJECT = auto() # Handle with asset library
# Image
IMAGE = auto() #needs refactor
IMAGE_USER = auto() #needs refactor
MOVIE_CLIP = auto() #unimplmented
TEXTURE = auto() #unimplemented
TEXT = auto() #unimplemented
SCENE = auto() #unimplemented
PARTICLE_SYSTEM = auto() #unimplemented
FONT = auto() #unimplemented
MASK = auto() #unimplemented
CRYPTOMATTE_ENTRIES = auto() #unimplemented
IMAGE_FORMAT_SETTINGS = auto() #unimplemented
FILE_SLOTS = auto() #unimplemented
LAYER_SLOTS = auto() #unimplemented
class NTPNodeSetting(NamedTuple):
name: str
st: ST
min_version: tuple = (3, 0, 0)
max_version: tuple = (4, 2, 0) #first version where a setting is invalid
def clean_string(string: str, lower: bool = True) -> str:
"""
Cleans up a string for use as a variable or file name
Parameters:
string (str): The input string
Returns:
string (str): The input string ready to be used as a variable/file
"""
if lower:
string = string.lower()
string = re.sub(r"[^a-zA-Z0-9_]", '_', string)
if keyword.iskeyword(string):
string = "_" + string
elif not (string[0].isalpha() or string[0] == '_'):
string = "_" + string
return string
def enum_to_py_str(enum: str) -> str:
"""
Converts an enum into a string usuable in the add-on
Parameters:
enum (str): enum to be converted
Returns:
(str): converted string
"""
return f"\'{enum}\'"
def str_to_py_str(string: str) -> str:
"""
Converts a regular string into one usuable in the add-on
Parameters:
string (str): string to be converted
Returns:
(str): converted string
"""
return f"\"{string}\""
def vec1_to_py_str(vec1) -> str:
"""
Converts a 1D vector to a string usable by the add-on
Parameters:
vec1: a 1d vector
Returns:
(str): string representation of the vector
"""
return f"[{vec1[0]}]"
def vec2_to_py_str(vec2) -> str:
"""
Converts a 2D vector to a string usable by the add-on
Parameters:
vec2: a 2D vector
Returns:
(str): string representation of the vector
"""
return f"({vec2[0]}, {vec2[1]})"
def vec3_to_py_str(vec3) -> str:
"""
Converts a 3D vector to a string usable by the add-on
Parameters:
vec3: a 3d vector
Returns:
(str): string representation of the vector
"""
return f"({vec3[0]}, {vec3[1]}, {vec3[2]})"
def vec4_to_py_str(vec4) -> str:
"""
Converts a 4D vector to a string usable by the add-on
Parameters:
vec4: a 4d vector
Returns:
(str): string version
"""
return f"({vec4[0]}, {vec4[1]}, {vec4[2]}, {vec4[3]})"
def array_to_py_str(array: bpy_types.bpy_prop_array) -> str:
"""
Converts a bpy_prop_array into a string
Parameters:
array (bpy_prop_array): Blender Python array
Returns:
(str): string version
"""
string = "("
for i in range(0, array.__len__()):
if i > 0:
string += ", "
string += f"{array[i]}"
string += ")"
return string
def color_to_py_str(color: mathutils.Color) -> str:
"""
Converts a mathutils.Color into a string
Parameters:
color (mathutils.Color): a Blender color
Returns:
(str): string version
"""
return f"mathutils.Color(({color.r}, {color.g}, {color.b}))"
def img_to_py_str(img : bpy.types.Image) -> str:
"""
Converts a Blender image into its string
Paramters:
img (bpy.types.Image): a Blender image
Returns:
(str): string version
"""
name = img.name.split('.', 1)[0]
format = img.file_format.lower()
return f"{name}.{format}"