forked from BrendanParmer/NodeToPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
153 lines (115 loc) · 3.26 KB
/
Copy pathutils.py
File metadata and controls
153 lines (115 loc) · 3.26 KB
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
import bpy
import mathutils
from bpy.types import bpy_prop_array
import keyword
import re
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
"""
repr_str = repr(string)
if repr_str.startswith("'") and repr_str.endswith("'"):
repr_str = "\"" + repr_str[1:-1].replace('\"', '\\"') + "\""
return repr_str
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 version_to_manifest_str(version) -> str:
return f"\"{version[0]}.{version[1]}.{version[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_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}"