1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
24 def randomize_selected(seed, delta, loc, rot, scale, scale_even):
27 from random import uniform
28 from mathutils import Vector
32 def rand_vec(vec_range):
33 return Vector(uniform(-val, val) for val in vec_range)
35 for obj in bpy.context.selected_objects:
39 obj.delta_location += rand_vec(loc)
41 obj.location += rand_vec(loc)
42 else: # otherwise the values change under us
43 uniform(0.0, 0.0), uniform(0.0, 0.0), uniform(0.0, 0.0)
45 if rot: # TODO, non euler's
48 obj.delta_rotation_euler[0] += vec[0]
49 obj.delta_rotation_euler[1] += vec[1]
50 obj.delta_rotation_euler[2] += vec[2]
52 obj.rotation_euler[0] += vec[0]
53 obj.rotation_euler[1] += vec[1]
54 obj.rotation_euler[2] += vec[2]
56 uniform(0.0, 0.0), uniform(0.0, 0.0), uniform(0.0, 0.0)
60 org_sca_x, org_sca_y, org_sca_z = obj.delta_scale
62 org_sca_x, org_sca_y, org_sca_z = obj.scale
65 sca_x = sca_y = sca_z = uniform(scale[0], - scale[0])
66 uniform(0.0, 0.0), uniform(0.0, 0.0)
68 sca_x, sca_y, sca_z = rand_vec(scale)
71 aX = -(sca_x * org_sca_x) + org_sca_x
72 aY = -(sca_x * org_sca_y) + org_sca_y
73 aZ = -(sca_x * org_sca_z) + org_sca_z
75 aX = sca_x + org_sca_x
76 aY = sca_y + org_sca_y
77 aZ = sca_z + org_sca_z
80 obj.delta_scale = aX, aY, aZ
82 obj.scale = aX, aY, aZ
84 uniform(0.0, 0.0), uniform(0.0, 0.0), uniform(0.0, 0.0)
87 from bpy.props import IntProperty, BoolProperty, FloatVectorProperty
90 class RandomizeLocRotSize(bpy.types.Operator):
91 '''Randomize objects loc/rot/scale'''
92 bl_idname = "object.randomize_transform"
93 bl_label = "Randomize Transform"
94 bl_options = {'REGISTER', 'UNDO'}
96 random_seed = IntProperty(
98 description="Seed value for the random generator",
103 use_delta = BoolProperty(
104 name="Transform Delta",
105 description=("Randomize delta transform values "
106 "instead of regular transform"),
109 use_loc = BoolProperty(
110 name="Randomize Location",
111 description="Randomize the location values",
114 loc = FloatVectorProperty(
116 description=("Maximun distance the objects "
117 "can spread over each axis"),
120 default=(0.0, 0.0, 0.0),
121 subtype='TRANSLATION',
123 use_rot = BoolProperty(
124 name="Randomize Rotation",
125 description="Randomize the rotation values",
128 rot = FloatVectorProperty(
130 description="Maximun rotation over each axis",
133 default=(0.0, 0.0, 0.0),
134 subtype='TRANSLATION',
136 use_scale = BoolProperty(
137 name="Randomize Scale",
138 description="Randomize the scale values",
141 scale_even = BoolProperty(
143 description="Use the same scale value for all axis",
147 '''scale_min = FloatProperty(name="Minimun Scale Factor",
148 description="Lowest scale percentage possible",
149 default=0.15, min=-1.0, max=1.0, precision=3)'''
151 scale = FloatVectorProperty(
153 description="Maximum scale randomization over each axis",
156 default=(0.0, 0.0, 0.0),
157 subtype='TRANSLATION',
160 def execute(self, context):
161 from math import radians
163 seed = self.random_seed
165 delta = self.use_delta
167 loc = None if not self.use_loc else self.loc
168 rot = None if not self.use_rot else self.rot * radians(1.0)
169 scale = None if not self.use_scale else self.scale
171 scale_even = self.scale_even
172 #scale_min = self.scale_min
174 randomize_selected(seed, delta, loc, rot, scale, scale_even)