所有分类
  • 所有分类
  • blender模型
  • 资源精选
  • 工程/原创
  • blender插件
  • HDR合集
  • 贴图纹理
  • 材质资产
  • 学习资料

多物体同步操作修改器插件

多物体添加、修改、删除修改器同步

我只在4.2测试过,其他版本理论上应该也可以

2025-02-06_11-55-24.jpg

通过网盘分享的文件:tongbuxiugaiqi.py

直接在软件里面选择py格式安装

 

源码

import bpy
# 记录上一次修改器状态
last_modifier_states = {}
# 记录上一次选中的对象
last_selected_objects = []
bl_info = {
    "name": "同步修改器",
    "author": "老黄",
    "version": (1, 0),
    "blender": (4, 2, 0),
    "location": "Properties > Modifiers",
    "description": "同步选中的多个物体修改器的添加,修改,删除操作。",
    "category": "Object",
}
def get_modifier_state(modifier):
    """
    获取修改器的状态,以元组形式返回可写属性的值
    """
    state = []
    for prop in modifier.rna_type.properties:
        if not prop.is_readonly:
            state.append(getattr(modifier, prop.identifier))
    return tuple(state)
def sync_modifier_changes(scene):
    global last_active_modifier_states, last_selected_objects
    context = bpy.context
    selected_objects = context.selected_objects
    active_object = context.active_object
    # 如果选中对象没有变化且活动对象没有变化,直接返回
    if selected_objects == last_selected_objects and active_object:
        current_active_states = {}
        for modifier in active_object.modifiers:
            modifier_key = (active_object.name, modifier.name)
            current_active_states[modifier_key] = get_modifier_state(modifier)
        # 检查是否有修改器添加
        for key in set(current_active_states) - set(last_active_modifier_states):
            sync_add_modifier(active_object, selected_objects, key)
        # 检查是否有修改器属性变化或删除
        for key in set(last_active_modifier_states):
            if key in current_active_states:
                if current_active_states[key] != last_active_modifier_states[key]:
                    sync_modify_modifier(active_object, selected_objects, key)
            else:
                sync_delete_modifier(active_object, selected_objects, key)
        last_active_modifier_states = current_active_states
    else:
        # 更新选中对象和活动对象状态
        last_selected_objects = selected_objects
        if active_object:
            last_active_modifier_states = {}
            for modifier in active_object.modifiers:
                modifier_key = (active_object.name, modifier.name)
                last_active_modifier_states[modifier_key] = get_modifier_state(modifier)
def sync_add_modifier(active_object, selected_objects, modifier_key):
    """
    同步添加修改器到其他选中对象
    """
    modifier_name = modifier_key[1]
    modifier = next((m for m in active_object.modifiers if m.name == modifier_name), None)
    if modifier:
        modifier_type = modifier.type
        for obj in selected_objects:
            if obj != active_object and obj.type == 'MESH':
                existing_mod = next((mod for mod in obj.modifiers if mod.type == modifier_type and mod.name == modifier_name), None)
                if existing_mod is None:
                    new_mod = obj.modifiers.new(name=modifier_name, type=modifier_type)
                else:
                    new_mod = existing_mod
                for prop in modifier.rna_type.properties:
                    if not prop.is_readonly:
                        setattr(new_mod, prop.identifier, getattr(modifier, prop.identifier))
def sync_modify_modifier(active_object, selected_objects, modifier_key):
    """
    同步修改修改器属性到其他选中对象
    """
    modifier_name = modifier_key[1]
    modifier = next((m for m in active_object.modifiers if m.name == modifier_name), None)
    if modifier:
        modifier_type = modifier.type
        for obj in selected_objects:
            if obj != active_object and obj.type == 'MESH':
                for mod in obj.modifiers:
                    if mod.type == modifier_type and mod.name == modifier_name:
                        for prop in modifier.rna_type.properties:
                            if not prop.is_readonly:
                                setattr(mod, prop.identifier, getattr(modifier, prop.identifier))
def sync_delete_modifier(active_object, selected_objects, modifier_key):
    """
    同步删除修改器到其他选中对象
    """
    modifier_name = modifier_key[1]
    for obj in selected_objects:
        if obj != active_object and obj.type == 'MESH':
            for mod in obj.modifiers[:]:
                if mod.name == modifier_name:
                    obj.modifiers.remove(mod)
def register():
    bpy.app.handlers.depsgraph_update_post.append(sync_modifier_changes)
def unregister():
    bpy.app.handlers.depsgraph_update_post.remove(sync_modifier_changes)
if __name__ == "__main__":
    register()
资源下载此资源仅限注册用户下载,请先
微信:bude6688
资源下载
下载价格免费
微信:bude6688
1
仅供交流学习研究使用,版权归原作者所有,禁止商业使用。

评论0

请先

扫码登录后若显示未登录,无需担心,这是显示异常,实则已登录,可直接下载所需资源。】 【 本月网站遭大量攻击,运行不太稳定。客服微信:bude6688赞助一下
显示验证码
没有账号?注册  忘记密码?

社交账号快速登录

豫公网安备 41152602000203号