-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertrex_matcher.py
195 lines (166 loc) · 5.38 KB
/
vertrex_matcher.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
bl_info = {
"name": "Vertex Matcher",
"description": "Matches Vertex position on one or more axis",
"author": "Sylwester Moniuszko-Szymanski",
"version": (0, 0, 3),
"blender": (2, 80, 0),
"location": "3D View > Tools",
"warning": "", # used for warning icon and text in addons panel
"wiki_url": "",
"tracker_url": "",
"category": "Modeling"
}
import bpy
import bmesh
from bpy.props import (BoolProperty,
PointerProperty
)
from bpy.types import (Panel,
PropertyGroup,
Operator
)
# ------------------------------------------------------------------------
# Scene Properties
# ------------------------------------------------------------------------
class VertexMatcherProperties(PropertyGroup):
x_axis: BoolProperty(
name="X",
description="Match Vertex on X axis",
default = False
)
y_axis: BoolProperty(
name="Y",
description="Match Vertex on Y axis",
default = False
)
z_axis: BoolProperty(
name="Z",
description="Match Vertex on Z axis",
default = False
)
def matchVertex(axis):
ob = bpy.context.object
me = ob.data
bm = bmesh.from_edit_mesh(me)
print(axis)
def get_axis(elem):
switcher = {
'x': elem.co.x,
'y': elem.co.y,
'z': elem.co.z
}
return switcher.get(axis,"Nothing")
def set_axis(first,elem):
if(axis=='x'):
elem.co.x = first.co.x
if(axis=='y'):
elem.co.y = first.co.y
if(axis=='z'):
elem.co.z = first.co.z
last_vertex = None
for elem in reversed(bm.select_history):
if isinstance(elem, bmesh.types.BMVert):
last_vertex = elem
break
for elem in bm.select_history:
set_axis(last_vertex, elem)
#different selection
selected_verts = list(filter(lambda v: v.select, bm.verts))
if(last_vertex == None):
last_vertex = selected_verts[-1]
for elem in reversed(selected_verts):
set_axis(last_vertex, elem)
#Used to insntantly see the result in editor
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.mode_set(mode = 'EDIT')
def matchDiagonaly():
ob = bpy.context.object
me = ob.data
bm = bmesh.from_edit_mesh(me)
selected = bm.select_history
first = selected[1]
second = selected[0]
print(first.co.x)
print(second.co.x)
print(first.co.y)
print(second.co.y)
print(first.co.z)
print(second.co.z)
iter = 0
for elem in selected :
if(iter<2):
iter +=1
continue
elem.co.x = second.co.x+(first.co.x-second.co.x)/iter
elem.co.y = second.co.y+(first.co.y-second.co.y)/iter
elem.co.z = second.co.z+(first.co.z-second.co.z)/iter
iter += 1
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.mode_set(mode = 'EDIT')
# ------------------------------------------------------------------------
# Operators
# ------------------------------------------------------------------------
class WM_OT_VertexMatcher(Operator):
bl_label = "Match Vertex Position"
bl_idname = "wm.vertex_matcher"
def execute(self, context):
scene = bpy.context.scene
v_matcher = scene.vertex_matcher
if(v_matcher.x_axis==True):
matchVertex('x')
if(v_matcher.y_axis==True):
matchVertex('y')
if(v_matcher.z_axis==True):
matchVertex('z')
return {'FINISHED'}
class WM_OT_MathDiagonaly(Operator):
bl_label = "Match Diagonaly"
bl_idname = "wm.diagonal_match"
def execute(self, context):
matchDiagonaly()
return {'FINISHED'}
# ------------------------------------------------------------------------
# Panel in Edit Mode
# ------------------------------------------------------------------------
class OBJECT_PT_VertexPanel(Panel):
bl_label = "Vertex Matcher"
bl_idname = "OBJECT_PT_vertex_matcher"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Vertex Tools"
bl_context = "mesh_edit"
@classmethod
def poll(self,context):
return context.object is not None
def draw(self, context):
layout = self.layout
scene = bpy.context.scene
mytool = scene.vertex_matcher
layout.prop(mytool, "x_axis")
layout.separator()
layout.prop(mytool, "y_axis")
layout.separator()
layout.prop(mytool, "z_axis")
layout.operator("wm.vertex_matcher")
layout.separator()
layout.operator("wm.diagonal_match")
layout.separator()
# ------------------------------------------------------------------------
# Registration
# ------------------------------------------------------------------------
classes = (
VertexMatcherProperties,
WM_OT_VertexMatcher,
WM_OT_MathDiagonaly,
OBJECT_PT_VertexPanel
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.types.Scene.vertex_matcher = PointerProperty(type=VertexMatcherProperties)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
del bpy.types.Scene.vertex_matcher