1
$\begingroup$

So im pretty new to this. I'm using python on blender and i created a box with this code

import bpy 

bpy.ops.mesh.primitive_cube_add()
box = bpy.context.active_object

box.location[2] = 4
box.location[0] = 2

The issue is, when i run the code blender creates a brand new cube and moves it

$\endgroup$
4
$\begingroup$

So lets add line-by-line comments to your code:

# Import bpy tells Python you're using the Blender main library
import bpy 

# create a brand new cube
bpy.ops.mesh.primitive_cube_add()

# make 'box' a reference to the active object
box = bpy.context.active_object

# set the box's z location to 4
box.location[2] = 4
# set the box's x location to 2
box.location[0] = 2

As you can perhaps see from the comments, every time you execute bpy.ops.mesh.primitive_cube_add(), you're going to get a new cube. Removing that line means you will move whatever object is active. You could explicitly move the default cube with:

 box = bpy.data.objects['Cube']

Where 'Cube' is the name in the outliner.

You can also move the box using a vector like this:

 box.location = (2,0,4)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.