Page MenuHome

Calling python code equivalent of Alt-D results in object not being selected or active
Closed, ArchivedPublic

Description

Blender 2.70

  1. Create a new project,
  2. change to Screenlayout to 'Scripting'
  3. Create a new script file by pressing the "+ New" button on the bottom menubar
  4. Paste the code below into the window.
  5. Run the script. Notice all is well.
  6. Delete the newly added cude
  7. Run the script again. Notice the hilite is orange. This means in python it is not active or selected

____ Code _____
import bpy

bpy.ops.object.select_all(action="DESELECT")
bpy.data.objects["Cube"].select = True
bpy.ops.object.duplicate_move_linked(OBJECT_OT_duplicate={"linked":True, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(2, 2, 2), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "remove_on_cancel":False, "release_confirm":False})

Event Timeline

Greg Moress (Imhotep) raised the priority of this task from to 90.
Greg Moress (Imhotep) updated the task description. (Show Details)
Greg Moress (Imhotep) edited a custom field.

Here, the new cube is selected but not active. Probably Alt+D additionally activates it whereas your code doesn't .

But it works the first time.
Also, I couldn't find an 'activate' method. Is that internal?

If you delete the first copy, there is no active object. Hence, after creating the second copy, the copy is also not activated.

It's the same if you do it manually: In a new scene, delete the lamp and the camera. Select all. Alt+D. Delete the new cube. Select All ("A" key, do not select the cube). Press Alt+D again. Now the new copy is also not selected.

This doesn't seem like correct behavior, since the first time it works. I changed the method to be duplicate (Shift-D) and get similar results. I also added the print(current object) -- So you can see it selects/activates the new object the first time, but not the second.
However, if you use the mouse to select the original cube, then when you run the script the SECOND (or third) cube is selected/activated.

Should I do something in addition to bpy.data.objects["Cube"].select = True
in order to make it work as expected?

bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(-1.43941, -0.658703, 2.10968), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "remove_on_cancel":False, "release_confirm":False})
print(bpy.context.active_object)

Willi (willi) added a comment.EditedMay 17 2014, 10:55 PM

It's by design that the copy is only made the active object if the original was the active object. This is true with the first time making a copy. It is not true with the second time because no object is active when making the copy.

What you can do is, after deleting the first copy, activate the cube again. Then making another copy activates the copy.

Lukas Tönne (lukastoenne) changed the task status from Unknown Status to Archived.May 19 2014, 8:22 AM

Explanation by @Willi (willi) is correct. Deleting the cube is not the same as undoing the duplicate operator, it leaves you with no active object.

There is a functional difference between active and selected, in that multiple objects can be selected at once, but only one can be active. This means that when there is no active object you can't simply chose one of the selected in general (it would add hidden rules, which is never a good idea).

Closing the report, there is nothing to do really.