forked from corrodedHash/blender-fractals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdragon.py
108 lines (79 loc) · 2.53 KB
/
dragon.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
import bpy
import bpy_extras
import bmesh
def _dragon(turn):
flip_count = 0
cur_bit = turn % 2
while turn > 0:
turn = turn // 2
if turn % 2 is not cur_bit:
cur_bit = turn % 2
flip_count += 1
return flip_count % 4
def _fold(direction):
if direction == 0:
return (0, 1)
if direction == 1:
return (1, 0)
if direction == 2:
return (0, -1)
if direction == 3:
return (-1, 0)
else:
raise ValueError
def _create_dragon(self, context):
me = bpy.data.meshes.new('dragonMesh')
ob = bpy.data.objects.new('DragonCurve', me)
bpy.context.scene.objects.link(ob)
bm = bmesh.new() # create an empty BMesh
bm.from_mesh(me) # fill it in from a Mesh
cur_vec = (0.0, 0.0, 0.0)
last_v1 = bm.verts.new(cur_vec)
last_v2 = bm.verts.new((cur_vec[0], cur_vec[1] + 1, cur_vec[2]))
for cur_turn in range(1, self.count):
dir_vec = _fold(_dragon(cur_turn))
cur_vec = (cur_vec[0] + dir_vec[0],
cur_vec[1], cur_vec[2] + dir_vec[1])
cur_v1 = bm.verts.new(cur_vec)
cur_v2 = bm.verts.new((cur_vec[0], cur_vec[1] + 1, cur_vec[2]))
bm.verts.index_update()
bm.faces.new((last_v1, last_v2, cur_v2, cur_v1))
last_v1 = cur_v1
last_v2 = cur_v2
# if self.merge_vertices:
# print(str(bpy.context.area.type))
# bpy.ops.mesh.remove_doubles()
bm.to_mesh(me)
class DragonCurve_add_object(bpy.types.Operator,
bpy_extras.object_utils.AddObjectHelper):
"""Create a new Dragon Curve"""
bl_idname = "mesh.add_dragoncurve"
bl_label = "Add Dragon Curve"
bl_options = {'REGISTER', 'UNDO'}
count = bpy.props.IntProperty(
name="Edge Count",
default=100,
min=1,
soft_min=1,
subtype='UNSIGNED',
description="Number of squares the dragon will consist of",
)
def iteration_update(self, context):
self.count = 2 ** self.iteration
iteration = bpy.props.IntProperty(
name="Iteration Count",
default=2,
min=1,
soft_min=1,
soft_max=10,
subtype='UNSIGNED',
description="Number of iterations of the dragon curve",
update=iteration_update)
merge_vertices = bpy.props.BoolProperty(
name="Merge vertices",
description="Remove duplicate vertices",
default=True
)
def execute(self, context):
_create_dragon(self, context)
return {'FINISHED'}