1
$\begingroup$

Im trying to check if the selected object has a specific modifier, adding the modifier if it doesn't exist, and then applying all the modifiers:

import bpy

obj = bpy.context.object

modifs=[]
for modifier in obj.modifiers:
    modifs.append(modifier.type)

if 'TRIANGULATE' not in modifs:]
    bpy.ops.object.modifier_add(type='TRIANGULATE')

for modif in modifs:
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modif)

Code above adds the modifier if it isn't present but it doesn't apply the modifiers to the object. If I paste bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modif) into the python console it applies the modifier. What am I doing wrong?

$\endgroup$
4
  • 1
    $\begingroup$ Try bpy.ops.object.convert(target='MESH') instead of the last 'for modif in modifs' loop above. $\endgroup$
    – lemon
    Commented Feb 29, 2020 at 19:57
  • $\begingroup$ Also in your script you should 'modif.append(modifier.name)' not 'modifier.type'. What is unclear: shoud the triangulate modifier be applied? $\endgroup$
    – lemon
    Commented Feb 29, 2020 at 20:05
  • $\begingroup$ Nah, type is nice in case there is another trianulate modifier @lemon $\endgroup$
    – brockmann
    Commented Feb 29, 2020 at 20:42
  • $\begingroup$ @brockmann, was talking about the first loop, not the triangulate. $\endgroup$
    – lemon
    Commented Mar 1, 2020 at 7:49

1 Answer 1

3
$\begingroup$

The list doesn’t contain the newly added triangulate modifier (if there is one). Also, it seems necessary to apply by name, not by type. The following works for me:

import bpy

obj = bpy.context.object

modif_types = [ modifier.type for modifier in obj.modifiers ]

if 'TRIANGULATE' not in modif_types:
    bpy.ops.object.modifier_add(type='TRIANGULATE')

modif_names = [ modifier.name for modifier in obj.modifiers ]

for modif in modif_names:
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modif)
$\endgroup$

You must log in to answer this question.

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