Script to make empty parent of a bone

I’m having some trouble finding a way to make a bone a child of ae Empty via a script. Does anyone know how this can be done? Thanks a lot:)

There is indeed a problem which can probably be solved, it some tells (us) how Shift -click (set the active object!) can be done via Python (API)
bpy.context.active_object is READONLY ;-(

but this script works?!


import bpy
bpy.ops.object.armature_add() #name is Armature
bpy.ops.object.add() #name is Empty and <b>the ACTIVE object</b>

empty = bpy.data.objects['Empty']
arma = bpy.data.objects['Armature']
#bpy.ops.object.posemode_toggle()
#first the child, then then the parent (always latest!)
#but from scripting it seem to be important|
#wchic object is 'active'???!!!
arma.select = True
empty.select = True
bpy.ops.object.parent_set()

is this for 2.49?

@binoculos: NO

If someone is looking for scripts for blender-2.4x
there is a whole bunch - nearly everything -
including such things like creating EMPTY for bones.
If you look in my signature-thread
there is even a script i made for creating EMPTYs
for every particle to constraint objects to those particle-movements.

creating an EMPTY in blender-2.54:


def MakeEmpty(Name):
    e = None
    for i in bpy.data.objects:
        if i.name == Name: e = i
    if e == None:
        bpy.ops.object.add(type='EMPTY')
        e = bpy.context.active_object
        e.name = Name
    return(e)

this creates a new EMPTY with name and if one with this name
already exists, it only returns this.
So if a script runs multiple times, the objects are only created once.

No, my example was for 2.54 SVN >32600 (W32 Vista)

I really need to use 2.49. I can find a lot of scripts to create empties for each bone in an armature but none to parent them to the empties :S

Try to use test-dr examples?! (sorry, I do not dive into 2.49 problems)

a part of my old blender-2.4x scriptings, you have to adjust it for your need
but the idea should be clear, it sets an ik-constraint for the bone with
an object as target - you can use other constraints like copy-location, i only used this, because i did not setup all bones, i wanted only the “end”-bones to follow an empty


	def make_armature_constraints(self, scene):
		ArmatObj = self.ArmatObject
		if ArmatObj.type != "Armature":
			print "Error, not type Armature"
			return
		ArmatData = Blender.Armature.Get(ArmatObj.data.name)
		print ArmatData.bones.values()
		pose = ArmatObj.getPose()
		for bone in ArmatData.bones.values():
			posebone = pose.bones[ bone.name ]
			if len(posebone.constraints) &gt; 0:
#				constraint = posebone.constraints.append(Blender.Constraint.Type.IKSOLVER)
				constraint = posebone.constraints[0]
				print constraint.influence
				print constraint[Constraint.Settings.TARGET]
#				print Constraint.Settings
#				print Constraint.Type
			else:
				for i in self.bones_setup:
					if i[0] == bone.name:
						break
				if bone.name == "head":
					continue
				if bone.name[:4] == "foot":
					continue
				if bone.name[:4] == "hand":
					continue
				if bone.name == "root":
					constraint = posebone.constraints.append(Blender.Constraint.Type.COPYLOC)
					constraint = posebone.constraints[len(posebone.constraints)-1]
					constraint.influence = 1.0
					constraint[Constraint.Settings.TARGET] = Blender.Object.Get("%s%0.02d"%(self.name,i[1]))
					constraint = posebone.constraints.append(Blender.Constraint.Type.COPYROT)
					constraint = posebone.constraints[len(posebone.constraints)-1]
					constraint.influence = 1.0
					constraint[Constraint.Settings.TARGET] = Blender.Object.Get("%s%0.02d"%(self.name,i[1]))
				else:
					#######	all bones except the root-bone ######
					constraint = posebone.constraints.append(Blender.Constraint.Type.IKSOLVER)
					constraint = posebone.constraints[len(posebone.constraints)-1]
					constraint.influence = 1.0
					constraint[Constraint.Settings.CHAINLEN] = 1
					constraint[Constraint.Settings.USETIP] = True
					constraint[Constraint.Settings.TARGET] = Blender.Object.Get("%s%0.02d"%(self.name,i[2]))
					print bone.name, "set ik", ("%s%0.02d"%(self.name,i[2]))
		pose.update()
		ArmatObj.select(1)
		ArmatObj.makeDisplayList()
		self.ArmatObject = ArmatObj
		return(ArmatObj)