通过Python在Maya中创建多个发射器的简单函数

时间:2017-04-28 16:06:02

标签: python maya mel

我试图创建一个创建发射器(从对象发出)的函数,但是我无法同时处理多个对象。

我希望emitter1nParticle1以及emitter2nParticle2相关联。

def particalWebSystem(webDensity,genoration):
    selectedObject = cmds.ls( selection = True )
    print selectedObject

    if len(selectedObject)==0:
        cmds.confirmDialog( title='Warning', message='Please select at least one object', button=['Close'], defaultButton='Close', cancelButton='Close', dismissString='Close' )

    if len(selectedObject)==1:
        print "<2"
    else:
        print ">2"
    emitting = cmds.emitter( selectedObject, n='emitter' )
    cmds.particle( n = 'nParticle#' )
    cmds.connectDynamic( 'nParticle#', em = 'emitter' )
    cmds.setAttr( "emitter.emitterType", 2 )

    cmds.setAttr( "nParticle#Shape.lifespanMode", 2 )
    cmds.setAttr( "nParticle#Shape.maxCount", webDensity ) 
    cmds.setAttr( "nParticle#Shape.lifespanRandom", generation )

1 个答案:

答案 0 :(得分:2)

您可以通过确保使用实际创建的发射器的名称来简化此问题 - 您永远不会知道您真正得到的名称,因为Maya会重命名。最好始终捕获您创建和处理的内容的名称,而不是希望您获得所要求的名称。

此版本创建一个函数,并在每个选定的对象上调用它。对于空选择,它将默默地执行任何操作,它还会返回在以后需要时创建的对象。

import maya.cmds as cmds

def setup_emitter(source_object, maxcount, life_random):
    source_object, emitting = cmds.emitter( source_object, n=source_object + "_emitter" )
    particle, particleshape = cmds.particle( n = source_object + "_particles" )
    cmds.connectDynamic( particle, em = emitting)
    cmds.setAttr( emitting + ".emitterType", 2 )

    cmds.setAttr( particleshape + ".lifespanMode", 2 )
    cmds.setAttr( particleshape + ".maxCount", maxcount ) 
    cmds.setAttr( particleshape + ".lifespanRandom", life_random )
    return emitting, particleshape

for selection in cmds.ls(sl=True, type='transform'):
        print setup_emitter(selection, 100, 100) # your values here