不是我牛逼,是豆包牛逼!
一个简化版的窗口
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
class MyDialog(gui.GeDialog):
def __init__(self):
self.char_input = ""
self.num_input = 0
gui.GeDialog.__init__(self)
def CreateLayout(self):
self.SetTitle("rename")
self.AddStaticText(2000, c4d.BFH_CENTER, name="阿拉丁的重命名工具")
# 创建行布局用于放置“命名和位数”相关的元素
self.GroupBegin(1004, c4d.BFH_LEFT | c4d.BFV_TOP, cols=6, rows=1)
self.GroupBorderSpace(10, 10, 10, 10)
self.AddStaticText(2000, c4d.BFH_LEFT, name="命名")
self.AddEditText(1000, c4d.BFH_LEFT, 120, 15)
self.AddStaticText(2001, c4d.BFH_LEFT, name="位数")
self.AddEditNumberArrows(1001, c4d.BFH_LEFT, 100, 15)
self.AddButton(1002, c4d.BFH_LEFT, 100, 15, "重命名物体")
self.AddButton(1003, c4d.BFH_LEFT, 100, 15, "重命名材质")
self.GroupEnd()
# 创建行布局用于放置“查找和替换”相关的元素
find_replace_row = self.GroupBegin(1005, c4d.BFH_LEFT | c4d.BFV_TOP, cols=5, rows=1)
self.GroupBorderSpace(10, 10, 10, 10)
self.AddStaticText(2002, c4d.BFH_LEFT, name="查找文本")
self.find_field = self.AddEditText(1007, c4d.BFH_LEFT, 120, 15)
self.AddStaticText(2003, c4d.BFH_LEFT, name="替换文本")
self.replace_field = self.AddEditText(1008, c4d.BFH_LEFT, 120, 15)
self.AddButton(1006, c4d.BFH_LEFT, 100, 15, "查找替换")
self.GroupEnd()
# c4d.BFH_LEFT 实际像素左对齐 #c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT 上下充满
return True
def Command(self, id, msg):
if id == 1002:
self.char_input = self.GetString(1000)
self.num_input = self.GetInt32(1001)
self.rename()
elif id == 1003:
self.char_input = self.GetString(1000)
self.num_input = self.GetInt32(1001)
self.renameMaterials()
elif id == 1006:
self.find_replace()
return True
def rename(self):
selection = doc.GetSelection()
i = 1
for obj in selection:
obj.SetName(f"{self.char_input}_{i:0{self.num_input}}")
i += 1
c4d.EventAdd()
def renameMaterials(self):
materials = doc.GetMaterials()
i = 1
for mat in materials:
mat.SetName (f"{self.char_input}_{i:0{self.num_input}}")
i += 1
c4d.EventAdd()
def find_replace(self):
self.find_text = self.GetString(1007)
self.replace_text = self.GetString(1008)
selection = doc.GetSelection()
if not selection:
c4d.gui.MessageDialog("没有选中任何对象。")
return
names = [obj.GetName() for obj in selection]
for i, obj in enumerate(selection):
new_name = names[i].replace(self.find_text, self.replace_text)
obj.SetName(new_name)
c4d.EventAdd()
if __name__ == '__main__':
dialog = MyDialog()
dialog.Open(c4d.DLG_TYPE_ASYNC, xpos=100, ypos=100, defaultw=200, defaulth=200)
def main():
# 如果这里有一个独立的 main 函数定义,可以在这里进行修改或补充
pass