Python Help - object.location does not move child empty objects or does in a weird way

Hello, I’m trying to assemble a robot in python by taking the parts and putting them together. My leg object for example has 2 empty objects as children for placement of other parts. When i copy this object and move it to “Root” object in the world center, the empties move in a very weird way. Any idea of what’s happnening??

import bpy
from random import random

def get_children(ob):
    return [ob_child for ob_child in Object.Get() if ob_child.parent == ob]

#Get the objects in the materials collection and a random material from there.
MATERIALS = bpy.data.collections["MATERIALS"]
MATERIALCOUNT = len(MATERIALS.objects)
RAND_PRIMARY_MAT = round(random()*(MATERIALCOUNT-1))
RAND_SECONDARY_MAT = round(random()*(MATERIALCOUNT-1))
PRIMARY_MAT = MATERIALS.objects[RAND_PRIMARY_MAT].data.materials[0]
SECONDARY_MAT = MATERIALS.objects[RAND_SECONDARY_MAT].data.materials[0] 

#select your collection by name (in this case the collection named "Collection")
MECHBUILD = bpy.data.collections["MECHBUILD"]
headCol = bpy.data.collections["PILOT"]
frontCol = bpy.data.collections["FRONT"]
topCol = bpy.data.collections["TOP"]
itemCol = bpy.data.collections["ITEM"]
legCol = bpy.data.collections["LEGS"]

#clean whatever's there
for obj in MECHBUILD.objects:
    bpy.data.objects.remove(obj, do_unlink=True)
    

numHead = len(headCol.objects)
numTop = len(topCol.objects)
numFront = len(frontCol.objects)
numItem = len(itemCol.objects)
numLeg = len(legCol.objects)


#Generate random number 
randHead = round(random()*(numHead-1))  
randTop = round(random()*(numTop-1))  
randFront = round(random()*(numFront-1))  
randItem = round(random()*(numItem-1))  
randLeg = round(random()*(numLeg-1)) 

headObj = headCol.objects[randHead]
topObj = topCol.objects[randTop]
frontObj = frontCol.objects[randFront]
itemObj = itemCol.objects[randItem]
legObj = legCol.objects[randLeg]

#creat a copy of the object and link it to the collection 
#newHead = headObj.copy()
#MECHBUILD.objects.link(newHead)

newLeg = legObj.copy()
newLeg.data = legObj.data.copy()
MECHBUILD.objects.link(newLeg)
for child in legObj.children:
    newLegChild = child.copy()
    newLegChild.parent = newLeg
    MECHBUILD.objects.link(newLegChild)

newLeg.name = "NEWLEG"
newLeg.data.materials[1] = PRIMARY_MAT
newLeg.data.materials[2] = SECONDARY_MAT

newTop = topObj.copy()
newTop.data = topObj.data.copy()
MECHBUILD.objects.link(newTop)
for child in topObj.children:
    newChild = child.copy()
    newChild.parent = newTop
    MECHBUILD.objects.link(newChild)
    
newTop.name = "NEWTOP"
newTop.data.materials[1] = PRIMARY_MAT
newTop.data.materials[2] = SECONDARY_MAT

newFront = frontObj.copy()
newFront.data = frontObj.data.copy()
MECHBUILD.objects.link(newFront)
newFront.name = "NEWFRONT"
newFront.data.materials[1] = PRIMARY_MAT
newFront.data.materials[2] = SECONDARY_MAT

newItem = itemObj.copy()
newItem.data = itemObj.data.copy()
newItem.name = "NEWITEM"
MECHBUILD.objects.link(newItem)
newItem.data.materials[1] = SECONDARY_MAT


newLeg.location = bpy.data.objects['Root'].location

newTop.location = bpy.data.objects['NEWLEG'].location
#newHead.location = bpy.data.objects['TRANSFORM_PILOT'].location
newFront.location =  bpy.data.objects['NEWLEG'].location
newItem.location =  bpy.data.objects['NEWTOP'].location
modifier = newItem.modifiers["Mirror"] # Get existing modifier with name "Array"
modifier.mirror_object = newTop```

define “moves in a very weird way”. unless you’re experiencing some critical bug in Blender nobody else has seen, it’s moving exactly in the way you’ve told it to move- so this weirdness is entirely subjective. We need your description of what is happening to help diagnose the problem.

What it does is, when it copies the robot part and its children, when I change the location of the parent(the part itself) the children don’t stay in the same relative position as you would expect with parenting.

It’s structured like this:

Legs

  • Torso Socket
  • Holster

Curtis Holt has a number of generators that do what you’re attempting to do and they work fine so I don’t think there’s a bug with Blender.

Post a blend that works with your script that demonstrates the wrong behavior.