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(name="Random Seed",
97 description="Seed value for the random generator",
98 default=0, min=0, max=1000)
100 use_delta = BoolProperty(name="Transform Delta",
101 description="Randomize delta transform values instead of regular transform", default=False)
103 use_loc = BoolProperty(name="Randomize Location",
104 description="Randomize the location values", default=True)
106 loc = FloatVectorProperty(name="Location",
107 description="Maximun distance the objects can spread over each axis",
108 default=(0.0, 0.0, 0.0), min=-100.0, max=100.0, subtype='TRANSLATION')
110 use_rot = BoolProperty(name="Randomize Rotation",
111 description="Randomize the rotation values", default=True)
113 rot = FloatVectorProperty(name="Rotation",
114 description="Maximun rotation over each axis",
115 default=(0.0, 0.0, 0.0), min=-180.0, max=180.0, subtype='TRANSLATION')
117 use_scale = BoolProperty(name="Randomize Scale",
118 description="Randomize the scale values", default=True)
120 scale_even = BoolProperty(name="Scale Even",
121 description="Use the same scale value for all axis", default=False)
123 '''scale_min = FloatProperty(name="Minimun Scale Factor",
124 description="Lowest scale percentage possible",
125 default=0.15, min=-1.0, max=1.0, precision=3)'''
127 scale = FloatVectorProperty(name="Scale",
128 description="Maximum scale randomization over each axis",
129 default=(0.0, 0.0, 0.0), min=-100.0, max=100.0, subtype='TRANSLATION')
131 def execute(self, context):
132 from math import radians
134 seed = self.random_seed
136 delta = self.use_delta
138 loc = None if not self.use_loc else self.loc
139 rot = None if not self.use_rot else self.rot * radians(1.0)
140 scale = None if not self.use_scale else self.scale
142 scale_even = self.scale_even
143 #scale_min = self.scale_min
145 randomize_selected(seed, delta, loc, rot, scale, scale_even)