2022-04-03 22:48:12 +08:00
|
|
|
import bpy, mathutils
|
|
|
|
from . import UTILS_functions
|
2020-07-19 15:05:43 +08:00
|
|
|
|
2020-09-02 23:04:21 +08:00
|
|
|
class BALLANCE_OT_super_align(bpy.types.Operator):
|
2022-11-19 14:30:31 +08:00
|
|
|
"""Align object with 3ds Max style"""
|
2020-09-02 23:04:21 +08:00
|
|
|
bl_idname = "ballance.super_align"
|
2020-08-31 21:56:08 +08:00
|
|
|
bl_label = "3ds Max Align"
|
2023-01-27 16:28:32 +08:00
|
|
|
bl_options = {'REGISTER', 'UNDO'}
|
2020-07-20 10:13:18 +08:00
|
|
|
|
|
|
|
align_x: bpy.props.BoolProperty(name="X position")
|
|
|
|
align_y: bpy.props.BoolProperty(name="Y position")
|
|
|
|
align_z: bpy.props.BoolProperty(name="Z position")
|
|
|
|
|
|
|
|
current_references: bpy.props.EnumProperty(
|
2022-11-19 14:30:31 +08:00
|
|
|
name="Reference (Active Object)",
|
2020-07-20 10:13:18 +08:00
|
|
|
items=(('MIN', "Min", ""),
|
|
|
|
('CENTER', "Center (bound box)", ""),
|
|
|
|
('POINT', "Center (axis)", ""),
|
|
|
|
('MAX', "Max", "")
|
|
|
|
),
|
2023-01-27 16:28:32 +08:00
|
|
|
default='POINT',
|
|
|
|
)
|
2020-07-20 10:13:18 +08:00
|
|
|
|
|
|
|
target_references: bpy.props.EnumProperty(
|
2022-11-19 14:30:31 +08:00
|
|
|
name="Target (Other Objects)",
|
2020-07-20 10:13:18 +08:00
|
|
|
items=(('MIN', "Min", ""),
|
|
|
|
('CENTER', "Center (bound box)", ""),
|
|
|
|
('POINT', "Center (axis)", ""),
|
|
|
|
('MAX', "Max", "")
|
|
|
|
),
|
2023-01-27 16:28:32 +08:00
|
|
|
default='POINT',
|
|
|
|
)
|
2020-07-20 10:13:18 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(self, context):
|
2022-04-03 22:48:12 +08:00
|
|
|
return _check_align_target()
|
2020-07-20 10:13:18 +08:00
|
|
|
|
|
|
|
def execute(self, context):
|
2022-04-03 22:48:12 +08:00
|
|
|
_align_object(self.align_x, self.align_y, self.align_z, self.current_references, self.target_references)
|
2020-07-20 10:13:18 +08:00
|
|
|
return {'FINISHED'}
|
|
|
|
|
2023-01-27 16:28:32 +08:00
|
|
|
"""
|
2020-07-20 10:13:18 +08:00
|
|
|
def invoke(self, context, event):
|
|
|
|
wm = context.window_manager
|
|
|
|
return wm.invoke_props_dialog(self)
|
2023-01-27 16:28:32 +08:00
|
|
|
"""
|
2020-07-20 10:13:18 +08:00
|
|
|
|
|
|
|
def draw(self, context):
|
|
|
|
layout = self.layout
|
|
|
|
col = layout.column()
|
|
|
|
col.label(text="Align axis")
|
|
|
|
|
|
|
|
row = col.row()
|
|
|
|
row.prop(self, "align_x")
|
|
|
|
row.prop(self, "align_y")
|
|
|
|
row.prop(self, "align_z")
|
|
|
|
|
|
|
|
col.prop(self, "current_references")
|
|
|
|
col.prop(self, "target_references")
|
|
|
|
|
|
|
|
# ============================== method
|
|
|
|
|
2022-04-03 22:48:12 +08:00
|
|
|
def _check_align_target():
|
2020-07-19 15:05:43 +08:00
|
|
|
if bpy.context.active_object is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
selected = bpy.context.selected_objects[:]
|
|
|
|
length = len(selected)
|
|
|
|
if bpy.context.active_object in selected:
|
|
|
|
length -= 1
|
|
|
|
if length == 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2022-04-03 22:48:12 +08:00
|
|
|
def _align_object(use_x, use_y, use_z, currentMode, targetMode):
|
2020-07-19 15:05:43 +08:00
|
|
|
if not (use_x or use_y or use_z):
|
|
|
|
return
|
|
|
|
|
|
|
|
# calc active object data
|
|
|
|
currentObj = bpy.context.active_object
|
|
|
|
currentObjBbox = [currentObj.matrix_world @ mathutils.Vector(corner) for corner in currentObj.bound_box]
|
2022-04-03 22:48:12 +08:00
|
|
|
currentObjRef = _provide_obj_reference_point(currentObj, currentObjBbox, currentMode)
|
2020-07-19 15:05:43 +08:00
|
|
|
|
|
|
|
# calc target
|
|
|
|
targetObjList = bpy.context.selected_objects[:]
|
|
|
|
if currentObj in targetObjList:
|
|
|
|
targetObjList.remove(currentObj)
|
|
|
|
|
|
|
|
# process each obj
|
|
|
|
for targetObj in targetObjList:
|
|
|
|
targetObjBbox = [targetObj.matrix_world @ mathutils.Vector(corner) for corner in targetObj.bound_box]
|
2022-04-03 22:48:12 +08:00
|
|
|
targetObjRef = _provide_obj_reference_point(targetObj, targetObjBbox, targetMode)
|
2020-07-19 15:05:43 +08:00
|
|
|
|
|
|
|
if use_x:
|
|
|
|
targetObj.location.x += currentObjRef.x - targetObjRef.x
|
|
|
|
if use_y:
|
|
|
|
targetObj.location.y += currentObjRef.y - targetObjRef.y
|
|
|
|
if use_z:
|
|
|
|
targetObj.location.z += currentObjRef.z - targetObjRef.z
|
|
|
|
|
2022-04-03 22:48:12 +08:00
|
|
|
def _provide_obj_reference_point(obj, vecList, mode):
|
2020-07-19 15:05:43 +08:00
|
|
|
refPoint = mathutils.Vector((0, 0, 0))
|
|
|
|
|
|
|
|
if (mode == 'MIN'):
|
|
|
|
refPoint.x = min([vec.x for vec in vecList])
|
|
|
|
refPoint.y = min([vec.y for vec in vecList])
|
|
|
|
refPoint.z = min([vec.z for vec in vecList])
|
|
|
|
elif (mode == 'MAX'):
|
|
|
|
refPoint.x = max([vec.x for vec in vecList])
|
|
|
|
refPoint.y = max([vec.y for vec in vecList])
|
|
|
|
refPoint.z = max([vec.z for vec in vecList])
|
|
|
|
elif (mode == 'CENTER'):
|
|
|
|
maxVecCache = mathutils.Vector((0, 0, 0))
|
|
|
|
minVecCache = mathutils.Vector((0, 0, 0))
|
|
|
|
|
|
|
|
minVecCache.x = min([vec.x for vec in vecList])
|
|
|
|
minVecCache.y = min([vec.y for vec in vecList])
|
|
|
|
minVecCache.z = min([vec.z for vec in vecList])
|
|
|
|
maxVecCache.x = max([vec.x for vec in vecList])
|
|
|
|
maxVecCache.y = max([vec.y for vec in vecList])
|
|
|
|
maxVecCache.z = max([vec.z for vec in vecList])
|
|
|
|
|
|
|
|
refPoint.x = (maxVecCache.x + minVecCache.x) / 2
|
|
|
|
refPoint.y = (maxVecCache.y + minVecCache.y) / 2
|
|
|
|
refPoint.z = (maxVecCache.z + minVecCache.z) / 2
|
|
|
|
else:
|
|
|
|
refPoint.x = obj.location.x
|
|
|
|
refPoint.y = obj.location.y
|
|
|
|
refPoint.z = obj.location.z
|
|
|
|
|
|
|
|
return refPoint
|