2
$\begingroup$

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!

$\endgroup$
7
  • 1
    $\begingroup$ 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 blender.stackexchange.com/questions/248274/… $\endgroup$
    – Gorgious
    Commented Jun 17, 2022 at 19:12
  • $\begingroup$ I don't understand operator overrides, I guess I should have clarified that. I don't know how they work $\endgroup$ Commented Jun 17, 2022 at 19:39
  • $\begingroup$ 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 $\endgroup$
    – Gorgious
    Commented Jun 17, 2022 at 20:28
  • $\begingroup$ This is also a good resource blender.stackexchange.com/questions/6101/… $\endgroup$
    – Gorgious
    Commented Jun 17, 2022 at 20:29
  • $\begingroup$ Oh wait, I think I get it now. Thank you. $\endgroup$ Commented Jun 17, 2022 at 20:39

1 Answer 1

3
$\begingroup$

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()

$\endgroup$
2
  • 1
    $\begingroup$ 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 :) $\endgroup$
    – Gorgious
    Commented Jun 18, 2022 at 21:25
  • $\begingroup$ @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. $\endgroup$ Commented Jun 18, 2022 at 23:30

You must log in to answer this question.

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