def _move(self): ml = self._moving_line tl = self._target_line ml_ps = [ obj for obj in bpy.data.objects if obj.fp_id in ml.fp_deps and obj.fp_id != 0 ] tl_ps = [ obj for obj in bpy.data.objects if obj.fp_id in tl.fp_deps and obj.fp_id != 0 ] pairs = [(i, j) for i in ml_ps for j in tl_ps] nearest = reduce( lambda rslt, pair: pair if ((loc(pair[1])[0] - loc(pair[0])[0])**2 + (loc(pair[1])[1] - loc(pair[0])[1])**2) < ((loc(rslt[1])[0] - loc(rslt[0])[0])**2 + (loc(rslt[1])[1] - loc(rslt[0])[1])**2) else rslt, pairs) farest = reduce( lambda r, p: p if (nearest[0] not in p and nearest[1] not in p) else r, pairs) nearest[0].fp_expression = nearest[1].fp_expression nearest[0].fp_angle = nearest[1].fp_angle fdiff = -sub(*[p.fp_angle for p in farest]) self._rotate_recur(farest[0], fdiff) farest[0].fp_expression = farest[1].fp_expression [ self._rotate_recur(obj, fdiff) for obj in bpy.data.objects if (nearest[0].fp_id in obj.fp_deps) or (obj.parent == nearest[0]) ]
def _get_angle_deg_points2(self, points): diffs = [(loc(points[1])[i] - loc(points[0])[i]) for i in range(2)] hyp = sqrt(reduce(add, [diff**2 for diff in diffs])) return round(rad2deg(asin(diffs[1] / hyp)), 2) if hyp else 0
def _move(self, context): ml = self._moving_line tl = self._target_line ml_ps = [ obj for obj in bpy.data.objects if obj.fp_id in ml.fp_deps and obj.fp_id != 0 ] tl_ps = [ obj for obj in bpy.data.objects if obj.fp_id in tl.fp_deps and obj.fp_id != 0 ] pairs = [(i, j) for i in ml_ps for j in tl_ps] nearest = reduce( lambda rslt, pair: pair if ((loc(pair[1])[0] - loc(pair[0])[0])**2 + (loc(pair[1])[1] - loc(pair[0])[1])**2) < ((loc(rslt[1])[0] - loc(rslt[0])[0])**2 + (loc(rslt[1])[1] - loc(rslt[0])[1])**2) else rslt, pairs) center = nearest[0] cocenter = reduce(lambda res, item: item if item != center else res, ml_ps, None) farest = reduce( lambda r, p: p if (nearest[0] not in p and nearest[1] not in p) else r, pairs) tl_ang_deg = self._get_angle_deg_points2(tl_ps) ml_ang_deg = self._get_angle_deg_points2(ml_ps) diff_ang_deg = tl_ang_deg - ml_ang_deg diff_ang_rad = deg2rad(diff_ang_deg) diff_loc = [ round(loc(nearest[1])[i] - loc(nearest[0])[i], 2) for i in range(3) ] # ????? diff_quat = [(loc(tl_ps[1])[i] - loc(tl_ps[0])[i]) >= 0 for i in range(2)] print("ml = ", ml) print("tl = ", tl) print("ml_ps = ", ml_ps) print("tl_ps = ", tl_ps) print("pairs = ", pairs) print("nearest = ", nearest) print("center = ", center) print("cocenter = ", cocenter) print("farest = ", farest) print("self._moving_contour", self._moving_contour) print("--------------------------------------") pairs = [] coincidence = False for obj_ml in ml_ps: for obj_tl in tl_ps: if obj_ml == obj_tl: coincidence = True break if coincidence: break if not coincidence: if tl_ps[0].location[0] >= tl_ps[1].location[0] and ml_ps[ 0].location[0] >= ml_ps[1].location[0]: pairs = [(ml_ps[0], tl_ps[0]), (ml_ps[1], tl_ps[1])] elif tl_ps[0].location[0] < tl_ps[1].location[0] and ml_ps[ 0].location[0] >= ml_ps[1].location[0]: pairs = [(ml_ps[0], tl_ps[0]), (ml_ps[1], tl_ps[1])] elif tl_ps[0].location[0] >= tl_ps[1].location[0] and ml_ps[ 0].location[0] < ml_ps[1].location[0]: pairs = [(ml_ps[0], tl_ps[0]), (ml_ps[1], tl_ps[1])] elif tl_ps[1].location[0] >= tl_ps[0].location[0] and ml_ps[ 1].location[0] >= ml_ps[0].location[0]: pairs = [(ml_ps[0], tl_ps[0]), (ml_ps[1], tl_ps[1])] for obj in bpy.context.selected_objects: obj.select = False bpy.context.scene.objects.active = None one_objects_of_ml = [ obj for obj in bpy.data.objects if obj.fp_id in self._moving_contour[1].fp_deps and obj.fp_id != 0 ] for obj_ml in one_objects_of_ml: for obj_ml_ps in ml_ps: if obj_ml_ps == obj_ml: for i in range(len(pairs)): if pairs[i][0] == obj_ml: pairs[i][1].select = True new_points = [] for i in range(len(self._moving_contour) - 2): coincidence = False new_points.append(self._point.create(bpy.context)) print("pairs", pairs) for obj in pairs: print("obj", obj) print("-----") print("obj[0].fp_id", obj[0].fp_id) print("self._moving_contour[i+1].fp_deps[0]", self._moving_contour[i + 1].fp_deps[0]) print("-----") if obj[0].fp_id == self._moving_contour[i + 1].fp_deps[0]: pair = [ ob for ob in bpy.data.objects if ob.fp_id == self._moving_contour[i + 1].fp_deps[1] and ob.fp_id != 0 ] pairs += [(pair[0], new_points[i])] coincidence = True break if not coincidence: pair = [ ob for ob in bpy.data.objects if ob.fp_id == self._moving_contour[i + 1].fp_deps[0] and ob.fp_id != 0 ] pairs += [(pair[0], new_points[i])] # print("pairs", pairs) for obj in bpy.data.objects: obj.select = False for i in range(len(self._moving_contour) - 1): for obj in bpy.data.objects: obj.select = False draw_line = Line() if i == len(self._moving_contour) - 2: pairs[i + 1][1].select = True pairs[0][1].select = True else: pairs[i + 1][1].select = True pairs[i + 2][1].select = True draw_line.create(context) for i in range(len(pairs) - 2): print("pairs[i+1]", pairs[i + 1]) print("pairs[i+2]", pairs[i + 2]) new_angle = get_angle(loc(pairs[i + 1][0]), loc(pairs[i + 2][0])) print("new_angle", new_angle) old_angle = get_angle(loc(ml_ps[0]), loc(ml_ps[1])) print("old_angle", old_angle) start_line_angle = get_angle(loc(tl_ps[0]), loc(tl_ps[1])) print("start_line_angle", start_line_angle) if old_angle == start_line_angle: pairs[i + 2][1].fp_angle = new_angle else: pairs[ i + 2][1].fp_angle = -new_angle - old_angle + start_line_angle print("distance", get_distance(loc(pairs[i + 1][0]), loc(pairs[i + 2][0]))) pairs[i + 2][1].fp_expression = str( get_distance(loc(pairs[i + 1][0]), loc(pairs[i + 2][0]))) print("-____________-") print("pairs", pairs) exist = False for obj in bpy.data.objects: for point_in_contour in pairs: if obj == point_in_contour[0]: exist = True break if not exist: for point_in_contour in pairs: if obj.parent == point_in_contour[0]: print("point_in_contour[1] = ", point_in_contour[1]) print("obj = ", obj) distance = str( get_distance(loc(point_in_contour[1]), loc(obj))) angle = get_angle(loc(point_in_contour[1]), loc(obj)) obj.parent = point_in_contour[1] obj.fp_expression = distance obj.fp_angle = angle exist = False for obj in bpy.data.objects: obj.select = False for obj in pairs: exist = 0 for ob in bpy.data.objects: if ob.fp_type == Line.FP_TYPE and ( ob.fp_deps[0] == obj[0].fp_id or ob.fp_deps[1] == obj[0].fp_id): exist += 1 if exist > 2: break if exist <= 2: for line in bpy.data.objects: if line.fp_type == Line.FP_TYPE: if line.fp_deps[1] == obj[0].fp_id: line.fp_deps[1] = 0 elif line.fp_deps[0] == obj[0].fp_id: line.fp_deps[0] = 0 obj[0].select = True bpy.ops.object.delete() for line in bpy.data.objects: if line.fp_type == Line.FP_TYPE and (line.fp_deps[0] == 0 or line.fp_deps[1] == 0): line.select = True bpy.ops.object.delete()