Fix T62695: OBJ mtllib fails when filename contains spaces
[blender-addons.git] / mesh_extra_tools / random_vertices.py
blobb21534dc5a411a0c0eb71162a16cf25a5d48f12b
1 # gpl authors: Oscurart, Greg
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 "wiki_url": "",
12 "category": "Mesh"}
15 import bpy
16 from bpy.types import Operator
17 import random
18 import bmesh
19 from bpy.props import (
20 BoolProperty,
21 FloatProperty,
22 IntVectorProperty,
26 def add_object(self, context, valmin, valmax, factor, vgfilter):
27 # select an option with weight map or not
28 mode = bpy.context.active_object.mode
29 # generate variables
30 objact = bpy.context.active_object
31 listver = []
32 warn_message = False
34 # switch to edit mode
35 bpy.ops.object.mode_set(mode='OBJECT')
36 bpy.ops.object.mode_set(mode='EDIT')
38 # bmesh object
39 odata = bmesh.from_edit_mesh(objact.data)
40 odata.select_flush(False)
42 # if the vertex is selected add to the list
43 for vertice in odata.verts[:]:
44 if vertice.select:
45 listver.append(vertice.index)
47 # If the minimum value is greater than the maximum,
48 # it adds a value to the maximum
49 if valmin[0] >= valmax[0]:
50 valmax[0] = valmin[0] + 1
52 if valmin[1] >= valmax[1]:
53 valmax[1] = valmin[1] + 1
55 if valmin[2] >= valmax[2]:
56 valmax[2] = valmin[2] + 1
58 odata.verts.ensure_lookup_table()
60 random_factor = factor
61 for vertice in listver:
62 odata.verts.ensure_lookup_table()
63 if odata.verts[vertice].select:
64 if vgfilter is True:
65 has_group = getattr(objact.data.vertices[vertice], "groups", None)
66 vertex_group = has_group[0] if has_group else None
67 vertexweight = getattr(vertex_group, "weight", None)
68 if vertexweight:
69 random_factor = factor * vertexweight
70 else:
71 random_factor = factor
72 warn_message = True
74 odata.verts[vertice].co = (
75 (((random.randrange(valmin[0], valmax[0], 1)) * random_factor) / 1000) +
76 odata.verts[vertice].co[0],
77 (((random.randrange(valmin[1], valmax[1], 1)) * random_factor) / 1000) +
78 odata.verts[vertice].co[1],
79 (((random.randrange(valmin[2], valmax[2], 1)) * random_factor) / 1000) +
80 odata.verts[vertice].co[2]
83 if warn_message:
84 self.report({'WARNING'},
85 "Some of the Selected Vertices don't have a Group with Vertex Weight assigned")
86 bpy.ops.object.mode_set(mode=mode)
89 class MESH_OT_random_vertices(Operator):
90 bl_idname = "mesh.random_vertices"
91 bl_label = "Random Vertices"
92 bl_description = ("Randomize the location of vertices by a specified\n"
93 "Multiplier Factor and random values in the defined range\n"
94 "or a multiplication of them and the Vertex Weights")
95 bl_options = {'REGISTER', 'UNDO'}
97 vgfilter: BoolProperty(
98 name="Vertex Group",
99 description="Use Vertex Weight defined in the Active Group",
100 default=False
102 factor: FloatProperty(
103 name="Factor",
104 description="Base Multiplier of the randomization effect",
105 default=1
107 valmin = IntVectorProperty(
108 name="Min XYZ",
109 description="Define the minimum range of randomization values",
110 default=(0, 0, 0)
112 valmax = IntVectorProperty(
113 name="Max XYZ",
114 description="Define the maximum range of randomization values",
115 default=(1, 1, 1)
118 @classmethod
119 def poll(cls, context):
120 return (context.object and context.object.type == "MESH" and
121 context.mode == "EDIT_MESH")
123 def execute(self, context):
124 add_object(self, context, self.valmin, self.valmax, self.factor, self.vgfilter)
126 return {'FINISHED'}
129 # Registration
131 def register():
132 bpy.utils.register_class(MESH_OT_random_vertices)
135 def unregister():
136 bpy.utils.unregister_class(MESH_OT_random_vertices)
139 if __name__ == '__main__':
140 register()