How Does Python Know The Parent?

I’m trying to learn how to use python in Blender. I use the console window and the info window to help me as much as I can by doing the actions I want to code in the 3D viewer and then copying the output of the info window into the script editor. When I parent a camera to an object, I get this output:

bpy.ops.object.parent_set(type=‘OBJECT’, xmirror=False, keep_transform=False)

I noticed that it doesn’t reference the object I parented to, which it must know somehow. So what am I missing?

the parent_set operator will parent the selected objects to the active object.

Thank you for answering me.

So how do I find out the name of the active object?

You can find the active object with bpy.context.active_object or bpy.context.object (I’m not sure why there are two ways to do the same thing; I’m guessing one is a legacy thing but either works just fine.)

For example you could store that in a variable like: MyVariableName = bpy.context.active_object and then pass MyVariableName around in various parts of your script when needed.

That’s to get the object. If you specifically want the object name then add .name to the end, e.g.

I think it has to do with the different versions.

This is what I’m looking for. A simple line of code that will give me the name of the active object.

Gotcha! Thanks a bunch! This will get me moving forward.

So now that I have a way of getting the name of the active object and putting it in a variable, how do I tell python to make that the active object at some later point in my code when it’s no longer the active object?

I used this in the console to get the active object’s name:

MyVariableName = bpy.context.active_object

I then deselected the object and typed this into the console: = MyVariableName

But it doesn’t make the object active again. What am I doing wrong?

“Active” is a separate thing from “Selected” and that’s also not how to set the active object anyway. :stuck_out_tongue: = MyVariableName

More broadly:

1 Like

own = bpy.context.scene.objects[‘Enemy.000’] = own
Traceback (most recent call last):
File “<blender_console>”, line 1, in
AttributeError: ‘Context’ object has no attribute ‘view_layer’

This is what i was trying to tell you in your other thread. You’re on an old version of Blender that is no longer maintained, so the advice you’re going to get is not always going to be relevant. the view_layer attribute was added in 2.80.

Blender 2,9 UPBGE is so buggy it crashes just about every time I do anything. I was told to stick with older version.

I get a lot of conflicting advice ion this site.

Nice to know but it doesn’t help with my problem.

I thought this site was supposed to be for help as much as for showing off creations.

i get that you’re frustrated, but you’re just going to continue to be frustrated asking questions for an out of date API. there is a section of the forum for the BGE, and everyone there is in the same boat you’re in- have you tried asking over there?

What forum is that?

BGE main forum is here:
BGE support forum is here:

Ya I’m in there and it’s no better.

I finally worked it out on my own. It was so simple I’m shocked that this wasn’t just explained to me.

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”

#Parent the camera to the tank
objects =
a = objects[‘Cube’]
b = objects[‘Enemy Camera’]
b.parent = a

because setting the parent doesn’t’ take the child/parent/world transform matrix relationships into consideration. as soon as you try doing it this way with an object that already has a local transform you’ll find out. the correct way to do it is to either use the operator (understanding how it chooses the parent, as was explained before), or setting the parent manually as you’ve done it and transforming the matrices manually (which is what the operator does for you).

At this rate, it’s going to take a lot of asking searching to get this little task done, because the information is being doled out very slowly to me.

I don’t need to be told that I’m using something that’s out of date when it’s still supported.

I don’t need to be told that I’m making mistakes if the person telling me isn’t going to help any further than that except to add more confusion.

I shouldn’t need to study days worth of material just to figure out how to program something so simple.

Perhaps because you’re making the wrong questions?!.. For example, this thread started with a simple question (‘how does the operator knows which objects to parent’), and it was answered right away.
If that raised more questions, it’s because you don’t fully understand the api. Reading the API documentation is a very good solution to this, starting by reading this page!

Well… that’s a bit relative. If your current knowledge is wrongly based, your assumption of ‘simple’ will never be simple.
It’s not that you need to read everything about blender or python… but understanding the basics of the API and how blender works is a bit of a must.

Also, bare in mind that UPBGE’s API has some differences from the Blender’s API. And as it is still under development, you shouldn’t consider it stable.