Newer
Older
temp = mid_align.copy()
temp.rotate(rot_val)
arc_pts.append(temp + piv)
elif TransDat.axis_lock is not None:
#if TransDat.axis_lock == 'X':
piv_norm = 0.0, 0.0, 1.0
dis_p_f = (piv - fre).length
dis_p_a = (piv - anc).length
if dis_p_f < dis_p_a:
ratio = 0.5
else: # dis_p_a < dis_p_f:
ratio = dis_p_a / dis_p_f * 0.5
mid_piv_free = piv.lerp(fre, ratio)
arc_pts = [mid_piv_free]
steps = 36
mid_align = mid_piv_free - piv
for a in range(1, steps+1):
rot_val = Quaternion(piv_norm, ang_step * a)
temp = mid_align.copy()
temp.rotate(rot_val)
arc_pts.append(temp + piv)
# Takes a ref_pts (ReferencePoints class) argument and modifies its member
# variable lp_ls (lock pt list). The lp_ls variable is assigned a modified list
# of 3D coordinates (if an axis lock was provided), the contents of the
# ref_pts' rp_ls var (if no axis lock was provided), or an empty list (if there
# wasn't enough ref_pts or there was a problem creating the modified list).
# todo : move inside ReferencePoints class ?
def set_lock_pts(ref_pts, pt_cnt):
if pt_cnt < 2:
TransDat.lock_pts = []
elif TransDat.axis_lock is None:
TransDat.lock_pts = ref_pts
if pt_cnt == 3:
set_arc_pts(ref_pts)
else:
new1 = ref_pts[1].copy()
ptls = [ref_pts[i].co3d for i in range(pt_cnt)] # shorthand
# finds 3D midpoint between 2 supplied coordinates
# axis determines which coordinates are assigned midpoint values
# if X, Anchor is [AncX, MidY, MidZ] and Free is [FreeX, MidY, MidZ]
if pt_cnt == 2: # translate
new0 = ref_pts[0].copy()
mid3d = ptls[0].lerp(ptls[1], 0.5)
new0.co3d = Vector([ ptls[0][0], mid3d[1], mid3d[2] ])
new1.co3d = Vector([ ptls[1][0], mid3d[1], mid3d[2] ])
new0.co3d = Vector([ mid3d[0], ptls[0][1], mid3d[2] ])
new1.co3d = Vector([ mid3d[0], ptls[1][1], mid3d[2] ])
new0.co3d = Vector([ mid3d[0], mid3d[1], ptls[0][2] ])
new1.co3d = Vector([ mid3d[0], mid3d[1], ptls[1][2] ])
if not vec3s_alm_eq(new0.co3d, new1.co3d):
# axis determines which of the Free's coordinates are assigned
# to Anchor and Pivot coordinates eg:
# if X, Anchor is [FreeX, AncY, AncZ] and Pivot is [FreeX, PivY, PivZ]
elif pt_cnt == 3: # rotate
new2 = ref_pts[2].copy()
mov_co = ref_pts[0].co3d.copy()
new1.co3d = Vector([ mov_co[0], ptls[1][1], ptls[1][2] ])
new2.co3d = Vector([ mov_co[0], ptls[2][1], ptls[2][2] ])
new1.co3d = Vector([ ptls[1][0], mov_co[1], ptls[1][2] ])
new2.co3d = Vector([ ptls[2][0], mov_co[1], ptls[2][2] ])
new1.co3d = Vector([ ptls[1][0], ptls[1][1], mov_co[2] ])
new2.co3d = Vector([ ptls[2][0], ptls[2][1], mov_co[2] ])
if not vec3s_alm_eq(new1.co3d, new2.co3d) and \
not vec3s_alm_eq(new1.co3d, mov_co) and \
not vec3s_alm_eq(new2.co3d, mov_co):
#new0 = ReferencePoint("piv", Colr.blue, mov_co)
new0 = ReferencePoint("fre", Colr.green, mov_co)
set_arc_pts([new0, new1, new2])
else:
set_arc_pts(ref_pts)
# Takes new_co (Vector) and old_co (Vector) as arguments. Calculates
# difference between the 3D locations in new_co and old_co to determine
# the translation to apply to the selected geometry.
def do_translation(new_co, old_co):
co_chg = -(old_co - new_co)
bpy.ops.transform.translate(value=co_chg)
# Performs a scale transformation using the provided s_fac (scale factor)
# argument. The scale factor is the result from dividing the user input
# measure (new_meas_stor) by the distance between the Anchor and Free
# (curr_meas_stor). After the scale is performed, settings are returned to
# their "pre-scaled" state.
# takes: ref_pts (ReferencePoints), s_fac (float)
def do_scale(ref_pts, s_fac):
# back up settings before changing them
piv_back = deepcopy(bpy.context.tool_settings.transform_pivot_point)
curs_back = bpy.context.scene.cursor.location.copy()
bpy.context.tool_settings.transform_pivot_point = 'CURSOR'
bpy.context.scene.cursor.location = ref_pts[1].co3d.copy()
ax_multip, cnstrt_bls = (s_fac, s_fac, s_fac), (True, True, True)
ax_multip, cnstrt_bls = (s_fac, 1, 1), (True, False, False)
ax_multip, cnstrt_bls = (1, s_fac, 1), (False, True, False)
ax_multip, cnstrt_bls = (1, 1, s_fac), (False, False, True)
bpy.ops.transform.resize(value=ax_multip, constraint_axis=cnstrt_bls)
# restore settings back to their pre "do_scale" state
bpy.context.scene.cursor.location = curs_back.copy()
bpy.context.tool_settings.transform_pivot_point = deepcopy(piv_back)
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
# end_a, piv_pt, and end_b are Vector based 3D coordinates
# coordinates must share a common center "pivot" point (piv_pt)
def get_line_ang_3d(end_a, piv_pt, end_b):
algn_a = end_a - piv_pt
algn_b = end_b - piv_pt
return algn_a.angle(algn_b)
# Checks if the 3 Vector coordinate arguments (end_a, piv_pt, end_b)
# will create an angle with a measurement matching the value in the
# argument exp_ang (expected angle measurement).
def ang_match3d(end_a, piv_pt, end_b, exp_ang):
ang_meas = get_line_ang_3d(end_a, piv_pt, end_b)
#print("end_a", end_a) # debug
#print("piv_pt", piv_pt) # debug
#print("end_b", end_b) # debug
#print("exp_ang ", exp_ang) # debug
#print("ang_meas ", ang_meas) # debug
return flts_alm_eq(ang_meas, exp_ang)
# Calculates rotation around axis or face normal at Pivot's location.
# Takes two 3D coordinate Vectors (piv_co and mov_co), rotation angle in
# radians (ang_diff_rad), and rotation data storage object (rot_dat).
# Aligns mov_co to world origin (0, 0, 0) and rotates aligned
# mov_co (mov_aligned) around axis stored in rot_dat. After rotation,
# removes world-origin alignment.
def get_rotated_pt(piv_co, ang_diff_rad, mov_co):
mov_aligned = mov_co - piv_co
rot_val, axis_lock = [], TransDat.axis_lock
if axis_lock is None: # arbitrary axis / spherical rotations
#print(' RotDat.piv_norm', RotDat.piv_norm, # debug
# '\n ang_diff_rad', ang_diff_rad) # debug
rot_val = Quaternion(TransDat.piv_norm, ang_diff_rad)
elif axis_lock == 'X':
rot_val = Euler((ang_diff_rad, 0.0, 0.0), 'XYZ')
elif axis_lock == 'Y':
rot_val = Euler((0.0, ang_diff_rad, 0.0), 'XYZ')
elif axis_lock == 'Z':
rot_val = Euler((0.0, 0.0, ang_diff_rad), 'XYZ')
mov_aligned.rotate(rot_val)
return mov_aligned + piv_co
# Finds out whether positive TransDat.new_ang_r or negative TransDat.new_ang_r
# will result in the desired rotation angle.
def find_correct_rot(ref_pts, pt_cnt):
ang_diff_rad, new_ang_rad = TransDat.ang_diff_r, TransDat.new_ang_r
piv_pt, move_pt = ref_pts[2].co3d, ref_pts[0].co3d
t_co_pos = get_rotated_pt(piv_pt, ang_diff_rad, move_pt)
t_co_neg = get_rotated_pt(piv_pt,-ang_diff_rad, move_pt)
set_lock_pts(ref_pts, pt_cnt)
if ang_match3d(lock_pts[1].co3d, lock_pts[2].co3d, t_co_pos, new_ang_rad):
#print("matched t_co_pos:", t_co_pos, ang_diff_rad)
return t_co_pos, ang_diff_rad
else:
#print("matched t_co_neg:", t_co_neg, -ang_diff_rad)
return t_co_neg, -ang_diff_rad
# Takes 2D Pivot Point (piv) for piv to temp lines, 2 possible rotation
# coordinates to choose between (rot_co3d_pos, rot_co3d_neg), and a
# 2D mouse location (mouse_co) for determining which rotation coordinate
# is closest to the cursor.
# Returns the rotation coordinate closest to the 2d mouse position and the
# rotation angles used to obtain the coordinates (rot_ang_rad).
# rot_co3d_pos == rotated coordinate positive, rot_co3d_neg == rot coor Negative
# todo : make rot_pos_co2d and rot_neg_co2d VertObj types ?
#def choose_0_or_180(piv, rot_co3d_pos, rot_pos_ang_rad, rot_co3d_neg, r_n_ang_r, mouse_co):
def choose_0_or_180(piv, rot_co3d_pos, rot_co3d_neg, rot_ang_rad, mouse_co):
#global reg_rv3d
#region, rv3d = reg_rv3d[0], reg_rv3d[1]
region = bpy.context.region
rv3d = bpy.context.region_data
rot_pos_co2d = loc3d_to_reg2d(region, rv3d, rot_co3d_pos)
rot_neg_co2d = loc3d_to_reg2d(region, rv3d, rot_co3d_neg)
ms_co_1_dis = (rot_pos_co2d - mouse_co).length
ms_co_2_dis = (rot_neg_co2d - mouse_co).length
# draw both buttons and show which is closer to mouse
psize_small, psize_large = 8, 14
if ms_co_1_dis < ms_co_2_dis:
draw_line_2d(piv2d, rot_pos_co2d, Colr.green)
draw_pt_2d(rot_pos_co2d, Colr.green, psize_large)
draw_pt_2d(rot_neg_co2d, Colr.grey, psize_small)
return rot_co3d_pos, rot_ang_rad
draw_line_2d(piv2d, rot_neg_co2d, Colr.green)
draw_pt_2d(rot_neg_co2d, Colr.green, psize_large)
draw_pt_2d(rot_pos_co2d, Colr.grey, psize_small)
return rot_co3d_neg, -rot_ang_rad
draw_pt_2d(rot_pos_co2d, Colr.grey, psize_small)
draw_pt_2d(rot_neg_co2d, Colr.grey, psize_small)
return None, None
# Reduces the provided rotation amount (new_ms_stor) to an "equivalent" value
# less than or equal to 180 degrees. Calculates the angle offset from
# curr_ms_stor to achieve a new_ms_stor value.
def prep_rotation_info(curr_ms_stor, new_ms_stor):
# workaround for negative angles and angles over 360 degrees
if new_ms_stor < 0 or new_ms_stor > 360:
new_ms_stor = new_ms_stor % 360
# fix for angles over 180 degrees
if new_ms_stor > 180:
TransDat.new_ang_r = radians(180 - (new_ms_stor % 180))
TransDat.new_ang_r = radians(new_ms_stor)
#print("TransDat.new_ang_r", TransDat.new_ang_r)
TransDat.ang_diff_r = radians(new_ms_stor - curr_ms_stor)
def create_z_orient(rot_vec):
x_dir_p = Vector(( 1.0, 0.0, 0.0))
y_dir_p = Vector(( 0.0, 1.0, 0.0))
z_dir_p = Vector(( 0.0, 0.0, 1.0))
if flt_lists_alm_eq(rot_vec, (0.0, 0.0, 0.0)) or \
flt_lists_alm_eq(rot_vec, z_dir_p):
return Matrix((x_dir_p, y_dir_p, z_dir_p)) # 3x3 identity
new_z = rot_vec.copy() # rot_vec already normalized
if flt_lists_alm_eq(new_y, (0.0, 0.0, 0.0)):
new_y = y_dir_p
new_x = new_y.cross(new_z)
new_x.normalize()
new_y.normalize()
return Matrix(((new_x.x, new_y.x, new_z.x),
(new_x.y, new_y.y, new_z.y),
(new_x.z, new_y.z, new_z.z)))
# Uses axis_lock or piv_norm from TransDat to obtain rotation axis.
# Then rotates selected objects or selected vertices around the
# 3D cursor using TransDat's ang_diff_r radian value.
pivot = pivot_co.copy()
constr_ax = False, False, False
#rot_matr = Matrix.Rotation(TransDat.ang_diff_r, 4, TransDat.piv_norm)
norml = TransDat.piv_norm
o_mat = create_z_orient(norml)
bpy.ops.transform.rotate(
orient_axis='Z',
orient_type='LOCAL',
#orient_type='GLOBAL',
orient_matrix=o_mat,
orient_matrix_type='LOCAL',
center_override=pivot,
else:
# back up settings before changing them
piv_back = deepcopy(bpy.context.tool_settings.transform_pivot_point)
bpy.context.tool_settings.transform_pivot_point = 'CURSOR'
curs_loc_back = bpy.context.scene.cursor.location.copy()
bpy.context.scene.cursor.location = pivot.copy()
'''
if axis_lock == 'X': constr_ax = True, False, False
elif axis_lock == 'Y': constr_ax = False, True, False
elif axis_lock == 'Z': constr_ax = False, False, True
bpy.ops.transform.rotate(value=-TransDat.ang_diff_r, orient_axis=axis_lock,
center_override=pivot.copy(), constraint_axis=constr_ax)
# restore settings back to their pre "do_rotate" state
bpy.context.scene.cursor.location = curs_loc_back.copy()
bpy.context.tool_settings.transform_pivot_point = deepcopy(piv_back)
editmode_refresh()
# Uses axis_lock or piv_norm from TransDat to obtain rotation axis.
# Then rotates selected objects or selected vertices around the
# 3D cursor using TransDat's ang_diff_r radian value.
piv_back = deepcopy(bpy.context.tool_settings.transform_pivot_point)
curs_back = bpy.context.scene.cursor.location.copy()
bpy.context.tool_settings.transform_pivot_point = 'CURSOR'
bpy.context.scene.cursor.location = self.pts[2].co3d.copy()
ops_lock = () # axis lock data for bpy.ops.transform
if axis_lock is None: ops_lock = TransDat.piv_norm
elif axis_lock == 'X': ops_lock = 1, 0, 0
elif axis_lock == 'Y': ops_lock = 0, 1, 0
elif axis_lock == 'Z': ops_lock = 0, 0, 1
bpy.ops.transform.rotate(value=TransDat.ang_diff_r, axis=ops_lock,
constraint_axis=(False, False, False))
# restore settings back to their pre "do_rotate" state
bpy.context.scene.cursor.location = curs_back.copy()
bpy.context.tool_settings.transform_pivot_point = deepcopy(piv_back)
# Updates lock points and changes curr_meas_stor to use measure based on
# lock points instead of ref_pts (for axis constrained transformations).
def updatelock_pts(self, ref_pts):
global curr_meas_stor
set_lock_pts(ref_pts, self.pt_cnt)
if TransDat.lock_pts == []:
if TransDat.axis_lock is not None:
self.report({'ERROR'}, 'Axis lock \''+ TransDat.axis_lock+
TransDat.lock_pts = ref_pts
TransDat.axis_lock = None
if self.pt_cnt < 2:
curr_meas_stor = 0.0
elif self.pt_cnt == 2:
curr_meas_stor = (lk_pts[0].co3d - lk_pts[1].co3d).length
elif self.pt_cnt == 3:
line_ang_r = get_line_ang_3d(lk_pts[1].co3d, lk_pts[2].co3d, lk_pts[0].co3d)
curr_meas_stor = degrees(line_ang_r)
# See if key was pressed that would require updating the axis lock info.
# If one was, update the lock points to use new info.
def axis_key_check(self, new_axis):
if self.pt_cnt > 1:
if new_axis != TransDat.axis_lock:
TransDat.axis_lock = new_axis
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
updatelock_pts(self, self.pts)
set_meas_btn(self)
# Adjusts settings so proc_click can run again for next possible transform
def reset_settings(self):
#print("reset_settings") # debug
global new_meas_stor
new_meas_stor = None
self.new_free_co = ()
self.mouse_co = Vector((-9900, -9900))
editmode_refresh()
if self.pt_cnt < 2:
self.meas_btn.is_drawn = False
set_lock_pts(self.pts, self.pt_cnt)
else:
updatelock_pts(self, self.pts)
self.meas_btn.is_drawn = True
set_meas_btn(self)
#self.snap_btn_act = True
self.addon_mode = CLICK_CHECK
# restore selected items (except Anchor)
# needed so GRABONLY and SLOW3DTO2D update selection correctly
#self.sel_backup.restore_selected()
# make sure last transform didn't cause points to overlap
if vec3s_alm_eq(self.pts[0].co3d, self.pts[1].co3d):
self.report({'ERROR'}, 'Free and Anchor share same location.')
# reset ref pt data
self.pt_cnt = 0
self.menu.change_menu(self.pt_cnt)
init_ref_pts(self)
self.highlight_mouse = True
#if self.pt_find_md == GRABONLY:
# create_snap_pt(self.left_click_co, self.sel_backup)
# runs transformation functions depending on which options are set.
# transform functions cannot be called directly due to use of pop-up for
# getting user input
def do_transform(self):
#print("do_transform") # debug
global curr_meas_stor, new_meas_stor
# Onto Transformations...
if self.transf_type == MOVE:
new_coor = get_new_3d_co(self, curr_meas_stor, new_meas_stor)
if new_coor is not None:
do_translation(new_coor, self.pts[0].co3d)
self.pts[0].co3d = new_coor.copy()
reset_settings(self)
elif self.transf_type == SCALE:
new_coor = get_new_3d_co(self, curr_meas_stor, new_meas_stor)
if new_coor is not None:
scale_factor = new_meas_stor / curr_meas_stor
do_scale(self.pts, scale_factor)
self.pts[0].co3d = new_coor.copy()
reset_settings(self)
elif self.transf_type == ROTATE:
self.pts[0].co3d = self.new_free_co.copy()
reset_settings(self)
# Run after XEDIT_OT_meas_inp_dlg pop-up disables popup_active.
# Checks to see if a valid number was input into the pop-up dialog and
# determines what to do based on what value was supplied to the pop-up.
def process_popup_input(self):
global curr_meas_stor, new_meas_stor
#print("process_popup_input") # debug
#print("curr_meas_stor", curr_meas_stor, " new_meas_stor", new_meas_stor) # debug
if new_meas_stor is not None:
self.addon_mode = DO_TRANSFORM
if self.transf_type == MOVE:
do_transform(self)
elif self.transf_type == SCALE:
do_transform(self)
elif self.transf_type == ROTATE:
prep_rotation_info(curr_meas_stor, new_meas_stor)
# if angle is flat...
if flts_alm_eq(curr_meas_stor, 0.0) or \
flts_alm_eq(curr_meas_stor, 180.0):
piv, mov = self.pts[2].co3d, self.pts[0].co3d
if flts_alm_eq(new_meas_stor, 0.0) or \
flts_alm_eq(new_meas_stor, 180.0):
self.new_free_co = get_rotated_pt(piv, ang_rad, mov)
do_transform(self)
else:
TransDat.rot_pt_pos = get_rotated_pt(piv, ang_rad, mov)
TransDat.rot_pt_neg = get_rotated_pt(piv, -ang_rad, mov)
self.addon_mode = GET_0_OR_180
else: # non-flat angle
self.new_free_co, TransDat.ang_diff_r = \
find_correct_rot(self.pts, self.pt_cnt)
do_transform(self)
else:
reset_settings(self)
def draw_rot_arc(colr):
reg = bpy.context.region
rv3d = bpy.context.region_data
last = loc3d_to_reg2d(reg, rv3d, TransDat.arc_pts[0])
p2d = loc3d_to_reg2d(reg, rv3d, TransDat.arc_pts[p])
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
draw_line_2d(last, p2d, Colr.white)
last = p2d
# Called when add-on mode changes and every time point is added or removed.
def set_help_text(self, mode):
text = ""
if mode == "CLICK":
if self.pt_cnt == 0:
text = "ESC/LMB+RMB - exits add-on, LMB - add ref point"
elif self.pt_cnt == 1:
text = "ESC/LMB+RMB - exits add-on, LMB - add/remove ref points, G - grab point, SHIFT+LMB enter mid point mode"
elif self.pt_cnt == 2:
text = "ESC/LMB+RMB - exits add-on, LMB - add/remove ref points, X/Y/Z - set axis lock, C - clear axis lock, G - grab point, SHIFT+LMB enter mid point mode, UP/DOWN - change tranform mode"
else: # self.pt_cnt == 3
text = "ESC/LMB+RMB - exits add-on, LMB - remove ref points, X/Y/Z - set axis lock, C - clear axis lock, G - grab point, SHIFT+LMB enter mid point mode, UP/DOWN - change tranform mode"
elif mode == "MULTI":
text = "ESC/LMB+RMB - exits add-on, SHIFT+LMB exit mid point mode, LMB - add/remove point"
elif mode == "GRAB":
text = "ESC/LMB+RMB - exits add-on, G - cancel grab, LMB - place/swap ref points"
elif mode == "POPUP":
text = "ESC/LMB+RMB - exits add-on, LMB/RMB (outside pop-up) - cancel pop-up input"
bpy.context.area.header_text_set(text)
# todo : move most of below to mouse_co update in modal?
def draw_callback_px(self, context):
reg = bpy.context.region
rv3d = bpy.context.region_data
ptsz_lrg = 20
ptsz_sml = 10
add_rm_co = Vector((self.rtoolsw, 0))
self.add_rm_btn.draw_btn(add_rm_co, self.mouse_co, self.shift_held)
# allow appending None so indexing does not get messed up
# causing potential false positive for overlap
pts2d = [p.get_co2d() for p in self.pts]
ms_colr = Colr.yellow
if self.pt_cnt < 3:
ms_colr = self.pts[self.pt_cnt].colr
lk_pts2d = None # lock points 2D
self.meas_btn.is_drawn = False # todo : cleaner btn activation
# note, can't chain above if-elif block in with one below as
# it breaks axis lock drawing
if self.grab_pt is not None: # not enabled if mod_pt active
line_beg = pts2d[self.grab_pt] # backup orignal co for move line
pts2d[self.grab_pt] = None # prevent check on grabbed pt
closest_pt, self.overlap_idx = closest_to_point(self.mouse_co, pts2d)
pts2d[self.grab_pt] = self.mouse_co
ms_colr = self.pts[self.grab_pt].colr
if not self.shift_held:
draw_line_2d(line_beg, self.mouse_co, self.pts[self.grab_pt].colr)
draw_pt_2d(closest_pt, Colr.white, ptsz_lrg)
elif self.mod_pt is not None:
ms_colr = self.pts[self.mod_pt].colr
m_pts2d = [loc3d_to_reg2d(reg, rv3d, p) for p in self.multi_tmp.ls]
closest_pt, self.overlap_idx = closest_to_point(self.mouse_co, m_pts2d)
draw_pt_2d(pts2d[self.mod_pt], Colr.white, ptsz_lrg)
if self.shift_held:
draw_pt_2d(self.mouse_co, Colr.black, ptsz_lrg)
if len(m_pts2d) > 1:
for mp in m_pts2d:
draw_pt_2d(mp, Colr.black, ptsz_lrg)
else:
draw_pt_2d(closest_pt, Colr.black, ptsz_lrg)
if len(m_pts2d) > 1:
for p in m_pts2d:
draw_pt_2d(p, ms_colr, ptsz_sml)
last_mod_pt = loc3d_to_reg2d(reg, rv3d, self.multi_tmp.ls[-1])
draw_line_2d(last_mod_pt, self.mouse_co, self.pts[self.mod_pt].colr)
else: # "Normal" mode
closest_pt, self.overlap_idx = closest_to_point(self.mouse_co, pts2d)
lin_p = pts2d
if self.shift_held:
draw_pt_2d(closest_pt, Colr.white, ptsz_lrg)
else:
draw_pt_2d(closest_pt, Colr.black, ptsz_lrg)
if TransDat.axis_lock is not None:
lk_pts2d = [p.get_co2d() for p in TransDat.lock_pts]
dpi = bpy.context.preferences.system.dpi
font_id, txt_sz = 0, 32
x_pos, y_pos = self.rtoolsw + 80, 36
blf.size(font_id, txt_sz, dpi)
blf.position(font_id, x_pos, y_pos, 0)
if self.pt_cnt == 2:
draw_line_2d(lin_p[0], lin_p[1], Colr.white)
if None not in (lin_p[0], lin_p[1]):
btn_co = lin_p[0].lerp(lin_p[1], 0.5)
self.meas_btn.draw_btn(btn_co, self.mouse_co)
self.meas_btn.is_drawn = True
elif self.pt_cnt == 3:
draw_rot_arc(self.pts[2].colr)
draw_line_2d(lin_p[0], lin_p[2], Colr.white)
draw_line_2d(lin_p[1], lin_p[2], Colr.white)
self.meas_btn.draw_btn(lin_p[2], self.mouse_co)
self.meas_btn.is_drawn = True
# draw reference points
for p in range(self.pt_cnt):
draw_pt_2d(pts2d[p], self.pts[p].colr, ptsz_sml)
# draw lock points
if lk_pts2d is not None:
for p in range(lp_cnt):
draw_pt_2d(lk_pts2d[p], self.pts[p].colr, ptsz_sml)
if self.highlight_mouse:
draw_pt_2d(self.mouse_co, ms_colr, ptsz_sml)
# draw mode selection menu
self.menu.draw(self.meas_btn.is_drawn)
def exit_addon(self):
restore_blender_settings(self.settings_backup)
bpy.context.area.header_text_set(None)
# todo : reset openGL settings?
#bgl.glColor4f()
#blf.size()
#blf.position()
#print("\n\nAdd-On Exited\n") # debug
# Checks if "use_region_overlap" is enabled and X offset is needed.
def get_reg_overlap():
rtoolsw = 0 # region tools (toolbar) width
#ruiw = 0 # region ui (Number/n-panel) width
system = bpy.context.preferences.system
area = bpy.context.area
for r in area.regions:
if r.type == 'TOOLS':
rtoolsw = r.width
#elif r.type == 'UI':
# ruiw = r.width
#return rtoolsw, ruiw
return rtoolsw
class XEDIT_OT_set_meas(bpy.types.Operator):
# Only launch Add-On from OBJECT or EDIT modes
@classmethod
def poll(self, context):
return context.mode == 'OBJECT' or context.mode == 'EDIT_MESH'
def modal(self, context, event):
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
context.area.tag_redraw()
if event.type in {'A', 'MIDDLEMOUSE', 'WHEELUPMOUSE',
'WHEELDOWNMOUSE', 'NUMPAD_1', 'NUMPAD_2', 'NUMPAD_3', 'NUMPAD_4',
'NUMPAD_6', 'NUMPAD_7', 'NUMPAD_8', 'NUMPAD_9', 'NUMPAD_0', 'TAB'}:
return {'PASS_THROUGH'}
if event.type == 'MOUSEMOVE':
self.mouse_co = Vector((event.mouse_region_x, event.mouse_region_y))
if event.type in {'LEFT_SHIFT', 'RIGHT_SHIFT'}:
if event.value == 'PRESS':
self.shift_held = True
#print("\nShift pressed") # debug
elif event.value == 'RELEASE':
self.shift_held = False
#print("\nShift released") # debug
if event.type == 'RIGHTMOUSE' and event.value == 'PRESS':
if self.lmb_held:
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
exit_addon(self)
return {'CANCELLED'}
else:
return {'PASS_THROUGH'}
if event.type == 'LEFTMOUSE' and event.value == 'PRESS':
self.lmb_held = True
elif event.type == 'UP_ARROW' and event.value == 'RELEASE':
if self.meas_btn.is_drawn:
self.menu.update_active(-1)
elif event.type == 'DOWN_ARROW' and event.value == 'RELEASE':
if self.meas_btn.is_drawn:
self.menu.update_active( 1)
elif event.type in {'RET', 'LEFTMOUSE'} and event.value == 'RELEASE':
# prevent click/enter that launched add-on from doing anything
if self.first_run:
self.first_run = False
return {'RUNNING_MODAL'}
if event.type == 'LEFTMOUSE':
self.lmb_held = False
#print("LeftMouse released") # debug
self.mouse_co = Vector((event.mouse_region_x, event.mouse_region_y))
#===========================
# Check for 0 or 180 click
#===========================
if self.addon_mode == GET_0_OR_180:
self.new_free_co, TransDat.ang_diff_r = choose_0_or_180(
self.pts[2], TransDat.rot_pt_pos, TransDat.rot_pt_neg,
TransDat.ang_diff_r, self.mouse_co
)
self.addon_mode = DO_TRANSFORM # todo : find why this needed
do_transform(self)
#===================================
# Check for click on Measure Button
#===================================
elif self.meas_btn.is_drawn and self.meas_btn.ms_over:
#print("\nMeas Button Clicked")
if can_transf(self):
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
self.addon_mode = WAIT_FOR_POPUP
popup_active = True
set_help_text(self, "POPUP")
bpy.ops.object.ms_input_dialog_op('INVOKE_DEFAULT')
#===========================================
# Check for click on "Add Selected" Button
#===========================================
elif self.add_rm_btn.ms_over:
if self.mod_pt is not None:
if not self.shift_held:
add_select_multi(self)
else:
if self.pt_cnt < 3:
new_select_multi(self)
exit_multi_mode(self)
self.menu.change_menu(self.pt_cnt)
elif self.grab_pt is not None:
co3d = None
if bpy.context.mode == "OBJECT":
if len(bpy.context.selected_objects) > 0:
if not self.shift_held:
co3d = bpy.context.selected_objects[0].location
else:
new_select_multi(self)
exit_multi_mode(self)
self.menu.change_menu(self.pt_cnt)
elif bpy.context.mode == "EDIT_MESH":
m_w = bpy.context.edit_object.matrix_world
bm = bmesh.from_edit_mesh(bpy.context.edit_object.data)
if len(bm.select_history) > 0:
if not self.shift_held:
for sel in bm.select_history:
if type(sel) is bmesh.types.BMVert:
break
elif type(sel) is bmesh.types.BMEdge or \
type(sel) is bmesh.types.BMFace:
co3d = Vector()
for v in sel.verts:
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
co3d = co3d / len(sel.verts)
break
else:
new_select_multi(self)
exit_multi_mode(self)
self.menu.change_menu(self.pt_cnt)
if co3d is not None:
if not in_ref_pts(self, co3d):
self.pts[self.grab_pt].co3d = co3d
else:
swap_ref_pts(self, self.grab_pt, self.swap_pt)
self.swap_pt = None
self.grab_pt = None
updatelock_pts(self, self.pts)
set_meas_btn(self)
else: # no grab or mod point
if self.shift_held:
if self.pt_cnt < 3:
new_select_multi(self)
if in_ref_pts(self, self.multi_tmp.get_co(), self.mod_pt):
self.report({'WARNING'}, 'Points overlap.')
self.pts[self.mod_pt].co3d = self.multi_tmp.get_co()
self.menu.change_menu(self.pt_cnt)
else:
add_select(self)
# todo : see if this is really a good solution...
if self.mod_pt is None:
set_help_text(self, "CLICK")
else:
set_help_text(self, "MULTI")
#===========================
# Point Place or Grab Mode
#===========================
elif self.mod_pt is None:
if self.overlap_idx is None: # no point overlap
if not self.shift_held:
if self.grab_pt is not None:
found_pt = find_closest_point(self.mouse_co)
if found_pt is not None:
if not in_ref_pts(self, found_pt):
self.pts[self.grab_pt].co3d = found_pt
self.grab_pt = None
if self.pt_cnt > 1:
updatelock_pts(self, self.pts)
set_mouse_highlight(self)
set_meas_btn(self)
set_help_text(self, "CLICK")
elif self.pt_cnt < 3:
found_pt = find_closest_point(self.mouse_co)
if found_pt is not None:
if not in_ref_pts(self, found_pt):
self.pts[self.pt_cnt].co3d = found_pt
self.pt_cnt += 1
self.menu.change_menu(self.pt_cnt)
if self.pt_cnt > 1:
updatelock_pts(self, self.pts)
#if self.pt_cnt
set_mouse_highlight(self)
set_meas_btn(self)
set_help_text(self, "CLICK")
''' Begin Debug
cnt = self.pt_cnt - 1
pt_fnd_str = str(self.pts[cnt].co3d)
pt_fnd_str = pt_fnd_str.replace("<Vector ", "Vector(")
pt_fnd_str = pt_fnd_str.replace(">", ")")
print("ref_pt_" + str(cnt) + ' =', pt_fnd_str)
#print("ref pt added:", self.cnt, "cnt:", self.cnt+1)
End Debug '''
else: # overlap
if self.grab_pt is not None:
if not self.shift_held:
if self.grab_pt != self.overlap_idx:
swap_ref_pts(self, self.grab_pt, self.overlap_idx)
set_meas_btn(self)
self.grab_pt = None
if self.pt_cnt > 1:
updatelock_pts(self, self.pts)
set_mouse_highlight(self)
set_meas_btn(self)
set_help_text(self, "CLICK")
elif not self.shift_held:
# overlap and shift not held == remove point
rem_ref_pt(self, self.overlap_idx)
set_meas_btn(self)
set_help_text(self, "CLICK")
else: # shift_held
# enable multi point mode
self.mod_pt = self.overlap_idx
self.multi_tmp.reset(self.pts[self.mod_pt].co3d)
self.highlight_mouse = True
set_help_text(self, "MULTI")
#===========================
# Mod Ref Point Mode
#===========================
else: # mod_pt exists
if self.overlap_idx is None: # no point overlap
if not self.shift_held:
# attempt to add new point to multi_tmp
found_pt = find_closest_point(self.mouse_co)
if found_pt is not None:
self.multi_tmp.try_add(found_pt)
mult_co3d = self.multi_tmp.get_co()
if in_ref_pts(self, mult_co3d, self.mod_pt):
self.report({'WARNING'}, 'Points overlap.')
self.pts[self.mod_pt].co3d = mult_co3d
else: # shift_held, exit multi_tmp
exit_multi_mode(self)
else: # overlap multi_tmp
if not self.shift_held:
# remove multi_tmp point
self.multi_tmp.rem_pt(self.overlap_idx)
# if all multi_tmp points removed,
# exit multi mode, remove edited point
if self.multi_tmp.co3d is None:
rem_ref_pt(self, self.mod_pt)
self.mod_pt = None
set_meas_btn(self)
set_help_text(self, "CLICK")
elif in_ref_pts(self, self.multi_tmp.co3d, self.mod_pt):
self.report({'WARNING'}, 'Points overlap.')
self.pts[self.mod_pt].co3d = self.multi_tmp.get_co()
else:
self.pts[self.mod_pt].co3d = self.multi_tmp.get_co()
else: # shift_held
exit_multi_mode(self)
if event.type == 'C' and event.value == 'PRESS':
#print("Pressed C\n") # debug
axis_key_check(self, None)
elif event.type == 'X' and event.value == 'PRESS':
#print("Pressed X\n") # debug
axis_key_check(self, 'X')
elif event.type == 'Y' and event.value == 'PRESS':
#print("Pressed Y\n") # debug
axis_key_check(self, 'Y')
elif event.type == 'Z' and event.value == 'PRESS':
#print("Pressed Z\n") # debug
axis_key_check(self, 'Z')
'''
elif event.type == 'D' and event.value == 'RELEASE':
# open debug console
__import__('code').interact(local=dict(globals(), **locals()))
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
'''
elif event.type == 'G' and event.value == 'RELEASE':
# if already in grab mode, cancel grab
if self.grab_pt is not None:
self.grab_pt = None
set_mouse_highlight(self)
set_help_text(self, "CLICK")
# else enable grab mode (if possible)
elif self.mod_pt is None:
if self.overlap_idx is not None:
self.grab_pt = self.overlap_idx
self.highlight_mouse = False
set_help_text(self, "GRAB")
elif event.type in {'ESC'} and event.value == 'RELEASE':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
exit_addon(self)
return {'CANCELLED'}
if self.force_quit:
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
exit_addon(self)
return {'FINISHED'}
# if the addon_mode is WAIT_FOR_POPUP, wait on POPUP to disable
# popup_active, then run process_popup_input
# would prefer not to do pop-up check inside draw_callback, but not sure
# how else to check for input. need higher level "input handler" class?
if self.addon_mode == WAIT_FOR_POPUP:
if not popup_active:
process_popup_input(self)
set_help_text(self, "CLICK")
elif self.addon_mode == GET_0_OR_180:
choose_0_or_180(TransDat.lock_pts[2], TransDat.rot_pt_pos,
TransDat.rot_pt_neg, TransDat.ang_diff_r, self.mouse_co)
return {'RUNNING_MODAL'}
def invoke(self, context, event):
if context.area.type == 'VIEW_3D':
args = (self, context)
# Add the region OpenGL drawing callback
# draw in view space with 'POST_VIEW' and 'PRE_VIEW'
self._handle = bpy.types.SpaceView3D.draw_handler_add(
draw_callback_px, args, 'WINDOW', 'POST_PIXEL')
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
self.settings_backup = backup_blender_settings()
self.mouse_co = Vector((event.mouse_region_x, event.mouse_region_y))
self.rtoolsw = get_reg_overlap() # region tools (toolbar) width
self.highlight_mouse = True # draw ref point on mouse
self.pts = []
self.pt_cnt = 0
self.lk_pts = []
self.multi_tmp = TempPoint()
self.meas_btn = ViewButton(Colr.red, Colr.white, 18, Colr.white, (0, 20))
self.add_rm_btn = ViewButton(Colr.red, Colr.white, 18, Colr.white, (190, 36))
self.overlap_idx = None
self.shift_held = False
#self.debug_flag = False
self.mod_pt = None
self.first_run = event.type in {'RET', 'LEFTMOUSE'} and event.value != 'RELEASE'
self.force_quit = False
self.grab_pt = None
self.new_free_co = ()
self.swap_pt = None
self.addon_mode = CLICK_CHECK
self.transf_type = "" # transform type
#self.pt_find_md = SLOW3DTO2D # point find mode
self.lmb_held = False
self.menu = MenuHandler("Set Measaure", 18, Colr.yellow, \
Colr.white, self.rtoolsw, context.region)
self.menu.add_menu(["Move", "Scale"])
self.menu.add_menu(["Rotate"])
context.window_manager.modal_handler_add(self)
init_blender_settings()
init_ref_pts(self)
set_transform_data_none()
editmode_refresh()
#print("Add-on started") # debug
self.add_rm_btn.set_text("Add Selected")
set_help_text(self, "CLICK")
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "View3D not found, cannot run operator")
return {'CANCELLED'}