forked from vvoovv/blender-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_scaling.py
233 lines (192 loc) · 7.95 KB
/
target_scaling.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
bl_info = {
"name": "Target Scaling",
"author": "Vladimir Elistratov <[email protected]>",
"modified_by": "Regis Nde Tene",
"version": (1, 0, 6),
"blender": (3, 5, 1),
"location": "View 3D > Edit Mode > Tool Shelf",
"description": "Scale your model to the correct target size",
"warning": "",
"wiki_url": "https://github.com/vvoovv/blender-tools/wiki/Target-Scaling",
"tracker_url": "https://github.com/vvoovv/blender-tools/issues",
"support": "COMMUNITY",
"category": "3D View",
}
import bpy, bmesh, mathutils
class TargetScalingPanel(bpy.types.Panel):
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_context = "mesh_edit"
bl_label = "Target Scaling"
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
col.prop(context.scene, "target_length", text="Target Length")
col.operator("edit.select_target_edge")
col.separator()
col.operator("edit.do_target_scaling")
col.operator("edit.apply_target_scaling")
col.operator("edit.copy_and_scale")
# Add boolean properties for scaling in X, Y, Z
col.prop(context.scene, "scale_x", text="Scale X")
col.prop(context.scene, "scale_y", text="Scale Y")
col.prop(context.scene, "scale_z", text="Scale Z")
def getSelectedEdgeLength(context):
obj = context.active_object
bm = bmesh.from_edit_mesh(obj.data)
edge = bm.select_history.active
l = -1
if isinstance(edge, bmesh.types.BMEdge):
v1 = obj.matrix_world @ edge.verts[0].co
v2 = obj.matrix_world @ edge.verts[1].co
l = (v1 - v2).length
l = round(l, 5)
return l
class SelectTargetEdge(bpy.types.Operator):
bl_idname = "edit.select_target_edge"
bl_label = "Select a target edge"
bl_description = "Select a target edge"
def execute(self, context):
l = getSelectedEdgeLength(context)
if l > 0:
context.scene.target_length = l
self.report({"INFO"}, "The target edge length is {}".format(l))
else:
self.report({"ERROR"}, "Select a single target edge!")
return {"FINISHED"}
class DoTargetScaling(bpy.types.Operator):
bl_idname = "edit.do_target_scaling"
bl_label = "Perform mesh scaling"
bl_description = "Perform whole mesh scaling, so the selected edge will be equal to the target one."
bl_options = {"UNDO"}
def execute(self, context):
l = getSelectedEdgeLength(context)
if l > 0:
if bpy.context.scene.unit_settings.system == 'IMPERIAL':
unit_scale = 0.3048 # 1 meter is 3.28084 feet
else:
unit_scale = 1.0
target_length_in_units = context.scene.target_length * unit_scale
# Determine scaling factors based on user inputs
scale_x = context.scene.scale_x
scale_y = context.scene.scale_y
scale_z = context.scene.scale_z
# Initialize scale factors to 1.0
scale_factors = [1.0, 1.0, 1.0]
# Apply user-defined scaling factors
if scale_x:
scale_factors[0] = 1.0 / l * target_length_in_units
if scale_y:
scale_factors[1] = 1.0 / l * target_length_in_units
if scale_z:
scale_factors[2] = 1.0 / l * target_length_in_units
# Apply the scaling
bpy.ops.transform.resize(value=scale_factors)
bpy.ops.mesh.select_all(action="DESELECT")
else:
self.report({"ERROR"}, "Select a single edge for target scaling!")
return {"FINISHED"}
class ApplyTargetScaling(bpy.types.Operator):
bl_idname = "edit.apply_target_scaling"
bl_label = "Apply target scaling"
bl_description = "Apply target scaling to selected edges."
bl_options = {"UNDO"}
def execute(self, context):
if bpy.context.scene.unit_settings.system == 'IMPERIAL':
unit_scale = 0.3048 # 1 meter is 3.28084 feet
else:
unit_scale = 1.0
target_length_in_units = context.scene.target_length * unit_scale
# Determine scaling factors based on user inputs
scale_x = context.scene.scale_x
scale_y = context.scene.scale_y
scale_z = context.scene.scale_z
# Initialize scale factors to 1.0
scale_factors = [1.0, 1.0, 1.0]
# Apply user-defined scaling factors
if scale_x:
scale_factors[0] = 1.0 / getSelectedEdgeLength(context) * target_length_in_units
if scale_y:
scale_factors[1] = 1.0 / getSelectedEdgeLength(context) * target_length_in_units
if scale_z:
scale_factors[2] = 1.0 / getSelectedEdgeLength(context) * target_length_in_units
# Apply the scaling
bpy.ops.transform.resize(value=scale_factors)
bpy.ops.mesh.select_all(action="DESELECT")
return {"FINISHED"}
class CopyAndScale(bpy.types.Operator):
bl_idname = "edit.copy_and_scale"
bl_label = "Copy and scale"
bl_description = "Create a copy of the object and perform target scaling on it."
bl_options = {"UNDO"}
def execute(self, context):
# Ensure we are in object mode
bpy.ops.object.mode_set(mode='OBJECT')
# duplicate the active object
bpy.ops.object.duplicate()
new_obj = context.active_object
# switch to edit mode on the new object
bpy.ops.object.mode_set(mode='EDIT')
if bpy.context.scene.unit_settings.system == 'IMPERIAL':
unit_scale = 0.3048 # 1 meter is 3.28084 feet
else:
unit_scale = 1.0
target_length_in_units = context.scene.target_length * unit_scale
# Determine scaling factors based on user inputs
scale_x = context.scene.scale_x
scale_y = context.scene.scale_y
scale_z = context.scene.scale_z
# Initialize scale factors to 1.0
scale_factors = [1.0, 1.0, 1.0]
# Apply user-defined scaling factors
if scale_x:
scale_factors[0] = 1.0 / getSelectedEdgeLength(context) * target_length_in_units
if scale_y:
scale_factors[1] = 1.0 / getSelectedEdgeLength(context) * target_length_in_units
if scale_z:
scale_factors[2] = 1.0 / getSelectedEdgeLength(context) * target_length_in_units
# do scaling on the new object
bpy.ops.transform.resize(value=scale_factors)
# deselect everything
bpy.ops.mesh.select_all(action="DESELECT")
# switch back to object mode
bpy.ops.object.mode_set(mode='OBJECT')
# set origin to the center of object's bounding box
bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS', center='BOUNDS')
return {"FINISHED"}
def register():
bpy.types.Scene.target_length = bpy.props.FloatProperty(
name="Target Length",
default=1.0,
min=0.0001,
description="The target edge length for scaling"
)
# Add boolean properties for scaling in X, Y, Z
bpy.types.Scene.scale_x = bpy.props.BoolProperty(
name="Scale X",
default=True,
description="Scale in the X direction"
)
bpy.types.Scene.scale_y = bpy.props.BoolProperty(
name="Scale Y",
default=True,
description="Scale in the Y direction"
)
bpy.types.Scene.scale_z = bpy.props.BoolProperty(
name="Scale Z",
default=True,
description="Scale in the Z direction"
)
classes = (TargetScalingPanel, SelectTargetEdge, DoTargetScaling, ApplyTargetScaling, CopyAndScale)
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
del bpy.types.Scene.target_length
del bpy.types.Scene.scale_x
del bpy.types.Scene.scale_y
del bpy.types.Scene.scale_z
classes = (TargetScalingPanel, SelectTargetEdge, DoTargetScaling, ApplyTargetScaling, CopyAndScale)
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()