10
$\begingroup$

Motivation: I would like to add multiple empties with sequential, but meaningful, names using the Python API with Blender.

Issue: I cannot name the objects as I add them.

Question: What can I add to the bpy.ops.object.add() code to give a name, or how can I change the name immediately after adding the object?

So far, to insert empties with (x,y,z) coordinates (1.1,1.1,1.1), (2.2,2.2,2.2), (3.3,3.3,3.3), and (4.4,4.4,4.4):

import bpy

x = (1.1,2.2,3.3,4.4)
y = (1.1,2.2,3.3,4.4)
z = (1.1,2.2,3.3,4.4)

for index,val in enumerate(x):
    bpy.ops.object.add(type='EMPTY', location=(x[index],y[index],z[index]))

They are added with sequential names:

  1. Empty.001
  2. Empty.002
  3. Empty.003
  4. Empty.004

Here is a screenshot of the output: Screenshot of Blender after running Python script

But, I want them to have names:

  1. SpecifiedName.001
  2. SpecifiedName.002
  3. SpecifiedName.003
  4. SpecifiedName.004

I cannot seem to just add the property name = "SpecifiedName" to read:

bpy.ops.object.add(type='EMPTY', location=(x[index],y[index],z[index])) ###DOES NOT WORK!!!###

If you are curious as to the final outcome I am looking to do, I want to plot several different datasets in x,y,z coordinates. Each empty from each dataset (and cooresponding name) will be linked to an mesh like a sphere or cube. This gives me the chance to animate a flythrough or rotation of 3D scatter plot data.

$\endgroup$

1 Answer 1

21
$\begingroup$

When you use bpy.ops.object.add() the newly created object becomes the active object, so right after creating the object you can alter it's name with

bpy.context.active_object.name = 'new_name'

It is also possible to create objects without using operators. This approach will create the objects without altering the existing selection or active object.

import bpy

x = (1.1,2.2,3.3,4.4)
y = (1.1,2.2,3.3,4.4)
z = (1.1,2.2,3.3,4.4)

for index,val in enumerate(x): 
    new_obj = bpy.data.objects.new('new_obj', None) 
    new_obj.location = (x[index],y[index],z[index])
    bpy.context.scene.objects.link(new_obj)

Here 'new_obj' will be the name of the new object, if it is non-unique it will also get a numeric extension.

$\endgroup$
3
  • $\begingroup$ So the 'new_obj' will automatically create an empty without a location? What exactly is 'bpy.context.scene.objects.link(new_obj)' linking to? Is there a way to link or duplicate another object that I have created previously? $\endgroup$
    – PhDeadlift
    Commented Jan 12, 2016 at 17:41
  • 3
    $\begingroup$ new() will create an object with a location of (0,0,0), you then set the location to place it where you want it. link() is adding the object to the scene - bpy.context.scene is the current scene. You can use new_obj = object.copy() to duplicate an existing object, then use link() to place it in the scene. Compare with this answer $\endgroup$
    – sambler
    Commented Jan 12, 2016 at 19:02
  • $\begingroup$ OK. Thanks sambler. This all work. I would upvote your answer, but I do not have enough reputation at this point. $\endgroup$
    – PhDeadlift
    Commented Jan 13, 2016 at 16:13

You must log in to answer this question.

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