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
|
# gpl: author Dannyboy
bl_info = {
"name": "Add Random Box Structure",
"author": "Dannyboy",
"version": (1, 0, 1),
"location": "View3D > Add > Make Box Structure",
"description": "Fill selected box shaped meshes with randomly sized cubes",
"warning": "",
"wiki_url": "",
"tracker_url": "dannyboypython.blogspot.com",
"category": "Object"}
import bpy
import random
from bpy.types import Operator
from bpy.props import (
BoolProperty,
FloatProperty,
FloatVectorProperty,
IntProperty,
)
class makestructure(Operator):
bl_idname = "object.make_structure"
bl_label = "Add Random Box Structure"
bl_description = ("Create a randomized structure made of boxes\n"
"with various control parameters\n"
"Needs an existing Active Mesh Object")
bl_options = {'REGISTER', 'UNDO'}
dc = BoolProperty(
name="Delete Base Mesh(es)",
default=True
)
wh = BoolProperty(
name="Stay Within Bounds",
description="Keeps cubes from exceeding base mesh bounds",
default=True
)
uf = BoolProperty(
name="Uniform Cube Quantity",
default=False
)
qn = IntProperty(
name="Cube Quantity",
default=10,
min=1, max=1500
)
mn = FloatVectorProperty(
name="Min Scales",
default=(0.1, 0.1, 0.1),
subtype='XYZ'
)
mx = FloatVectorProperty(
name="Max Scales",
default=(2.0, 2.0, 2.0),
subtype='XYZ'
)
lo = FloatVectorProperty(
name="XYZ Offset",
default=(0.0, 0.0, 0.0),
subtype='XYZ'
)
rsd = FloatProperty(
name="Random Seed",
default=1
)
@classmethod
def poll(cls, context):
obj = context.active_object
return obj is not None and obj.type == "MESH" and obj.mode == "OBJECT"
def draw(self, context):
layout = self.layout
box = layout.box()
box.label(text="Options:")
box.prop(self, "dc")
box.prop(self, "wh")
box.prop(self, "uf")
box = layout.box()
box.label(text="Parameters:")
box.prop(self, "qn")
box.prop(self, "mn")
box.prop(self, "mx")
box.prop(self, "lo")
box.prop(self, "rsd")
def execute(self, context):
rsdchange = self.rsd
oblst = []
uvyes = 0
bpy.ops.group.create(name='Cubagrouper')
bpy.ops.group.objects_remove()
for ob in bpy.context.selected_objects:
oblst.append(ob)
for obj in oblst:
bpy.ops.object.select_pattern(pattern=obj.name) # Select base mesh
bpy.context.scene.objects.active = obj
if obj.data.uv_layers[:] != []:
uvyes = 1
else:
uvyes = 0
bpy.ops.object.group_link(group='Cubagrouper')
dim = obj.dimensions
rot = obj.rotation_euler
if self.uf is True:
area = dim.x * dim.y * dim.z
else:
area = 75
for cube in range(round((area / 75) * self.qn)):
random.seed(rsdchange)
pmn = self.mn # Proxy values
pmx = self.mx
if self.wh is True:
if dim.x < pmx.x: # Keeping things from exceeding proper size
pmx.x = dim.x
if dim.y < pmx.y:
pmx.y = dim.y
if dim.z < pmx.z:
pmx.z = dim.z
if 0.0 > pmn.x: # Keeping things from going under zero
pmn.x = 0.0
if 0.0 > pmn.y:
pmn.y = 0.0
if 0.0 > pmn.z:
pmn.z = 0.0
sx = (random.random() * (pmx.x - pmn.x)) + pmn.x # Just changed self.mx and .mn to pmx.
sy = (random.random() * (pmx.y - pmn.y)) + pmn.y
sz = (random.random() * (pmx.z - pmn.z)) + pmn.z
if self.wh is True: # This keeps the cubes within the base mesh
ex = (random.random() * (dim.x - sx)) - ((dim.x - sx) / 2) + obj.location.x
wy = (random.random() * (dim.y - sy)) - ((dim.y - sy) / 2) + obj.location.y
ze = (random.random() * (dim.z - sz)) - ((dim.z - sz) / 2) + obj.location.z
elif self.wh is False:
ex = (random.random() * dim.x) - (dim.x / 2) + obj.location.x
wy = (random.random() * dim.y) - (dim.y / 2) + obj.location.y
ze = (random.random() * dim.z) - (dim.z / 2) + obj.location.z
bpy.ops.mesh.primitive_cube_add(
radius=0.5, location=(ex + self.lo.x, wy + self.lo.y, ze + self.lo.z)
)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.transform.resize(
value=(sx, sy, sz), constraint_axis=(True, True, True),
constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED',
proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True
)
bpy.ops.object.mode_set(mode='OBJECT')
select = bpy.context.object # This is used to keep something selected for poll()
bpy.ops.object.group_link(group='Cubagrouper')
rsdchange += 3
bpy.ops.object.select_grouped(type='GROUP')
bpy.ops.transform.rotate(
value=rot[0], axis=(1, 0, 0), constraint_axis=(False, False, False),
constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED',
proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True
)
bpy.ops.transform.rotate(
value=rot[1], axis=(0, 1, 0), constraint_axis=(False, False, False),
constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED',
proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True
)
bpy.ops.transform.rotate(
value=rot[2], axis=(0, 0, 1), constraint_axis=(False, False, False),
constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED',
proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True
)
bpy.context.scene.objects.active = obj # Again needed to avoid poll() taking me down
bpy.ops.object.make_links_data(type='MODIFIERS')
bpy.ops.object.make_links_data(type='MATERIAL')
if uvyes == 1:
bpy.ops.object.join_uvs()
bpy.ops.group.objects_remove()
bpy.context.scene.objects.active = select
if self.dc is True:
bpy.context.scene.objects.unlink(obj)
return {'FINISHED'}
def register():
bpy.utils.register_class(makestructure)
def unregister():
bpy.utils.unregister_class(makestructure)
if __name__ == "__main__":
register()
|