Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,8 @@ def run(ctx, filepath, opts) -> Report:
- **Action と NLA**(`blender/animation.py`): クリップごとに Action を 1 つ作り(`unity_clip_guid` / `unity_clip_path`)、対象のデータブロック
(オブジェクトかシェイプキーの Key)ごとにスロットを作る(Blender 4.4 以降のスロット付き Action)。対象ごとにクリップの名前の NLA トラックを
ミュートで足すので、読み込んだ直後の見た目は変わらない。キーはクリップの開始からの秒 × シーンのフレームレートで、ストリップはシーンの開始フレームに置く。
ループするクリップ(`m_LoopTime`)の F カーブには Cycles モディファイアーを付ける。
ループするクリップ(`m_LoopTime`)は、ストリップの繰り返し(`repeat`)をシーンのフレーム範囲の終わりまでにする(`fbx_motion.loop_strip`)。
NLA はストリップの範囲の外を評価しないので、F カーブの Cycles モディファイアー(これも付ける。Action を直接再生するとき用)だけではループしない。
- シェイプキー: 値を 1/100 にしてキーと接線をそのままベジェにする。100 を超える重みは `slider_max` を広げる。
- 表示: `hide_viewport` / `hide_render` を階段(CONSTANT)で、値が 0.5 未満なら隠す。`m_IsActive` は Unity の階層での子孫にも当てる
(スキンメッシュはアーマチュアの子孫にしない)。
Expand Down Expand Up @@ -1145,7 +1146,7 @@ Blender の FBX インポーターはテイクごとにテイクの名前の Act
アーマチュアの数を `clip_notes` に出す。
- FBX インポーターは、アニメーションを読むかどうかによらずシーンのフレームレートをファイルのものにする(今までと同じ)。
フレームレートの違う FBX を続けて読むと、先に読んだ FBX のモーションの速さがずれる(未対応)。
- ループ(`loopTime`)はまだ NLA に反映しない(ストリップは範囲の終わりで止まる)。
- ループ(`loopTime`)は、`.anim` と同じくストリップの繰り返しにする(`loop_strip`。まとめたアーマチュアに写すときも回数を引き継ぐ)。

### 4.8 `blender/textures.py`

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ When several motion-only models (no meshes, one armature) share the same skeleto
that has all the motions as NLA tracks (Models only). An FBX without a skin uses its default pose as the rest pose, so
the rest pose differs from file to file; the keys are converted so that each motion keeps its pose on the remaining
armature. A skeleton that lacks some bones (for example weapon bones) is merged too; the missing bones stay at rest.
When a scene places the same model many times, the actions of its takes are shared.
When a scene places the same model many times, the actions of its takes are shared. Looping clips (Loop Time in Unity)
repeat their strip until the end of the scene's frame range.

#### Generic `.anim` clips

Expand All @@ -201,7 +202,7 @@ Generic animation clips (`.anim`) in the package become one action per clip. The
A clip does not say which model it belongs to, so the importer looks for the imported object under which the most curve
paths resolve, and treats it as the object the clip is played on (all placements of the same model get the clip).
Each clip gets an NLA track on every object or shape key it animates. Keys start at the scene's start frame and use the scene's frame rate.
Looping clips get a Cycles modifier on their F-curves.
Looping clips (Loop Time in Unity) repeat their strip until the end of the scene's frame range.

Clips that only switch a state are skipped: clips whose values never change, or that end within 0.1 seconds. Unity's
Animator uses such clips as states and makes the motion with transitions (outfit toggles and expressions of VRChat avatars,
Expand Down
5 changes: 3 additions & 2 deletions README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ FBX の中のアニメーションのテイク(モーション集や、`モデ
同じ骨格のモーションだけのモデル(メッシュが無く、アーマチュアが 1 体)が複数あれば、1 体のアーマチュアにまとめて、全モーションを
NLA トラックとして並べます(Models 単位)。スキンの無い FBX はファイルの既定の姿勢をレストにするので、レスト姿勢はファイルごとに
違います。残すアーマチュアでも同じ姿勢になるよう、キーを変換します。一部のボーン(武器用のボーンなど)が無い骨格もまとめます
(無いボーンはレストのまま)。シーンで同じモデルを何度も置くときは、テイクの Action を共有します。
(無いボーンはレストのまま)。シーンで同じモデルを何度も置くときは、テイクの Action を共有します。ループするクリップ(Unity の Loop Time)は、
シーンのフレーム範囲の終わりまでストリップを繰り返します。

#### Generic な `.anim`

Expand All @@ -181,7 +182,7 @@ NLA トラックとして並べます(Models 単位)。スキンの無い FB

クリップは対象のモデルを持たないので、カーブの階層パスがいちばん多く当てはまる読み込んだオブジェクトを、クリップを再生する
オブジェクトとみなします(同じモデルを複数置いていれば、すべてに付けます)。動かすオブジェクトとシェイプキーごとに
NLA トラックを足します。キーはシーンの開始フレームから、シーンのフレームレートで並べます。ループするクリップの F カーブには Cycles モディファイアーを付けます。
NLA トラックを足します。キーはシーンの開始フレームから、シーンのフレームレートで並べます。ループするクリップ(Unity の Loop Time)は、シーンのフレーム範囲の終わりまでストリップを繰り返します。

状態を切り替えるだけのクリップ(値が変わらないもの、0.1 秒以下で終わるもの)は読み込みません。Unity の Animator はこうしたクリップを
ステートとして使い、動きは遷移で作ります(VRChat のアバターの衣装の切り替えや表情、車両のハンドルの角度など)。
Expand Down
3 changes: 2 additions & 1 deletion tests/expectations_synthetic_anim.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
{ "track": "HatOff", "object": "Hat", "seconds": 0, "hidden": false },
{ "track": "HatOff", "object": "Hat", "seconds": 0.75, "hidden": true },
{ "track": "Wave", "object": "Body", "seconds": 0, "hidden": true },
{ "track": "DoorOpen", "object": "Door", "rotation_delta": { "from": 0, "to": 1, "axis": [0, 0, 1], "degrees": -90 } },
{ "track": "DoorOpen", "object": "Door", "rotation_delta": { "from": 0, "to": 0.5, "axis": [0, 0, 1], "degrees": -45 } },
{ "track": "DoorOpen", "object": "Door", "rotation_delta": { "from": 0, "to": 1.5, "axis": [0, 0, 1], "degrees": -45 } },
{ "track": "DoorOpen", "object": "Knob", "seconds": 0, "distance_to": "Door", "distance": 111.8034 }
]
}
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations_synthetic_anim_scene.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@
"object": "Door",
"rotation_delta": {
"from": 0,
"to": 1,
"to": 0.5,
"axis": [
0,
0,
1
],
"degrees": -90
"degrees": -45
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion tests/expectations_synthetic_motion.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"values": [
{ "track": "Walk_Start", "object": "Rig", "seconds": 0.2, "bone_direction": { "bone": "Spine", "vector": [0, -0.2938, 0.9559] } },
{ "track": "Walk", "object": "Rig", "seconds": 0.5, "bone_direction": { "bone": "Spine", "vector": [0, -0.7071, 0.7071] } },
{ "track": "Walk", "object": "Rig", "seconds": 1, "bone_direction": { "bone": "Spine", "vector": [0, -1, 0] } },
{ "track": "Walk", "object": "Rig", "seconds": 1.5, "bone_direction": { "bone": "Spine", "vector": [0, -0.7071, 0.7071] } },
{ "track": "Rig|run_take", "object": "Rig", "seconds": 1.5, "bone_direction": { "bone": "Spine", "vector": [0, -0.5039, 0.8638] } },
{ "track": "Rig|run_take", "object": "Rig", "seconds": 0, "bone_direction": { "bone": "Spine", "vector": [0, 0.4961, 0.8682] } },
{ "track": "Rig|run_take", "object": "Rig", "seconds": 1, "bone_direction": { "bone": "Spine", "vector": [0, -0.5039, 0.8638] } },
{ "track": "CharRig|wave_take", "object": "CharRig", "seconds": 1, "bone_direction": { "bone": "Spine", "vector": [0, -0.7071, 0.7071] } }
Expand Down
3 changes: 3 additions & 0 deletions unitypackage_loader/blender/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from ..core.animation_targets import Binding, TargetObject, TargetTree, bind_paths
from ..core.model_space import ModelUnits, local_basis, local_values
from ..core.transform import IDENTITY, UNITY_TO_BLENDER, Mat4, inverse_affine
from .fbx_motion import loop_strip
from .log import warn_skipped
from .model_import import FBX_NODE_PROP, MODEL_GUID_PROP, node_name

Expand Down Expand Up @@ -205,6 +206,8 @@ def _add_track(self, datablock, action, slot, name: str) -> None:
nla_strip = track.strips.new(name, self.frame_start, action)
if nla_strip.action_slot != slot:
nla_strip.action_slot = slot
if action.use_cyclic:
loop_strip(nla_strip, self.scene)
track.mute = not active

# 各種のカーブ ------------------------------------------------------
Expand Down
19 changes: 18 additions & 1 deletion unitypackage_loader/blender/fbx_motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from __future__ import annotations

import math
import re
from dataclasses import dataclass, field

Expand Down Expand Up @@ -63,6 +64,7 @@ class _Clip:
action: bpy.types.Action
start: float # Blender のフレーム(Action の中の範囲)
end: float
loop: bool = False


def _slot_owner(slot, objects: list[bpy.types.Object]):
Expand Down Expand Up @@ -184,7 +186,7 @@ def _clips(self, info: ModelImporterInfo | None, cached: dict[str, bpy.types.Act
if count:
self.stats.state += 1
continue
clips.append(_Clip(setting.name, action, setting.first_frame + ANIM_OFFSET, setting.last_frame + ANIM_OFFSET))
clips.append(_Clip(setting.name, action, setting.first_frame + ANIM_OFFSET, setting.last_frame + ANIM_OFFSET, setting.loop))
return clips

def _add_track(self, owner: bpy.types.ID, clip: _Clip, slot) -> None:
Expand All @@ -196,9 +198,23 @@ def _add_track(self, owner: bpy.types.ID, clip: _Clip, slot) -> None:
if strip.action_slot != slot:
strip.action_slot = slot
set_strip_range(strip, clip.start, clip.end)
if clip.loop:
loop_strip(strip, self.scene)
track.mute = not active


def loop_strip(strip, scene: bpy.types.Scene) -> None:
"""ループするクリップ(Unity の Loop Time)のストリップを、シーンのフレーム範囲の終わりまで繰り返す。

NLA はストリップの範囲の外を評価しない(Action の Cycles モディファイアーは範囲の中でしか働かない)ので、繰り返しの回数で表す。
"""
length = strip.action_frame_end - strip.action_frame_start
if length <= 0:
return
span = scene.frame_end - strip.frame_start + 1
strip.repeat = float(max(1, math.ceil(span / length)))


def set_strip_range(strip, start: float, end: float) -> None:
"""ストリップが使う Action の範囲を変え、ストリップの長さをそれに合わせる(置く位置は変えない)。"""
begin = strip.frame_start
Expand Down Expand Up @@ -304,6 +320,7 @@ def copy_track(animation_data, track, strip, action: bpy.types.Action) -> None:
if slot is not None and new_strip.action_slot != slot:
new_strip.action_slot = slot
set_strip_range(new_strip, strip.action_frame_start, strip.action_frame_end)
new_strip.repeat = strip.repeat
new_track.mute = track.mute or has_active


Expand Down
2 changes: 1 addition & 1 deletion unitypackage_loader/blender_manifest.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
schema_version = "1.0.0"

id = "unitypackage_loader"
version = "1.18.0-dev.12"
version = "1.18.0-dev.13"
name = "Unitypackage Importer"
tagline = "Import meshes, materials and textures from .unitypackage files"
maintainer = "utagestudio"
Expand Down
Loading