I've an object with pivot point in the centre (0,0,0). I want to mirror it by python on the x-axis and do it for all objects in my current scene.
I tried this, but the second line seems not to be applied:
def mirror_object_along_x(obj):
bpy.context.scene.objects.active = obj
bpy.ops.transform.mirror(constraint_axis=(True, False, False), constraint_orientation='GLOBAL', proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
Or I tried also this but then the indices of the generated triangles are in the wrong order:
def mirror_object_along_x(obj):
obj.matrix_world[0][0] *= -1
I found this answer here:
How to use a pivot point to mirror mesh with script
But it also doesn't work. I'm using Blender 2.79a.
UPDATE
To be more specific. I want to:
Loop all objects in my current scene and mirror them on the x-axis. The original object shall not be there anymore but only the mirrored one.
I try to use the approach form @batFINGER and it looks like that right now:
import bpy
import bmesh
from mathutils import Matrix
def mirror_object_along_x(obj, axis):
'''Mirrors the x-coordinates of the object.'''
'''obj: the object to be mirrored'''
'''axis: the axis to mirror the object on => x, y, z = 0, 1, 2'''
transformationMatrix = Matrix.Scale(-1, 4, Matrix.Identity(3)[axis])
pivotPointGlocalCoordinates = bpy.context.scene.cursor_location
objectData = obj.data
newBmesh = bmesh.new()
newBmesh.from_mesh(objectData)
bmesh.ops.transform(newBmesh, matrix=transformationMatrix, space=Matrix.Translation(-pivotPointGlocalCoordinates), verts=newBmesh.verts)
# recalculate the normals
bmesh.ops.recalc_face_normals(newBmesh, faces=newBmesh.faces)
# write to mesh
newBmesh.to_mesh(objectData)
objectData.update()
newBmesh.clear() # clear for the next one
newBmesh.free() # free the memory used by bmesh
def main():
for obj in bpy.data.objects:
if obj.type == 'MESH':
mirror_object_along_x(obj, 0)
main()
lpp = ob.matrix_world.inverted() * pp # pivot point local coords
is missing? I didn't include it because I haven't seen the variablelpp
being used later. Does this line of code need to be executed anyway? $\endgroup$ – Bruno Bieri Nov 21 '18 at 6:48