-
Notifications
You must be signed in to change notification settings - Fork 1
/
rock-o-matic.py
342 lines (282 loc) · 11.9 KB
/
rock-o-matic.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
bl_info = {
'name': 'Rock-o-Matic',
'author': 'Klaus Silveira',
'version': (0, 1, 0),
'blender': (2, 69, 0),
'location': 'View3D > Add > Mesh',
'description': 'Generates rocks using various techniques.',
'warning': '',
'wiki_url': 'http://wiki.blender.org/index.php?title=Extensions:2.6/Py/Scripts/Add_Mesh/Rock_o_Matic',
'tracker_url': 'https://github.com/klaussilveira/rock-o-matic/issues',
'category': 'Add Mesh'
}
import bpy
import bmesh
import math
import mathutils
import random
class MeshBase():
def get_mesh(self):
mesh = bpy.data.meshes.new('Rock')
self.bmesh.to_mesh(mesh)
self.bmesh.free()
return mesh
class CubeBase(MeshBase):
def __init__(self, rock_size, rock_scale_vector, rock_scale_factor):
self.bmesh = bmesh.new()
scaleMatrix = mathutils.Matrix.Scale(rock_scale_factor, 4, rock_scale_vector)
bmesh.ops.create_cube(self.bmesh, size=rock_size, matrix=scaleMatrix)
class IcosphereBase(MeshBase):
def __init__(self, rock_size, rock_scale_vector, rock_scale_factor):
self.bmesh = bmesh.new()
scaleMatrix = mathutils.Matrix.Scale(rock_scale_factor, 4, rock_scale_vector)
bmesh.ops.create_icosphere(self.bmesh, subdivisions=1, diameter=rock_size, matrix=scaleMatrix)
class UvSphereBase(MeshBase):
def __init__(self, rock_size, rock_scale_vector, rock_scale_factor):
self.bmesh = bmesh.new()
scaleMatrix = mathutils.Matrix.Scale(rock_scale_factor, 4, rock_scale_vector)
bmesh.ops.create_uvsphere(self.bmesh, u_segments=5, v_segments=5, diameter=rock_size, matrix=scaleMatrix)
base_factory = {
'CubeBase': CubeBase,
'IcosphereBase': IcosphereBase,
'UvSphereBase': UvSphereBase,
}
class GenericRecipe():
def make(self, obj):
firstSmooth = obj.modifiers.new(name='Subdivide 1', type='SUBSURF')
firstSmooth.levels = 2
secondSmooth = obj.modifiers.new(name='Subdivide 2', type='SUBSURF')
secondSmooth.levels = 4
voronoiMap = bpy.data.textures.new('Basic voronoi', type='VORONOI')
voronoiMap.color_mode = 'INTENSITY'
voronoiMap.weight_2 = random.uniform(0.2, 0.8)
voronoiMap.weight_3 = random.uniform(0, 1)
voronoiMap.noise_intensity = 1.0
voronoiMap.nabla = 0.3
voronoiMap.noise_scale = random.uniform(1.0, 1.5)
cell = obj.modifiers.new(name='Basic voronoi', type='DISPLACE')
cell.texture = voronoiMap
cell.strength = random.uniform(0.5, 1)
cloudMap = bpy.data.textures.new('Basic simplex', type='CLOUDS')
cloudMap.noise_depth = random.randint(6, 8)
cloudMap.noise_scale = random.uniform(0.8, 1)
cloud = obj.modifiers.new(name='Basic simplex', type='DISPLACE')
cloud.texture = cloudMap
cloud.strength = random.uniform(0.2, 0.4)
noiseMap = bpy.data.textures.new('Noise', type='NOISE')
noise = obj.modifiers.new(name='Noise', type='DISPLACE')
noise.texture = noiseMap
noise.strength = 0.01
finalSmooth = obj.modifiers.new(name='Final smooth', type='SUBSURF')
finalSmooth.levels = 2
class ErodedRecipe():
def make(self, obj):
firstSmooth = obj.modifiers.new(name='Subdivide 1', type='SUBSURF')
firstSmooth.levels = 2
secondSmooth = obj.modifiers.new(name='Subdivide 2', type='SUBSURF')
secondSmooth.levels = 4
algorithms = ['DISTANCE', 'DISTANCE_SQUARED', 'MINKOVSKY_FOUR']
cellMap = bpy.data.textures.new('Cellular shape', type='VORONOI')
cellMap.distance_metric = random.choice(algorithms)
cellMap.noise_intensity = random.uniform(0.8, 1)
cellMap.noise_scale = random.uniform(0.7, 1)
cell = obj.modifiers.new(name='Cellular shape', type='DISPLACE')
cell.texture = cellMap
cell.strength = 0.2
cell.mid_level = 0.5
algorithms = ['VORONOI_F1', 'VORONOI_F2', 'BLENDER_ORIGINAL', 'IMPROVED_PERLIN']
noiseMap = bpy.data.textures.new('Base noise', type='CLOUDS')
noiseMap.noise_basis = random.choice(algorithms)
noiseMap.noise_depth = 2
noiseMap.noise_scale = 0.2
noise = obj.modifiers.new(name='Base noise', type='DISPLACE')
noise.texture = noiseMap
noise.strength = 0.02
crackMap = bpy.data.textures.new('Cracks', type='VORONOI')
crackMap.distance_metric = 'MINKOVSKY_FOUR'
crackMap.noise_intensity = 0.8
crackMap.noise_scale = random.uniform(0.8, 1.0)
cracks = obj.modifiers.new(name='Cracks', type='DISPLACE')
cracks.texture = crackMap
cracks.strength = 0.1
finalSmooth = obj.modifiers.new(name='Final smooth', type='SUBSURF')
finalSmooth.levels = 2
class BoulderRecipe():
def make(self, obj):
firstSmooth = obj.modifiers.new(name='Subdivide 1', type='SUBSURF')
firstSmooth.levels = 2
secondSmooth = obj.modifiers.new(name='Subdivide 2', type='SUBSURF')
secondSmooth.levels = 4
cellMap = bpy.data.textures.new('Cellular shape', type='VORONOI')
cellMap.distance_metric = 'DISTANCE_SQUARED'
cellMap.weight_1 = 2
cellMap.weight_2 = 2
cellMap.weight_3 = 2
cellMap.weight_4 = 2
cellMap.noise_intensity = 1
cellMap.noise_scale = 1
cell = obj.modifiers.new(name='Cellular shape', type='DISPLACE')
cell.texture = cellMap
cell.strength = random.uniform(0.5, 0.7)
cell.mid_level = random.uniform(0.6, 0.8)
crackMap = bpy.data.textures.new('Cracks', type='MUSGRAVE')
crackMap.octaves = 2
crackMap.lacunarity = 2
crackMap.dimension_max = 1
crackMap.offset = 1.2
crackMap.musgrave_type = 'HETERO_TERRAIN'
crackMap.noise_basis = 'VORONOI_CRACKLE'
crackMap.noise_scale = 7
cracks = obj.modifiers.new(name='Cracks', type='DISPLACE')
cracks.texture = crackMap
cracks.strength = 0.05
cloudMap = bpy.data.textures.new('Detailing', type='CLOUDS')
cloudMap.noise_type = 'HARD_NOISE'
cloudMap.noise_basis = 'IMPROVED_PERLIN'
cloudMap.noise_depth = 2
cloudMap.noise_scale = random.uniform(0.4, 0.5)
cloud = obj.modifiers.new(name='Detailing', type='DISPLACE')
cloud.texture = cloudMap
cloud.strength = 0.05
cloud.mid_level = 0.5
finalSmooth = obj.modifiers.new(name='Final smooth', type='SUBSURF')
finalSmooth.levels = 2
class ToonRecipe():
def make(self, obj):
firstSmooth = obj.modifiers.new(name='Subdivide 1', type='SUBSURF')
firstSmooth.levels = 2
secondSmooth = obj.modifiers.new(name='Subdivide 2', type='SUBSURF')
secondSmooth.levels = 4
cellMap = bpy.data.textures.new('Cellular shape', type='VORONOI')
cellMap.distance_metric = 'DISTANCE_SQUARED'
cellMap.noise_intensity = random.uniform(1, 1.2)
cellMap.noise_scale = 1
cell = obj.modifiers.new(name='Cellular shape', type='DISPLACE')
cell.texture = cellMap
cell.strength = 0.2
cell.mid_level = 0.5
finalSmooth = obj.modifiers.new(name='Final smooth', type='SUBSURF')
finalSmooth.levels = 2
recipe_factory = {
'GenericRecipe': GenericRecipe,
'ErodedRecipe': ErodedRecipe,
'BoulderRecipe': BoulderRecipe,
'ToonRecipe': ToonRecipe,
}
class RockOMatic(bpy.types.Operator):
bl_idname = 'mesh.rockomatic'
bl_label = 'Rock-o-Matic'
bl_options = {'REGISTER', 'UNDO'}
rock_recipe = bpy.props.EnumProperty \
(
name = 'Recipe',
description = 'Choose the rock generation recipe',
items = [
('GenericRecipe', 'Generic', 'Generates generic rock forms', 1),
('ErodedRecipe', 'Eroded', 'Generates eroded rocks', 2),
('BoulderRecipe', 'Boulder', 'Generates boulder rocks', 3),
('ToonRecipe', 'Toon', 'Generates simplistic rocks with hard edges', 4),
],
default = 'GenericRecipe'
)
rock_base = bpy.props.EnumProperty \
(
name = 'Base geometry',
description = 'Choose the base geometry to generate the rock on',
items = [
('CubeBase', 'Cube', 'Use cube as base', 1),
('IcosphereBase', 'Icosphere', 'Use iscosphere as base', 2),
('UvSphereBase', 'UvSphere', 'Use UV sphere as base', 3),
],
default = 'CubeBase'
)
rock_size = bpy.props.FloatProperty \
(
name = 'Overall size',
description = 'Overall size of the rock to generate'
)
rock_scale_vector = bpy.props.FloatVectorProperty \
(
name = 'Direction',
description = 'Direction to apply the scaling',
subtype = 'XYZ',
unit = 'LENGTH'
)
rock_scale_factor = bpy.props.FloatProperty \
(
name = 'Factor',
description = 'Amount of scaling to apply in the determined direction'
)
generate_lowpoly = bpy.props.BoolProperty \
(
name = 'Generate low-poly?',
description = 'Allow the creation of a low-poly mesh for map baking',
default = False
)
def execute(self, context):
self.generate_rock(context)
return {'FINISHED'}
def invoke(self, context, event):
self.generate_rock(context)
return {'FINISHED'}
def draw(self, context):
column = self.layout.column(align=True)
column.operator('mesh.rockomatic', text='Regenerate')
column.separator()
column.prop(self, 'rock_recipe')
column.prop(self, 'rock_base')
column.prop(self, 'generate_lowpoly')
column.label('Proportions')
column.prop(self, 'rock_size')
column.prop(self, 'rock_scale_vector')
column.prop(self, 'rock_scale_factor')
def set_defaults(self):
if self.rock_size == 0:
self.rock_size = random.uniform(1, 5)
if not any(self.rock_scale_vector):
self.rock_scale_vector = (random.uniform(1, 3), random.uniform(1, 3), random.uniform(1, 3))
if self.rock_scale_factor == 0:
self.rock_scale_factor = random.uniform(0.3, 3)
def clear_rocks(self):
rock_list = [item.name for item in bpy.data.objects if item.name.startswith('Rock')]
for old_rock in rock_list:
if bpy.data.objects[old_rock].hide:
bpy.data.objects[old_rock].hide = False
bpy.data.objects[old_rock].select = True
bpy.ops.object.delete()
def generate_rock(self, context):
self.clear_rocks()
self.set_defaults()
# generate mesh and apply recipe
rock = base_factory[self.rock_base](self.rock_size, self.rock_scale_vector, self.rock_scale_factor)
obj = bpy.data.objects.new('Rock', rock.get_mesh())
recipe = recipe_factory[self.rock_recipe]()
recipe.make(obj)
# add to scene and select
context.scene.objects.link(obj)
context.scene.objects.active = obj
obj.select = True
if self.generate_lowpoly:
lowpoly_obj = obj.copy()
lowpoly_obj.data = obj.data.copy()
lowpoly_obj.scale = obj.scale
lowpoly_obj.location = obj.location
lowpoly_obj.name = 'Rock_LowPoly'
lowpoly_obj.hide = True
for modifier in lowpoly_obj.modifiers:
if (modifier.name == 'Final smooth'):
lowpoly_obj.modifiers.remove(modifier)
decimate = lowpoly_obj.modifiers.new(name='Decimation', type='DECIMATE')
decimate.ratio = 0.01
decimate.use_collapse_triangulate = True
context.scene.objects.link(lowpoly_obj)
def add_to_menu(self, context):
self.layout.operator('mesh.rockomatic', icon='PLUGIN')
def register():
bpy.utils.register_class(RockOMatic)
bpy.types.INFO_MT_mesh_add.append(add_to_menu)
def unregister():
bpy.utils.unregister_class(RockOMatic)
bpy.types.INFO_MT_mesh_add.remove(add_to_menu)
if __name__ == '__main__':
register()