importbpydefstrVector3(v3):returnstr(v3.x)+","+str(v3.y)+","+str(v3.z)# create a new cubebpy.ops.mesh.primitive_cube_add()# newly created cube will be automatically selectedcube=bpy.context.selected_objects[0]# change namecube.name="MyLittleCube"# change its locationcube.location=(0.0,5.0,0.0)# doneprint("Done creating MyCube at position "+strVector3(cube.location)+" with name "+cube.name)

Then name the file to blender_cube_creation.py. Finally, click on Run Script.

Then you will see that Blender will create a new cube, select it, rename into "MyLittleCube", and translate to (0.0, 5.0, 0.0).

Tips

If you hold mouse over each Blender's UI element for a while, it will show a tool tip of python function that you can use it in the script.

Play around with each function in Python Console window at the bottom before combining them up in full script.

To know how to do something in code for certain actions we did directly on Blender GUI, just do some actions on Blender first, then look at Info window (on the top), you will see something like this for history of python functions called. Then you know which functions to call in python code.

I blog about projects I'm working on. Mostly about mobile games, tech, web service, little tiny things I built for myself or for public, and tech/game industry-wide. Currently I'm living in Shenzhen, China. I'm running a small creative company with my girlfriend here.