-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
155 lines (119 loc) · 3.8 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
import bpy
import time
timer = 0
def log_info(msg, self = None):
"""Log an info message to console."""
print(msg)
if self:
self.report({'INFO'}, msg)
def log_warn(msg, self = None):
"""Log a warning message to console."""
print("Warning: " + msg)
if self:
self.report({'WARNING'}, msg)
def log_error(msg, self = None):
"""Log an error message to console and raise an exception."""
print("Error: " + msg)
if self:
self.report({'ERROR'}, msg)
def start_timer():
global timer
timer = time.perf_counter()
def log_timer(msg, unit = "s"):
global timer
duration = time.perf_counter() - timer
if unit == "ms":
duration *= 1000
elif unit == "us":
duration *= 1000000
elif unit == "ns":
duration *= 1000000000
elif unit == "m":
duration /= 60
elif unit == "h":
duration /= 3600
print(msg + ": " + str(duration) + " " + unit)
# remove any .001 from the material name
def strip_name(name):
if name[-3:].isdigit() and name[-4] == ".":
name = name[:-4]
return name
def get_active_object():
return bpy.context.view_layer.objects.active
def set_active_object(obj):
try:
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
return (bpy.context.active_object == obj)
except:
return False
def try_select_object(obj):
try:
obj.select_set(True)
return True
except:
return False
def try_select_objects(objects, clear_selection = False):
if clear_selection:
clear_selected_objects()
result = True
for obj in objects:
if not try_select_object(obj):
result = False
return result
def clear_selected_objects():
try:
bpy.ops.object.select_all(action='DESELECT')
return True
except:
return False
def set_mode(mode):
if bpy.context.object == None:
if mode != "OBJECT":
log_error("No context object, unable to set any mode but OBJECT!")
return False
return True
else:
bpy.ops.object.mode_set(mode=mode)
if bpy.context.object.mode != mode:
log_error("Unable to set " + mode + " on object: " + bpy.context.object.name)
return False
return True
def find_cc3_rig():
cc3_import_props = bpy.context.scene.CC3ImportProps
for p in cc3_import_props.import_objects:
obj = p.object
if obj.type == "ARMATURE":
if "Base_Spine01" in obj.pose.bones or "CC_Base_Spine01" in obj.pose.bones:
return obj
return None
def get_cc3_import_meshes():
cc3_import_props = bpy.context.scene.CC3ImportProps
meshes = []
for p in cc3_import_props.import_objects:
obj = p.object
if obj.type == "MESH":
meshes.append(obj)
return meshes
def rest_pose(rig):
if rig is not None and rig.type == "ARMATURE":
rig.data.pose_position = 'REST'
return True
return False
def remove_collection(coll, item):
for i in range(0, len(coll)):
if coll[i] == item:
coll.remove(i)
return
def get_tex_image_size(node):
if node is not None:
return node.image.size[0] if node.image.size[0] > node.image.size[1] else node.image.size[1]
return 64
def check_blender_version(version: str):
major, minor, subversion = version.split(".")
blender_version = bpy.app.version
v_test = int(major) * 1000000 + int(minor) * 1000 + int(subversion)
v_this = blender_version[0] * 1000000 + blender_version[1] * 1000 + blender_version[2]
if v_this >= v_test:
return True
return False