# Create and return a cube using a python script

In the above code, I need to change the location and the dimension of the cube. I remark that only the dimensions along the z-axis is changed. How can I change the name of the cube? I have not been able to find out how to do this in the documentation.

  import bpy
from mathutils import Matrix
D = bpy.data
C = bpy.context
scene = C.scene

# ... lots of code omitted for brevity and clarity

#------------------House-------------

#Wall1
bpy.context.object.data.name = "Cube1" #to change the name but it does not work
#here to change the dimensions, but only the z-axis dimension has been changed
bpy.context.object.dimensions[0] = 4
bpy.context.object.dimensions[1] = 0.05
bpy.context.object.dimensions[2] = 2.5

• When you assign a string to bpy.context.object.data.name it will overwrite the data name, see: i.stack.imgur.com/HtmtO.png instead of the object name... Please avoid asking multiple questions. Thanks
– p2or
Jun 24 '15 at 7:05

When you write

bpy.context.object.data.name = "Cube1" #to change the name but it does not work


you are changing the name of the MESH datablock. To change the name of the OBJECT datablock:

bpy.context.object.name = "Cube1"


I would recommend altering the scale of the object instead of the dimensions.

bpy.context.object.scale = (2, 0.025, 1.25)


to resize (scaling ) the cube, we can use this function

#Wall1
#resize the cube
bpy.ops.transform.resize(value=(4, .05, 2.5))


to rename, we can use this instruction

# Get the cube object and rename it.
cube = bpy.context.object
cube.name = 'Wall1'


but I did not understand why this first way did not work

• Resize function working great Dec 6 '20 at 13:30