对材质同样也有效
被AI干失业的卖衣服的小姐姐,开的士的小哥哥都可以再就业的易上手教程,
先看效果!
对物体命名也是,相当的美观
先提出需求我想在maya中批量重命名物体怎么办?AI给你弄个短代码 ,放进AI进化一下。。
import maya.cmds as cmds
allMat = cmds.ls(mat=True)
for mat in allMat:cmds.rename("Name")
长代码,再次进化。。
import maya.cmds as cmdsclass MayaRenameDialog:def __init__(self):self.char_input = ""self.num_input = 0def create_layout(self):self.window = cmds.window(title="Maya 重命名工具")layout = cmds.columnLayout()cmds.text(label="阿拉丁的重命名工具", align='center')cmds.text(label="前缀")self.prefix_field = cmds.textField()cmds.text(label="位数")self.digit_field = cmds.intField(value=4)cmds.button(label="重命名物体", command=self.rename_objects)cmds.button(label="重命名材质", command=self.rename_materials)return Truedef rename_objects(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)selection = cmds.ls(selection=True)i = 1for obj in selection:new_name = self.char_input+str(i).zfill(self.num_input)cmds.rename(obj, new_name)i += 1def rename_materials(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)materials = cmds.ls(mat=True)i = 1for mat in materials:new_name = self.char_input + str(i).zfill(self.num_input)cmds.rename(new_name)i += 1if __name__ == '__main__':dialog = MayaRenameDialog()dialog.create_layout()cmds.showWindow(dialog.window)
这代码不是太完美,继续问豆包AI。:
我发现它命名都是从3开始,而材质编辑器里有三个默认的材质球,这个我不想影响它们。
回答一
如果你不想影响 Maya 材质编辑器中的三个默认材质球,可以在重命名材质的代码中添加一个条件判断,排除这三个默认材质球。在这个版本中,定义了一个default_materials
列表,列出了三个默认材质球的名称。在重命名循环中,检查当前材质球是否在这个列表中,如果不在才进行重命名操作。这样就可以避免影响默认材质球。
以下是修改后的代码:
import maya.cmds as cmds
import timeclass MayaRenameDialog:def __init__(self):self.char_input = ""self.num_input = 0def create_layout(self):self.window = cmds.window(title="Maya 重命名工具")layout = cmds.columnLayout()cmds.text(label="阿拉丁的重命名工具", align='center')cmds.text(label="前缀")self.prefix_field = cmds.textField()cmds.text(label="位数")self.digit_field = cmds.intField(value=4)cmds.button(label="重命名物体", command=self.rename_objects)cmds.button(label="重命名材质", command=self.rename_materials)return Truedef rename_objects(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)selection = cmds.ls(selection=True)i = 1for obj in selection:new_name = self.char_input+str(i).zfill(self.num_input)cmds.rename(obj, new_name)i += 1def rename_materials(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)materials = cmds.ls(mat=True)default_materials = ['lambert1', 'particleCloud1', 'shaderGlow1'] # 这里列出默认的三个材质球名称i = 1for mat in materials:if mat not in default_materials:print("Before rename: {mat}")if cmds.lockNode(mat, query=True, lock=True):cmds.lockNode(mat, lock=False)print("解锁材质{mat}.")new_name = self.char_input + str(i).zfill(self.num_input)try:cmds.rename(mat, new_name)time.sleep(0.1) # 添加短暂的延迟except RuntimeError as e:if "Cannot rename a read only node." in str(e):print("错误重命名{mat}. 它可能被锁定或者是只读状态.")i += 1if __name__ == '__main__':dialog = MayaRenameDialog()dialog.create_layout()cmds.showWindow(dialog.window)
回答二
以下是对这个问题的分析和解决方案:
分析
Maya 材质编辑器中默认有三个材质球(lambert1、particleCloud1、shaderGlow1),你代码中的循环从第一个材质开始处理,所以会对默认材质球进行操作。如果不想影响这三个默认材质球,可以在循环中跳过它们。
解决方案
在rename_materials
方法的循环中添加条件判断来跳过这三个默认材质球的处理,以下是修改后的代码:
代码中,在rename_materials
方法中,当材质球是默认的三个材质球时,使用continue
语句跳过当前循环,直接进入下一个材质球的处理,从而避免对默认材质球的重命名操作。
import maya.cmds as cmds
import timeclass MayaRenameDialog:def __init__(self):self.char_input = ""self.num_input = 0def create_layout(self):self.window = cmds.window(title="Maya 重命名工具")layout = cmds.columnLayout()cmds.text(label="阿拉丁的重命名工具", align='center')cmds.text(label="前缀")self.prefix_field = cmds.textField()cmds.text(label="位数")self.digit_field = cmds.intField(value=4)cmds.button(label="重命名物体", command=self.rename_objects)cmds.button(label="重命名材质", command=self.rename_materials)return Truedef rename_objects(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)selection = cmds.ls(selection=True)i = 1for obj in selection:new_name = self.char_input + str(i).zfill(self.num_input)cmds.rename(obj, new_name)i += 1def rename_materials(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)materials = cmds.ls(mat=True)i = 1for mat in materials:if mat in ["lambert1", "particleCloud1", "shaderGlow1"]:continueprint("Before rename: {mat}")if cmds.lockNode(mat, query=True, lock=True):cmds.lockNode(mat, lock=False)print("解锁材质{mat}.")new_name = self.char_input + str(i).zfill(self.num_input)try:cmds.rename(mat, new_name)time.sleep(0.1)except RuntimeError as e:if "Cannot rename a read only node." in str(e):print("错误重命名{mat}. 它可能被锁定或者是只读状态.")i += 1if __name__ == '__main__':dialog = MayaRenameDialog()dialog.create_layout()cmds.showWindow(dialog.window)
然而,还是不太完美!然后我想在代码中加入一个查找替换功能,并且在UI里面也对应有查找文本输入框替换文本输入框和一个按钮叫查找替换,继续叫AI给我进化。。。
这就更牛逼了!
import maya.cmds as cmds
import timeclass MayaRenameDialog:def __init__(self):self.char_input = ""self.num_input = 0self.find_text = ""self.replace_text = ""def create_layout(self):self.window = cmds.window(title="Maya 重命名工具")layout = cmds.columnLayout()cmds.text(label="阿拉丁的重命名工具", align='center')cmds.text(label="前缀")self.prefix_field = cmds.textField()cmds.text(label="位数")self.digit_field = cmds.intField(value=4)cmds.text(label="查找文本")self.find_field = cmds.textField()cmds.text(label="替换文本")self.replace_field = cmds.textField()cmds.button(label="查找替换", command=self.find_replace)cmds.button(label="重命名物体", command=self.rename_objects)cmds.button(label="重命名材质", command=self.rename_materials)return Truedef rename_objects(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)selection = cmds.ls(selection=True)i = 1for obj in selection:new_name = self.char_input + str(i).zfill(self.num_input)cmds.rename(obj, new_name)i += 1def rename_materials(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)materials = cmds.ls(mat=True)default_materials = ['lambert1', 'particleCloud1', 'shaderGlow1']i = 1for mat in materials:if mat not in default_materials:print("Before rename: {mat}")if cmds.lockNode(mat, query=True, lock=True):cmds.lockNode(mat, lock=False)print("解锁材质{mat}.")new_name = self.char_input + str(i).zfill(self.num_input)try:cmds.rename(mat, new_name)time.sleep(0.1)except RuntimeError as e:if "Cannot rename a read only node." in str(e):print("错误重命名{mat}. 它可能被锁定或者是只读状态.")i += 1def find_replace(self, *args):self.find_text = cmds.textField(self.find_field, query=True, text=True)self.replace_text = cmds.textField(self.replace_field, query=True, text=True)selection = cmds.ls(selection=True)for obj in selection:current_name = objnew_name = current_name.replace(self.find_text, self.replace_text)cmds.rename(obj, new_name)if __name__ == '__main__':dialog = MayaRenameDialog()dialog.create_layout()cmds.showWindow(dialog.window)
剩下的就只排版一下界面美观一下即可了。哦哈哈!
我如何把窗口的尺寸和位置做个定义?xpos=100, ypos=100, defaultw=200, defaulth=200类似这样的
import maya.cmds as cmds
import timeclass MayaRenameDialog:def __init__(self):self.char_input = ""self.num_input = 0self.find_text = ""self.replace_text = ""def create_layout(self):# 指定窗口位置和尺寸self.window = cmds.window(title="Maya 重命名工具", widthHeight=(400, 200), topLeftCorner=(100, 100))layout = cmds.columnLayout()cmds.text(label="阿拉丁的重命名工具", align='center')cmds.text(label="前缀")self.prefix_field = cmds.textField()cmds.text(label="位数")self.digit_field = cmds.intField(value=4)cmds.text(label="查找文本")self.find_field = cmds.textField()cmds.text(label="替换文本")self.replace_field = cmds.textField()cmds.button(label="查找替换", command=self.find_replace)cmds.button(label="重命名物体", command=self.rename_objects)cmds.button(label="重命名材质", command=self.rename_materials)return Truedef rename_objects(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)selection = cmds.ls(selection=True)i = 1for obj in selection:new_name = self.char_input + str(i).zfill(self.num_input)cmds.rename(obj, new_name)i += 1def rename_materials(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)materials = cmds.ls(mat=True)default_materials = ['lambert1', 'particleCloud1', 'shaderGlow1']i = 1for mat in materials:if mat not in default_materials:print("Before rename: {mat}")if cmds.lockNode(mat, query=True, lock=True):cmds.lockNode(mat, lock=False)print("解锁材质{mat}.")new_name = self.char_input + str(i).zfill(self.num_input)try:cmds.rename(mat, new_name)time.sleep(0.1)except RuntimeError as e:if "Cannot rename a read only node." in str(e):print("错误重命名{mat}. 它可能被锁定或者是只读状态.")i += 1def find_replace(self, *args):self.find_text = cmds.textField(self.find_field, query=True, text=True)self.replace_text = cmds.textField(self.replace_field, query=True, text=True)selection = cmds.ls(selection=True)for obj in selection:current_name = objnew_name = current_name.replace(self.find_text, self.replace_text)cmds.rename(obj, new_name)if __name__ == '__main__':dialog = MayaRenameDialog()dialog.create_layout()cmds.showWindow(dialog.window)
或者跟c4d里的python一样看齐,
import maya.cmds as cmds
import timeclass MayaRenameDialog:def __init__(self):self.char_input = ""self.num_input = 0self.find_text = ""self.replace_text = ""def create_layout(self, xpos, ypos, defaultw, defaulth):self.window = cmds.window(title="Maya 重命名工具", widthHeight=(defaultw, defaulth), topLeftCorner=(xpos, ypos))layout = cmds.columnLayout()cmds.text(label="阿拉丁的重命名工具", align='center')cmds.text(label="前缀")self.prefix_field = cmds.textField()cmds.text(label="位数")self.digit_field = cmds.intField(value=4)cmds.text(label="查找文本")self.find_field = cmds.textField()cmds.text(label="替换文本")self.replace_field = cmds.textField()cmds.button(label="查找替换", command=self.find_replace)cmds.button(label="重命名物体", command=self.rename_objects)cmds.button(label="重命名材质", command=self.rename_materials)return Truedef rename_objects(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)selection = cmds.ls(selection=True)i = 1for obj in selection:new_name = self.char_input + str(i).zfill(self.num_input)cmds.rename(obj, new_name)i += 1def rename_materials(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)materials = cmds.ls(mat=True)default_materials = ['lambert1', 'particleCloud1', 'shaderGlow1']i = 1for mat in materials:if mat not in default_materials:print("Before rename: {mat}")if cmds.lockNode(mat, query=True, lock=True):cmds.lockNode(mat, lock=False)print("解锁材质{mat}.")new_name = self.char_input + str(i).zfill(self.num_input)try:cmds.rename(mat, new_name)time.sleep(0.1)except RuntimeError as e:if "Cannot rename a read only node." in str(e):print("错误重命名{mat}. 它可能被锁定或者是只读状态.")i += 1def find_replace(self, *args):self.find_text = cmds.textField(self.find_field, query=True, text=True)self.replace_text = cmds.textField(self.replace_field, query=True, text=True)selection = cmds.ls(selection=True)for obj in selection:current_name = objnew_name = current_name.replace(self.find_text, self.replace_text)cmds.rename(obj, new_name)if __name__ == '__main__':dialog = MayaRenameDialog()dialog.create_layout(xpos=100, ypos=100, defaultw=300, defaulth=200)cmds.showWindow(dialog.window)
越来越喜欢编代码了!来来来!继续问AI!现在在ui排布的界面里,查找文本和替换文本以及他们的输入框都是另起一行排布的我想让它们保持在同一行怎么做到? cmds.text(label="查找文本") self.find_field = cmds.textField() cmds.text(label="替换文本") self.replace_field = cmds.textField()
豆包AI: 在 Maya 的 cmds
中可以使用 rowLayout
(行布局)来将多个 UI 元素放在同一行。以下是修改后的代码:
import maya.cmds as cmds
import timeclass MayaRenameDialog:def __init__(self):self.char_input = ""self.num_input = 0self.find_text = ""self.replace_text = ""def create_layout(self, xpos=100, ypos=100, defaultw=200, defaulth=200):self.window = cmds.window(title="Maya 重命名工具", widthHeight=(defaultw, defaulth), topLeftCorner=(xpos, ypos))layout = cmds.columnLayout()cmds.text(label="阿拉丁的重命名工具", align='center')cmds.text(label="前缀")self.prefix_field = cmds.textField()cmds.text(label="位数")self.digit_field = cmds.intField(value=4)# 创建行布局用于放置查找和替换相关的元素find_replace_row = cmds.rowLayout(numberOfColumns=3)cmds.text(label="查找文本")self.find_field = cmds.textField()cmds.text(label="替换文本")self.replace_field = cmds.textField()cmds.setParent('..') # 返回上一级布局cmds.button(label="查找替换", command=self.find_replace)cmds.button(label="重命名物体", command=self.rename_objects)cmds.button(label="重命名材质", command=self.rename_materials)return Truedef rename_objects(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)selection = cmds.ls(selection=True)i = 1for obj in selection:new_name = self.char_input + str(i).zfill(self.num_input)cmds.rename(obj, new_name)i += 1def rename_materials(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)materials = cmds.ls(mat=True)default_materials = ['lambert1', 'particleCloud1', 'shaderGlow1']i = 1for mat in materials:if mat not in default_materials:print("Before rename: {mat}")if cmds.lockNode(mat, query=True, lock=True):cmds.lockNode(mat, lock=False)print("解锁材质{mat}.")new_name = self.char_input + str(i).zfill(self.num_input)try:cmds.rename(mat, new_name)time.sleep(0.1)except RuntimeError as e:if "Cannot rename a read only node." in str(e):print("错误重命名{mat}. 它可能被锁定或者是只读状态.")i += 1def find_replace(self, *args):self.find_text = cmds.textField(self.find_field, query=True, text=True)self.replace_text = cmds.textField(self.replace_field, query=True, text=True)selection = cmds.ls(selection=True)for obj in selection:current_name = objnew_name = current_name.replace(self.find_text, self.replace_text)cmds.rename(obj, new_name)if __name__ == '__main__':dialog = MayaRenameDialog()dialog.create_layout(xpos=100, ypos=100, defaultw=200, defaulth=200)cmds.showWindow(dialog.window)
运行一下,额。。错误 # Error: RuntimeError: file <maya console> line 26: Too many children in layout: rowLayout5 #
直接复制,再提问 豆包AI 。
你可以尝试以下方法来解决这个问题:
- 检查
rowLayout
中的子元素数量,确保没有添加过多的元素。如果可能的话,减少子元素的数量或者使用其他布局方式来组织这些元素。 - 确认每个子元素都被正确地添加到布局中,并且没有重复添加。
- 检查是否有其他布局嵌套在这个
rowLayout
中,可能导致子元素数量的混乱。
以下是修改后的代码,确保只在rowLayout
中添加必要的元素:额,这里它傻逼了,陷入了死循环的修改状态,手动👀看一下。
最后整理完ui的代码如下:
import maya.cmds as cmds
import time
import reclass MayaRenameDialog:def __init__(self):self.char_input = ""self.num_input = 0self.find_text = ""self.replace_text = ""self.prefix_to_add = ""self.suffix_to_add = ""self.index_to_add = 0self.orderNUM = Noneself.num_input = Nonedef create_layout(self, xpos=100, ypos=100, defaultw=350, defaulth=100):self.window = cmds.window(title="Maya 重命名工具", widthHeight=(defaultw, defaulth), topLeftCorner=(xpos, ypos))layout = cmds.columnLayout()cmds.text(label="阿拉丁的重命名工具", align='center')# 创建行布局用于放置“重命名按钮”相关的元素rename_row = cmds.rowLayout(numberOfColumns=4)cmds.text(label="重命名", parent=rename_row)self.prefix_field = cmds.textField(parent=rename_row)cmds.text(label="位数", parent=rename_row)self.digit_field = cmds.intField(value=4, parent=rename_row)cmds.setParent('..') # 返回上一级布局# 创建行布局用于放置“重命名物体和材质按钮”相关的元素renameObjMatBT_row = cmds.rowLayout(numberOfColumns=2)cmds.button(label="重命名物体", command=self.rename_objects, parent=renameObjMatBT_row)cmds.button(label="重命名材质", command=self.rename_materials, parent=renameObjMatBT_row)cmds.setParent('..') # 返回上一级布局# 创建行布局用于放置“查找和替换”相关的元素find_replace_row = cmds.rowLayout(numberOfColumns=5)cmds.text(label="查找文本", parent=find_replace_row)self.find_field = cmds.textField(parent=find_replace_row)cmds.text(label="替换文本", parent=find_replace_row)self.replace_field = cmds.textField(parent=find_replace_row)cmds.button(label="查找替换", command=self.find_replace, parent=find_replace_row)cmds.setParent('..') # 返回上一级布局# 创建行布局用于增加前缀的元素add_prefix_row = cmds.rowLayout(numberOfColumns=3)cmds.text(label="增加前缀", parent=add_prefix_row)self.prefix_to_add_field = cmds.textField(parent=add_prefix_row)cmds.button(label="应用前缀", command=self.add_prefix, parent=add_prefix_row)cmds.setParent('..') # 返回上一级布局# 创建行布局用于增加后缀的元素add_suffix_row = cmds.rowLayout(numberOfColumns=3)cmds.text(label="增加后缀", parent=add_suffix_row)self.suffix_to_add_field = cmds.textField(parent=add_suffix_row)cmds.button(label="应用后缀", command=self.add_suffix, parent=add_suffix_row)cmds.setParent('..') # 返回上一级布局# 创建行布局用于增加序号的元素add_index_row = cmds.rowLayout(numberOfColumns=4)cmds.text(label="增加序号", parent=add_index_row)self.orderNUM = cmds.intField(parent=add_index_row)cmds.button(label="应用序号", command=self.add_index, parent=add_index_row)cmds.button(label="删除序号", command=self.delete_index, parent=add_index_row)cmds.setParent('..') # 返回上一级布局return Truedef rename_objects(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)selection = cmds.ls(selection=True)i = 1for obj in selection:new_name = self.char_input +'_'+ str(i).zfill(self.num_input)cmds.rename(obj, new_name)i += 1def rename_materials(self, *args):self.char_input = cmds.textField(self.prefix_field, query=True, text=True)self.num_input = cmds.intField(self.digit_field, query=True, value=True)materials = cmds.ls(mat=True)default_materials = ['lambert1', 'particleCloud1', 'shaderGlow1']i = 1for mat in materials:if mat not in default_materials:print("Before rename: {mat}")if cmds.lockNode(mat, query=True, lock=True):cmds.lockNode(mat, lock=False)print("解锁材质{mat}.")new_name = self.char_input + str(i).zfill(self.num_input)try:cmds.rename(mat, new_name)time.sleep(0.1)except RuntimeError as e:if "Cannot rename a read only node." in str(e):print("错误重命名{mat}. 它可能被锁定或者是只读状态.")i += 1def find_replace(self, *args):self.find_text = cmds.textField(self.find_field, query=True, text=True)self.replace_text = cmds.textField(self.replace_field, query=True, text=True)selection = cmds.ls(selection=True)for obj in selection:current_name = objnew_name = current_name.replace(self.find_text, self.replace_text)cmds.rename(obj, new_name)def add_prefix(self, *args):self.prefix_to_add = cmds.textField(self.prefix_to_add_field, query=True, text=True)selection = cmds.ls(selection=True)for obj in selection:current_name = objnew_name = self.prefix_to_add + '_'+ current_namecmds.rename(obj, new_name)def add_suffix(self, *args):self.suffix_to_add = cmds.textField(self.suffix_to_add_field, query=True, text=True)selection = cmds.ls(selection=True)for obj in selection:current_name = objlast_underscore_index = current_name.rfind('_')if last_underscore_index!= -1:new_name = current_name[:last_underscore_index] + '_' + self.suffix_to_add + current_name[last_underscore_index:]else:new_name = current_name + '_' + self.suffix_to_addcmds.rename(obj, new_name)def add_index(self, *args):self.num_input = cmds.intField(self.orderNUM, query=True, value=True)selection = cmds.ls(selection=True)i = 1for obj in selection:current_name = objnew_name = current_name + '_' + str(i).zfill(self.num_input)cmds.rename(obj, new_name)i += 1def delete_index(self, *args):selection = cmds.ls(selection=True)for obj in selection:current_name = objlast_underscore_index = current_name.rfind('_')if last_underscore_index!= -1:new_name = current_name[:last_underscore_index]else:new_name = current_namecmds.rename(obj, new_name)if __name__ == '__main__':dialog = MayaRenameDialog()dialog.create_layout(xpos=100, ypos=100, defaultw=350, defaulth=160)cmds.showWindow(dialog.window)