Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from stackoverflow.com Jan 17 '16 at 18:17

This question came from our site for professional and enthusiast programmers.

up vote 3 down vote accepted

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.

share|improve this answer
    
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? – PhDeadlift Jan 12 '16 at 17:41
    
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 – sambler Jan 12 '16 at 19:02
    
OK. Thanks sambler. This all work. I would upvote your answer, but I do not have enough reputation at this point. – PhDeadlift Jan 13 '16 at 16:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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