Cleanup: trailing space
[blender-addons.git] / mesh_tools / random_vertices.py
blobad6d8b4152a2e22ebd3046dfa4cfb32457cc31c2
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 bl_info = {
4 "name": "Random Vertices",
5 "author": "Oscurart, Greg",
6 "version": (1, 3),
7 "blender": (2, 63, 0),
8 "location": "Object > Transform > Random Vertices",
9 "description": "Randomize selected components of active object",
10 "warning": "",
11 "doc_url": "",
12 "category": "Mesh",
16 import bpy
17 from bpy.types import Operator
18 import random
19 import bmesh
20 from bpy.props import (
21 BoolProperty,
22 FloatProperty,
23 IntVectorProperty,
27 def add_object(self, context, valmin, valmax, factor, vgfilter):
28 # select an option with weight map or not
29 mode = bpy.context.active_object.mode
30 # generate variables
31 objact = bpy.context.active_object
32 listver = []
33 warn_message = False
35 # switch to edit mode
36 bpy.ops.object.mode_set(mode='OBJECT')
37 bpy.ops.object.mode_set(mode='EDIT')
39 # bmesh object
40 odata = bmesh.from_edit_mesh(objact.data)
41 odata.select_flush(False)
43 # if the vertex is selected add to the list
44 for vertice in odata.verts[:]:
45 if vertice.select:
46 listver.append(vertice.index)
48 # If the minimum value is greater than the maximum,
49 # it adds a value to the maximum
50 if valmin[0] >= valmax[0]:
51 valmax[0] = valmin[0] + 1
53 if valmin[1] >= valmax[1]:
54 valmax[1] = valmin[1] + 1
56 if valmin[2] >= valmax[2]:
57 valmax[2] = valmin[2] + 1
59 odata.verts.ensure_lookup_table()
61 random_factor = factor
62 for vertice in listver:
63 odata.verts.ensure_lookup_table()
64 if odata.verts[vertice].select:
65 if vgfilter is True:
66 has_group = getattr(objact.data.vertices[vertice], "groups", None)
67 vertex_group = has_group[0] if has_group else None
68 vertexweight = getattr(vertex_group, "weight", None)
69 if vertexweight:
70 random_factor = factor * vertexweight
71 else:
72 random_factor = factor
73 warn_message = True
75 odata.verts[vertice].co = (
76 (((random.randrange(valmin[0], valmax[0], 1)) * random_factor) / 1000) +
77 odata.verts[vertice].co[0],
78 (((random.randrange(valmin[1], valmax[1], 1)) * random_factor) / 1000) +
79 odata.verts[vertice].co[1],
80 (((random.randrange(valmin[2], valmax[2], 1)) * random_factor) / 1000) +
81 odata.verts[vertice].co[2]
84 if warn_message:
85 self.report({'WARNING'},
86 "Some of the Selected Vertices don't have a Group with Vertex Weight assigned")
87 bpy.ops.object.mode_set(mode=mode)
90 class MESH_OT_random_vertices(Operator):
91 bl_idname = "mesh.random_vertices"
92 bl_label = "Random Vertices"
93 bl_description = ("Randomize the location of vertices by a specified\n"
94 "Multiplier Factor and random values in the defined range\n"
95 "or a multiplication of them and the Vertex Weights")
96 bl_options = {'REGISTER', 'UNDO'}
98 vgfilter: BoolProperty(
99 name="Vertex Group",
100 description="Use Vertex Weight defined in the Active Group",
101 default=False
103 factor: FloatProperty(
104 name="Factor",
105 description="Base Multiplier of the randomization effect",
106 default=1
108 valmin: IntVectorProperty(
109 name="Min XYZ",
110 description="Define the minimum range of randomization values",
111 default=(0, 0, 0)
113 valmax: IntVectorProperty(
114 name="Max XYZ",
115 description="Define the maximum range of randomization values",
116 default=(1, 1, 1)
119 @classmethod
120 def poll(cls, context):
121 return (context.object and context.object.type == "MESH" and
122 context.mode == "EDIT_MESH")
124 def execute(self, context):
125 add_object(self, context, self.valmin, self.valmax, self.factor, self.vgfilter)
127 return {'FINISHED'}
130 # Registration
132 def register():
133 bpy.utils.register_class(MESH_OT_random_vertices)
136 def unregister():
137 bpy.utils.unregister_class(MESH_OT_random_vertices)
140 if __name__ == '__main__':
141 register()