c4d的重命名工具(支持模型和材质) 及 python窗口定义

不是我牛逼,是豆包牛逼!

一个简化版的窗口

import c4d
from c4d import gui

class MyDialog(gui.GeDialog):
    def __init__(self):
        super().__init__()
        self.SetTitle("My Dialog")

    def CreateLayout(self):
        # 设置对话框布局
        return True

my_dialog = MyDialog()
my_dialog.Open(c4d.DLG_TYPE_ASYNC, xpos=100, ypos=100, defaultw=300, defaulth=200)

进化了的重命名工具

保存为py文件单独调用会运行速度更快

 使用方法:扩展菜单,用户脚本,运行脚本

import c4d
from c4d import gui

# 根据自定义的元素名设定 ID 号:
Title_Text = 101
GP1 = 1
ReName_Text = 11
ReName_Field = 12
DigNum_Text = 13
DigNum_Field = 14
ReName_Bt = 1001
GP2 = 2
Fi_Text = 15
FiText_Field = 16
Re_Text = 17
ReText_Field = 18
FiRe_Bt = 1003
GP3 = 3
Pre_Text = 19
PreText_Field = 20
UsePreText_Bt = 1004
GP4 = 4
Suf_Text = 21
SufText_Field = 22
UseSufText_Bt = 1005
GP5 = 5
Index_Text = 23
IndexNum_Field = 24
AddIndexNum_Bt = 1006
DelFront_Bt = 1007
DelEnd_Bt = 1008

class MyDialog(gui.GeDialog):
    '''def __init__(self):
        self.ReName_Field = "Name"
        self.DigNum_Field = 2
        self.FiText_Field = "FindText"
        self.ReText_Field = "ReText"
        self.PreText_Field = "preName"
        self.SufText_Field = "SufName"
        self.IndexNum_Field = 1'''

    def CreateLayout(self):
        self.SetTitle("rename")
        self.AddStaticText(Title_Text, c4d.BFH_CENTER, name="阿拉丁的重命名工具")
        # 创建行布局用于放置“命名和位数”相关的元素 GP1
        self.GroupBegin(GP1, c4d.BFH_LEFT | c4d.BFV_TOP, cols=6, rows=1)
        self.GroupBorderSpace(1, 1, 1, 1)
        self.AddStaticText(ReName_Text, c4d.BFH_LEFT, name="命名")
        self.AddEditText(ReName_Field, c4d.BFH_LEFT, 120, 15)
        self.AddStaticText(DigNum_Text, c4d.BFH_LEFT, name="位数")
        self.AddEditNumberArrows(DigNum_Field, c4d.BFH_LEFT, 40, 15)
        self.AddButton(ReName_Bt, c4d.BFH_LEFT, 100, 15, "重命名")
        self.GroupEnd()
        # 创建行布局用于放置“查找和替换”相关的元素 GP2
        self.GroupBegin(GP2, c4d.BFH_LEFT | c4d.BFV_TOP, cols=5, rows=1)
        self.GroupBorderSpace(1, 1, 1, 1)
        self.AddStaticText(Fi_Text, c4d.BFH_LEFT, name="查找文本")
        self.AddEditText(FiText_Field, c4d.BFH_LEFT, 120, 15) 
        self.AddStaticText(Re_Text, c4d.BFH_LEFT, name="替换文本")
        self.AddEditText(ReText_Field, c4d.BFH_LEFT, 120, 15)
        self.AddButton(FiRe_Bt, c4d.BFH_LEFT, 100, 15, "查找替换")
        self.GroupEnd()
        # 创建行布局用于增加前缀的元素 GP3
        self.GroupBegin(GP3, c4d.BFH_LEFT | c4d.BFV_TOP, cols=5, rows=1)
        self.GroupBorderSpace(1, 1, 1, 1)
        self.AddStaticText(Pre_Text, c4d.BFH_LEFT, name="增加前缀")
        self.AddEditText(PreText_Field, c4d.BFH_LEFT, 120, 15)
        self.AddButton(UsePreText_Bt, c4d.BFH_RIGHT, name="增加前缀")
        self.GroupEnd()
        # 创建行布局用于增加后缀的元素 GP4
        self.GroupBegin(GP4, c4d.BFH_LEFT | c4d.BFV_TOP, cols=5, rows=1)
        self.GroupBorderSpace(1, 1, 1, 1)
        self.AddStaticText(Suf_Text, c4d.BFH_LEFT, name="增加后缀")
        self.AddEditText(SufText_Field, c4d.BFH_LEFT, 120, 15)
        self.AddButton(UseSufText_Bt, c4d.BFH_LEFT, name="增加后缀")
        self.GroupEnd()
        # 创建行布局用于增加序号的元素 GP5
        self.GroupBegin(GP5, c4d.BFH_LEFT | c4d.BFV_TOP, cols=5, rows=1)
        self.GroupBorderSpace(1, 1, 1, 1)
        self.AddStaticText(Index_Text, c4d.BFH_LEFT, name="增删序号")
        self.AddEditNumberArrows(IndexNum_Field, c4d.BFH_LEFT, 40, 15) 
        self.AddButton(AddIndexNum_Bt, c4d.BFH_LEFT, name="增加序号")
        self.AddButton(DelFront_Bt, c4d.BFH_LEFT, name="删除前段")
        self.AddButton(DelEnd_Bt, c4d.BFH_LEFT, name="删除后段")
        self.GroupEnd()
        return True

    def Command(self, id, msg):
        if id == ReName_Bt:
            self.rename()
        elif id == FiRe_Bt:
            self.find_replace()
        elif id == UsePreText_Bt:
            self.add_prefix()
        elif id == UseSufText_Bt:
            self.add_suffix()
        elif id == AddIndexNum_Bt:
            self.add_index()
        elif id == DelFront_Bt:
            self.del_Findex()
        elif id == DelEnd_Bt:
            self.del_Eindex()
        return True
        
    def get_Mesh_Mat(self):
        doc = c4d.documents.GetActiveDocument()
        selection = doc.GetSelection()
        materials = doc.GetActiveMaterials()
        if not selection and not materials:
            c4d.gui.MessageDialog("请选择模型或者材质。")
        return selection,materials
        
    def rename(self):
        char_input = self.GetString(ReName_Field)
        dig_num_input = self.GetString(DigNum_Field)
        doc = c4d.documents.GetActiveDocument()
        selection, materials = self.get_Mesh_Mat()
        if not selection and not materials:
            c4d.gui.MessageDialog("请选择模型或者材质。")
            return
        if selection:
            i = 1
            for obj in selection:
                obj.SetName(f"{char_input}_{i:0{dig_num_input}}")
                i += 1
        else:
            i = 1
            for mat in materials:
                mat.SetName(f"{char_input}_{i:0{dig_num_input}}")
                i += 1
        c4d.EventAdd()

    def find_replace(self):
        find_text = self.GetString(FiText_Field)
        replace_text = self.GetString(ReText_Field)
        selection, materials = self.get_Mesh_Mat()
        names = [obj.GetName() for obj in selection] or [obj.GetName() for obj in materials]
        if selection:
            for i, obj in enumerate(selection):
                new_name = names[i].replace(find_text, replace_text)
                obj.SetName(new_name)
        else:
            for i, obj in enumerate(materials):
                new_name = names[i].replace(find_text, replace_text)
                obj.SetName(new_name)
        c4d.EventAdd()

    def sub_process_AddPrefix(self,char_input,obj):#定义add_prefix的一个子函数
        current_name = obj.GetName()
        new_name = char_input + '_' + current_name
        obj.SetName(new_name)
        
    def add_prefix(self):
        char_input  = self.GetString(PreText_Field)
        selection, materials = self.get_Mesh_Mat()
        if selection:
            for obj in selection:
                self.sub_process_AddPrefix(char_input,obj)
        else:
            for obj in materials:
                self.sub_process_AddPrefix(char_input,obj)
        c4d.EventAdd()
        
    # 增加Chr后缀的subFun
    def sub_process_AddSuffix(self, obj):
        current_name = obj.GetName()
        # 从后往前找第一个非数字字符的索引
        non_digit_index = -1
        for i in range(len(current_name) - 1, -1, -1):
            if not current_name[i].isdigit():
                non_digit_index = i
                break
        return non_digit_index

    # 增加Chr后缀
    def add_suffix(self):
        char_input = self.GetString(SufText_Field)
        selection, materials = self.get_Mesh_Mat()
        for obj in selection:
            current_name = obj.GetName()
            non_digit_index = self.sub_process_AddSuffix(obj)
            if non_digit_index!= -1:
                new_name = current_name[:non_digit_index]+ '_' +char_input + current_name[non_digit_index:]
            else:
                new_name = current_name+ '_' + char_input
            obj.SetName(new_name)
        c4d.EventAdd()

        
    #增加序号
    def add_index(self):
        dig_num_input = self.GetInt32(IndexNum_Field)
        selection = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_SELECTIONORDER)
        i = 1
        for obj in selection:
            current_name = obj.GetName()
            new_name = current_name + '_' + str(i).zfill(dig_num_input)
            obj.SetName(new_name)
            i += 1
        c4d.EventAdd()
        
    #删除num前缀的subFun
    def sub_process_DelFront(self, obj):
        current_name = obj.GetName()
        FDigit_index = -1
        for i, c in enumerate(current_name):
            if not c.isdigit():
                FDigit_index = i
                break
        if FDigit_index!= -1:
            EndChr = current_name.find('_', FDigit_index)
            if EndChr!= -1:
                # 找到_后面数字字符的结束位置
                digit_end_index = EndChr + 1
                while digit_end_index < len(current_name) and current_name[digit_end_index].isdigit():
                    digit_end_index += 1
                new_name = current_name[:EndChr] + current_name[digit_end_index:]
            else:
                new_name = current_name
        else:
            new_name = current_name
        return new_name
    #删除num前缀
    def del_Findex(self):
        selection, materials = self.get_Mesh_Mat()
        if selection:
            for obj in selection:
                new_name = self.sub_process_DelFront(obj)
                obj.SetName(new_name)
        else:
            for obj in materials:
                new_name = self.sub_process_DelFront(obj)
                obj.SetName(new_name)
        c4d.EventAdd()
    #删除num后缀的SubFun
    def sub_process_DelBack(self, obj):
        current_name = obj.GetName()
        # 从后往前找第一个非数字字符的索引
        non_digit_index = -1
        for i in range(len(current_name) - 1, -1, -1):
            if not current_name[i].isdigit():
                non_digit_index = i
                break
        if non_digit_index!= -1:
            # 找到这个非数字字符之后的第一个_
            start_underscore_index = current_name.find('_', non_digit_index)
            if start_underscore_index!= -1:
                # 找到_后面数字字符的结束位置
                digit_end_index = start_underscore_index + 1
                while digit_end_index < len(current_name) and current_name[digit_end_index].isdigit():
                    digit_end_index += 1
                new_name = current_name[:start_underscore_index] + current_name[digit_end_index:]
            else:
                new_name = current_name
        else:
            new_name = current_name
        return new_name
    #删除num后缀
    def del_Eindex(self):
        selection, materials = self.get_Mesh_Mat()
        if selection:
            for obj in selection:
                new_name = self.sub_process_DelBack(obj)
                obj.SetName(new_name)
        else:
            for obj in materials:
                new_name = self.sub_process_DelBack(obj)
                obj.SetName(new_name)
        c4d.EventAdd()
        
if __name__ == '__main__':
    dialog = MyDialog()
    dialog.Open(c4d.DLG_TYPE_ASYNC, xpos=100, ypos=100, defaultw=200, defaulth=240)

def main():
    pass

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值