Python 2d gravity/accretion sim

EDIT: I’ve put some more time into this script, it’s nowhere near what I have in mind but I figure it’s worth posting the new code since it could help someone else with similar hurtles and/or be a fun little toy to waste 15-20 minutes with.

ORIGINAL POST: This started as a lark, then the more I messed with it the more I thought it could be a fun little
script. So I added a UI and thought I’d release it into the wild to see if anyone thought it was
worth commenting on.

It’s basically a hacky 2D gravity/accretion simulator.
http://www.funkboxing.com/blender/untitled.jpg
start by creating a field of ‘globs’
add a ‘sun’ if you want (stationary gravity well)
add ‘vector tails’, to visualize the globs velocity
if you want you can rotate and scale the ‘vector tails’ to adjust the starting condition of the sim also you can reposition and scale the ‘globs’ and ‘suns’
then animate - the results are baked to fcurves.

that’s the theory anyway. I have a lot of other features I’d like to add but right now even these are buggy and the sim is very slow. anyway it’s fun and I’m still learning 2.5 API. This was made on 2.56a.

right now I need help from anyone who can generate a metaball with python with bpy.data (not bpy.ops)
obviously the cubes are just a stop-gap.
also there are several other known issues I am working on - the most critical of which is
the fact that I cannot seem to get rid of objects by unlinking. if you play with this for
awhile you may notice your file size goes to >100M with no objects in the scene. If you
figure out why let me know.

UI will be in physics panel,
to try just hit ‘createGlobs’ then ‘animateGlobs’
then play the animation

here’s the code. It’s ugly. clearly a work in progress…
http://www.funkboxing.com/blender/gravity.txt

That a funny little thingy. The usability remains slightly unclear to me, but that’s of course boring, irrelevant, and unpoetical. I hope you add more stuff to it.

It only uses one of my four processor cores, so slow yes, but that should be simple to fix I assume.

Keep it up!

The why is easy. Your globs are unlinked from the scene but not deleted. If you look at the outliner window > view datablocks > meshes : you can see dozens of objects mglob.NNN and vectail.NNN.

Now: how to actually delete them and properly remove them from the blend file… ? I am both a blender and python newbie, so I wouldn’t know.

I’m studying your code because I am trying to figure out how to animate stuff via python:
http://blenderartists.org/forum/showthread.php?t=208391

cube = bpy.data.objects['Cube']
mesh = cube.data

bpy.data.objects.remove(cube)
bpy.data.meshes.remove(mesh)

.remove() fails if the target has any users so you probably want to wrap it in a try: block.


metaball = bpy.data.metaballs.new('foo')
<add verts, etc... to metaball>
foo = bpy.data.objects.new('foo', metaball)
context.scene.objects.link(foo)

I got a script to run on playback but it was ugly
i forgot where I found this.

  1. create Empty
  2. add single keyframe to Zrot (frame 1)
  • should have default expanded poly generator on it
  1. add single driver to Yloc (frame1)
  • in Drivers> choose - Scripted sxpression
  • enter float(exec("
    ".join([l.body for l in bpy.data.texts[“RUN”].lines]))==None)
  1. then a script called ‘RUN’ should execute every time the frame changes
  • Zrot of Empty drives expression which (i think) grabs the text datablock of the script and runs it

here’s a quick thing I did. set up the empty as above…
then make a cube (named ‘Cube’) subdivide it a lot, then run this-


import bpy
import random
ob = bpy.data.objects['Cube']
verts = ob.data.vertices
for v in verts:
    rxnum = round(random.uniform(-0.01 ,0.01), 4)
    rynum = round(random.uniform(-0.01 ,0.01), 4)
    rznum = round(random.uniform(-0.01 ,0.01), 4)    
    v.co[0] = v.co[0] + rxnum
    v.co[1] = v.co[1] + rynum
    v.co[2] = v.co[2] + rznum    
scene = bpy.context.scene

hey thanks
your code helped me write a flush function that checks for 0 users and removes and
if it has any trouble it raises a flag to run itself again.
for some reason flushing sometimes requires a few loops - weird…
I must be doing something wrong…

and on the metaballs… also worked, though I don’t get really get the ‘metaelements’
so my metaballs dont look like the ones made by the add menu

anyway…
I’m putting REVISED version of the script up
mostly just fixes, no metaballs yet.
http://www.funkboxing.com/blender/gravity.txt

I tried a bit how to delete the objects.

Since they do show in the outliner’s datablock, it makes sense they would show in bpy.data, and they do:

bpy.data.meshes[‘mglob.002’].

but even using the autocomplete feature, I don’t see a method to delete them.

Right-clicking in the datablock itself does not bring the option to remove them. Even the select/deselect and hide/unhide option do not seem to work.

I guess you best bet is to file this as a bug report. It does look like a bug (??)… At least you’d get back from the people who understand this the best.

I tried the method suggested by Uncle Entity but it doesn’t work:


>>> o = bpy.data.objects['glob-011']
>>> m = o.data
>>> bpy.data.objects.remove(o)
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
SystemError: Error: Object "glob-011" must have zero users to be removed, found 1.

as expected the remove() call didn’t work because I hadn’t removed the the object from the scene.
I do so and recall remove()


>>> bpy.data.objects.remove(o)
>>> bpy.data.meshes.remove(m)

There is no error but the object’s mesh is still in the outliner’s datablock.

try to save close and re open the file

sometimes list are upated only when saved and re open i hink!

happy 2.5

NEW VERSION - See top.

I’m still having trouble with metaballs and also trouble with data sticking around even after unlinking and my .blends growing to obscene sizes.
Any ideas would be great. Thanks.