# Blender 形态键批量删除脚本

### Why

因为 Blender CM3D2 插件在转移形态键时会全部转移，没有办法控制某个形态键是否转移，因为必须将其删除。

像 LoBody 改之类的体型，有 400 多个形态键，如果你不删除掉不想要的形态键的话，转移形态键可能要非常久。

<br>

### Mute 是什么

mute 就是静音，就是取消打勾

![图片](https://github.com/user-attachments/assets/5241fb7d-8033-4348-b64e-fdd60884c16b)

<br>

### 如何使用

选中你想修改的网格，最好是物体模式

然后切换到 scripting 模式，点新建

然后粘贴下面的脚本并执行

![图片](https://github.com/user-attachments/assets/9b8c8f57-fe84-44ee-86ef-c1b4dcf5fb9a)

<br>

### 工作流

#### 如果你想批量删除不想要的形态键 选项 A

1. 手动 mute 需要的形态键
2. 使用 `反转形态键的 mute 状态` 脚本
3. 使用 `删除所有 mute 的形态键` 脚本

#### 如果你想批量删除不想要的形态键 选项 B

1. 使用 `mute 所有形态键` 脚本
2. 手动取消 mute 想要的形态键
3. 使用 `删除所有 mute 的形态键` 脚本

<br>

### 脚本

许可证：The Unlicense

#### 反转形态键的 mute 状态：

```
import bpy

# 获取当前选中的对象
obj = bpy.context.active_object

# 确保对象存在并具有形态键
if obj and obj.data.shape_keys:
    shape_keys = obj.data.shape_keys.key_blocks
    for shape_key in shape_keys:
        # 反转形态键的 mute 状态
        shape_key.mute = not shape_key.mute
        print(f"Shape Key '{shape_key.name}' mute状态已切换为: {shape_key.mute}")
else:
    print("选中的对象没有形态键或没有选中对象。")
```

#### mute 所有形态键：

```
import bpy

# 获取当前选中的对象
obj = bpy.context.active_object

# 确保对象存在并具有形态键
if obj and obj.data.shape_keys:
    shape_keys = obj.data.shape_keys.key_blocks
    for shape_key in shape_keys:
        # 设置 mute 为 True
        shape_key.mute = True
        print(f"Shape Key '{shape_key.name}' 已被 mute.")
else:
    print("选中的对象没有形态键或没有选中对象。")

```

#### 删除所有 mute 的形态键：

```
import bpy

# 获取当前选中的对象
obj = bpy.context.active_object

# 确保对象存在并且具有形态键
if obj and obj.data.shape_keys:
    # 获取形态键列表
    shape_keys = obj.data.shape_keys
    key_blocks = shape_keys.key_blocks
    
    # 收集需要删除的形态键名称，但跳过基础形态键 Basis
    keys_to_delete = [key_block.name for key_block in key_blocks  if key_block.mute and key_block.name != "Basis"]
    
    # 删除静音状态的形态键
    for key_name in keys_to_delete:
        obj.shape_key_remove(key_blocks[key_name])
    
    print(f"已删除 {len(keys_to_delete)} 个静音状态的形态键。")
else:
    print("当前选中的对象没有形态键。")
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://90135.gitbook.io/com3d2_simple_mod_guide_chinese/fu-lu/blender-xing-tai-jian-pi-liang-shan-chu-jiao-ben.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
