Simulating Realistic Flocking Behavior in Carrara using PyCarrara

FractalDimensiaFractalDimensia Posts: 0
edited December 1969 in Carrara Discussion

Continuing with my research into using PyCarrara to create animation, I thought I would advance the first of two interests I have in using PyCarrara - this one on simulating realistic flocking behavior.

I am developing a Python script that will drive a number of objects to simulate realistic flocking - what you would see in birds, bats, insects, fish, horses, sheep, etc.

The following video shows a "proof of concept" animation in simulating flocking behavior in Carrara using PyCarrara. In this example, all animation (except camera movement) are driven by the PyCarrara script. Objects are randomly placed in a ring and then allowed to "fly" on their own. There is nothing random built into the animation. All actions are driven by a set of rules about the "flock." Emergent behavior arises from these few basic rules.

http://youtu.be/6fZGMlzZhZg

In upcoming work, I will tweak the motion to reduce jitter, add additional flocking rules (e.g., boundary containment and attraction) to create more complex behavior, and add object animation, again all driven by the Python script. I'll post my results on this thread.

Once I have a working simulation, I'll share my .CAR and Python script files to anyone interested.

«1

Comments

  • bighbigh Posts: 7,623
    edited December 1969

    Continuing with my research into using PyCarrara to create animation, I thought I would advance the first of two interests I have in using PyCarrara - this one on simulating realistic flocking behavior.

    I am developing a Python script that will drive a number of objects to simulate realistic flocking - what you would see in birds, bats, insects, fish, horses, sheep, etc.

    The following video shows a "proof of concept" animation in simulating flocking behavior in Carrara using PyCarrara. In this example, all animation (except camera movement) are driven by the PyCarrara script. Objects are randomly placed in a ring and then allowed to "fly" on their own. There is nothing random built into the animation. All actions are driven by a set of rules about the "flock." Emergent behavior arises from these few basic rules.

    http://youtu.be/6fZGMlzZhZg

    In upcoming work, I will tweak the motion to reduce jitter, add additional flocking rules (e.g., boundary containment and attraction) to create more complex behavior, and add object animation, again all driven by the Python script. I'll post my results on this thread.

    Once I have a working simulation, I'll share my .CAR and Python script files to anyone interested.

    way over my head :ohh:

  • FractalDimensiaFractalDimensia Posts: 0
    edited December 1969

    bigh said:
    way over my head :ohh:

    Sorry, Big. Let me try it this way. When I am done with the script, there is little you will have to understand about how it works to get good flocking animation results.

    That is, anyone with PyCarrara will be able to set up their flocking objects, make a few (directed) changes to the script I am writing, "import" the script into their Carrara scene, and render the animation. That is literally it!

    An example scenario: Suppose you have a space scene where 50 fighter spacecraft are flying towards a planet. Your options are:
    1) create flight paths for each ship individually (too time consuming and prone to difficulties if you change them)
    2) use a replicator (not realistic enough because they move in unison)
    3) use the PyCarrara script I am writing, tweaking a few parameters, and get a realistic "gaggle" of spaceships flying

    Other scenarios: army of soldiers marching, butterflies flying in an open field, herd of horses running through a valley, mechanical spiders attacking, etc.

    I hope that help! :)

  • evilproducerevilproducer Posts: 8,900
    edited December 1969

    It would be great if you could get formations in the script, such as the formations that Canadian geese fly in, or the way a squadron of fighters flies in formation.

  • DartanbeckDartanbeck Posts: 13,976
    edited December 1969

    Dartan want....
    ...Dartan Want!!!! :)

  • FractalDimensiaFractalDimensia Posts: 0
    edited December 1969

    It would be great if you could get formations in the script, such as the formations that Canadian geese fly in, or the way a squadron of fighters flies in formation.

    Interesting you should mention that! I was just reading about the rules to create this behavior. A project was done to done with that being one of the extensions. You can read a brief summary here:
    http://www.navgen.com/3d_boids/

    I'll add that as an additional rule, EP. It's a good addition!

  • head waxhead wax Posts: 6,751
    edited October 2013

    well and wow that's some simulation, nice job!! it's really interesting to watch your result and reverse enigineer it in my head - not that I know code but I did design a data base for my wife's business about a million years ago that used plenty of code (Borland Paradox)

    so you could make the randomising stimulation music or the tonal values of a painting ? etc.

    really impressive, it reminded me of a flock of wwi planes, red baron watchout

    Post edited by head wax on
  • DartanbeckDartanbeck Posts: 13,976
    edited December 1969

    So, do you think that this could be applied to people with a walk cycle clip attached - basically having them walk like that, perhaps bump into one another and slightly alter course accordingly?
    Even if the answer is not a simple "yes", I still want this. Very cool FD! Very Cool!

    My little birds that I made have NLA clips that supply their in-flight motion, so you just slide the clips from side to side, expand and contract them slightly to get variance in how their wings flap, etc., But in this video, they're all just in a group that rotates up overhead. You see, the NLA doesn't include any global transitions, just the bird's movement. It'll be cool to see them in action with your script. It's actually a pretty heavy morph to make these into the gulls in the video. Their normal state is a small dragon-like fairy beast. The beauty of them is that I made them look pretty darn cool, with very few polygons and a very simple rig and morph system. So you should be able to use a lot of them.

  • FractalDimensiaFractalDimensia Posts: 0
    edited December 1969

    head wax said:
    well and wow that's some simulation, nice job!! it's really interesting to watch your result and reverse enigineer it in my head - not that I know code but I did design a data base for my wife's business about a million years ago that used plenty of code (Borland Paradox)

    so you could make the randomising stimulation music or the tonal values of a painting ? etc.

    really impressive, it reminded me of a flock of wwi planes, red baron watchout

    I used Borland Paradox, so know what you mean. :)

    It is not hard to add some randomization to the script, but it is pretty natural without it. And, yes, you can use music and other things to make it even more "interesting." In fact, my second interest in using PyCarrara is to manipulate objects (shape, color, position, etc.) with music. PyCarrara examples come with using MIDI files, but I am working on some code that will use WAV, MP3, and other formats as well.

    Armed with how to write in Python, PyCarrara is limited literally only by what you can envision.

  • FractalDimensiaFractalDimensia Posts: 0
    edited December 1969

    So, do you think that this could be applied to people with a walk cycle clip attached - basically having them walk like that, perhaps bump into one another and slightly alter course accordingly?
    Even if the answer is not a simple "yes", I still want this. Very cool FD! Very Cool!

    My little birds that I made have NLA clips that supply their in-flight motion, so you just slide the clips from side to side, expand and contract them slightly to get variance in how their wings flap, etc., But in this video, they're all just in a group that rotates up overhead. You see, the NLA doesn't include any global transitions, just the bird's movement. It'll be cool to see them in action with your script. It's actually a pretty heavy morph to make these into the gulls in the video. Their normal state is a small dragon-like fairy beast. The beauty of them is that I made them look pretty darn cool, with very few polygons and a very simple rig and morph system. So you should be able to use a lot of them.

    Hi, Dart. Yes, you can create a "flock" of people. The tweaking is just in how close you let them get to each other, the speed they move, etc. One issue I have at the moment is terrain following. There is no way I know of yet to get terrain information into the Python script so it can adjust moving objects accordingly. For things moving on a surface, the surface would need to be flat. Still working that. Would be easy if I could get access to the "Shoot Ray" algorithm buried in Carrara. I could shoot vertical rays at the terrain and get back z-axis information.

    BTW, PyCarrara can even produce the walk cycle if you want. That is one of the examples Frederic used. So each person can have a slightly different walk cycle (e.g., Zombie walks? ;) ). (I'm still thinking through that one, but don't see that it is a problem.)

    The birds you show are nice. Nicely done! And yes, they could be done using my Python script (once it is complete). You will (for example) be able to add an "attractor" object to your scene, and have the objects be attracted to (or repelled by) that object. In your example, drop a ball in the scene, have it move in the direction you want the flock to fly (at the speed you want), make it invisible, add the birds (# limited only by computer memory), and adjust/run the script. Python script can be used to create bird animation as well (a bird walk cycle?), so each wing beat could be animated through the script. To make it more realistic, you could randomly set (e.g., 2 second) pauses in bird animation, so birds would soar.

  • PhilemoPhilemo Posts: 922
    edited October 2013

    I've been working on something very similar some years ago (a crowd simulation).
    I gave up, mainly for personal reasons (it was a turn in my life), but I had a working prototype at the time.
    See thread in the old forum

    Anyway, I have a lot of experience with PyCarrara, simulation, steering...

    While I don't feel like restarting this project, I may help. So, If you need any help, or just a sounding board, please say so (forum or PM).

    Post edited by Philemo on
  • Max TurladMax Turlad Posts: 84
    edited December 1969

    FractalDimensia,

    cool and very interesting. Thank you for sharing it with us.

  • FractalDimensiaFractalDimensia Posts: 0
    edited December 1969

    Philemo said:
    I've been working on something very similar some years ago (a crowd simulation).
    I gave up, mainly for personal reasons (it was a turn in my life), but I had a working prototype at the time.
    See thread in the old forum

    Anyway, I have a lot of experience with PyCarrara, simulation, steering...

    While I don't feel like restarting this project, I may help. So, If you need any help, or just a sounding board, please say so (forum or PM).

    Phil, will do! I'm good with using this thread for discussions so others can read what we're thinking. (If we need to talk bad about Dart or EP, we can use PM. ;) ) The discussions already have helped sparked new ideas on this scripting.

    I'm open to whatever interest you have (or anyone else has) in taking this. I can share the script at any time. You can work with it (run tests, create scenarios, etc.) and send me what you've learned, and I'll integrate your stuff back into what I am doing. Or

    I want to keep this script "living" by continuously updating and improving it until I've bumped up against real limitations (e.g., PyCarrara script limitations, computer hardware limitations, mental limitations ;) ).

    Just let me know how you want to proceed! :)

  • FractalDimensiaFractalDimensia Posts: 0
    edited December 1969

    Here's a new simulation run of a slightly modified version of the script.

    http://youtu.be/V18pfHFJoro

    Changes made:
    1) New rule: "Containment" - You can define a bounding box to keep the objects inside. When they move outside that box, they naturally turn back. In the video, the bounding region (x,y) is shown by the colored tile floor.
    2) Some tweaking in the motion to make them a bit more natural. (Still working a few issues.)
    3) All camera motion is driven by the script. At the moment, this is pretty basic. It points at the center of the flock, and move accordingly. Working on more complex camera behavior.

    Next:
    1) New rule: "Attraction" - Objects tend to move towards (or away from if repelled) an object in the scene, and follow that object if it is moving.
    2) Add z-axis (altitude) variation to the flight
    3) New rule: "V Formation" - Objects tend to fly in a V

  • PhilemoPhilemo Posts: 922
    edited December 1969

    Phil, will do! I'm good with using this thread for discussions so others can read what we're thinking. (If we need to talk bad about Dart or EP, we can use PM. ;) ) The discussions already have helped sparked new ideas on this scripting.

    I'm open to whatever interest you have (or anyone else has) in taking this. I can share the script at any time. You can work with it (run tests, create scenarios, etc.) and send me what you've learned, and I'll integrate your stuff back into what I am doing. Or

    I want to keep this script "living" by continuously updating and improving it until I've bumped up against real limitations (e.g., PyCarrara script limitations, computer hardware limitations, mental limitations ;) ).

    Just let me know how you want to proceed! :)

    I think the easiest way is to create a project in code.google.com (or sourceforge, but I think google is easier).
    This is what they say :


    Instant project creation on any topic
    Subversion, Mercurial, and Git code hosting with 2 gigabytes of storage space
    Integrated source code browsing and code review tools to make it easy to view code, review contributions, and maintain a high quality code base
    An issue tracker and project wiki that are simple, yet flexible and powerful, and can adapt to any development process

  • FractalDimensiaFractalDimensia Posts: 0
    edited December 1969

    Philemo said:
    I think the easiest way is to create a project in code.google.com (or sourceforge, but I think google is easier).
    This is what they say :


    Instant project creation on any topic
    Subversion, Mercurial, and Git code hosting with 2 gigabytes of storage space
    Integrated source code browsing and code review tools to make it easy to view code, review contributions, and maintain a high quality code base
    An issue tracker and project wiki that are simple, yet flexible and powerful, and can adapt to any development process

    Hmmm.... "maintain a high quality code base"? Don't think they've invented a tool to help me there! ;)

    Okay, will look into that later today and see what it will take to set something up. I'll post what I discover in this thread. Thanks.

  • PhilemoPhilemo Posts: 922
    edited December 1969

    And I would definitely suggest CGKIT to help with 3D maths, skeleton simulation, bvh import... and Simpy to easily allow your boids to interact.

  • FractalDimensiaFractalDimensia Posts: 0
    edited October 2013

    Philemo said:
    And I would definitely suggest CGKIT to help with 3D maths, skeleton simulation, bvh import... and Simpy to easily allow your boids to interact.

    Thanks, Phil. Great ideas. Did a quick read of these two tools and agree, they would be useful. So far, the 3D math has been pretty basic, but think it could get heavy quickly. (E.g., Was thinking it would be possible to do terrain following by reading the terrain as a mesh or even a JPG file with altitude determined by pixel intensity.)

    The hardest part (it seems from first blush) will be integrating CGKIT features into PyCarrara manipulation.

    Unfortunately, I have been coding in Python for less than 2 weeks now, so I'm still green on importing modules. Maybe after I start up a project in Google Code, you can have a look at what I have so far and offer some specific suggestions how this would work?

    Post edited by FractalDimensia on
  • PhilemoPhilemo Posts: 922
    edited December 1969

    [

    The hardest part (it seems from first blush) will be integrating CGKIT features into PyCarrara manipulation.

    Unfortunately, I have been coding in Python for less than 2 weeks now, so I'm still green on importing modules. Maybe after I start up a project in Google Code, you can have a look at what I have so far and offer some specific suggestions how this would work?

    Yes I will do that.

  • FractalDimensiaFractalDimensia Posts: 0
    edited December 1969

    Philemo said:
    [

    The hardest part (it seems from first blush) will be integrating CGKIT features into PyCarrara manipulation.

    Unfortunately, I have been coding in Python for less than 2 weeks now, so I'm still green on importing modules. Maybe after I start up a project in Google Code, you can have a look at what I have so far and offer some specific suggestions how this would work?

    Yes I will do that.

    Great!

  • FractalDimensiaFractalDimensia Posts: 0
    edited October 2013

    Over the past few days, I've made great progress in simulating flocking behavior. So much so that, in the next few days,

    I will be releasing a draft version of the script I've dubbed "PySwarm."

    PySwarm is a Python script specifically designed for use with PyCarrara, that makes it simple to create realistic swarming and flocking behavior in Carrara. In this first public release, you will be able to easily activate and mix six different group behavior settings that, when combined, can bring an incredible amount of flexibility and realism to your Carrara animations. These behavior settings are:

    1) Cohesion: Individuals in the group attempt to stay close together
    2) Separation: Individuals maintain safe distances
    3) Alignment: Individuals attempt to move in the same direction and speed
    4) Containment: Individuals stay within a defined region of space
    5) Attraction: Individuals move towards an object in the scene (even if it is moving)
    6) Speed Limit: Individuals do not exceed a specified speed

    Watch a demo reel I put together on PySwarm V0.3, which shows how adjustments in these 6 behavior settings create different group patterns.
    http://youtu.be/U-dW1ZwGCFc

    What will be included in this release:
    - PySwarm V0.3 script file: an easy to use text file editable in any word editor
    - The .CAR scene used to produce all of the sample simulations shown in the demo reel.
    - A Users Guide, giving detailed information how to modify and use the PySwarm script in Carrara scenes.

    What is next:
    1) With this release, I will be creating a Google Project that will allow people to assist in advancing this capability further, and post their examples, tweaks, enhancment suggestions, and bugs. Thanks, Phil, for the suggestion! Stay tuned.
    2) Continue adding more behavior settings. Currently, the list includes:
    - Landing: Individuals will light or perch on the ground if they get close (momentarily) and then continue.
    - V Formation: Individuals will move in a formation.
    - Predator-Prey: With two groups (one predators, one prey), predators attempt to move towards their prey while the prey attempt to flee.
    3) Add the ability to insert walk cycles for
    4) Building several scenes that use PySwarm.

    Post edited by FractalDimensia on
  • DartanbeckDartanbeck Posts: 13,976
    edited December 1969

    Superb! This is great news. Perhaps I can pick up on some of the script jargon from my days with the Neverwinter Nights community and help out. We'll see what it looks like. Nevertheless, I am very interested in obtaining this! :)

  • FractalDimensiaFractalDimensia Posts: 0
    edited December 1969

    Superb! This is great news. Perhaps I can pick up on some of the script jargon from my days with the Neverwinter Nights community and help out. We'll see what it looks like. Nevertheless, I am very interested in obtaining this! :)

    Thought you might. :)

    Finally fixed the link to the "Demo Reel" video.

    http://youtu.be/U-dW1ZwGCFc

  • PhilWPhilW Posts: 4,707
    edited December 1969

    I'm loving these experiments, flocking behaviour is something missing from Carrara and you are making great progress here. It would be great to see an example where there are some static objects to avoid / fly around. Great work - I'm impressed!

  • FractalDimensiaFractalDimensia Posts: 0
    edited December 1969

    PhilW said:
    I'm loving these experiments, flocking behaviour is something missing from Carrara and you are making great progress here. It would be great to see an example where there are some static objects to avoid / fly around. Great work - I'm impressed!

    Thanks, Phil. At the moment, I have more ideas to pursue than time to do them in. I have a feeling this project will keep me busy for the next few weeks at least. But it's incredibly fun!

    The object avoidance would not be too difficult to add, so I'll put that on the list. I'm already working up the inverse version of the bounding area that would handle most avoidance, except for more complex shapes. I'm also looking into ways to modify the bounding area to be other shapes, such as spheres and cylinders. Having multiple bounding areas would provide more complex bounding/avoidance areas.

  • PhilWPhilW Posts: 4,707
    edited December 1969

    It will be fun watching your progress on this!

  • FractalDimensiaFractalDimensia Posts: 0
    edited December 1969

    For those still interested in more on to this thread, I thought I would share a few YouTube videos that will give you an idea of where I envision seeing the PySwarm script going. It's still a bit off, but you can get a visionary idea from these:

    http://www.youtube.com/watch?v=3bTqWsVqyzE (the first animation video to use BOID (or swarming) algorithm - circa 1985)

    http://www.youtube.com/watch?v=-jF5sAqBp4w (more modern use of the technology)

    http://www.youtube.com/watch?v=kJiKKNCQorQ (simulated school of fish)

    http://www.youtube.com/watch?v=eakKfY5aHmY (real life starlings showing flocking behavior soon to be achievable with PySwarm)

    I am currently working on a "Paper Fighter Aircraft" simulation I will release with the script. I should have it completed in a few days.

  • PhilWPhilW Posts: 4,707
    edited December 1969

    Thanks for sharing these, all impressive stuff. The footage of the starlings set to music is simply magical!

  • CbirdCbird Posts: 223
    edited December 1969

    No music, but this video catches the beautiful waves of silver, then dark as the sandpiper flock changes direction. One of my favourite natural sights!

    http://youtu.be/0SlbspPz8K0

  • PhilWPhilW Posts: 4,707
    edited December 1969

    A flock of Cbirds!!

  • CbirdCbird Posts: 223
    edited December 1969

    PhilW said:
    A flock of Cbirds!!

    Scary thought! That would be more questions than even the great Carrarists here could cope with. :-)

Sign In or Register to comment.