2
$\begingroup$

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()
$\endgroup$
7
  • 1
    $\begingroup$ I'm 100% sure this has been done before... Now to find the right keyword combination to make the search engine dig it up... $\endgroup$
    – Gorgious
    Commented Jul 12, 2021 at 16:47
  • $\begingroup$ Something like this? $\endgroup$
    – Robin Betts
    Commented Jul 12, 2021 at 17:07
  • $\begingroup$ 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. $\endgroup$
    – saseptim
    Commented Jul 13, 2021 at 9:30
  • $\begingroup$ Also related, blender.stackexchange.com/questions/229030/… $\endgroup$
    – batFINGER
    Commented Jul 14, 2021 at 12:31
  • $\begingroup$ 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 $\endgroup$
    – saseptim
    Commented Jul 14, 2021 at 13:19

0

You must log in to answer this question.

Browse other questions tagged .