-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAviutl_camera_export.py
211 lines (165 loc) · 6.27 KB
/
Aviutl_camera_export.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# -*- coding: utf-8 -*-
import bpy
import math
from mathutils import Vector
from bpy_extras.io_utils import ExportHelper
import configparser
from copy import deepcopy
# import numpy as np
bl_info = {
"name": "AviUtl Camera Exporting",
"author": "Aodaruma",
"version": (1, 1, 0),
"blender": (4, 10, 0),
"location": "File > Export > Camera Export (.exo)",
"description": "Exporting Camera data to exo file",
"warning": "",
"support": "COMMUNITY",
"wiki_url": "",
"tracker_url": "",
"category": "Import-Export",
}
####################################
# class UIpanel(bpy.types.Panel):
# bl_label = "BPM marker"
# bl_space_type = "VIEW_3D"
# bl_region_type = "TOOLS"
# bl_category = "Tools"
# def draw(self, context):x
# self.layout.operator("my.mark", text="Mark")
# ------------------------------------------------ #
class ExportAviutlCamera(bpy.types.Operator, ExportHelper):
bl_idname = "export_scene.export_aul_camera"
bl_label = "Export Camera"
bl_description = "Export Camera file (.exo)"
bl_options = {"REGISTER", "UNDO", "PRESET"}
filename_ext = ".exo"
scale: bpy.props.FloatProperty(
name="scale for AviUtl camera",
description="Camera unit scale for AviUtl camera",
default=100.0,
) # type: ignore
# obj_name = ""
def __init__(self):
pass
def execute(self, context):
print("Execute was called.")
# self.parse_command_line_options()
# if (self.obj_name == ""):
# print("No suitable object name was provided")
# return {'FINISHED'}
print("Executing......")
self.export()
print("Finished")
return {"FINISHED"}
def export(self):
scene = bpy.context.scene
obj = scene.objects
config = configparser.ConfigParser()
config.optionxform = str # for case-sensitive
config["exedit"] = {
"width": scene.render.resolution_x,
"height": scene.render.resolution_y,
"rate": scene.render.fps,
"scale": 1,
# "length" : scene.frame_end - scene.frame_start
# "audio_rate" : 44100
# "audio_ch" : 2
}
def convTarget(m, fd):
# ref: https://blender.stackexchange.com/questions/13738/how-to-calculate-the-direction-and-up-vector-of-a-camera
q = m.to_quaternion()
nz = Vector((0, 0, 1))
vd = q @ Vector((0.0, 0.0, -1.0))
vu = q @ Vector((0.0, 1.0, 0.0))
target = (vd * fd + m.to_translation()) * self.scale
s_phi = math.sqrt(1 - vd.dot(nz) ** 2)
# print(s_phi)
if s_phi > 0:
t = vu.dot(nz) / s_phi
if -1 <= t <= 1:
rz = math.acos(t)
else:
rz = 0
elif s_phi == 0:
e = m.to_euler("ZYX")
rz = e[2]
rz *= 180 / math.pi
tx, ty, tz = target
# print(target)
return tx, ty, tz, rz
def convFOV(x):
# ゴリ押し申し訳ねえ...
return 1.155e-5 * x**3 - 5.1839e-5 * x**2 + 0.561 * x + 0.0477
i = -1
pre = []
frame = scene.frame_start
while frame <= scene.frame_end:
# print("exporting '{} / {}'".format(i+2,scene.frame_end-scene.frame_start))
print("----------")
scene.frame_set(frame)
# print(pre)
# print("----")
# loc, rot, fov, fd = [], [], 0, 0
o = scene.camera
if o is None:
print("No camera found.")
return {"CANCELLED"}
cam: bpy.types.Camera = o.data # type: ignore
mtx = o.matrix_world
loc = mtx.to_translation() * self.scale
fov = cam.angle / math.pi * 180
fd = cam.dof.focus_distance
if i >= 0:
tx, ty, tz, rz = convTarget(mtx, fd)
ptx, pty, ptz, prz = convTarget(pre["mtx"], pre["fd"])
config[str(i)] = {"start": i + 1, "end": i + 2, "layer": 1}
if i > 0:
config[str(i)]["chain"] = "1"
config[str(i) + ".0"] = {
"_name": "カメラ制御",
"X": "{:.1f},{:.1f},1".format(pre["loc"][0], loc[0]),
"Y": "{:.1f},{:.1f},1".format(-pre["loc"][2], -loc[2]),
"Z": "{:.1f},{:.1f},1".format(pre["loc"][1], loc[1]),
"目標X": "{:.1f},{:.1f},1".format(ptx, tx),
"目標Y": "{:.1f},{:.1f},1".format(-ptz, -tz),
"目標Z": "{:.1f},{:.1f},1".format(pty, ty),
"目標レイヤ": "0",
"傾き": "{:.2f},{:.2f},1".format(prz, rz),
"深度ぼけ": "0",
"視野角": "{:.2f},{:.2f},1".format(
convFOV(pre["fov"]), convFOV(fov)
),
"Zバッファ/シャドウマップを有効にする": "1",
}
# print()
now = {"loc": loc, "fov": fov, "fd": fd, "mtx": mtx}
pre = deepcopy(now)
i += 1
frame += 1
with open(self.filepath, "w", encoding="shift_jis", newline="\r\n") as f:
config.write(f, space_around_delimiters=False)
return {"FINISHED"}
# Define a function to create the menu option for exporting.
def create_menu(self, context):
self.layout.operator(ExportAviutlCamera.bl_idname, text="Export camera (.exo)")
####################################
classes = ExportAviutlCamera
def register():
# for c in classes:
# bpy.utils.register_class(c)
bpy.utils.register_class(ExportAviutlCamera)
bpy.types.TOPBAR_MT_file_export.append(create_menu)
print(bl_info["name"] + " -> ON")
def unregister():
# for c in reversed(classes):
bpy.utils.unregister_class(ExportAviutlCamera)
bpy.types.TOPBAR_MT_file_export.remove(create_menu)
print(bl_info["name"] + " -> OFF")
if __name__ == "__main__":
print("Registering...")
register(classes)
print("Executing...")
bpy.ops.export_scene.export_aul_camera()
####################################
# print("done!")