7
$\begingroup$

I'm trying to move a shape. The shape is in the scene at the end of the script. But it does not move, as the bmesh.ops.translate() seems to be doing nothing.

(Blender 2.76)

# A new mesh
bm = bmesh.new()

# Make a circle
bmesh.ops.create_circle(
    bm,
    cap_ends = True,    # Fill with faces
    cap_tris = True,    # Triangles
    diameter = 2,
    segments = 6 )

# This should move the shape but its not
# I'm missing something?
bmesh.ops.translate(
    bm,
    vec = (5 ,5 , 0) )

# "write the bmesh into a new mesh"
mesh = bpy.data.meshes.new('Mesh')
bm.to_mesh( mesh )

# Add the mesh to the scene
scene = bpy.context.scene
hexagon = bpy.data.objects.new("Hex", mesh)
scene.objects.link( hexagon )

The object appears at the center of the scene.

How can I get this thing to move somewhere else?

$\endgroup$
9
$\begingroup$

You need to give bmesh.ops.translate a list of verts to translate.

Here I've used all verts,

bmesh.ops.translate(
    bm,
    verts=bm.verts,
    vec = (5 ,5 , 0) )

This will translate the mesh verts, the object will still be at (0, 0, 0). To move the object, don't use the bmesh.translate op and simply set its location

hexagon.location = (5, 5, 0)

We can then make a copy and move it somewhere else

anotherhex = hexagon.copy()
anotherhex.location = (10, 10, 5)

both objects share the same mesh "Mesh". Translating the vertices of the mesh will be evident for all objects using that mesh.

The coordinate system for blender is described here https://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Coordinate_Spaces_in_Blender#Global_and_local_coordinates

Vertex coordinates are local, relative to the origin of the object. Look at the default cube, the coords are the eight combinations of 1 and -1 for x, y, z from the origin at (0, 0, 0). Moving the object via its location, (scaling, rotating for that matter) leaves the local coordinates the same.

Translating the vertices moves them locally from the origin of the object, and leaving origin in the same position. Akin to selecting all verts in edit mode and translating.

For most cases making a primitive with origin at (0,0,0) and then moving the object is the way to go, especially for multiple objects that share a mesh.

$\endgroup$
  • 1
    $\begingroup$ What is the difference between using translate() and location? $\endgroup$ – Craig Dec 8 '15 at 20:54
  • 1
    $\begingroup$ Craig, this is one of the basic fundamentals of 3d meshes, and hence is not as easy to explain, or for that matter find good docs... added an explanation to the Ans. $\endgroup$ – batFINGER Dec 9 '15 at 10:43

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.