12
$\begingroup$

I'm generating metaballs with a python script, and I need to be able to toggle the negative option for some of the metaballs. (Edit: negative changes the object's "influence" to subtract from the other metaballs, instead of adding like normal metaballs; the negative checkbox can be found in a metaball object's Object Data section while in Edit Mode.)

def metbaseobj(N=3,scale=1.0):
    '''adds N metaball objects to provide the base shape'''
    shapes = ['CAPSULE','ELLIPSOID','BALL','CUBE']
    metbaseinfo = []

    for each in range(N):
        shape = shapes[random.randint(0,1)]
        loc=(scale*random.random(),scale*random.random(),scale*random.random())
        rot=(random.random(),random.random(),random.random())
        bpy.ops.object.metaball_add(type=shape,location=loc,rotation=rot)
        #[toggle metaball influence to be negative here]
        metbaseinfo.append((shape,loc,rot))

    return(metbaseinfo)

I found a documentation page that makes me think use_negative=True is what I need, I just can't figure out how to use it exactly. Thanks for any help, I'll keep looking in the meantime.

$\endgroup$
0

2 Answers 2

8
$\begingroup$

It seems you already found the wiki docs on metaballs, lots of juicy content there indeed, perhaps after reading/running this script that documentation will make more sense.

You seem to be adding multiple separate metaball objects, instead add one metaball object, and internally to that object create metaball elements, then set their interaction type, something like this.

import bpy

scene = bpy.context.scene

# add metaball object
mball = bpy.data.metaballs.new("MyBall")
obj = bpy.data.objects.new("MyBallObject", mball)
scene.objects.link(obj)

# test with different values, for viewport do you need ultra high res?
mball.resolution = 0.16  # View resolution
# mball.render_resolution = you decide

# first metaball element doesn't exist yet - create it.
ele = mball.elements.new()
ele.co = (0.0, 0.0, 0.0)
ele.use_negative = False
ele.radius = 2.0

# adding extra metaball elements to the object 
ele = mball.elements.new()
ele.co = (-0.726, 1.006, 0.559)
ele.use_negative = True
ele.radius = 0.82279

And that's it.

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ I think this will work for me, thanks @zeffii! $\endgroup$ Commented Jun 25, 2013 at 6:25
  • $\begingroup$ @CodyReisdorf, i will fix up a better example lateron, check back! $\endgroup$
    – zeffii
    Commented Jun 25, 2013 at 6:30
3
$\begingroup$

I think your question can be easily addressed by defining what a MetaBall is in Blender.

MetaBall is a collection of MetaElements.

So in order to use/update the use_negative property, you need to get access to an element from the collection that is part of metaball.

In the following example, the metaball consists of 3 elements of 2 different types (BALL and CAPSULE) and one of the elements has use_negative property set to True.

Constituents of a MetaBall

    >>> metaballobject = C.active_object
    >>> metaballdata = metaballobject.data
    >>> metaelements = metaballdata.elements
    >>> metaelements[0].type, metaelements[0].use_negative
    ('BALL', False)
    >>> metaelements[1].type, metaelements[1].use_negative
    ('CAPSULE', True)
    >>> metaelements[2].type, metaelements[2].use_negative
    ('CAPSULE', False)

Toggling the use_negative property for some elements

    >>> metaelements[2].use_negative = True
    >>> metaelements[1].use_negative = False

Resulting metaball after modifying elements

Resulting metaball after editing elements

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .