11
$\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$
18
$\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$
  • 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 Apr 23 '16 at 21:49
  • 2
    $\begingroup$ For Blender 2.8: bpy.context.scene.collection.objects.link( o ) $\endgroup$ – Logic1 Mar 19 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.