Replace selected objects with curves or metaspheres

I’m trying to write a simple automation script that, for each object inside selection, would copy it’s coordinates to variable, delete object, and add another object (like a curve or metasphere) using the coords from variable.

Being a complete noob to both Python and bpy libs doesn’t help, even though I know it should be a rather simple task.

For starters, I have a problem with ‘cycling’ through selected objects, so i don’t know how to tell Blender to choose a single (active?) object from selection, on which I can then perform an operation (like add it’s coords to the end of (x1,y1,z1…xn,yn,zn) type list and delete object when done), and then have it choose another.
Or is there a better way to do this?

Any help appreciated, but no ready code please, I’m here to learn! :slight_smile:

Any help appreciated, but no ready code please, I’m here to learn!

SURE!
Here goes

first up in python you need to bring in the bpy module, this will give you access to all of blender python commands (blender python, blender py, b py, bpy)… so we need to import it in


import bpy

from there you can make a loop such as


for a in bpy.context.selected_objects:
    print(a.name)

which will print out all selected objects names into the system console… What that is doing is what is called a loop…
blender reports a list of objects under bpy.context.selected_objects
it is saying first entry in this list, set as the variable a, then run the code following which is indented
then is there a second entry? if so run again, and so on and so on for every item in that list

or


print(bpy.context.active_object.name)

will print the active objects name or even


print(bpy.context.active_object.location)

will print a vector, of x y z locations of the active object

experiment in the python console to see what you can and can’t access!! thats how i learnt a lot about bpy

try selecting one object, then switching to the python console, and type bpy.context.selected_objects[0]. then press ctrl+ space, it will give a list of options that you can access from there

you can try say setting the location of an object via here aswell such as bpy.context.selected_objects[0].location[0] = 5 (set the x location of the selected object to 5

Once you learn a bit about this, look into bmesh and its commands. thats waht you will need to know for the verts.

You may want to simply replace the objects with dupligroup Empties then swap the group name. A lot simpler than creating a bunch of objects and unlinking ones and relinking others to the scene.

Whoa! :smiley:
Thanks a lot, this was of great help and really pushed me forward. I managed to resolve this!
At first I tried it like this:

import bpy
for a in bpy.context.selected_objects:

    coordinates=bpy.context.active_object.location
    bpy.ops.object.metaball_add(type='BALL', radius=1, location=coordinates)

but that generated all the metaballs using initial coordinates from a single active object. No wonder, since it didn’t update the location variable on each pass :P.
So then on irc someone told me to use a.location instead, and it went like this:

import bpy

for a in bpy.context.selected_objects:
       
    bpy.ops.object.metaball_add(type='BALL', radius=1, location=a.location)

And it worked!
Now I’m trying to figure out how to delete the initial objects. Although it’s better to just spawn the metaballs on a separate layer, I wanna try and write a code that addresses and deletes each of the objects as the script goes through them.

I tried relinking objects using the good old Ctrl-L (searched the forum, saw this is preferred method), but apparently relinking a mesh object to metasphere is not allowed (kind of not surprising looking at how metaspheres are dealt with).
The present method is slooooow, but it works with anything I want, and I can write functions for parameters of each object I want to generate :slight_smile:

Well, that’s a start of a wonderful adventure :slight_smile: