Python API : OBJECT_OT_convert can't be fully abstracted from context with override #93188

Open
opened 2021-11-18 14:55:59 +01:00 by Gorgious · 12 comments

System Information
Operating system: Windows-10-10.0.19041-SP0 64 Bits
Graphics card: NVIDIA GeForce GTX 1660 SUPER/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 472.39

Blender Version
Broken: version: 2.93.5, branch: master, commit date: 2021-10-05 12:04, hash: a791bdabd0
It didn't work in latest Beta build either.
Worked: I don't think it ever worked otherwise ?

Short description of error
Overriding the context of an operator with a dictionary enables us to call them from pretty much anywhere in the code without having to make sure the context is correct (selected objects, active object, called from a particular editor, etc.)

However the OBJECT_OT_convert called with bpy.ops.object.convert operator still requires an active object to be selected to function with an override, which defeats the purpose of using an override.

It is explained quite thoroughly in this BSE Q&A https://blender.stackexchange.com/a/235048/86891

Exact steps for others to reproduce the error
Script to reproduce in an empty file :

import bpy

bpy.ops.curve.primitive_bezier_curve_add()

# Commenting these 2 lines removes the RuntimeError (context incorrect) on last line
bpy.context.active_object.select_set(False)
bpy.context.view_layer.objects.active = None

bpy.ops.object.convert({"selected_objects": bpy.data.objects}, target="MESH")
**System Information** Operating system: Windows-10-10.0.19041-SP0 64 Bits Graphics card: NVIDIA GeForce GTX 1660 SUPER/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 472.39 **Blender Version** Broken: version: 2.93.5, branch: master, commit date: 2021-10-05 12:04, hash: `a791bdabd0` It didn't work in latest Beta build either. Worked: I don't think it ever worked otherwise ? **Short description of error** Overriding the context of an operator with a dictionary enables us to call them from pretty much anywhere in the code without having to make sure the context is correct (selected objects, active object, called from a particular editor, etc.) However the `OBJECT_OT_convert` called with `bpy.ops.object.convert` operator still requires an active object to be selected to function with an override, which defeats the purpose of using an override. It is explained quite thoroughly in this BSE Q&A https://blender.stackexchange.com/a/235048/86891 **Exact steps for others to reproduce the error** Script to reproduce in an empty file : ``` import bpy bpy.ops.curve.primitive_bezier_curve_add() # Commenting these 2 lines removes the RuntimeError (context incorrect) on last line bpy.context.active_object.select_set(False) bpy.context.view_layer.objects.active = None bpy.ops.object.convert({"selected_objects": bpy.data.objects}, target="MESH") ```
Author

Added subscriber: @nathan.hild

Added subscriber: @nathan.hild
Author

I can add that bpy.ops.object.join has the same limitation :

This works :

bpy.context.view_layer.objects.active = next(o for o in bpy.data.objects if o.type == 'MESH')
for obj in bpy.data.objects:        
    obj.select_set(True)
bpy.ops.object.join()

This doesn't :

bpy.ops.object.join(
    {
        "selected_objects" : bpy.data.objects,
        "active_object": next(o for o in bpy.data.objects if o.type == 'MESH')
    }
)

Related thread on BSE https://blender.stackexchange.com/a/243663/86891

I can add that `bpy.ops.object.join` has the same limitation : This works : ``` bpy.context.view_layer.objects.active = next(o for o in bpy.data.objects if o.type == 'MESH') for obj in bpy.data.objects: obj.select_set(True) bpy.ops.object.join() ``` This doesn't : ``` bpy.ops.object.join( { "selected_objects" : bpy.data.objects, "active_object": next(o for o in bpy.data.objects if o.type == 'MESH') } ) ``` Related thread on BSE https://blender.stackexchange.com/a/243663/86891
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

Join relies on selected_editable_objects, this works:

bpy.ops.object.join(
    {
        "selected_editable_objects" : bpy.data.objects,
        "active_object": next(o for o in bpy.data.objects if o.type == 'MESH')
    }
)
`Join` relies on `selected_editable_objects`, this works: ``` bpy.ops.object.join( { "selected_editable_objects" : bpy.data.objects, "active_object": next(o for o in bpy.data.objects if o.type == 'MESH') } ) ```
Author

Oh, my bad, thanks for the clarification. :)

Is there a similar context override that I've missed for Convert ?

Oh, my bad, thanks for the clarification. :) Is there a similar context override that I've missed for `Convert` ?
Member

Changed status from 'Needs Triage' to: 'Needs Developer To Reproduce'

Changed status from 'Needs Triage' to: 'Needs Developer To Reproduce'
Member

Added subscriber: @dr.sybren

Added subscriber: @dr.sybren
Member

Regarding the Convert operator: I think it's true, this cannot be fully abstracted with override context.

Instead of relying on Object only, this operator takes into account the Base (which is kind of an abstraction of Object that can handle stuff like selection or visibilty in different viewlayers).
Doing this is useful, because it can then handle the needed stuff on the Base & new Base properly when a new converted object is created.
And afaict, python does not have access to this Base "stuff".
I dont actually think it is absolutely mandatory that this operator works this way (I think it could first rely on Object only -- and only later handle the Base as well -- which could make the whole usecase of this report possible), however, I am unsure if this could be called a bug (dont think it is, it is probably just a limitation of this particular operator -- and there are workarounds using python differently here).

Since @dr.sybren would probably know this in more depth (and would know if there is some sort of guideline or "promised support" of overriding context): could this be called a bug?

Regarding the `Convert` operator: I think it's true, this cannot be fully abstracted with override context. Instead of relying on `Object` only, this operator takes into account the `Base` (which is kind of an abstraction of `Object` that can handle stuff like selection or visibilty in different viewlayers). Doing this is useful, because it can then handle the needed stuff on the Base & new Base properly when a new converted object is created. And afaict, python does not have access to this `Base` "stuff". I dont actually think it is absolutely mandatory that this operator works this way (I think it could first rely on `Object` only -- and only later handle the `Base` as well -- which could make the whole usecase of this report possible), however, I am unsure if this could be called a bug (dont think it is, it is probably just a limitation of this particular operator -- and there are workarounds using python differently here). Since @dr.sybren would probably know this in more depth (and would know if there is some sort of guideline or "promised support" of overriding context): could this be called a bug?
Philipp Oeser removed the
Interest
Python API
label 2023-02-10 09:04:29 +01:00
Contributor

Any updates on this issue? Met this one on Blender 3.6.

Any updates on this issue? Met this one on Blender 3.6.

Join relies on selected_editable_objects

This also applies to bpy.ops.object.convert: it relies on selected_editable_objects. Furthermore, for current Blender versions you'll want to do the context override differently:

all_objects = bpy.data.objects
with bpy.context.temp_override(
        active_object=all_objects[0],
        selected_editable_objects=all_objects):
    bpy.ops.object.convert(target="MESH")

And having said all that, it still won't work because the C code has a check for base_act->flag & BASE_SELECTED in there; in other words, the active object has to be marked as selected. It doesn't test whether the object is part of selected_editable_objects, but rather it tests that the object itself is selected. And so that's what you have to ensure to make it work.

The code doesn't rely on the active object much, it only does so in two places:

  • when the active object is a metaball, it triggers some special behaviour
  • when the converted object was the active object, the result of the conversion is marked as active as well

I think the former could be a bug, as it should check the type of the object that's being converted, rather than the active object (the active one could be a mesh, and the selection could include metaballs). The latter shouldn't completely stop the conversion either, as if there is no active object before, it's fine if there is no active object afterwards.

> `Join` relies on `selected_editable_objects` This also applies to `bpy.ops.object.convert`: it relies on `selected_editable_objects`. Furthermore, for current Blender versions you'll want to do the context override differently: ```python all_objects = bpy.data.objects with bpy.context.temp_override( active_object=all_objects[0], selected_editable_objects=all_objects): bpy.ops.object.convert(target="MESH") ``` And having said all that, it still won't work because the C code has a check for `base_act->flag & BASE_SELECTED` in there; in other words, the active object has to be marked as selected. It doesn't test whether the object is part of `selected_editable_objects`, but rather it tests that the object itself is selected. And so that's what you have to ensure to make it work. The code doesn't rely on the active object much, it only does so in two places: - when the active object is a metaball, it triggers some special behaviour - when the converted object was the active object, the result of the conversion is marked as active as well I think the former could be a bug, as it should check the type of the object that's being converted, rather than the active object (the active one could be a mesh, and the selection could include metaballs). The latter shouldn't completely stop the conversion either, as if there is no active object before, it's fine if there is no active object afterwards.

This issue still occurs in 4.0.2. Is the fact that it cannot be used with context.temp_override not considered a bug? Reading Sybren's reply above I don't quite understand why the check for the object to be marked selected, rather than be in selected_editable_objects cannot be changed.

This issue still occurs in 4.0.2. Is the fact that it cannot be used with `context.temp_override` not considered a bug? Reading Sybren's reply above I don't quite understand why the check for the object to be marked selected, rather than be in `selected_editable_objects` cannot be changed.

This issue still occurs in 4.0.2.

Thanks for re-testing before writing here :)

Is the fact that it cannot be used with context.temp_override not considered a bug?

I don't think so, I'm not aware of any requirement that operators should be fully overridable with context.temp_override. Of course it would be nice, but I don't think that requirement is there now.

Reading Sybren's reply above I don't quite understand why the check for the object to be marked selected, rather than be in selected_editable_objects cannot be changed.

I'm wondering what the reason is you're using this operator to begin with. Metaballs and Surfaces can be converted to mesh by calling the_object.to_mesh(). Granted, that mesh then might need some special handling (because it's an evaluated one, so it needs to get copied before you can assign it to another object), but at least it doesn't require anything in terms of overrides, selection state, etc.

PS: don't get me wrong, I'm not pushing back against your needs & frustrations. I'm merely trying to paint a more complete picture, and asking questions to figure out your exact needs.

> This issue still occurs in 4.0.2. Thanks for re-testing before writing here :) > Is the fact that it cannot be used with `context.temp_override` not considered a bug? I don't think so, I'm not aware of any requirement that operators should be fully overridable with `context.temp_override`. Of course it would be nice, but I don't think that requirement is there now. > Reading Sybren's reply above I don't quite understand why the check for the object to be marked selected, rather than be in `selected_editable_objects` cannot be changed. I'm wondering what the reason is you're using this operator to begin with. Metaballs and Surfaces can be converted to mesh by calling `the_object.to_mesh()`. Granted, that mesh then might need some special handling (because it's an evaluated one, so it needs to get copied before you can assign it to another object), but at least it doesn't require anything in terms of overrides, selection state, etc. PS: don't get me wrong, I'm *not* pushing back against your needs & frustrations. I'm merely trying to paint a more complete picture, and asking questions to figure out your exact needs.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#93188
No description provided.