-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbaking_namepair_creator_2_8.py
244 lines (177 loc) · 7.17 KB
/
baking_namepair_creator_2_8.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# ##### 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 #####
# Select the lowpoly and highpoly objects, and press the Create Baking Namepair button in the Relations tab. The suffixes _high and _low are added automatically based on the vertex count. The name of the low poly object is used as the new name's base.
bl_info = {
'name': "Baking Namepair Creator",
'author': "Paweł Łyczkowski",
'version': (1, 0, 3),
'blender': (2, 80, 0),
'location': "View3D > Toolbar > Relations Tab",
'description': "Set's up a baking namepair.",
'warning': "",
'wiki_url': "",
'tracker_url': "",
'category': 'Object'
}
import bpy, bmesh
from bpy.types import Operator
import string
import random
class BakingNamepair(bpy.types.Operator):
'''Tooltip'''
bl_description = "BakingNamepair"
bl_idname = "object.baking_namepair"
bl_label = "BakingNamepair"
bl_options = { 'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
selected = bpy.context.selected_objects
depsgraph = bpy.context.evaluated_depsgraph_get()
high_poly = None
low_poly = None
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
if len(selected) == 2:
tempmesh1 = bmesh.new()
tempmesh1.from_object(selected[0], depsgraph, deform=True, cage=False, face_normals=True)
vertex_count1 = len(tempmesh1.verts)
tempmesh2 = bmesh.new()
tempmesh2.from_object(selected[1], depsgraph, deform=True, cage=False, face_normals=True)
vertex_count2 = len(tempmesh2.verts)
if vertex_count1 != vertex_count2:
#The meshes are not the same, set up which is which.
if vertex_count1 > vertex_count2:
high_poly = selected[0]
low_poly= selected[1]
else:
high_poly = selected[1]
low_poly= selected[0]
#Set up a random name, if option toggled.
if bpy.context.scene.r_generate_random_name:
low_poly.name = id_generator()
if bpy.context.scene.r_also_rename_datablock:
low_poly.data.name = low_poly.name
else:
#Check if lowpoly doesn't already end in "_low".
low_poly_name_len = len(low_poly.name)
if low_poly.name[low_poly_name_len - 1] == "w" and low_poly.name[low_poly_name_len - 2] == "o" and low_poly.name[low_poly_name_len - 3] == "l" and low_poly.name[low_poly_name_len - 4] == "_":
#It does, remove the "_low".
low_poly.name = low_poly.name[:low_poly_name_len - 4]
#Check if the names are not yet occupied.
high_poly_check = bpy.data.objects.get(low_poly.name + "_high")
low_poly_check = bpy.data.objects.get(low_poly.name + "_low")
if high_poly_check == None and low_poly_check == None:
#They are not, add suffixes.
high_poly.name = low_poly.name + "_high"
if bpy.context.scene.r_also_rename_datablock:
high_poly.data.name = high_poly.name
low_poly.name = low_poly.name + "_low"
if bpy.context.scene.r_also_rename_datablock:
low_poly.data.name = low_poly.name
#Hide, if option toggled.
if bpy.context.scene.r_hide_after_renaming:
selected[0].hide_viewport = True
selected[1].hide_viewport = True
elif low_poly_check == None and high_poly.name == low_poly.name + "_high":
#Only the highpoly is, so add suffix only to the lowpoly.
low_poly.name = low_poly.name + "_low"
if bpy.context.scene.r_also_rename_datablock:
low_poly.data.name = low_poly.name
#Hide, if option toggled.
if bpy.context.scene.r_hide_after_renaming:
selected[0].hide_viewport = True
selected[1].hide_viewport = True
else:
#Low poly name occupied.
self.report({'ERROR'}, "One of the names already occupied. Please rename the low poly object.")
else:
self.report({'ERROR'}, "Same vertex count.")
tempmesh1.free
tempmesh2.free
else:
self.report({'ERROR'}, "Invalid number of objects selected.")
return {'FINISHED'}
class OpToggleRenamed(bpy.types.Operator):
bl_idname = "object.toggle_viewport_renamed_objects"
bl_label = "Hide(/Unhide) all renamed High/Low poly objects"
bl_description = "Hide/Unhide all renamed High/Low poly objects in selected collections"
bl_options = {'INTERNAL', 'UNDO'}
group : bpy.props.EnumProperty(items = {
('HIGH', "High polys", "High"),
('LOW', "Low polys", "Low"),
('ALL', "All renamed", "All"),
})
action : bpy.props.EnumProperty(items = {
('SHOW', "Show", "Show"),
('HIDE', "Hide", "Hide")
})
def execute(self, context):
if self.group == 'HIGH':
suffixes = "_high"
elif self.group == 'LOW':
suffixes = "_low"
else:
suffixes = ("_low", "_high")
for obj in bpy.data.objects:
if not obj.name.endswith(suffixes):
continue
if self.action == 'SHOW':
obj.hide_viewport = False
else:
obj.hide_viewport = True
return {'FINISHED'}
class addButtonsInObjectMode(bpy.types.Panel):
bl_idname = "RELATIONS_PT_baking_namepair_objectmode"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_label = "Baking Namepair"
bl_context = "objectmode"
def draw(self, context):
layout = self.layout
# col = layout.column(align=True)
layout.operator("object.baking_namepair", text="Create Baking Namepair")
layout.prop(context.scene, "r_generate_random_name", text="Generate Random Name")
layout.prop(context.scene, "r_hide_after_renaming", text="Hide After Renaming")
layout.prop(context.scene, "r_also_rename_datablock", text="Also Rename Datablock")
self._draw_toggle_viewport(context, 'HIGH', layout)
self._draw_toggle_viewport(context, 'LOW', layout)
self._draw_toggle_viewport(context, 'ALL', layout)
def _draw_toggle_viewport(self, context, group, layout):
row = layout.row()
row.label(text=group.lower())
op = row.operator("object.toggle_viewport_renamed_objects", text="SHOW")
op.group = group
op.action = 'SHOW'
op = row.operator("object.toggle_viewport_renamed_objects", text="HIDE")
op.group = group
op.action = 'HIDE'
def register():
bpy.types.Scene.r_generate_random_name = bpy.props.BoolProperty(default= False)
bpy.types.Scene.r_hide_after_renaming = bpy.props.BoolProperty(default= False)
bpy.types.Scene.r_also_rename_datablock = bpy.props.BoolProperty(default= True)
bpy.utils.register_class(BakingNamepair)
bpy.utils.register_class(OpToggleRenamed)
bpy.utils.register_class(addButtonsInObjectMode)
def unregister():
bpy.utils.unregister_class(BakingNamepair)
bpy.utils.unregister_class(OpToggleRenamed)
bpy.utils.unregister_class(addButtonsInObjectMode)
if __name__ == "__main__":
register()