-
Notifications
You must be signed in to change notification settings - Fork 78
/
Edge_To_Curve.py
166 lines (140 loc) · 5.99 KB
/
Edge_To_Curve.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
# ##### 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 #####
bl_info = {
"name": "Edges To Curve",
"category": "Mesh",
"location": "View3D > Edge menu or Context menu",
"description": "Converts selected edges into curve with extrusion",
"author": "Andreas Strømberg, Chris Kohl",
"wiki_url": "https://github.com/Stromberg90/Scripts/tree/master/Blender",
"tracker_url": "https://github.com/Stromberg90/Scripts/issues",
"blender": (2, 80, 0),
"version": (1, 0, 4),
}
import bpy
class MeshMode:
VERTEX = (True, False, False)
EDGE = (False, True, False)
FACE = (False, False, True)
class ObjectMode:
OBJECT = "OBJECT"
EDIT = "EDIT"
POSE = "POSE"
SCULPT = "SCULPT"
VERTEX_PAINT = "VERTEX_PAINT"
WEIGHT_PAINT = "WEIGHT_PAINT"
TEXTURE_PAINT = "TEXTURE_PAINT"
PARTICLE_EDIT = "PARTICLE_EDIT"
GPENCIL_EDIT = "GPENCIL_EDIT"
class EventType:
MOUSEMOVE = "MOUSEMOVE"
WHEELUPMOUSE = "WHEELUPMOUSE"
WHEELDOWNMOUSE = "WHEELDOWNMOUSE"
LEFTMOUSE = "LEFTMOUSE"
RIGHTMOUSE = "RIGHTMOUSE"
ESC = "ESC"
class ModalEdgeToCurve(bpy.types.Operator):
bl_idname = "object.edge_to_curve"
bl_label = "Edges To Curve"
bl_description = "Takes selected mesh edges and converts them into a curve."
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
return (
context.active_object.mode == ObjectMode.EDIT
and context.active_object.type == "MESH"
or context.active_object.type == "CURVE"
)
def execute(self, context):
context.object.data.bevel_depth = self.value / 100.0
context.object.data.bevel_resolution = self.resolution
return {"FINISHED"}
def modal(self, context, event):
if event.type == EventType.MOUSEMOVE: # Apply
self.value = max(0, (event.mouse_x - self.start_value))
elif event.type == EventType.WHEELUPMOUSE:
self.resolution += 1
elif event.type == EventType.WHEELDOWNMOUSE and self.resolution > 1:
self.resolution -= 1
elif event.type == EventType.LEFTMOUSE: # Confirm
if context.active_object.type != "CURVE":
context.view_layer.objects.active = self.original_object
bpy.ops.object.select_all(action="DESELECT")
self.original_object.select_set(True)
bpy.ops.object.mode_set(mode=ObjectMode.EDIT)
context.tool_settings.mesh_select_mode = MeshMode.EDGE
return {"FINISHED"}
elif event.type in {EventType.RIGHTMOUSE, EventType.ESC}: # Cancel
if context.active_object.type == "CURVE":
context.object.data.bevel_depth = 0
else:
bpy.ops.object.delete()
context.view_layer.objects.active = self.original_object
bpy.ops.object.select_all(action="DESELECT")
self.original_object.select_set(True)
bpy.ops.object.mode_set(mode=ObjectMode.EDIT)
context.tool_settings.mesh_select_mode = MeshMode.EDGE
return {"CANCELLED"}
self.execute(context)
return {"RUNNING_MODAL"}
def invoke(self, context, event):
self.value = 0.0
self.start_value = event.mouse_x
self.resolution = 2
self.original_object = bpy.context.active_object
if context.active_object.type == "CURVE":
context.object.data.fill_mode = "FULL"
context.object.data.bevel_resolution = self.resolution
self.execute(context)
context.window_manager.modal_handler_add(self)
return {"RUNNING_MODAL"}
else:
if context.tool_settings.mesh_select_mode[0]:
context.tool_settings.mesh_select_mode = MeshMode.EDGE
if not context.object.data.total_vert_sel > 1:
return {"CANCELLED"}
context.tool_settings.mesh_select_mode = MeshMode.VERTEX
bpy.ops.mesh.duplicate()
bpy.ops.mesh.separate()
bpy.ops.object.mode_set(mode=ObjectMode.OBJECT)
curve_object = context.selected_objects[-1]
context.view_layer.objects.active = curve_object
bpy.ops.object.select_all(action="DESELECT")
curve_object.select_set(True)
bpy.ops.object.convert(target="CURVE")
context.object.data.fill_mode = "FULL"
bpy.ops.object.shade_smooth()
context.object.data.bevel_resolution = self.resolution
self.execute(context)
context.window_manager.modal_handler_add(self)
return {"RUNNING_MODAL"}
def EdgeToCurveMenuItem(self, context):
layout = self.layout
layout.separator()
layout.operator_context = "INVOKE_DEFAULT"
layout.operator("object.edge_to_curve", text="Edges to Curve")
def register():
bpy.utils.register_class(ModalEdgeToCurve)
bpy.types.VIEW3D_MT_edit_mesh_edges.append(EdgeToCurveMenuItem)
bpy.types.VIEW3D_MT_edit_mesh_context_menu.append(EdgeToCurveMenuItem)
def unregister():
bpy.utils.unregister_class(ModalEdgeToCurve)
bpy.types.VIEW3D_MT_edit_mesh_edges.remove(EdgeToCurveMenuItem)
bpy.types.VIEW3D_MT_edit_mesh_context_menu.remove(EdgeToCurveMenuItem)
if __name__ == "__main__":
register()