-
Notifications
You must be signed in to change notification settings - Fork 2
/
blendxus.py
119 lines (93 loc) · 3.04 KB
/
blendxus.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
bl_info = {
"name": "Blendxus",
"author": "Christopher Kopic",
"version": (1, 0),
"blender": (2, 7, 8),
"location": "",
"description": "Adds Operator for creating edges between close vertices",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Object"}
import bpy
import bmesh
import mathutils
from bpy.types import Operator
from bpy.props import FloatProperty, IntProperty, BoolProperty
class Blendxus(bpy.types.Operator):
"""Blendxus"""
bl_idname = "object.blendxus"
bl_label = "Blendxus"
bl_options = {'REGISTER', 'UNDO'}
max_connects = IntProperty(
name="Maximum Connections",
default=3,
min=1,
description="Maximum number of connections per vertex"
)
max_distance = FloatProperty(
name="Maximum Distance",
default=0.2,
min=0,
description="Maximum distance for creating connetions"
)
del_edge_face = BoolProperty(
name="Delete Initial Edges and Faces",
default=True,
description="Deletes all existing edges and faces to make the new connections visible"
)
convert_curve = BoolProperty(
name="Export to Curve",
default=False,
options={'SKIP_SAVE'},
description="Converts edgenet to curve for rendering. Effect won't be editable afterwards"
)
def execute(self, context):
obj = context.active_object
if obj.mode == 'EDIT':
bpy.ops.object.editmode_toggle()
# prepare bmesh
me = obj.data
ori = bmesh.new()
ori.from_mesh(me)
# delete faces and edges
if self.del_edge_face:
ori.edges.ensure_lookup_table()
bmesh.ops.delete(ori, geom=ori.edges, context=4)
# create kd-Tree
ori.verts.ensure_lookup_table()
size = len(ori.verts)
kd = mathutils.kdtree.KDTree(size)
for i, v in enumerate(ori.verts):
kd.insert(v.co, i)
kd.balance()
# create connections
for vert in ori.verts:
count = 0
for (co, index, dist) in kd.find_range(vert.co, self.max_distance):
if vert.index != index:
bmesh.ops.contextual_create(ori, geom=[vert, ori.verts[index]])
count += 1
if count > self.max_connects:
break
# apply changes
ori.to_mesh(me)
ori.free()
# convert to curve
if self.convert_curve:
bpy.ops.object.convert(target='CURVE')
obj.data.bevel_depth = 0.001
obj.data.fill_mode = 'FULL'
# update viewport
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
return {'FINISHED'}
@classmethod
def poll(cls, context):
ob = context.active_object
return ob is not None and ob.mode == 'OBJECT' and ob.type == 'MESH'
def register():
bpy.utils.register_class(Blendxus)
def unregister():
bpy.utils.unregister_class(Blendxus)
if __name__ == "__main__":
register()