2

I am trying to use bpy.ops for things like separating meshes, changing bpy.context.mode, adding modifiers and deleting meshes. However, I am unsure how to change which object bpy.ops operates on.

Here is my code

object.select_set(True)
bpy.ops.object.delete()

I have heard to avoid bpy.ops but I do not know any alternatives to my usages of bpy.ops so any suggestions on alternatives would be appreciated.

Thank You!

  • 1
    bpy.data.objects.remove(obj) for this specific usecase. However in general what you are looking for is an operator override. I set out to create a list of them out there maybe it will be of some help https://blender.stackexchange.com/questions/248274/a-comprehensive-list-of-operator-overrides – Gorgious Jun 17 '22 at 19:12
  • I don't understand operator overrides, I guess I should have clarified that. I don't know how they work – Cerealmarrow100 Jun 17 '22 at 19:39
  • Your question asks how to provide parameters to operators that are not part of the context, the answer is to use an operator override. If you don't want or cannot learn how to use them, I'm afraid you can't do what you want, sorry. If you want to know how they work, I provided you with a keyword to search the internet for resources and a link to the official docs. Apart from that I'm afraid your problem can't be solved – Gorgious Jun 17 '22 at 20:28
  • This is also a good resource https://blender.stackexchange.com/questions/6101/poll-failed-context-incorrect-example-bpy-ops-view3d-background-image-add – Gorgious Jun 17 '22 at 20:29
  • Oh wait, I think I get it now. Thank you. – Cerealmarrow100 Jun 17 '22 at 20:39
  • @Gorgious et al, note that operator overrides are deprecated in 3.2 and scheduled to be removed in 3.3, to be replace by calls to [bpy.context.temp_override()[(https://docs.blender.org/api/current/bpy.types.Context.html#bpy.types.Context.temp_override) – Marty Fouts Jun 17 '22 at 21:05
  • How does it work for mesh operators, is it similar to object ones? – Cerealmarrow100 Jun 17 '22 at 21:32

1 Answers1

3

Here is a more complete answer to your question, some of which has already been provided to you in comments by Gorgious.

  • It is good to avoid bpy.ops where there is an alternative; because of performance issues.
  • In the case of your example, there are three solutions:
    1. Not using bpy.ops as described by Gorgious: bpy.data.objects.remove(obj) This solution is preferred for performance reasons as it uses the relavent low level function.
    2. Using bpy.ops: bpy.context.view_layer.objects.active = obj; bpy.ops.object.delete() This solution sets the active object to your object and then deletes it.
    3. Provide bpy.ops with an alternative context.

Before Blender 3.2 you provided an alternative context by using a context override as described in this answer, as mentioned by Gorgious.

But context overrides are deprecated in Blender 3.2 and scheduled to be removed in blender 3.3, being replaced by temp_override()

Marty Fouts
  • 33,070
  • 10
  • 35
  • 79
  • 1
    Hey Marty, thanks for elaborating on this Q&A. I just want to add I don't think it's exact to say that context overrides are deprecated. From what I understood the current syntax involving a dictionary with parameters and their values is deprecated in favor of a more streamlined syntax with temp_override, but the underlying functionality is still more or less the same. Please correct me if I'm wrong :) – Gorgious Jun 18 '22 at 21:25
  • @Gorgious At this level you are correct. The exact statement from the release nodes is "Passing the context to operators as a dictionary has been deprecated as part of the inclusion of Context.temp_override which should be used instead." But "as a dictionary" is a good definition of "context override" so I use that as a shorthand. Thanks for keeping me honest. ;) I'm not going to argue about whether or not temp_override's syntax is more streamlined, though. – Marty Fouts Jun 18 '22 at 23:30