访问生成的pf源的paticleIndex

时间:2014-03-21 08:11:52

标签: maxscript

在场景中创建PF源节点时,我可以在maxscript中选择此系统,并使用for循环和pf.particleIndex

遍历所有粒子

然而,当我使用maxscript遍历场景中的几何体并为每个对象生成PF源时,pf.NumParticles()返回0,而pf.particleIndex = i的pf.particlePosition为coord.x返回0 0 0 ,.y和.z

geo = #()

for OBJ in Geometry do (
    append geo OBJ
)

for OBJ in geo do ( 
    with undo off (
        -- define color for current object
        r = random 128 192
        g = random 128 192
        b = random 128 192

        -- create a particle system
        pf = PF_Source() 
        pf.Quantity_Viewport = 100

        ParticleFlow.BeginEdit()

        -- enable render
        rp = RenderParticles()
        pf.AppendAction rp

        -- create birth
        a1 = Birth()
        a1.Amount = random 1000 2000
        a1.Emit_Start = 0
        a1.Emit_Stop = 0

        -- position to surface of current object
        a2 = Position_Object()
        a2.Location = 3
        a2.Emitter_Objects = #(OBJ)

        -- show in viewport
        a3 = DisplayParticles()
        a3.type = 2
        a3.color = (color r g b)

        -- add the event to the flow
        e1 = Event()
        e1.AppendAction a1
        e1.AppendAction a2
        e1.AppendAction a3

        ParticleFlow.EndEdit()

        -- create the "complete" particle system
        pf.appendInitialActionList e1

        -- get particle coordinates
        particleAmount = pf.NumParticles()
        print particleAmount
        for i = 1 to particleAmount do (
            pf.particleIndex = i
            coord = pf.particlePosition
            print coord
        )
    )
)

-- garbage collection
gc()

有没有办法,我不知道,在脚本中刷新PF源,以便它知道系统中有粒子。

1 个答案:

答案 0 :(得分:0)

PF存在一个已知问题,当使用maxscript创建它时,你必须重新加载它,例如使用hold / fetch:

geo = geometry as array

for OBJ in geo do
(
    -- create a particle system
    local pf = PF_Source Quantity_Viewport:100
    -- save its identifier for later
    local handle = pf.iNode.handle
    local particleAmount = random 1000 2000

    ParticleFlow.BeginEdit()

    -- enable render
    pf.AppendAction (RenderParticles())

    -- add the event to the flow
    local e1 = Event()
    -- create birth
    e1.AppendAction (Birth Amount:particleAmount Emit_Start:0 Emit_Stop:0)
    -- position to surface of current object
    e1.AppendAction (Position_Object Location:3 Emitter_Objects:#(OBJ))
    -- show in viewport
    e1.AppendAction (DisplayParticles type:2 color:(random [128,128,128] [192,192,192]))

    ParticleFlow.EndEdit()

    -- create the "complete" particle system
    pf.appendInitialActionList e1

    -- force particle flow to update particles
    holdMaxFile()
    fetchMaxFile quiet:true

    -- get the particle flow node again
    pf = maxOps.getNodeByHandle handle

    -- get particle coordinates
    for i = 1 to particleAmount do
    (
        pf.particleID = i
        coord = pf.particlePosition
        format "index: %, pos: %\n" pf.particleIndex coord
    )
)

另一方面,如果您只需要表面上的随机粒子,PArray可能是更好的解决方案:

geo = geometry as array

for OBJ in geo do
(
    local particleAmount = random 1000 2000
    local pa = PArray emitter:obj formation:0 speed:0 Birth_Rate:particleAmount viewPercent:100 \
        Emitter_Start:0f Emitter_Stop:0f wirecolor:(random [128,128,128] [192,192,192])

    for i = 1 to particleAmount do
        format "index: %, pos: %\n" i (particlePos pa i)
)