Search code examples
pythonchipmunkpymunk

Pymunk -- Finding a body's associated shapes and constraints


I'm trying to find some way to iterate over the constraints dependent on a specific body in pymunk. There seem to be multiple ways to do so in Chipmunk, but I can't find an equivalent in pymunk. I would like to see some way to detect constraints such that I don't have to manually keep track of all of them.

I'd also like to see a way to detect the shapes associated with a body. In general, I'd like to be able to automatically remove a body's shapes and constraints when I remove the body from a space. Is that possible?


Solution

  • Latest trunk version of pymunk has two new (as of today) shapes and constraints properties on the Body class. I went with always return the shapes/constraints regardless of if they are added to the space or not.

    So now you can just do:

    >>> import pymunk
    >>> b = pymunk.Body()
    >>> b2 = pymunk.Body()
    >>> j = pymunk.PivotJoint(b,b2,(0,0))
    >>> s = pymunk.Circle(b,3)
    >>> b.constraints
    set([<pymunk.constraint.PivotJoint object at 0x02521890>])
    >>> b.shapes
    set([<pymunk.Circle object at 0x025218F0>])
    

    (This will be included in pymunk 3.1.)