How Does Python Know The Parent?

One question leads to the next, mostly due to the answers being very vague by providing a single code statement with little to no further explanation.

Like I said.

I’m not using the UPBGE for that reason. I was told early on to use an older version, but it wasn’t really explained why. I found out on my own by trying both and consistently having the UPBGE carsh.

I just want a few lines of code.

What operator?

Being vague doesn’t help.

What do you want to do exactly?
Because if it’s just setting one object as the parent of another, then you already know how:
objectA.parent = objectB
But if this is not really what you want, then you’re in a XY problem loop.

1 Like

I just want to be able to create and parent a camera to an object. I’m using the BGE in Blender 2.79.

This isn’t an XY problem. I know what I want to do.

The closest I’ve gotten is similar to what you show:

objects =
a = objects['Cube']
b = objects['Enemy Camera']
b.parent = a

But it doesn’t work now. It worked the first time, but then someone told me it would cause problems if I didn’t deal with something to do with the world positioning. They didn’t really explain so they just created more confusion/run-around.

Here’s what I have right now (hacked together from different sources and not working)::

import bpy

#Add new camera to scene (works)
bpy.ops.object.camera_add(location=(0.885581, 0, 2.01546), rotation=(1.5708, -0, 1.5708))

#Assign a name to the camera (works) = “Enemy Camera”

def move_camera(cont):

own =  cont.owner

# grab the enemy by name
enemy = own.scene.objects['Cube']

#grab the active camera or use the same line as enemy, but with the camera name
camera = own.scene.objects['Enemy Camera']

#set camera worldposition
camera.worldPosition = enemy.worldPosition.copy()

#add an offset so camera is not inside the enemy
camera.worldPosition.z += 2.0
camera.worldPosition.y -= 0.5

#Parent the camera to enemy
objects =
a = objects['Cube']
b = objects['Enemy Camera']
b.parent = a

Ahh, so you don’t want just to parent the camera to the object!!!

You want to parent the camera and move the camera to the object’s position and align the camera to some axis of the parent!!!

from mathutils import Matrix

obj =['Cube']
cam =['Enemy Camera']

#reset cam matrix and align it to Y, with a bit of translation in YZ
cam_matrix=Matrix(((1.0, 0.0, 0.0, 0.0),
                  (0.0, 0.0, -1.0, moveforward),
                  (0.0, 1.0, 0.0, moveup),
                  (0.0, 0.0, 0.0, 1.0)))
cam.matrix_world = cam_matrix
cam.parent = obj
1 Like

Well yeah, that should be a given. But I already figured that part out so it shouldn’t need to be explained,should it? I could have detailed more, like the fact that the object the camera is parented to will move around, that I will be switching to and from that camera to others, that the camera will hold properties, etc. Are you saying all that matters too?

I just gave you the problem broken down to just what I need. I left that other code in there to show exactly what I had so far.

Your code doesn’t work, btw.

I found the solution somewhere else, and it appears to work fine. Do you see any problems with it?

import bpy

#Add new camera to scene
bpy.ops.object.camera_add(location=(0.885581, 0, 2.01546), rotation=(1.5708, -0, 1.5708))

#Assign a name to the camera = “Enemy Camera”

a =[‘Cube’]
b =[‘Enemy Camera’]

bpy.ops.object.select_all(action=‘DESELECT’) #deselect all object = True = True #select the object for the ‘parenting’ = a #the active object will be the parent of all selected object


#Now The parent of b is a

It does work here. Note that I didn’t include import bpy because it’s known that you need it.

Not the most pleasant code I’ve ever seen, but it should work.

I added that line. It didn’t work.

I didn’t write it. I did what you and others told me to do. At least I have something that works and is understandable enough to see what it’s doing so I can learn from it. Better than a single line of code like most people were throwing at me and expecting me to understand how to use.

That’s a nice sense of entitlement you have there, goes great with that giant chip on your shoulder.

I can’t speak for anyone else but I’m personally done trying to help you with anything, now or in the future.

any idea if it is possible to find with what option parent was created. I want to unparent an object, to parent it with an empty and then get back to the first parent. but they are several options possible when parenting. BTW I wonder how parent is done in Blender and if it could have an equivalent in Python. I ask it there because I didn’t find this question anywhere Is it possible to know with what option a parent was created?