Maya python 脚本,用于将几条曲线合并为一条

Maya python script for combine few curves to one

嗨,我是 python 和脚本编写方面的新手,阅读了很多教程并尝试创建脚本以将 curveShapes 组合成具有多形状的一条曲线,它对我来说很好用。但是这里我有一个错误,当我在启动 Maya 后第一次启动脚本时,它会给我回溯,如果它是 运行 一次,它不会给出任何错误或回溯:

// Error: Not enough objects or values.
# Traceback (most recent call last):
#   File "<maya console>", line 2, in <module>
#   File "C:/Users/.../maya/2017/scripts\CreateOneCurve.py", line 17, in <module>
#     cmds.parent(r=True, s=True)
# RuntimeError: Not enough objects or values. //

这是我的脚本:

#Funcion for create list of objects
def listCurveObj():
    shapeList = cmds.ls(cmds.listRelatives(s=True), s=True)
    groupList = cmds.ls(cmds.group(em=True, n='Curve#'))
    listAllobjects = []
    for obj in groupList:
        listAllobjects.extend(shapeList)
        listAllobjects.extend(groupList)
    return listAllobjects

#Create one Curve
cmds.select(listCurveObj())
cmds.parent(r=True, s=True)

#Clean scene
transforms =  cmds.ls(type='transform')
deleteList = []
for tran in transforms:
    if cmds.nodeType(tran) == 'transform':
        children = cmds.listRelatives(tran, c=True) 
        if children == None:
            print '%s, has no childred' %(tran)
            deleteList.append(tran)

if len(deleteList) > 0:            
   cmds.delete(deleteList)

有人可以帮忙吗?

嗨,我试图用 pymel 解决我的问题并重做,所以现在我的第一部分工作完美:

import pymel.core as pm

#Get list of objects
shapeList = pm.ls(pm.listRelatives(c=True))
groupList = pm.ls(pm.group(em=True, n='Curve#'))
listAll = []
for obj in groupList:
    listAll.extend(shapeList)
    listAll.extend(groupList)

#Parent objects to one Curve
pm.select(listAll)
pm.parent(r=True, s=True)

但是第二个(干净的场景)不起作用,所以我想我在列出清单时遇到了问题,我不知道如何接收空组列表或如何比较两个列表并删除相同的项目?

嗨,好的,现在我解决了所有错误和警告并完成了我的第一个 python 脚本!它很好用。因此,此脚本通过标准 Maya 附加曲线工具将几条曲线组合成一条多形状曲线,而无需闭合点!所以现在看起来像:

import pymel.core as pm

#Get list of objects
shapeList = pm.ls(pm.listRelatives(c=True))
groupList = pm.ls(pm.group(em=True, n='Curve#'))
listAll = []
for obj in groupList:
    listAll.extend(shapeList)
    listAll.extend(groupList)

#Parent objects to one Curve
pm.select(listAll)
pm.parent(r=True, s=True)

#Clean scene
trans = pm.ls(tr=True)
parents = pm.listRelatives(pm.ls(), type='transform', p=True)
deleteList=[]
for obj in trans:
    if obj not in parents:
        deleteList.append(obj)
        print '%s, has no childred' %(obj)   

if len(deleteList) > 0: 
   pm.delete(deleteList)

好像过分了,为什么不这样呢?

import maya.cmds as mc

def combineCurves(curves = []):
    crvGrp = mc.group(n = curves[0], em=1)
    for crv in curves:
        crvShape = mc.listRelatives(crv, shapes = 1)
        mc.parent(crvShape,crvGrp,s=1,r=1)
        mc.delete(crv)
        
combineCurves(mc.ls(selection=1))