2

I am currently doing the following (all via bpy in Python):

  1. Load a triangle mesh in Blender, and convert it to quads. The mesh is a flat object (like a bent piece of pape).
  2. Add a texture to the mesh.
  3. Adjust the camera to a fixed position looking center straight down on the mesh.
  4. Render the image.
  5. Export the quad mesh to an .obj file.

So now I have the quad mesh in an obj file and a rendered 2D image. I would like to find the alignment / mapping of each vertex in the mesh to its location on the 2D image, via bpy if possible.

Thanks!

Rendered Image

view in Blender

Update: I got this to work: Here is the image shown, with a scatter plot of the 2d coordinates lain on top:

Here is the render code:

bpy.data.scenes['Scene'].render.resolution_x = 448
bpy.data.scenes['Scene'].render.resolution_y = 448
bpy.data.scenes['Scene'].render.resolution_percentage = 100

render_scale = scene.render.resolution_percentage / 100 render_size = ( int(scene.render.resolution_x * render_scale), int(scene.render.resolution_y * render_scale), )

verts = list((mesh.matrix_world @ vert.co for vert in mesh.data.vertices))

coords_2d = [world_to_camera_view(scene, camera, coord).to_tuple()[:2] for coord in verts]

adjusted_coords_2d = [(render_size[0] * (u), render_size[1] * (1 - v)) for u, v in coords_2d] pk.dump(adjusted_coords_2d, open(inds_output_path, 'wb'))

Here is the display code:

image = plt.imread(img_path)

inds = pk.load(open(inds_path, 'rb')) inds_a = np.array(inds)

plt.figure() plt.imshow(image) plt.scatter(inds_a[:, 0], inds_a[:, 1]) plt.show()

saseptim
  • 21
  • 2
  • 1
    I'm 100% sure this has been done before... Now to find the right keyword combination to make the search engine dig it up... – Gorgious Jul 12 '21 at 16:47
  • Something like this? – Robin Betts Jul 12 '21 at 17:07
  • Thanks! I had seen this post, but not managed to get it to work. With a combination of the top 2 answers and comments, and fooling around, I managed to get it to work. – saseptim Jul 13 '21 at 09:30
  • Also related, https://blender.stackexchange.com/questions/229030/2d-bounding-box-around-object-for-different-cameras-different-positions-ble/229829#229829 – batFINGER Jul 14 '21 at 12:31
  • another interesting point is that with the square images, the whole thing works only WITHOUT mesh.matrix_world @ vert.co - converting to world coordinates. As soon as I convert to world coordinates, this also does not work – saseptim Jul 14 '21 at 13:19
  • The link within the link posted https://blender.stackexchange.com/a/214597/15543 shows how to handle aspect ratio. Try it out, adds a "2d bbox mesh" around context object and shifts camera to center it. (don't need the camera shift of course) – batFINGER Jul 14 '21 at 13:52
  • I got it working. I switched the "adjustment line" to adjusted_coords_2d = [(render_size[0] * (u), render_size[1] * (1 - v)) for u, v in coords_2d] and added the use of mesh.matrix_world as per documentation of world_to_camera_view – saseptim Jul 14 '21 at 14:25

0 Answers0