1 # SPDX-License-Identifier: GPL-2.0-or-later
4 "name": "Random Vertices",
5 "author": "Oscurart, Greg",
8 "location": "Object > Transform > Random Vertices",
9 "description": "Randomize selected components of active object",
17 from bpy
.types
import Operator
20 from bpy
.props
import (
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
31 objact
= bpy
.context
.active_object
36 bpy
.ops
.object.mode_set(mode
='OBJECT')
37 bpy
.ops
.object.mode_set(mode
='EDIT')
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
[:]:
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
:
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)
70 random_factor
= factor
* vertexweight
72 random_factor
= factor
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]
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(
100 description
="Use Vertex Weight defined in the Active Group",
103 factor
: FloatProperty(
105 description
="Base Multiplier of the randomization effect",
108 valmin
: IntVectorProperty(
110 description
="Define the minimum range of randomization values",
113 valmax
: IntVectorProperty(
115 description
="Define the maximum range of randomization values",
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
)
133 bpy
.utils
.register_class(MESH_OT_random_vertices
)
137 bpy
.utils
.unregister_class(MESH_OT_random_vertices
)
140 if __name__
== '__main__':