Query a Partcile Position

384 views
Skip to first unread message

AJ

unread,
May 13, 2012, 9:17:45 PM5/13/12
to python_inside_maya
Hey,

Lets say I have a particle that is moving base on a field for 10
frames.
How can I query its position on each frame and write it in text file?

Many thanks for you help in advance.

Justin Israel

unread,
May 14, 2012, 2:18:29 PM5/14/12
to python_in...@googlegroups.com
Here is a simple approach using the commands module:

def getParticlePositions(pObject, pId, start, end):
    cTime = cmds.currentTime(q=True)
    positions = {}
    fromStart = True
    for f in xrange(start, end+1):
        cmds.runup(maxFrame=f, fsf=fromStart)
        fromStart = False
        try:
            pos = cmds.particle(pObject, q=True, id=pId, attribute='position')
        except RuntimeError:
            pos = None
        positions[f] = pos
    cmds.currentTime(cTime)
    return positions
    
p = getParticlePositions('particle1', 2, 10, 15)

with open("points.txt", 'w') as f:
    for item in sorted(p.iteritems()):
        f.write("%d\t%s\n" % item)

The function does a runup over each frame and then gets the particle position.
I'm sure you could get a bit more control using the API if needed.



Kenneth Ibrahim

unread,
May 14, 2012, 2:18:18 PM5/14/12
to python_in...@googlegroups.com
You should be able to do something like this to get the point positions on a per-frame basis:

import pymel.core as pm

pars_shape = pm.PyNode( 'particleShape1' )


for frame in range( start, end+1 ):
    pm.currentTime( frame )


    positions = []

    for pt in pars_shape.points:

        positions.append( pt.worldPosition )


    f = open( '/tmp/parPositions.%04d.txt' % frame, 'w' )
    f.writelines( positions )
    f.close()

You could also use the 'with' directive for better file stream handling.

Cheers, Ken

On Sun, May 13, 2012 at 6:17 PM, AJ <ali.jaf...@gmail.com> wrote:



--
"God is a metaphor for a mystery that absolutely transcends all human categories of thought. It's as simple as that!" - Joseph Campbell

Martin La Land Romero

unread,
May 14, 2012, 2:34:39 PM5/14/12
to python_in...@googlegroups.com
I would start by getting information about the selected particle, something like

Select the particle shape > changed to select by component type then > select the desire particle.
>
import maya.cmds as cmds

myselPart = cmds.ls(sl = True)

print myselPart


That's a start


Martin



On Sun, May 13, 2012 at 6:17 PM, AJ <ali.jaf...@gmail.com> wrote:



--
Martin La Land Romero
www.martinromerovfx.com
http://martinromerovfx.blogspot.com/
martin...@gmail.com
(415)261-2172

AJ

unread,
May 18, 2012, 9:34:56 AM5/18/12
to python_inside_maya
Thank you all for responds,
They were very helpful.
I also found this page which has nice way of doing it.
Take a look out it:

http://www.fundza.com/rms/ri_mel/sample_scripts_I/index.html

many thanks again for your help



On May 14, 2:34 pm, Martin La Land Romero <martinmrom...@gmail.com>
wrote:
> I would start by getting information about the selected particle, something
> like
>
> Select the particle shape > changed to select by component type then >
> select the desire particle.
>
> import maya.cmds as cmds
>
> myselPart = cmds.ls(sl = True)
>
> print myselPart
>
> That's a start
>
> Martin
>
> On Sun, May 13, 2012 at 6:17 PM, AJ <ali.jafargh...@gmail.com> wrote:
> > Hey,
>
> > Lets say I have a particle that is moving base on a field for 10
> > frames.
> > How can I query its position on each frame and write it in text file?
>
> > Many thanks for you help in advance.
>
> > --
> > view archives:http://groups.google.com/group/python_inside_maya
> > change your subscription settings:
> >http://groups.google.com/group/python_inside_maya/subscribe
>
> --
> Martin La Land Romerowww.martinromerovfx.comhttp://martinromerovfx.blogspot.com/
> martinmrom...@gmail.com
> (415)261-2172
Reply all
Reply to author
Forward
0 new messages