0

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

  • and https://blender.stackexchange.com/questions/13738/how-to-calculate-the-direction-and-up-vector-of-a-camera – batFINGER Aug 10 '20 at 15:20
  • '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..? – Robin Betts Aug 10 '20 at 15:20
  • 2
    out of interest why the donut picture? – batFINGER Aug 10 '20 at 15:21

2 Answers2

0

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()

0

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)