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
4 changes: 3 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- シェーダー本体、C#、Humanoid のアニメーション・ボーンのカーブ・Animator Controller、Expression メニュー等は読み込まない。
Generic な `.anim`(シェイプキー・表示の切り替え・オブジェクトの Transform)は、ミュートした NLA トラックの Action にする(`core/animation.py`。#209)。
値が変わらないか 0.1 秒以下で終わる、状態を切り替えるだけのクリップ(Animator のステート用)は読み込まない。
FBX の中のテイクは、.meta の clipAnimations どおりのクリップにして同じく NLA に並べ、同じ骨格のモーションだけのモデルは
レストの違いを変換して 1 体のアーマチュアにまとめる(`blender/fbx_motion.py`。#276)。オプションは Animation(Muted / Active / None)。

## 設計の要点(詳細は DESIGN.md)

Expand Down Expand Up @@ -49,7 +51,7 @@
```
unitypackage_loader/ Extension 本体(blender_manifest.toml、Blender 4.5 以降の Extension 形式)
core/ bpy 非依存。単体テスト可能。bpy を import しないこと
blender/ bpy 依存(importer / options / log / prepare / model_import / session_materials / scene_placement / scene_objects / terrain / unity_meshes / animation / materials / nodes / textures / toon_group / outline)
blender/ bpy 依存(importer / options / log / prepare / model_import / session_materials / scene_placement / scene_objects / terrain / unity_meshes / animation / fbx_motion / materials / nodes / textures / toon_group / outline)
operators/ File > Import、モデル選択ダイアログ、再構築、アウトライン
ui/ Preferences、サイドバー "UPI" タブ
tests/ unittest(bpy 不要)+ Blender 上の統合テスト+合成パッケージ生成
Expand Down
43 changes: 40 additions & 3 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ Material:
- マテリアル: `.mat` を解析し、**Blender で意味を持つ情報だけ**をノードに反映。
- テクスチャ: `.mat` から参照されている画像のみ展開して読み込む(オプションで全画像)。
- アニメーションクリップ: Generic な `.anim` のシェイプキー・表示の切り替え・オブジェクトの Transform を、クリップごとの Action にしてミュートした NLA トラックに並べる(§4.7.1。#209)。
- モデルの中のモーション: FBX のテイクを、.meta の `clipAnimations` どおりのクリップにして NLA に並べる。同じ骨格のモーションだけのモデルは 1 体のアーマチュアにまとめる(§4.7.2。#276)。

### 読み込まない
- シェーダー本体(`.shader` `.cginc`)、C#(`.cs` `.dll`)、Animator Controller(`.controller`)と Humanoid の `.anim`・ボーンのカーブ(§4.7.1)、Expression/Parameter アセット、PhysBone 等の MonoBehaviour、prefab 階層(シーンを読み込む場合はモデルの配置と、その上の GameObject、ライト・カメラだけ再現する)、アイコン画像、`preview.png`。
Expand Down Expand Up @@ -364,8 +365,8 @@ File > Import > Unitypackage (.unitypackage) .unitypackage を 3D View に
| VRM via VRM Add-on | Bool = ON | `.vrm` を VRM add-on に委譲する。add-on が無ければ glTF インポーターにフォールバックし警告で案内 |
| Import bundled .blend files | Bool = OFF | 同梱 `.blend` を append する。`.blend` はドライバー式等で Python を実行し得るため既定 OFF。ON のとき UI に警告を出す。OFF なら該当モデルはスキップして警告に理由を出す |
| FBX Importer | Enum: `Auto` / `New (C++)` / `Legacy (Python)` = `Auto` | `Auto` と New は `wm.fbx_import`、Legacy だけ `import_scene.fbx`。Scale(常に表示)はモデルのインポーターに渡す |
| Import Animation / Ignore Leaf Bones | Bool = OFF / ON | FBX インポーターへ渡す(アーマチュアとシェイプキーは常に読む) |
| Animation Clips | Bool = ON | パッケージの Generic な `.anim` を Action にし、ミュートした NLA トラックで並べる(§4.7.1。読み込んだ直後の見た目は変わらないので既定 ON) |
| Animation | Enum: `Muted` / `Active` / `None` = `Muted` | モデルのテイク(§4.7.2)とパッケージの `.anim`(§4.7.1)を NLA トラックにする。`Muted` はすべてミュート(読み込んだ直後の見た目は変わらない)、`Active` はオブジェクトごとに最初の 1 本だけミュートを外す(上のトラックが下を上書きするので 1 本だけ)、`None` は読まない(FBX インポーターの `use_anim` も切る)。以前の Import Animation(FBX に渡すだけ、既定 OFF)と Animation Clips(#209)を 1 つにした(#276) |
| Ignore Leaf Bones | Bool = ON | FBX インポーターへ渡す(アーマチュアとシェイプキーは常に読む) |

**Materials**
| 項目 | 型 / 既定値 | 説明 |
Expand Down Expand Up @@ -656,6 +657,7 @@ unitypackage_loader/
│ ├─ terrain.py # Terrain のメッシュと塗り分けのマテリアル(SessionTerrains。#205)
│ ├─ unity_meshes.py # Unity の Mesh アセットから作るメッシュ(SessionMeshes。#216)
│ ├─ animation.py # クリップの Action と NLA トラック(ClipImporter。#209)
│ ├─ fbx_motion.py # モデルのテイクのクリップへの分割、モーションだけのアーマチュアのまとめ(MotionBuilder。#276)
│ ├─ log.py # 捕まえて続けた例外のログ(Verbose Console Log はオペレーターが渡す)
│ ├─ scene_objects.py # シーンの Empty・ライト・カメラ・複製・行列の当てはめ(計算は core/model_space.py)・非表示
│ ├─ materials.py # ノード生成
Expand Down Expand Up @@ -1008,7 +1010,8 @@ def resolve(model_entry, fbx_material_names, index) -> dict[str, str | None]:

1 回の読み込みは `_ImportSession`(`importer.py`)が手順を並べる: 解析(`analyze`)→ 読み込むモデルの決定 → 展開(`extract`)→
画像の読み込み → prefab のグループごと(`import_group`)かシーンごと(`SceneImport`。`scene_placement.py`)のモデルの読み込み →
足りなかったテクスチャの補い(`fill_missed_textures`)→ アニメーションクリップ(`import_clips`。§4.7.1)。マテリアルの対応付けと組み立ては `SessionMaterials`(`session_materials.py`)、
足りなかったテクスチャの補い(`fill_missed_textures`)→ モーションだけのアーマチュアのまとめ(`merge_motion_rigs`。§4.7.2)→ アニメーションクリップ(`import_clips`。§4.7.1)。
モデルのテイクは、モデルを読み込むたびに `build_model` から `MotionBuilder.arrange` で NLA に並べる(§4.7.2)。マテリアルの対応付けと組み立ては `SessionMaterials`(`session_materials.py`)、
モデルファイルの読み込みは `model_import.py` にある。以前は `importer.py` 1 つ(約 1,900 行)だったのを、役割ごとに分けた(#198)。
下の擬似コードは最初の設計で、流れを示すもの。

Expand Down Expand Up @@ -1103,6 +1106,40 @@ def run(ctx, filepath, opts) -> Report:
`clip_notes` に出す(警告にはしない)。
- Python から `NlaTrack.mute` を切り替えても依存関係は組み直されず、一度評価したトラックは切り替わらない(NLA エディターでの切り替えでは組み直される)。
統合テストはコレクションを付け外しして組み直させてから値を確かめる。
- Animation が `Active` なら、対象ごとに最初のトラックだけミュートを外す(モデルのテイクのトラックが先にあれば、そちらが残る)。

### 4.7.2 モデルの中のモーション(#276)

モーション集(32 RPG Animations)や `@` 付きの FBX(Everyday Motion Pack)は、モーションを `.anim` ではなく FBX のテイクに持つ。
Blender の FBX インポーターはテイクごとにテイクの名前の Action を作り、最初のテイクをアクティブにする(多くは 1 フレームの T ポーズ)。

- **クリップの設定**(`core/meta.py` の `ModelClip`): .meta の `animations.clipAnimations` の `name` / `takeName` / `firstFrame` /
`lastFrame` / `loopTime` と、`importAnimation`(0 ならテイクを並べない)。フレームは FBX のフレームレートで、テイクの時刻と同じ数え方。
- **NLA**(`blender/fbx_motion.py` の `MotionBuilder.arrange`): モデルを読み込むたびに、新しい Action をテイクの名前で引き
(.meta のテイク名と完全一致 → Blender の連番を外した形)、クリップごとにクリップの名前のトラックとストリップを足す。
ストリップの範囲は firstFrame / lastFrame + 1(FBX インポーターの `anim_offset`)で、同じテイクのクリップは Action を共有する。
スロットの対象は、スロットの識別子と読み込んだオブジェクトの FBX のノード名(`unity_fbx_node`)で引く。クリップの設定が無ければ
テイクごとに全体を 1 クリップにする。0.1 秒以下のクリップ(`STATE_CLIP_MAX_LENGTH`。T ポーズのテイク)は置かない。
どのクリップにも使われないテイクの Action は消し、アクティブな Action は外す(読み込んだ直後の見た目をアニメーションを読まない
ときと同じにする)。Action には `unity_take` / `unity_take_model` を残す。
- **使い回し**: シーンで同じモデルを配置ごとに読み直すときは、最初に読んだときの Action を使い、新しく作られた分は消す。
前の読み込みが失敗して Action が消えていたら作り直す。
- **アーマチュアのまとめ**(`merge_motion_rigs`。Models 単位): メッシュが無く、アーマチュアが 1 体で、トラックを持つモデルを
モーションだけのモデルとし、骨格(ボーンの名前と親の組)が含まれる関係にあり、置き場所(親のワールドの行列)が同じものを
1 体にまとめる。残すのはボーンがいちばん多く、その中で先に読んだもの。
- スキンの無い FBX はファイルの既定の姿勢をレストにするので、レストはファイルごとに違う(32 RPG Animations では指のボーンで
100 以上違った)。親からの相対のレストを R(元)、R'(残す側)とすると、ローカルの行列 B を B' = R'⁻¹·R·B にすれば同じ姿勢になる
(`retarget_action`)。キーの時刻ごとに求め直し、LINEAR のキーにする(フレームごとにキーがあるので見た目は変わらない)。
- FBX のルートのボーンがアーマチュアのオブジェクトになったもの(Everyday Motion Pack の Bip001)は、オブジェクトの Transform が
ファイルの最初のフレームの値で、モーションごとに違う。オブジェクトのカーブは絶対値なのでそのまま使い、動かさないチャンネルには
元のアーマチュアの値の一定のキーを足す。
- 32 RPG Animations(31 体 → 1 体)と Everyday Motion Pack(244 体 → 1 体。21 秒)で、元の FBX を直接読んだときと、ボーンの
ワールドの姿勢が 1e-4 以内で一致することを確かめた。
- **レポート**: 置いたクリップを `clips`("モデルのパス: クリップ名")に、外したクリップの数・見つからなかったテイク・まとめた
アーマチュアの数を `clip_notes` に出す。
- FBX インポーターは、アニメーションを読むかどうかによらずシーンのフレームレートをファイルのものにする(今までと同じ)。
フレームレートの違う FBX を続けて読むと、先に読んだ FBX のモーションの速さがずれる(未対応)。
- ループ(`loopTime`)はまだ NLA に反映しない(ストリップは範囲の終わりで止まる)。

### 4.8 `blender/textures.py`

Expand Down
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A Blender Extension that imports `.unitypackage` files directly from File > Impo
(including armatures and shape keys) with the material settings and textures from the Unity side applied.

- Supported Blender: 4.5 or later (developed on 5.2 LTS, tested on 4.5 LTS and 5.2 LTS)
- What is imported: FBX / OBJ / glTF / VRM / Collada (Blender 4.5 only; removed in Blender 5.0) / bundled .blend files (opt-in), `.mat` files (lilToon / MToon / Poiyomi / Standard / URP / HDRP / VRChat Mobile (Quest) shaders; others on a best-effort basis using generic rules), referenced textures, and Generic animation clips (`.anim`: blend shapes, object visibility and transforms) as actions on muted NLA tracks
- What is imported: FBX / OBJ / glTF / VRM / Collada (Blender 4.5 only; removed in Blender 5.0) / bundled .blend files (opt-in), `.mat` files (lilToon / MToon / Poiyomi / Standard / URP / HDRP / VRChat Mobile (Quest) shaders; others on a best-effort basis using generic rules), referenced textures, and animation as actions on muted NLA tracks: the animation takes inside models (split into Unity's clips; motion-only FBX files with the same skeleton are merged into one armature) and Generic animation clips (`.anim`: blend shapes, object visibility and transforms)
- What is not imported: shader source code, C# scripts, Humanoid animation clips, animation curves on bones, Animator Controllers, Expression menus, etc. Prefab hierarchies, lights and cameras are reproduced only when a scene is imported.
- VRM: when the [VRM format](https://extensions.blender.org/add-ons/vrm/) add-on is installed, `.vrm` models are handed to it (MToon materials, humanoid rig, spring bones and expressions are reproduced by the add-on). Without it, the glTF importer is used and materials are rebuilt from the bundled `.mat` files.

Expand Down Expand Up @@ -79,8 +79,8 @@ The options show Material Mode and Scale; everything else is folded into the Mod
| (always shown) | Material Mode | `Auto` (Toon for toon-style shaders, Principled for PBR) / `Principled BSDF` / `Toon (Node Group)` / `Unlit (Emission)` / `Names Only` |
| | Scale | Scale passed to the model importer |
| Model | FBX Importer | Prefer the new C++ importer (`Auto`) / choose explicitly |
| | Import Animation / Ignore Leaf Bones | Passed to the FBX importer (OFF / ON by default) |
| | Animation Clips | Turn the package's Generic `.anim` clips into actions on muted NLA tracks (default ON). See [Animation clips](#animation-clips) |
| | Animation | `Muted` (default): import the models' animation takes and the package's `.anim` clips as muted NLA tracks / `Active`: the same, but unmute the first track of each object / `None`: do not import animation. See [Animation clips](#animation-clips) |
| | Ignore Leaf Bones | Passed to the FBX importer (default ON) |
| | VRM via VRM Add-on | Import `.vrm` with the VRM format add-on when installed (default ON); otherwise fall back to the glTF importer and rebuild materials from `.mat` |
| | Import Bundled .blend Files | Append objects from `.blend` files inside the package (default OFF). See [Security notes](#security-notes) |
| Materials | Force Opaque | Ignore Unity's Cutout / Transparent settings |
Expand Down Expand Up @@ -172,6 +172,26 @@ Textures > Extract To to `Custom Path` and choose a short folder such as `C:\upi

### Animation clips

Animation is added as **muted** NLA tracks named after each clip, so the scene looks the same right after the import.
To preview a clip, unmute its track in the NLA editor (or click its star to solo it), or select the strip and press Tab to
edit the action. With Animation set to `Active`, the first track of each object is unmuted instead (only one, because
unmuted NLA tracks override each other).

#### Motions inside models

Animation takes inside FBX files (for example motion packs, or `model@motion.fbx` files) are split into the clips that the
model's `.meta` defines in Unity (Model Import Settings > Animation: name, first and last frame). All clips of a take share
one action, and each strip plays its own frame range. Models without clip settings get one clip per take. Short clips
(0.1 seconds or shorter, such as a T-pose take) are skipped, and the active action that the FBX importer assigns is cleared.

When several motion-only models (no meshes, one armature) share the same skeleton, they are merged into one armature
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.

#### Generic `.anim` clips

Generic animation clips (`.anim`) in the package become one action per clip. These curves are read:

- Blend shape weights (`blendShape.<name>`, 0–100 in Unity) → shape key values (0–1)
Expand All @@ -180,16 +200,14 @@ 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 is added as a **muted** NLA track (named after the clip) on every object or shape key it animates, so the
scene looks the same right after the import. To preview a clip, unmute its track in the NLA editor (or click its star to solo it),
or select the strip and press Tab to edit the action. Keys start at the scene's start frame and use the scene's frame rate.
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.

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,
the steering angle of a vehicle, and so on), so they have nothing to play back in Blender.

Not imported (the report lists how many were skipped): state clips (above), Humanoid clips (the body motion is stored as muscle curves, so the whole clip is skipped, including its other curves),
Not imported (the report lists how many were skipped): state clips (above), Humanoid clips (the body motion is stored as muscle curves, so the whole clip is skipped, including its other curves; the original motion is usually in an FBX in the package, which is imported as above),
curves on bones, clips that only animate the object they are played on (no path, so the object is unknown), and curves on
materials or object references. Clips whose paths match no imported object are skipped too. Animator Controllers are not read.

Expand Down
Loading
Loading