1
$\begingroup$

I'm trying to use Blender without invoking the GUI. How do you invoke the bpy.ops functions without a GUI? Is there a bpy.data.objects equivalent of by.ops.object.modifier_apply for instance?

$\endgroup$
1
$\begingroup$

As far as I know, there's no single low-level command that does this like the bpy.ops.object.modifier_apply operator, but this is possible without operators.

You can do this by using the to_mesh method, which generates a new version of the object's mesh data, if you wish - with modifier's applied.

In this example I'm generating a version of the mesh data with modifier's applied, and assigning it directly back into the source object's data.

This updates the mesh geometry, but doesn't remove the modifier (since we simply generated a copy, and haven't actually applied the modifier yet).

So eventually all we need to do is to remove the modifier using the object.modifiers.remove(modifier) method.

import bpy

obj        = bpy.context.object
myModifier = obj.modifiers['Subsurf']

# Apply modifier to existing object data
obj.data = obj.to_mesh( 
    apply_modifiers = True, 
    scene           = bpy.context.scene, 
    settings        = 'PREVIEW' 
)

# Remove modifier from modifier's list
obj.modifiers.remove( myModifier )
$\endgroup$
7
  • $\begingroup$ Thank you for your help. This work fine if I use Blender with a GUI, but not without. This simple script: import bpy bpy.data.images.load("test.jpg") bpy.data.textures['displaceImage'].image = bpy.data.images['test.jpg'] bpy.ops.export_mesh.stl(filepath="test.stl", use_mesh_modifiers=True ) works as well with a GUI, but not without. This puzzles me since there is no reference to a context in it. $\endgroup$
    – S'rCat
    Dec 21 '15 at 22:20
  • 1
    $\begingroup$ or use modifiers.clear() to remove all modifiers $\endgroup$
    – zeffii
    Dec 21 '15 at 22:21
  • $\begingroup$ Not sure I understood. This didn't work for you? By the way you can use either method (mine or the simple apply_modifier operator) with or without a UI, it's simply a matter of context for using the operator (mainly have the object whose modifiers you want to apply selected and active). $\endgroup$
    – TLousky
    Dec 21 '15 at 22:22
  • $\begingroup$ Here is how I'm invoking the scripts: "blender 4x4UVSquare.blend -d -P 'Displace.py'" -> This invocation writes a displaced test.stl object. "blender 4x4UVSquare.blend --background -d -P 'Displace.py'" -> this invocation write a smooth, un-displaced test.stl $\endgroup$
    – S'rCat
    Dec 21 '15 at 22:29
  • $\begingroup$ Can you add the error messages you get to your original question? And optimally your code or the relevant bits? I'm guessing this is a matter of not having a reference to the right object (if you're using the method specified above), or not having selected the object and made it active if you're using the apply_modifier operator. $\endgroup$
    – TLousky
    Dec 21 '15 at 22:32

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.