-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandomize_transforms.py
More file actions
94 lines (72 loc) · 3.2 KB
/
randomize_transforms.py
File metadata and controls
94 lines (72 loc) · 3.2 KB
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
"""
Randomize Transforms - Applies seeded random offsets to location, rotation, scale.
Unlike Blender's built-in Randomize Transform, this version accepts a numeric
seed so the result is always reproducible: the same seed always produces the
exact same arrangement of objects. Change the seed to get a new variation.
"""
import bpy
import random
from typing import Optional
def randomize_transforms(context, props) -> Optional[str]:
"""Apply seeded random offsets to all selected objects' transforms.
Args:
context: Current Blender context.
props: RandomizeProperties instance holding current settings.
Returns:
Error string on failure, None on success.
"""
objects = list(context.selected_objects)
if not objects:
return "No objects selected"
rng = random.Random(props.seed)
for obj in objects:
if props.use_location:
obj.location.x += rng.uniform(props.loc_min[0], props.loc_max[0])
obj.location.y += rng.uniform(props.loc_min[1], props.loc_max[1])
obj.location.z += rng.uniform(props.loc_min[2], props.loc_max[2])
if props.use_rotation:
obj.rotation_euler.x += rng.uniform(props.rot_min[0], props.rot_max[0])
obj.rotation_euler.y += rng.uniform(props.rot_min[1], props.rot_max[1])
obj.rotation_euler.z += rng.uniform(props.rot_min[2], props.rot_max[2])
if props.use_scale:
if props.uniform_scale:
f = rng.uniform(props.scale_min, props.scale_max)
obj.scale.x *= f
obj.scale.y *= f
obj.scale.z *= f
else:
obj.scale.x *= rng.uniform(props.scale_min, props.scale_max)
obj.scale.y *= rng.uniform(props.scale_min, props.scale_max)
obj.scale.z *= rng.uniform(props.scale_min, props.scale_max)
return None
class OBJECT_OT_randomize_transforms(bpy.types.Operator):
"""Apply seeded random offsets to the selected objects' transforms"""
bl_idname = "object.randomize_transforms_seeded"
bl_label = "Randomize Transforms"
bl_description = (
"Add controlled random variation to location, rotation and/or scale. "
"The seed makes results reproducible: same seed → same result. "
"Increment the seed to get a different arrangement"
)
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT' and len(context.selected_objects) > 0
def execute(self, context):
wm = context.window_manager
props = wm.pivotier.randomize
error = randomize_transforms(context, props)
if error:
self.report({'WARNING'}, error)
return {'CANCELLED'}
return {'FINISHED'}
# ─── Registration ─────────────────────────────────────────────────────────────
classes = (
OBJECT_OT_randomize_transforms,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)