/
__init__.py
189 lines (157 loc) · 5.78 KB
/
__init__.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
bl_info = {
"name": "Procedural Mushrooms",
"category": "Add Mesh",
"author": "Rafael",
"version": (1, 0),
"blender": (2, 80, 0),
}
import bpy
from .util import optional, linkAndSelect
from .shroom import MushroomProps, Mushroom, MushroomPG, generatorEnums, generatorMap
from .spline import asEdgeObject
#############################################
class GrowShroom(bpy.types.Operator):
bl_idname = "mesh.shroom_generate"
bl_label = "Generate Mushroom(s)"
bl_options = {"REGISTER", "UNDO"}
func : bpy.props.EnumProperty(
name="Function",
description="The generation function to use",
items=generatorEnums
)
LODr : bpy.props.IntProperty(
name="LOD radius",
description="Number of samples in radial direction",
default=10, min=2
)
LODp : bpy.props.IntProperty(
name="LOD phi",
description="Number of samples in angular direction",
default=16, min=3
)
count : bpy.props.IntProperty(
name="Count",
description="How many mushrooms to generate",
default=4, min=1
)
noise : bpy.props.BoolProperty(
name="Noise",
description="Adds some irregularities to the mushroom",
default=False)
exportCrossSec : bpy.props.BoolProperty(
name="Cross Section",
description="Create an Object for the cross section",
default=False)
exportMesh : bpy.props.BoolProperty(
name="Mesh",
description="Create an Object for the mesh",
default=True)
def execute(self, context):
for pos in range(-self.count//2, self.count//2):
shroom = generatorMap[self.func]()
if self.exportCrossSec:
obj = asEdgeObject(shroom.upperHat(), shroom.lowerHat(), shroom.shaft(), LOD=self.LODr)
obj.location = (pos, 0, 0)
shroom.store(obj.mushroom)
linkAndSelect(obj, context)
obj = asEdgeObject(shroom.shaftRadiusSpline(), LOD=self.LODr)
obj.location = (pos, 0, 0)
linkAndSelect(obj, context)
if self.exportMesh:
obj = shroom.toMeshObject(self.LODr, self.LODp, self.noise)
obj.location = (pos, 0, 0)
linkAndSelect(obj, context)
return {'FINISHED'}
#############################################
class EvolveShroom(bpy.types.Operator):
bl_idname = "mesh.shroom_evolve"
bl_label = "Mutate Mushroom"
bl_options = {"REGISTER", "UNDO"}
count : bpy.props.IntProperty(
name="Variations",
description="How many different mushrooms to generate from this one",
default=4, min=1
)
radiation : bpy.props.FloatProperty(
name="Radiation",
description="Strength of mutation",
default=70, step=100,
min=0, max=100
)
def execute(self, context):
shroom = Mushroom.load(context.object.mushroom)
for _ in range(self.count):
newShroom = shroom.mutate(self.radiation)
obj = newShroom.toMeshObject(10, 16)
obj.location = context.object.location
obj.location[1] += 1
linkAndSelect(obj, context)
return {'FINISHED'}
#############################################
class EditShroom(bpy.types.Operator, MushroomProps):
bl_idname = "mesh.shroom_edit"
bl_label = "Edit Mushroom"
bl_options = {"REGISTER", "UNDO"}
LODr : bpy.props.IntProperty(
name="LOD radius",
description="Number of samples in radial direction",
default=10, min=2
)
LODp : bpy.props.IntProperty(
name="LOD phi",
description="Number of samples in angular direction",
default=16, min=3
)
noise : bpy.props.BoolProperty(
name="Noise",
description="Adds some irregularities to the mushroom",
default=False)
def invoke(self, context, event):
if context.object is not None:
Mushroom.load(context.object.mushroom).store(self)
return self.execute(context)
def execute(self, context):
shroom = Mushroom.load(self)
obj = shroom.toMeshObject(self.LODr, self.LODp, self.noise)
obj.location = optional(context.object, obj).location
obj.location[1] += 1
linkAndSelect(obj, context)
return {'FINISHED'}
#############################################
class CombineShrooms(bpy.types.Operator):
bl_idname = "mesh.shroom_combine"
bl_label = "Combine Mushrooms"
bl_options = {"REGISTER", "UNDO"}
count : bpy.props.IntProperty(
name="Offspring",
description="How many mushrooms to generate",
default=2, min=1
)
def execute(self, context):
parents = [co.mushroom for co in context.selected_objects]
for i in range(self.count):
shroom = Mushroom.procreate(*parents)
obj = shroom.toMeshObject(10, 16, False)
obj.location = optional(context.object, obj).location
obj.location[1] += 1
linkAndSelect(obj, context)
return {'FINISHED'}
#############################################
# un-/register formalities
#############################################
def register():
bpy.utils.register_class(MushroomPG)
bpy.utils.register_class(GrowShroom)
bpy.utils.register_class(EditShroom)
bpy.utils.register_class(EvolveShroom)
bpy.utils.register_class(CombineShrooms)
bpy.types.Object.mushroom = bpy.props.PointerProperty(type=MushroomPG)
def unregister():
bpy.utils.unregister_class(GrowShroom)
bpy.utils.unregister_class(EditShroom)
bpy.utils.unregister_class(EvolveShroom)
bpy.utils.unregister_class(CombineShrooms)
bpy.utils.unregister_class(MushroomPG)
del bpy.types.Object.mushroom
if __name__ == "__main__":
register()