| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | # ##### BEGIN GPL LICENSE BLOCK ##### | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | #  modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | #  as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | #  of the License, or (at your option) any later version. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | #  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | #  GNU General Public License for more details. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | #  along with this program; if not, write to the Free Software Foundation, | 
					
						
							| 
									
										
										
										
											2010-02-12 13:34:04 +00:00
										 |  |  | #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # ##### END GPL LICENSE BLOCK ##### | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  | # <pep8 compliant> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | import bpy | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | def randomize_selected(seed, loc, rot, scale, scale_even, scale_min): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     import random | 
					
						
							|  |  |  |     from random import uniform | 
					
						
							| 
									
										
										
										
											2010-04-11 14:22:27 +00:00
										 |  |  |     from mathutils import Vector | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     random.seed(seed) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def rand_vec(vec_range): | 
					
						
							| 
									
										
										
										
											2010-04-17 19:05:53 +00:00
										 |  |  |         return Vector([uniform(-val, val) for val in vec_range]) | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for obj in bpy.context.selected_objects: | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  |         if loc: | 
					
						
							|  |  |  |             obj.location += rand_vec(loc) | 
					
						
							| 
									
										
										
										
											2010-09-07 15:17:42 +00:00
										 |  |  |         else:  # otherwise the values change under us | 
					
						
							| 
									
										
										
										
											2010-01-19 09:24:39 +00:00
										 |  |  |             uniform(0.0, 0.0), uniform(0.0, 0.0), uniform(0.0, 0.0) | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-07 15:17:42 +00:00
										 |  |  |         if rot:  # TODO, non euler's | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  |             vec = rand_vec(rot) | 
					
						
							|  |  |  |             obj.rotation_euler[0] += vec[0] | 
					
						
							|  |  |  |             obj.rotation_euler[1] += vec[1] | 
					
						
							|  |  |  |             obj.rotation_euler[2] += vec[2] | 
					
						
							| 
									
										
										
										
											2010-01-19 09:24:39 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             uniform(0.0, 0.0), uniform(0.0, 0.0), uniform(0.0, 0.0) | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if scale: | 
					
						
							|  |  |  |             org_sca_x, org_sca_y, org_sca_z = obj.scale | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if scale_even: | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  |                 sca_x = sca_y = sca_z = uniform(scale[0], - scale[0]) | 
					
						
							| 
									
										
										
										
											2010-01-19 15:57:02 +00:00
										 |  |  |                 uniform(0.0, 0.0), uniform(0.0, 0.0) | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 sca_x, sca_y, sca_z = rand_vec(scale) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             aX = sca_x + org_sca_x | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  |             bX = org_sca_x * scale_min | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             aY = sca_y + org_sca_y | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  |             bY = org_sca_y * scale_min | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             aZ = sca_z + org_sca_z | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  |             bZ = org_sca_z * scale_min | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  |             if aX < bX: | 
					
						
							|  |  |  |                 aX = bX | 
					
						
							|  |  |  |             if aY < bY: | 
					
						
							|  |  |  |                 aY = bY | 
					
						
							|  |  |  |             if aZ < bZ: | 
					
						
							|  |  |  |                 aZ = bZ | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             obj.scale = aX, aY, aZ | 
					
						
							| 
									
										
										
										
											2010-01-19 09:24:39 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             uniform(0.0, 0.0), uniform(0.0, 0.0), uniform(0.0, 0.0) | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | from bpy.props import * | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RandomizeLocRotSize(bpy.types.Operator): | 
					
						
							| 
									
										
										
										
											2010-02-11 23:13:47 +00:00
										 |  |  |     '''Randomize objects loc/rot/scale''' | 
					
						
							|  |  |  |     bl_idname = "object.randomize_transform" | 
					
						
							|  |  |  |     bl_label = "Randomize Transform" | 
					
						
							| 
									
										
										
										
											2010-03-01 00:03:51 +00:00
										 |  |  |     bl_options = {'REGISTER', 'UNDO'} | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     random_seed = IntProperty(name="Random Seed", | 
					
						
							|  |  |  |         description="Seed value for the random generator", | 
					
						
							|  |  |  |         default=0, min=0, max=1000) | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  |     use_loc = BoolProperty(name="Randomize Location", | 
					
						
							|  |  |  |         description="Randomize the scale values", default=True) | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-07 11:53:00 +00:00
										 |  |  |     loc = FloatVectorProperty(name="Location", | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  |         description="Maximun distance the objects can spread over each axis", | 
					
						
							| 
									
										
										
										
											2010-02-01 22:04:33 +00:00
										 |  |  |         default=(0.0, 0.0, 0.0), min=-100.0, max=100.0, subtype='TRANSLATION') | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     use_rot = BoolProperty(name="Randomize Rotation", | 
					
						
							|  |  |  |         description="Randomize the rotation values", default=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     rot = FloatVectorProperty(name="Rotation", | 
					
						
							|  |  |  |         description="Maximun rotation over each axis", | 
					
						
							| 
									
										
										
										
											2010-02-01 22:04:33 +00:00
										 |  |  |         default=(0.0, 0.0, 0.0), min=-180.0, max=180.0, subtype='TRANSLATION') | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     use_scale = BoolProperty(name="Randomize Scale", | 
					
						
							|  |  |  |         description="Randomize the scale values", default=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     scale_even = BoolProperty(name="Scale Even", | 
					
						
							|  |  |  |         description="Use the same scale value for all axis", default=False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     scale_min = FloatProperty(name="Minimun Scale Factor", | 
					
						
							|  |  |  |         description="Lowest scale percentage possible", | 
					
						
							| 
									
										
										
										
											2010-01-19 15:57:02 +00:00
										 |  |  |         default=0.15, min=-1.0, max=1.0, precision=3) | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  |     scale = FloatVectorProperty(name="Scale", | 
					
						
							|  |  |  |         description="Maximum scale randomization over each axis", | 
					
						
							| 
									
										
										
										
											2010-02-01 22:04:33 +00:00
										 |  |  |         default=(0.0, 0.0, 0.0), min=-100.0, max=100.0, subtype='TRANSLATION') | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  |     def execute(self, context): | 
					
						
							|  |  |  |         from math import radians | 
					
						
							|  |  |  |         seed = self.properties.random_seed | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 09:51:09 +00:00
										 |  |  |         loc = None if not self.properties.use_loc else self.properties.loc | 
					
						
							|  |  |  |         rot = None if not self.properties.use_rot else self.properties.rot * radians(1.0) | 
					
						
							|  |  |  |         scale = None if not self.properties.use_scale else self.properties.scale | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         scale_even = self.properties.scale_even | 
					
						
							| 
									
										
										
										
											2010-01-19 09:36:40 +00:00
										 |  |  |         scale_min = self.properties.scale_min | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         randomize_selected(seed, loc, rot, scale, scale_even, scale_min) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return {'FINISHED'} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def menu_func(self, context): | 
					
						
							|  |  |  |     if context.mode == 'OBJECT': | 
					
						
							|  |  |  |         self.layout.operator(RandomizeLocRotSize.bl_idname, | 
					
						
							| 
									
										
										
										
											2010-02-11 23:13:47 +00:00
										 |  |  |         text="Randomize Transform") | 
					
						
							| 
									
										
										
										
											2010-01-19 00:59:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-14 11:21:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def register(): | 
					
						
							|  |  |  |     bpy.types.VIEW3D_MT_transform.append(menu_func) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 23:32:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-14 11:21:21 +00:00
										 |  |  | def unregister(): | 
					
						
							|  |  |  |     bpy.types.VIEW3D_MT_transform.remove(menu_func) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-16 09:55:07 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     register() |