Last active
September 7, 2021 12:24
-
-
Save simonecesano/2cefcfac7485939a18249c4672e4a5ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from mathutils import Vector, Matrix | |
from bpy_extras import view3d_utils | |
import bmesh | |
import numpy as np | |
import os, sys | |
import cv2 | |
context = bpy.context | |
scene = context.scene | |
ob = context.object | |
mw = ob.matrix_world | |
me = ob.data | |
def region_3d(context): | |
for area in context.screen.areas: | |
if area.type == 'VIEW_3D': | |
space = area.spaces.active | |
r3d = space.region_3d | |
plane_no = r3d.view_rotation @ Vector((0, 0, -1)) | |
region = area.regions[-1] | |
break | |
else: | |
assert False, "Requires a $D view" | |
return region, r3d, area | |
def vertex_positions_2d(obj, region, r3d): | |
bm = bmesh.new() | |
bm.from_mesh(obj.data) | |
bm.transform(obj.matrix_world) | |
v_2d_co = np.array( | |
[view3d_utils.location_3d_to_region_2d( | |
region, | |
r3d, | |
v.co) for v in bm.verts] | |
) | |
return v_2d_co | |
def screenshot(P_filename, P_path = None): | |
import bpy | |
L_saveAs = P_filename | |
L_saveAs = os.path.join(P_path, L_saveAs) if P_path else os.path.join("/tmp", L_saveAs) | |
print("Scene saved in " + L_saveAs) | |
#XXX: switching to 3D full view = maximize scene in main window | |
#bpy.context.window.screen = bpy.data.screens['3D View Full'] | |
for window in bpy.context.window_manager.windows: | |
screen = window.screen | |
for area in screen.areas: | |
if area.type == 'VIEW_3D': | |
for space in area.spaces: | |
if space.type == 'VIEW_3D': | |
for region in area.regions: | |
if region.type == 'WINDOW': | |
L_altBpyCtx = { | |
'area' : area, 'blend_data': bpy.context.blend_data, 'region' : None, 'scene' : window.scene, 'space' : space, | |
'screen' : window.screen, 'window' : window | |
} | |
bpy.ops.screen.screenshot(L_altBpyCtx, filepath = L_saveAs, full = False) | |
break | |
break | |
break | |
region, r3d, area = region_3d(context) | |
print(region, r3d) | |
positions=vertex_positions_2d(ob, region, r3d) | |
screenshot('screenshotto.png', '.') | |
def put_dots(img_path, positions): | |
img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED) | |
red = [0,0,255] | |
height, width, channels = img.shape | |
for p in positions: | |
# print(p, p[0], p[1]) | |
cv2.circle(img, (round(p[0]), height - round(p[1])), 6, (0,0,255), -1) | |
cv2.imwrite('./foo.jpg', img) | |
pass | |
put_dots('./screenshotto.png', positions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment