export anim cam 2d positionwithin bg cam

0.9.8

- feat: `Export Camera 2D Position To AE` to export 'anim cam' (or selected cam) frame center pixel coordinate within scene camera.
  - write txt file as after effects postion clipboard data
This commit is contained in:
Pullusb
2022-02-09 12:27:29 +01:00
parent d43e59b7c7
commit c5ea98b4d0
4 changed files with 144 additions and 33 deletions
+71 -9
View File
@@ -9,6 +9,9 @@ from collections import defaultdict
from time import time
import json
### -- node basic
def create_node(type, tree=None, **kargs):
'''Get a type, a tree to add in, and optionnaly multiple attribute to set
return created node
@@ -70,6 +73,8 @@ def create_aa_nodegroup(tree):
return ng
## -- object and scene settings
def copy_settings(obj_a, obj_b):
exclusion = ['bl_rna', 'id_data', 'identifier','name_property','rna_type','properties', 'stamp_note_text','use_stamp_note',
'settingsFilePath', 'settingsStamp', 'select', 'matrix_local', 'matrix_parent_inverse',
@@ -95,7 +100,6 @@ def copy_settings(obj_a, obj_b):
# print(f"can't set {attr}")
pass
def set_file_output_format(fo):
fo.format.file_format = 'OPEN_EXR'
fo.format.color_mode = 'RGBA'
@@ -108,7 +112,6 @@ def set_file_output_format(fo):
# fo.format.color_depth = '8'
# fo.format.compression = 15
def set_scene_aa_settings(scene=None, aa=True):
'''aa == using native AA, else disable scene AA'''
if not scene:
@@ -229,7 +232,8 @@ def get_view_layer(name, scene=None):
pass_vl = scene.view_layers.new(name)
return pass_vl
### node location tweaks
## -- node location tweaks
def real_loc(n):
if not n.parent:
@@ -260,7 +264,7 @@ def get_frame_transform(f, node_tree=None):
return loc, dim
## get all frames with their real transform.
## -- get all frames with their real transform.
def bbox(f, frames):
xs=[]
@@ -322,7 +326,7 @@ def get_frames_bbox(node_tree):
return frames_bbox
## nodes helper functions
## -- nodes helper functions
def clear_nodegroup(name, full_clear=False):
'''remove duplication of a nodegroup (.???)
@@ -359,7 +363,6 @@ def rearrange_rlayers_in_frames(node_tree):
rl.location.y = top
top -= rl.dimensions.y + 20 # place next down by height + gap of 20
def rearrange_frames(node_tree):
frame_d = get_frames_bbox(node_tree) # dic : {frame_node:(loc vector, dimensions vector), ...}
if not frame_d:
@@ -616,7 +619,7 @@ def nodegroup_merge_inputs(ngroup):
out = ngroup.outputs.new('NodeSocketColor', ngroup.inputs[0].name)
ngroup.links.new(aa.outputs[0], ng_out.inputs[0])
## --- renumbering funcs ---
## -- renumbering funcs
def get_numbered_output(out, slot_name):
'''Return output slot name without looking for numbering ???_
@@ -911,6 +914,8 @@ def show_message_box(_message = "", _title = "Message Box", _icon = 'INFO'):
bpy.context.window_manager.popup_menu(draw, title = _title, icon = _icon)
## -- camera framing and object anim checks
def get_bbox_3d(ob):
bbox_coords = ob.bound_box
return [ob.matrix_world @ Vector(b) for b in bbox_coords]
@@ -1032,7 +1037,6 @@ def set_border_region_from_coord(coords, scn=None, margin=30, export_json=True):
# export_crop_to_json(scn)
return pixel_bbox2d_coords
def get_gp_box_all_frame(ob, cam=None):
'''set crop to object bounding box considering whole animation. Cam should not be animated (render in bg_cam)
return 2d bbox in pixels
@@ -1159,7 +1163,6 @@ def get_bbox_2d(ob, cam=None):
return [Vector(b) for b in bbox2d_coords]
def set_box_from_selected_objects(scn=None, cam=None, export_json=False):
scn = scn or bpy.context.scene
cam = cam or scn.camera
@@ -1171,6 +1174,65 @@ def set_box_from_selected_objects(scn=None, cam=None, export_json=False):
_bbox_px = set_border_region_from_coord(coords, margin=30, scn=scn, export_json=export_json)
def get_cam_frame_center_world(cam):
'''get camera frame center world position in 3d space'''
## ortho cam note: scale must be 1,1,1 (parent too) to fit right in cam-frame rectangle
import numpy as np
frame = cam.data.view_frame()
mat = cam.matrix_world
frame = [mat @ v for v in frame]
# return np.add.reduce(frame) / 4
return Vector(np.sum(frame, axis=0) / 4)
def get_coord_in_cam_space(scene, cam_ob, co, ae=False):
'''Get 2d coordinate of vector in cam space
:scene: scene where camera is used (needed to get resolution)
:cam_ob: camera object
:co: the Vector3 coordinate to find in cam space
:ae: if True, Return after effects coord, top-left corner origin (blender is bottom-left)
'''
import bpy_extras
co_2d = bpy_extras.object_utils.world_to_camera_view(scene, cam_ob, co)
if ae:
# y coordinate from top
co_2d = Vector((co_2d.x, 1 - co_2d.y))
## Convert to pixel values based on scene resolution and percentage
render_scale = scene.render.resolution_percentage / 100
render_size = (
int(scene.render.resolution_x * render_scale),
int(scene.render.resolution_y * render_scale),
)
return (
round(co_2d.x * render_size[0]), # x
round(co_2d.y * render_size[1]), # y
)
## -- After effects exports
def get_ae_keyframe_clipboard_header(scn):
import textwrap
t = f'''\
Adobe After Effects 8.0 Keyframe Data
Units Per Second {scn.render.fps}
Source Width {scn.render.resolution_x}
Source Height {scn.render.resolution_y}
Source Pixel Aspect Ratio 1
Comp Pixel Aspect Ratio 1
Transform Position
Frame X pixels Y pixels Z pixels
'''
return textwrap.dedent(t)
## -- Collection handle
def get_collection_childs_recursive(col, cols=[], include_root=True):
'''return a list of all the sub-collections in passed col'''