Luaスクリプト for YMM4

Luaスクリプト for YMM4 サンプル集

YukkuriMovieMaker4 の「Luaスクリプト」映像エフェクトで使えるサンプルと、 利用可能な変数・関数・定数の一覧をまとめたページです。 各サンプルはエフェクトのスクリプト欄にそのまま貼り付けて利用できます。 スクリプトエンジンは MoonSharp (Lua 5.2 互換) です。

変数・関数の詳細な仕様は、スクリプトエディタのヘルプ、または docs/LuaScript.ja-jp.txt を参照してください。


目次


サンプルの読み方

スクリプト内では、変更可能な obj の値(座標・拡大率・回転・不透明度など)を 書き換えると描画に反映されます。読み戻されるのは x, y, z, ox, oy, zoom, aspect, alpha, rx, ry, rz (およびラジアン版 rxr, ryr, rzr、独立スケール sx, sy)です。

time はアイテム基準の経過秒数、obj.t はアイテム内の進行割合 (0.0 ~ 1.0) です。グローバル変数の状態はフレーム間で保持されません。 math.random は毎フレーム同じシードで初期化されるため、同一フレームでは 同じ値を返します。安定した乱数が必要な場合は anim.rand を使います。


1. 基本

1-1. フェードイン (デフォルトスクリプト)

obj.alpha = math.min(time * 255, 255)

1-2. フェードアウト

obj.alpha = math.max((obj.totalframe - obj.frame) / obj.framerate * 255, 0)

1-3. Track0 で不透明度を制御

obj.alpha = obj.track0

1-4. 時間に応じて回転

obj.rz = time * 90

1-5. Track0 でズームを制御

obj.zoom = obj.zoom * (1 + obj.track0 / 100)

2. 座標と動き

2-1. 左右の揺れ

Track0 を振幅(px)、Track1 を周波数(回/秒)として使います。

local amp = obj.track0
local freq = obj.track1
obj.x = obj.x + math.sin(time * anim.tau * freq) * amp

2-2. 円運動

local r = obj.track0
local dx, dy = anim.polar(r, time * 90)
obj.x = obj.x + dx
obj.y = obj.y + dy

2-3. バウンド (跳ねる)

local h = obj.track0
obj.y = obj.y - anim.bounce(anim.pingpong(time, 1)) * h

2-4. ゆらぎ (ノイズによる手ぶれ風)

obj.x = obj.x + (anim.noise(time * 2, 0, 0) - 0.5) * 20
obj.y = obj.y + (anim.noise(0, time * 2, 0) - 0.5) * 20
obj.rz = (anim.noise(0, 0, time * 2) - 0.5) * 10

2-5. 中心オフセットを基準に振り子運動

回転中心 ox, oy をずらして振り子のように揺らします。

obj.oy = -obj.hh
obj.rz = math.sin(time * anim.tau) * 30

3. 回転

3-1. 度とラジアン

rx, ry, rz は度、rxr, ryr, rzr はラジアンです。 ラジアン版を変更するとそちらが優先して反映されます。

obj.rzr = time * anim.tau

3-2. Y軸回転で立体的に回す

obj.ry = time * 180

3-3. 加速しながら回転 (イージング)

obj.rz = anim.ease_in(obj.t) * 360

4. 拡大・変形

4-1. 弾むように登場 (スケールイン)

local p = anim.duration(time, 0.3)
obj.zoom = obj.zoom * anim.elastic(p)

4-2. アスペクト比で横伸び・縦伸び

aspect は正で横方向に縮小、負で縦方向に縮小します。

obj.aspect = math.sin(time * anim.tau) * 0.5

4-3. X・Y を独立して拡大 (sx, sy)

ぷにぷにと潰れる動きを sx, sy で表現します。

local s = math.sin(time * anim.tau * 2) * 0.2
obj.sx = 1 + s
obj.sy = 1 - s

5. 不透明度・フェード

5-1. 遅延フェードイン

local p = anim.duration(anim.delay(time, 0.5), 1)
obj.alpha = p * 255

5-2. 点滅 (矩形波)

obj.alpha = anim.square(time, 4) * 255

5-3. 滑らかな明滅 (正弦波)

obj.alpha = anim.oscillate(time, 64, 255, 1)

6. anim 補間・イージング

6-1. 2点間をイージングで移動

local tx, ty = 200, -100
local p = anim.ease_in_out(obj.t)
obj.x = anim.lerp(obj.x, tx, p)
obj.y = anim.lerp(obj.y, ty, p)

6-2. 行き過ぎてから戻る (back)

obj.x = obj.x + anim.back(obj.t) * 200

6-3. しきい値で切り替え (step / smoothstep)

obj.alpha = anim.smoothstep(0, 0.5, obj.t) * 255

補間・イージング系関数の一覧です。

関数説明
anim.lerp(a, b, t)a と b を t (0.0~1.0) で線形補間
anim.smoothstep(e0, e1, x)e0~e1 をエルミート補間で滑らかに遷移
anim.smootherstep(e0, e1, x)2次導関数まで連続な、より滑らかな遷移
anim.ease_in(t)2次イーズイン (加速)
anim.ease_out(t)2次イーズアウト (減速)
anim.ease_in_out(t)2次イーズイン・アウト (加速後減速)
anim.elastic(t)弾むようなスプリングイージング
anim.back(t)行き過ぎてから戻るイージング
anim.bounce(t)バウンドするイージング
anim.step(edge, x)x が edge 以上なら 1.0、未満なら 0.0
anim.bezier(t, p0, p1, p2, p3)t における3次ベジェ曲線の値

7. anim 範囲・周期

7-1. 値の範囲変換 (map)

obj.rz = anim.map(obj.track0, 0, 100, -180, 180)

7-2. 三角波で往復

obj.x = obj.x + (anim.triangle(time, 1) - 0.5) * 200

範囲・周期系関数の一覧です。

関数説明
anim.clamp(v, lo, hi)v を lo ~ hi の範囲に収める
anim.map(v, a1, b1, a2, b2)範囲 a1~b1 の v を a2~b2 に変換
anim.norm(v, lo, hi)v が lo~hi のどの割合 (0.0~1.0) かを取得
anim.wrap(v, lo, hi)v を lo ~ hi でループさせる
anim.pingpong(t, len)0 ~ len を往復する値
anim.sign(v)符号 (正=1, 負=-1, ゼロ=0)
anim.fract(v)小数部分のみを取得
anim.oscillate(t, lo, hi, freq)freq の周波数で lo~hi を正弦波で往復
anim.triangle(t, freq)freq の周波数で 0.0~1.0 を往復する三角波
anim.square(t, freq)freq の周波数で 1.0 と 0.0 を切り替える矩形波
anim.duration(t, dur)時間 dur に対する割合 (0.0~1.0, clamp あり)
anim.delay(t, d)時間 d 経過後から開始する時間 (0以下は0)

8. anim ベクトル・幾何

8-1. 座標を回転させる

local rx, ry = anim.rotate(obj.track0, 0, time * 90)
obj.x = obj.x + rx
obj.y = obj.y + ry

8-2. 2点間の距離で挙動を変える

local d = anim.dist(obj.x, obj.y, 0, 0)
obj.alpha = anim.clamp(255 - d, 0, 255)

ベクトル・幾何系関数の一覧です。

関数説明
anim.len(x, y)ベクトル (x, y) の長さ
anim.dist(x1, y1, x2, y2)2点間の距離
anim.dot(x1, y1, x2, y2)2ベクトルの内積
anim.normalize(x, y)正規化した方向ベクトル (Tuple で返却)
anim.polar(r, a)半径 r・角度 a(度) から移動量 (x, y)
anim.rotate(x, y, a)(x, y) を a(度) 回転させた座標

9. anim ノイズ・乱数

9-1. 安定した乱数で個体差をつける

anim.rand はシード引数に基づく決定的な乱数です。レイヤー番号をシードにすると オブジェクトごとに異なる固定値が得られます。

local offset = anim.rand(-50, 50, obj.layer)
obj.x = obj.x + offset

9-2. ノイズで揺らぐ不透明度

obj.alpha = anim.noise(time, obj.layer, 0) * 255
関数説明
anim.noise(x, y, z)1~3次元の滑らかなノイズ (0.0~1.0)
anim.rand(min, max, s)シード s に基づく安定した乱数

10. anim 色

10-1. 時間で色相を回す (虹色)

local hue = anim.wrap(time * 60, 0, 360)
local cr, cg, cb = anim.hsv_to_rgb(hue, 1, 1)
local pd = obj.getpixeldata()
local n = pd.width * pd.height
for i = 0, n - 1 do
    local base = i * 4
    pd:set(base + 1, pd:get(base + 1) * cr / 255)
    pd:set(base + 2, pd:get(base + 2) * cg / 255)
    pd:set(base + 3, pd:get(base + 3) * cb / 255)
end
関数説明
anim.hsv_to_rgb(h, s, v)HSV (色相0~360, 彩度0~1, 明度0~1) を RGB に変換
anim.rgb_to_hsv(r, g, b)RGB (0~255) を HSV に変換

11. anim 定数

定数説明
anim.tau円周率の2倍 (math.pi * 2)
anim.eネイピア数 (math.exp(1))
anim.phi黄金比 ((1 + math.sqrt(5)) / 2)
anim.sqrt22の平方根 (math.sqrt(2))

12. 他オブジェクトの参照 (obj.getobject)

obj.getobject(tag [, frame]) は、対象アイテムの「備考」欄に入力した文字列を タグとして、そのアイテムの描画情報を取得します。備考欄はプロパティエリアの上側にあります。 第2引数 frame を指定すると、そのタイムラインフレーム時点の値を取得します (省略時は現在フレーム)。返り値は対象アイテム自身の座標・回転・拡大率であり、 グループ制御・カメラ制御による変形は含みません。一致するアイテムが無い場合は nil を返します。同じタグのアイテムが複数あるときは、対象フレームで 表示中のものを優先します。

12-1. 発射元の座標に合わせる

local src = obj.getobject("routersys")
if src then
    obj.x = src.x
    obj.y = src.y
end

12-2. 発射元へ向かって移動する

local target = obj.getobject("routersys")
if target then
    local p = anim.ease_in_out(obj.t)
    obj.x = anim.lerp(obj.x, target.x, p)
    obj.y = anim.lerp(obj.y, target.y, p)
end

12-3. 発射元へ向きながら移動する (ファンネル風)

local target = obj.getobject("routersys")
if target then
    local p = anim.ease_in_out(obj.t)
    local nx = anim.lerp(obj.x, target.x, p)
    local ny = anim.lerp(obj.y, target.y, p)
    obj.rz = math.deg(math.atan2(target.y - obj.y, target.x - obj.x))
    obj.x = nx
    obj.y = ny
end

12-4. 一定速度で接近し、到達したら停止

local target = obj.getobject("routersys")
if target then
    local dx = target.x - obj.x
    local dy = target.y - obj.y
    local dist = anim.len(dx, dy)
    if dist > 0 then
        local step = math.min(dist, obj.track0 * obj.time)
        obj.x = obj.x + dx / dist * step
        obj.y = obj.y + dy / dist * step
    end
end

12-5. 表示中のときだけ追従する

local target = obj.getobject("routersys")
if target and target.exist then
    obj.x = target.x
    obj.y = target.y
end

12-6. 発射元の拡大率・回転・不透明度も引き継ぐ

local src = obj.getobject("routersys")
if src then
    obj.x = src.x
    obj.y = src.y
    obj.zoom = src.zoom
    obj.rz = src.rz
    obj.alpha = src.alpha
end

12-7. 発射時の座標に固定する

弾の発射フレーム (弾アイテムの開始フレーム) は timelineframe - frame で 求まります。そのフレーム時点の発射元座標を取得すれば、発射元が動いても弾の原点は 発射位置に固定されます。

local fire = timelineframe - frame
local src = obj.getobject("routersys", fire)
if src then
    obj.x = src.x
    obj.y = src.y
end

返り値テーブルのメンバーは以下のとおりです。

メンバー説明
exist対象フレームが表示期間内か (boolean)
x / y / z描画位置 (ピクセル)
zoom / sx / sy拡大率 (1.0 で等倍)
rx / ry / rzX / Y / Z 軸回転角 (度)
rxr / ryr / rzrX / Y / Z 軸回転角 (ラジアン)
alpha不透明度 (0.0 ~ 255.0)
layerレイヤー番号

13. ピクセル操作

ピクセル操作は実際の入力画像へアクセスするため、初回アクセス時に画像データの 転送が発生します。座標は画像の左上を (0, 0) とし、右・下方向が正です。 取得値はストレートアルファ相当です。

13-1. getpixel / setpixel で赤成分を反転

for y = 0, obj.h - 1 do
    for x = 0, obj.w - 1 do
        local r, g, b, a = obj.getpixel(x, y)
        obj.setpixel(x, y, 255 - r, g, b, a)
    end
end

13-2. getpixeldata でグレースケール (高速)

obj.getpixeldata() はピクセルへ直接アクセスできるプロキシを返します。 インデックスは 1 始まりで、チャンネルは R, G, B, A の繰り返しです。 pixel(x, y) の R チャンネルのインデックスは (y * obj.w + x) * 4 + 1 です。

local pd = obj.getpixeldata()
local w = pd.width
local h = pd.height
for y = 0, h - 1 do
    for x = 0, w - 1 do
        local base = (y * w + x) * 4
        local r = pd:get(base + 1)
        local g = pd:get(base + 2)
        local b = pd:get(base + 3)
        local gray = r * 0.299 + g * 0.587 + b * 0.114
        pd:set(base + 1, gray)
        pd:set(base + 2, gray)
        pd:set(base + 3, gray)
    end
end

13-3. 中心からの距離でフェード (円形ビネット)

local pd = obj.getpixeldata()
local w = pd.width
local h = pd.height
local cx = w / 2
local cy = h / 2
local maxd = anim.len(cx, cy)
for y = 0, h - 1 do
    for x = 0, w - 1 do
        local base = (y * w + x) * 4
        local d = anim.len(x - cx, y - cy)
        local k = anim.clamp(1 - d / maxd, 0, 1)
        pd:set(base + 4, pd:get(base + 4) * k)
    end
end

obj.putpixeldata() は AviUtl との互換性のために存在しますが、 このプラグインでは変更が自動的に反映されるため呼び出す必要はありません (no-op)。

関数戻り値説明
obj.getpixel(x, y)r, g, b, a指定座標のピクセルを取得 (各成分 0~255)
obj.setpixel(x, y, r, g, b [, a])なし指定座標へ書き込み (a 既定 255)
obj.getpixeldata()PixelDataProxy全ピクセルへの直接アクセスプロキシ
obj.putpixeldata()なし互換用の空実装

14. シーン情報 (scene)

14-1. シーンサイズに対する相対配置

obj.x = scene.cx * obj.track0 / 100
メンバー説明
scene.widthシーンの横解像度 (ピクセル)
scene.heightシーンの縦解像度 (ピクセル)
scene.cx横解像度の半分 (= scene.width / 2)
scene.cy縦解像度の半分 (= scene.height / 2)

15. 環境・メタ情報 (ymm4)

15-1. グループ内のインデックスで時間差をつける

local d = ymm4.group_index * 0.1
local p = anim.duration(anim.delay(time, d), 0.5)
obj.alpha = p * 255

15-2. グループ内の進行割合で配置をずらす

obj.x = obj.x + ymm4.group_ratio * 400

15-3. 出力時のみ挙動を変える

if ymm4.is_saving then
    obj.alpha = 255
else
    obj.alpha = 128
end
メンバー説明
ymm4.group_indexグループ制御内の現在のインデックス (0始まり)
ymm4.group_countグループ制御内の総アイテム数
ymm4.group_ratioグループ内の進行割合 (group_index / group_count)
ymm4.timeline_totalframeタイムライン全体の総フレーム数
ymm4.timeline_totaltimeタイムライン全体の総時間 (秒)
ymm4.time_ratio現在の再生位置の割合 (0.0 ~ 1.0)
ymm4.is_saving動画出力中のみ true
ymm4.is_playing再生中のみ true
ymm4.is_paused一時停止中のみ true
ymm4.scene_id現在のシーンを識別する文字列 (Guid)

16. グローバル変数

以下の変数はスクリプトのグローバルスコープで直接参照できます (すべて読み取り専用)。

変数説明
timeオブジェクト基準の現在時間 (秒)。obj.time と同じ
frameオブジェクト基準の現在フレーム番号。obj.frame と同じ
totalframeオブジェクトの総フレーム数。obj.totalframe と同じ
framerateフレームレート (fps)。obj.framerate と同じ
timelineframeタイムライン先頭からの絶対フレーム番号
timelinetimeタイムライン基準の現在時間 (秒)
layer配置されているレイヤー番号。obj.layer と同じ
obj / scene / ymm4 / anim各テーブル (本ページ参照)

17. 標準ライブラリ

利用できる標準ライブラリは以下に限定されます。io, os, debug, ffi, require 等は使用できません。

math

abs, ceil, cos, exp, floor, fmod, huge, log, max, min, modf, pi, random, randomseed, sin, sqrt, tan, atan, atan2, deg, rad, ldexp, frexp, sinh, cosh, tanh, type, tointeger, maxinteger, mininteger

string

format, len, sub, upper, lower, rep, reverse, find, match, gmatch, gsub, byte, char, dump, pack, unpack, packsize

table

insert, remove, sort, concat, unpack, move

bit32

band, bor, bxor, bnot, lshift, rshift, arshift, extract, replace, btest, countlz, countrz

基本関数

type, tostring, tonumber, select, error, assert, print, ipairs, pairs, next, unpack, setmetatable, getmetatable, rawget, rawset, rawequal, rawlen, pcall, xpcall


クイックリファレンス

obj (変更可能メンバー)

メンバー説明
obj.x / obj.y / obj.z描画位置 (ピクセル)
obj.ox / obj.oy / obj.oz回転中心のオフセット (ピクセル)
obj.zoom拡大率 (sx と sy の平均、1.0 で等倍)
obj.aspectアスペクト比 (-1.0 ~ 1.0)
obj.sx / obj.syX / Y 方向の独立拡大率
obj.alpha不透明度 (0.0 ~ 255.0)
obj.rx / obj.ry / obj.rzX / Y / Z 軸回転角 (度)
obj.rxr / obj.ryr / obj.rzrX / Y / Z 軸回転角 (ラジアン)

obj (読み取り専用メンバー)

メンバー説明
obj.w / obj.h入力画像の幅 / 高さ (ピクセル)
obj.hw / obj.hh幅 / 高さの半分
obj.cx / obj.cy画像中心 (= hw / hh)
obj.cz / obj.sz常に 0 / 常に 1
obj.diagonal対角線長 (sqrt(w^2 + h^2))
obj.time / obj.frameアイテム基準の経過時間 (秒) / フレーム
obj.totalframe / obj.totaltimeアイテムの総フレーム数 / 総時間 (秒)
obj.tアイテム内の進行割合 (0.0 ~ 1.0)
obj.framerateフレームレート (fps)
obj.layerレイヤー番号
obj.index / obj.numグループ内のインデックス / 総数
obj.track0 ~ obj.track3エフェクトパネルの各トラックバーの値

obj 関数

関数説明
obj.getobject(tag [, frame])他アイテムの描画情報を取得 (本ページ 12 章)
obj.getpixel(x, y)ピクセル取得
obj.setpixel(x, y, r, g, b [, a])ピクセル書き込み
obj.getpixeldata()ピクセル直接アクセスプロキシ
obj.putpixeldata()互換用の空実装

非対応・制限事項


Luaスクリプト for YMM4 / MIT License / https://github.com/routersys/YMM4-LuaScript