-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard_exporter.py
64 lines (50 loc) · 1.97 KB
/
card_exporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import bpy
from bpy_extras.object_utils import world_to_camera_view
from mathutils import Vector
path = '<path-to-monogame-projects-content-folder>'
filename = 'animation.txt'
scene = bpy.context.scene
object = bpy.data.objects['Plane']
plane = bpy.data.meshes['Plane']
polygons = plane.polygons
cam = bpy.data.objects['Camera']
# https://blender.stackexchange.com/questions/882/how-to-find-image-coordinates-of-the-rendered-vertex
render_scale = scene.render.resolution_percentage / 100
render_size = (
int(scene.render.resolution_x * render_scale),
int(scene.render.resolution_y * render_scale)
)
# https://blenderartists.org/t/how-to-get-vertices-coordinates-from-animated-mesh/565581/4
# Loop through all frames:
origin_coord = world_to_camera_view(scene, cam, plane.vertices[2].co)
origin = ((round(origin_coord.x * render_size[0]), round(origin_coord.y * render_size[1])))
origin_x = None
origin_y = None
total_output = ''
for frame in range(scene.frame_start, scene.frame_end):
# Set the frame.
scene.frame_set(frame)
# Create a temporary mesh.
mesh = object.to_mesh()
# Apply local roation, position, and scale.
mesh.transform(object.matrix_world)
vertices = mesh.vertices
verts = [vert.co for vert in vertices]
coords_2d = [world_to_camera_view(scene, cam, coord) for coord in verts]
pixel_coords = [(round(coord.x * render_size[0]), round(coord.y * render_size[1])) for coord in coords_2d]
# We consider the first point (top left corner) of the first frame to be the origin.
if not origin_x:
origin_x = pixel_coords[2][0]
origin_y = pixel_coords[2][1]
# Clear the temporary mesh.
object.to_mesh_clear()
output = ''
for x, y in pixel_coords:
x -= origin_x
y = origin_y - y
output += f'{x} {y} '
# The order is bottom left, bottom right, top left, top right
total_output += output + '\n'
with open(path + filename, 'w+') as f:
f.write(total_output)
print(total_output)