/
edit_dimensions.py
129 lines (99 loc) · 3.72 KB
/
edit_dimensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright 2016 Jake Dube
#
# ##### BEGIN GPL LICENSE BLOCK ######
# This file is part of MeshTools.
#
# MeshTools 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 3 of the License, or
# (at your option) any later version.
#
# MeshTools 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 MeshTools. If not, see <http://www.gnu.org/licenses/>.
# ##### END GPL LICENSE BLOCK #####
import bpy
from bpy.types import Panel, Operator, PropertyGroup, Scene
from bpy.utils import register_class, unregister_class
from bpy.props import FloatProperty, PointerProperty
bl_info = {
"name": "Mesh Tools",
"author": "Jake Dube",
"version": (1, 0),
"blender": (2, 78, 0),
"location": "View3D > Mesh > Transform > Set Dimensions",
"description": "Sets dimensions for selected vertices.",
"category": "Mesh"}
def calc_bounds():
"""Calculates the bounding box for selected vertices. Requires applied scale to work correctly. """
# for some reason we must change into object mode for the calculations
mode = bpy.context.object.mode
bpy.ops.object.mode_set(mode='OBJECT')
mesh = bpy.context.object.data
verts = [v for v in mesh.vertices if v.select]
# [+x, -x, +y, -y, +z, -z]
v = verts[0].co
bounds = [v.x, v.x, v.y, v.y, v.z, v.z]
for v in verts:
if bounds[0] < v.co.x:
bounds[0] = v.co.x
if bounds[1] > v.co.x:
bounds[1] = v.co.x
if bounds[2] < v.co.y:
bounds[2] = v.co.y
if bounds[3] > v.co.y:
bounds[3] = v.co.y
if bounds[4] < v.co.z:
bounds[4] = v.co.z
if bounds[5] > v.co.z:
bounds[5] = v.co.z
bpy.ops.object.mode_set(mode=mode)
return bounds
def safe_divide(a, b):
if b != 0:
return a / b
return 1
class SetDimensions(Operator):
bl_label = "Set Dimensions"
bl_idname = "view3d.set_dimensions_mt"
bl_description = "Sets dimensions of selected vertices"
bl_options = {'REGISTER', 'UNDO'}
bl_context = "editmode"
new_x = FloatProperty(name="X", min=0, default=1, unit='LENGTH')
new_y = FloatProperty(name="Y", min=0, default=1, unit='LENGTH')
new_z = FloatProperty(name="Z", min=0, default=1, unit='LENGTH')
def invoke(self, context, event):
bounds = calc_bounds()
self.new_x = bounds[0] - bounds[1]
self.new_y = bounds[2] - bounds[3]
self.new_z = bounds[4] - bounds[5]
return {'FINISHED'}
def execute(self, context):
bounds = calc_bounds()
bpy.ops.object.mode_set(mode='EDIT')
x = safe_divide(self.new_x, (bounds[0] - bounds[1]))
y = safe_divide(self.new_y, (bounds[2] - bounds[3]))
z = safe_divide(self.new_z, (bounds[4] - bounds[5]))
bpy.ops.transform.resize(value=(x, y, z))
return {'FINISHED'}
def draw(self, context):
layout = self.layout
box = layout.box()
box.label("New dimensions:")
box.prop(self, "new_x")
box.prop(self, "new_y")
box.prop(self, "new_z")
def add_button(self, context):
self.layout.operator(SetDimensions.bl_idname, icon="PLUGIN")
def register():
register_class(SetDimensions)
bpy.types.VIEW3D_MT_transform.append(add_button)
def unregister():
unregister_class(SetDimensions)
bpy.types.VIEW3D_MT_transform.remove(add_button)
if __name__ == "__main__":
register()