2
$\begingroup$

I want to translate some vertices in X axis with constraint_orientation="VIEW" but I dont' know how calc the values??...

next code should work but I don´t know how calc the value in this parameters values = (??????)(as vector)

bpy.ops.transform.translate(value=values, constraint_axis=(True, False, False), constraint_orientation="VIEW")

also I tried with bmesh but then I have a lot of parameters with less values

thanks

$\endgroup$

1 Answer 1

2
$\begingroup$

To calculate the values you convert the original value using the orientation matrix to the world space :

values = intended_value * Orientation_matrix

in the script this would be ( moving 1 unit on the X-axis of the view ) :

import bpy
from mathutils import *

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        viewport = area.regions[4]
        v_m = area.spaces[0].region_3d.view_matrix  #orientation matrix
        #vector conversion from view space to world space
        values = Vector((1,0,0))*v_m
        #the translation is done in global orientation without any constraints
        bpy.ops.transform.translate(value=values)
        #bpy.context.object.location += values   #another way to translate
        print(values)

for bmesh operator (the calculation is done inside the operator ) you pass the translation vector and the matrix of the space used for this translation :

import bpy
import bmesh
from mathutils import *

#get the view matrix
v_m = None
for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        viewport = area.regions[4]
        v_m = area.spaces[0].region_3d.view_matrix

#create a bmesh
obj = bpy.context.active_object
bpy.ops.object.mode_set(mode='EDIT')
bm = bmesh.new()
bm.from_mesh(obj.data)

#translation
vec = Vector((1,0,0)) #one unit on the X-axis 
all_verts = bm.verts  #the vertices to be translated
bmesh.ops.translate(bm, vec =(1,0,0), space = v_m, verts = all_verts)

bpy.ops.object.mode_set(mode='OBJECT')
bm.to_mesh(obj.data)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .