Advertisement
cfox04

Untitled

Jan 7th, 2022
1,013
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def project_3d_point(p: Vector):
  2.     region_data = bpy.context.region_data
  3.  
  4.     # Get the two components to calculate M
  5.     modelview_matrix = region_data.view_matrix
  6.     projection_matrix = region_data.perspective_matrix
  7.  
  8.     # Compute P’ = M * P
  9.     p1 = projection_matrix @ modelview_matrix @ Vector((p.x, p.y, p.z, 1))
  10.  
  11.     # Normalize in: x’’ = x’ / w’, y’’ = y’ / w’
  12.     p2 = Vector(((p1.x/p1.w, p1.y/p1.w)))
  13.  
  14.     return p2
  15.  
  16. # Project points onto 2D plane
  17. corners2d = []
  18. for vert in selected_verts:
  19.     corners2d.append(project_3d_point(vert))
  20.  
  21. # Sort the 2d points in a winding order
  22. sort_order = list(sort_points(np.array(corners2d)))
  23. sorted_corners = [None] * 4
  24. sorted_corners[0] = selected_verts[sort_order.index(0)]
  25. sorted_corners[1] = selected_verts[sort_order.index(1)]
  26. sorted_corners[2] = selected_verts[sort_order.index(2)]
  27. sorted_corners[3] = selected_verts[sort_order.index(3)]
Advertisement
RAW Paste Data Copied
Advertisement