Store object in variable and then print its name

Why this code is not working ? i want to create an object then save it to a variable , then trying to get the name.

import bpy

cc=bpy.ops.mesh.primitive_cube_add()
print(cc.name)

The issue will be the return type of primitive_cube_add(), I’d check the documentation for the object reference and the type that is returned if you wish to program it this way.

This works however. This uses the fact that whenever you create an object it is then selected which is why you can access it by accessing the currently selected objects:

import bpy

bpy.ops.mesh.primitive_cube_add()
cc = bpy.context.selected_objects[0]
print(cc.name)

Thanks,

I understand now , sorry because i use python in other app (nuke) , my mindset automatically assume that every object creation will return the instance data, so i can pass it to variable. In nuke, if i create object , i can pass the instance data to variable directly without have to go to selected object. Anyway thanks for explaining this.

Your welcome. I would have tried the same to be honest as I’m used to programming in C# within Unity, but this works at least and doesn’t seem the worst for efficiency. :slight_smile: