0
$\begingroup$

I have a blender file with this scene (below) seen from the camera. Now I need to get the 3D points of the mesh (the doughnut) in world space according to the camera perspective, pragmatically. Meaning I need a python script I can run to save a text file with the list of 3D points I need. I also need to know the camera direction vector.

I'm using Blender 2.83 and I have python 3.7 installed. Thanks in advance.

enter image description here

$\endgroup$
3
  • $\begingroup$ and blender.stackexchange.com/questions/13738/… $\endgroup$
    – batFINGER
    Commented Aug 10, 2020 at 15:20
  • $\begingroup$ 'In world space according to the camera perspective' is rather confusing. World space is world space, whatever the camera is looking at, maybe you want camera space, or even screen space..? $\endgroup$
    – Robin Betts
    Commented Aug 10, 2020 at 15:20
  • 2
    $\begingroup$ out of interest why the donut picture? $\endgroup$
    – batFINGER
    Commented Aug 10, 2020 at 15:21

2 Answers 2

0
$\begingroup$

I hope this can help

import bpy
import bmesh
import json

bpy.ops.object.mode_set(mode="EDIT")

bm = bmesh.new()
# write the object name you want between quotes
ob = bpy.data.objects["obj_name"]
# if Camera has a different name in your scene change it here too
cam = bpy.data.objects["Camera"]
bm = bmesh.from_edit_mesh(ob.data)

points = {}
for v in bm.verts:
    obMat = ob.matrix_world
    points["V"+str(v.index)] = list(obMat @ v.co - cam.location)

with open('data.txt', 'w') as outfile:
    json.dump(points, outfile, indent=4)
$\endgroup$
4
  • $\begingroup$ I'm getting error: "The mesh must be in editmode" on the line bm = bmesh.from_edit_mesh(ob.data) How do I make it in edit mode? $\endgroup$ Commented Aug 10, 2020 at 20:34
  • $\begingroup$ Another thing, which points would I get in the end by this code? All set of point of the mesh, or only those facing the camera? $\endgroup$ Commented Aug 10, 2020 at 20:35
  • $\begingroup$ the first problem can be fixed by moving to the mesh editing mode, i added it in a pythonic way to the code. $\endgroup$ Commented Aug 10, 2020 at 20:41
  • $\begingroup$ All the mesh points of the object will be added $\endgroup$ Commented Aug 10, 2020 at 20:41
0
$\begingroup$

you would need sth like this to get the object in world coordinates

import bpy
from mathutils import Euler, Matrix, Vector

            object_rotation = obj.rotation_euler
            object_translation = obj.location
            H_m2w = Matrix.Translation(Vector(object_translation)) @ Euler(
                object_rotation, 'XYZ').to_matrix().to_4x4()

            cam_H_m2c = H_m2w.inverted()

            cam_R_m2c = cam_H_m2c.to_quaternion().to_matrix()
            cam_t_m2c = cam_H_m2c.to_translation()
$\endgroup$
1
  • $\begingroup$ How do I get the 3D points still? Which of these objects I can obtain it from? $\endgroup$ Commented Aug 10, 2020 at 20:12

Not the answer you're looking for? Browse other questions tagged .