11
$\begingroup$

Is there a simple way of triangulating all faces in a mesh in the python API? I am already aware of that you can do it in blender by selecting an object in edit mode and pressing ctrl+T, but I need to do so in a python script.

Thank you in advance!

$\endgroup$
0

1 Answer 1

17
$\begingroup$

You could use the bmesh.ops.triangulate

See TextEditor > Templates > Python > Bmesh simple edit mode for some of the boilerplate.

For any mesh in object mode.

# This example assumes we have a mesh in object mode.

import bpy
import bmesh

def triangulate_object(obj):
    me = obj.data
    # Get a BMesh representation
    bm = bmesh.new()
    bm.from_mesh(me)

    bmesh.ops.triangulate(bm, faces=bm.faces[:])
    # V2.79 : bmesh.ops.triangulate(bm, faces=bm.faces[:], quad_method=0, ngon_method=0)

    # Finish up, write the bmesh back to the mesh
    bm.to_mesh(me)
    bm.free()

# Get the active object (could be any mesh object)
triangulate_object(bpy.context.active_object)

For any mesh in edit mode.

# This example assumes we have a mesh object in edit-mode

import bpy
import bmesh

def triangulate_edit_object(obj):
    me = obj.data
    # Get a BMesh representation
    bm = bmesh.from_edit_mesh(me)

    bmesh.ops.triangulate(bm, faces=bm.faces[:])
    # V2.79 : bmesh.ops.triangulate(bm, faces=bm.faces[:], quad_method=0, ngon_method=0)

    # Show the updates in the viewport
    # and recalculate n-gon tessellation.
    bmesh.update_edit_mesh(me, True)

# Get the edit mesh
triangulate_edit_object(bpy.context.edit_object)
$\endgroup$
5
  • 1
    $\begingroup$ Thank you for the answer, this is what worked for me in the end. $\endgroup$
    – Kirjain
    Commented Jan 27, 2016 at 13:02
  • 1
    $\begingroup$ In Blender 2.81 I used bmesh.ops.triangulate(bm, faces=bm.faces[:], quad_method='BEAUTY', ngon_method='BEAUTY') to make above work. $\endgroup$
    – WDC
    Commented Mar 9, 2020 at 12:36
  • $\begingroup$ It turns out that the script mentioned by WDC is exactly the one that we would see if we went to the Faces Menu (Ctrl + F) and actually floated our cursor over the Faces to Tris option until the tool tip appears. One can very often find the python script which triggers from using the UI by consulting the menus in this way. If you don't see the python scripts listed in the tooltips for menu items, then you may need to enable this feature in User Preferences under Interface > Python Tooltips. Saves a lot of guess work and downtime to be able to refer to these. $\endgroup$
    – R-800
    Commented Mar 7, 2021 at 7:15
  • $\begingroup$ @R-800 Please note the difference between bmesh.ops as mentioned by WDC and bpy.ops as seen mousing over UI. $\endgroup$
    – batFINGER
    Commented Mar 9, 2021 at 6:49
  • $\begingroup$ without bmesh, you can do: ob = context.object ; mesh = ob.data ; loop = mesh.loop_triangles ; tri = loop[0] ; $\endgroup$
    – Daniol Dan
    Commented May 13 at 23:14

You must log in to answer this question.

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