Create an object on the location of a selected object

Hey there!

I’m quite new to python and got most of the addon working as I want it to work, except for one thing that keeps eluding me. The idea behind the addon broken down is the following;

  1. Select the object(bone in this case).
  2. Create Copy Rotation, Copy Location bone constraints on them.
  3. Spawn an empty on the location of the bone (The problem. Can’t figure out how to get the location of the bone and then supply it into the empties location parameter.)
  4. Select the Empty as the target on both of the constraints (Having some issues with this one too. Not sure how to make it select the bone/empty automatically.

This is the code I have thus far;

import bpy

selected_objects = bpy.context.selected_objects
bpy.ops.object.posemode_toggle()
bpy.ops.pose.constraint_add(type='COPY_ROTATION') #Adds copy rotation in pose mode
bpy.ops.pose.constraint_add(type='COPY_LOCATION') #Adds copy location in pose mode
bpy.ops.object.posemode_toggle()
bpy.ops.object.empty_add(type='PLAIN_AXES', align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.ops.object.posemode_toggle()
bpy.context.object.pose.bones["Bone.002"].constraints["Copy Rotation"].target = bpy.data.objects["Empty"]
bpy.context.object.pose.bones["Bone.002"].constraints["Copy Location"].target = bpy.data.objects["Empty"]

Any help would be greatly appreciated. Sorry if these are very newbie questions, not all that experienced in python.

By the way, you have a lot of “ops” operators, which are finicky and prone to failure. You may consider researching alternate ways to preform these tasks :slight_smile:

1 Like