22
$\begingroup$

How to add an empty object with python using bpy and not using any ops? My code so far is doing this:

bpy.ops.object.empty_add(type='PLAIN_AXES',radius=RADIUS,location=loc)

For normal object like mesh or lamp there is object_data created using normal bpy.data.object.new calls, but for empties it seems that this method does not exist. So is this possible to avoid using the operators? The reason I'm asking is because when I add an empty using bpy.ops it changes the active object, etc. causing the script to be more bloated with unnecessary code lines.

$\endgroup$

2 Answers 2

43
$\begingroup$

This is possible by placing None as the object data:

#                          Name,   Data
o = bpy.data.objects.new( "empty", None )
bpy.context.scene.objects.link( o )

You can later access its unique empty props like any ol' empty:

o.empty_draw_size = 2
o.empty_draw_type = 'PLAIN_AXES'

For Blender 2.8, the API change a little bit:

o = bpy.data.objects.new( "empty", None )

# due to the new mechanism of "collection"
bpy.context.scene.collection.objects.link( o )

# empty_draw was replaced by empty_display
o.empty_display_size = 2
o.empty_display_type = 'PLAIN_AXES'   
$\endgroup$
2
  • 1
    $\begingroup$ WOW!!! That was what I was looking for! Thank you. To not waste B.SE service I would like to point out that this was not mentioned in any documentation I could find about objects. It seems to be very easy, much simpler that creating any other objects. $\endgroup$
    – piotao
    Commented Apr 23, 2016 at 21:49
  • 4
    $\begingroup$ For Blender 2.8: bpy.context.scene.collection.objects.link( o ) $\endgroup$
    – Logic1
    Commented Mar 19, 2019 at 8:29
1
$\begingroup$

I made a function based on the answer by TLousky (Tested in Blender 3.2)

# Makes an empty, at location, stores it in existing collection
def make_empty(name, location, coll_name): #string, vector, string of existing coll
    empty_obj = bpy.data.objects.new( "empty", None, )
    empty_obj.name = name
    empty_obj.empty_display_size = 1 
    bpy.data.collections[coll_name].objects.link(empty_obj)
    empty_obj.location = location
    return empty_obj
$\endgroup$

You must log in to answer this question.

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