1
$\begingroup$

I'm looking for a way to store selected object / curve name so that I can reference it in a script.

The idea is to select a curve (with the mouse), then copy another object to its location(with a script). I would also need to reference the original object so I can fill in some modifiers. Problems I'm having now: In order to duplicate that "other" object, I need to select it... which causes me to lose focus from the currently selected original object.

rough code is...

bpy.context.selected_objects 

bpy.data.objects['OBJ1'].select = True #select an already created object

bpy.ops.object.duplicate() #duplicate OBJ1

# move duplicated object to same place as originally selected.
# figure out a way to copy location info from original selection.
bpy.data.objects.new.location 

bpy.ops.object.modifier_add(type='ARRAY')
bpy.context.object.modifiers["Array"].fit_type = 'FIT_CURVE'

#curvename would be the original thing selected
bpy.context.object.modifiers["Array"].curve = bpy.data.objects[curveName]

Thank you very much for your help.

$\endgroup$
  • 1
    $\begingroup$ You can add modifiers without the ops with eg blender.stackexchange.com/questions/58676/… Also you can dupe an object with ob = obj.copy() and then link it to scene with scene.objects.link(ob) Oh and btw to get code formatting indent all lines of code by 4 spaces. $\endgroup$ – batFINGER Aug 15 '16 at 19:36
  • $\begingroup$ Thanks for the response. Aye, that's just some place holder code. Main problem I'm having is referencing the originally selected object. Is there a way to store it's name and then bring focus back to it. I've only really needed to bpy.context.selected_objects and bpy.context.active_object, in the past... so this is a bit more difficult for me. $\endgroup$ – admbro Aug 15 '16 at 19:53
  • 1
    $\begingroup$ Plain and simple curve = context.active_object Then after dupe or other operators that change the active object dupe = context.active_object after which curve will still refer to the curve that was the active object when you assigned it. To set the active context object back to curve use scene.objects.active = curve $\endgroup$ – batFINGER Aug 15 '16 at 20:06
1
$\begingroup$

You would want to store the object by doing:

object1 = bpy.data.objects["Cube"]

then you can use:

object2.location = object1.location

to set object2's location to be the same as object1's location.

$\endgroup$
1
$\begingroup$

for the selection part of things: Why not simply make the user select both items in a specific order? Blender stores the objects in selection order in this property:

bpy.context.selected_objects

That means, if you first select a cube, then a sphere, then a circle, the property content will be:

[bpy.data.objects['Cube'], bpy.data.objects['Sphere'], bpy.data.objects['Circle']]

To access them, you could use the index:

bpy.context.selected_objects[0]  # = first selected object
bpy.context.selected_objects[1]  # = second selected object
bpy.context.selected_objects[-1]  # = last selected object

Also, the last selected object will be the active one:

bpy.context.active_object
$\endgroup$

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.