Python UI inconsistent with the documentation #113732

Open
opened 2023-10-15 02:35:15 +02:00 by Ethical Vegan · 7 comments

Blender Version
Broken: 3.6.1
Worked: NA

When baking a texture using Python, the following code correctly bakes the normal map:
import bpy
bpy.ops.object.bake(type='NORMAL')

On the other hand, the following does not work:
import bpy
bpy.context.scene.cycles.bake_type = 'NORMAL'
bpy.ops.object.bake()

But as you can see in the image, the interface is telling us to write "bpy.context.scene.cycles.bake_type = 'NORMAL'"

**Blender Version** Broken: 3.6.1 Worked: NA When baking a texture using Python, the following code correctly bakes the normal map: import bpy bpy.ops.object.bake(type='NORMAL') On the other hand, the following does not work: import bpy bpy.context.scene.cycles.bake_type = 'NORMAL' bpy.ops.object.bake() But as you can see in the image, the interface is telling us to write "bpy.context.scene.cycles.bake_type = 'NORMAL'"
Ethical Vegan added the
Priority
Normal
Type
Report
Status
Needs Triage
labels 2023-10-15 02:35:15 +02:00
Member

Calling bpy.ops.object.bake() will use the default value of its type argument which is 'COMBINED'.

Setting scene.cycles.bake_type changes the type argument of the bpy.ops.object.bake operator that is displayed in the UI: https://projects.blender.org/blender/blender/src/branch/blender-v3.6-release/intern/cycles/blender/addon/ui.py#L1928

Hovering the mouse over with Bake button shows the type argument:
image

No bug here as far as I can tell.

Calling `bpy.ops.object.bake()` will use the default value of its `type` argument which is `'COMBINED'`. Setting `scene.cycles.bake_type` changes the `type` argument of the `bpy.ops.object.bake` operator that is displayed in the UI: https://projects.blender.org/blender/blender/src/branch/blender-v3.6-release/intern/cycles/blender/addon/ui.py#L1928 Hovering the mouse over with Bake button shows the `type` argument: ![image](/attachments/6dae800e-f989-4843-b5c1-2bde65b446ae) No bug here as far as I can tell.
8.2 KiB
Author

I think I understand what the problem is, let me explain.
I should have mentioned that "type" is the only parameter that works like that. For all the other parameters, modifying what is displayed in the UI affects bpy.ops.object.bake(). In the documentation https://docs.blender.org/api/current/bpy.ops.object.html, we have the following:

bpy.ops.object.bake(type='COMBINED', pass_filter={}, filepath='', width=512, height=512, margin=16, margin_type='EXTEND', use_selected_to_active=False, max_ray_distance=0.0, cage_extrusion=0.0, cage_object='', normal_space='TANGENT', normal_r='POS_X', normal_g='POS_Y', normal_b='POS_Z', target='IMAGE_TEXTURES', save_mode='INTERNAL', use_clear=False, use_cage=False, use_split_materials=False, use_automatic_name=False, uv_layer='')

In the description of the parameters, nothing tells us that the "type" parameter works differently than all others.

Write now we have to write the following:
import bpy
bpy.context.scene.cycles.bake_type = 'NORMAL'
bpy.context.scene.render.bake.normal_g = 'NEG_Y'
bpy.context.scene.render.bake.use_selected_to_active = True

bpy.ops.object.bake(type=bpy.context.scene.cycles.bake_type)

For example, "bpy.context.scene.render.bake.normal_g" modifies the UI dropdown, and it does not need to be written again inside bpy.ops.object.bake().
The paramater "type" is the only one that needs to be inside bpy.ops.object.bake().

Shouldn't it be "bpy.context.scene.render.bake_type" instead of "bpy.context.scene.cycles.bake_type"?
So "render" instead of "cycles"?

"bpy.context.scene.cycles" I think was used in older Blender versions, as you can see here: https://docs.blender.org/api/2.80/bpy.types.CyclesRenderSettings.html

I think I understand what the problem is, let me explain. I should have mentioned that "type" is the only parameter that works like that. For all the other parameters, modifying what is displayed in the UI affects bpy.ops.object.bake(). In the documentation https://docs.blender.org/api/current/bpy.ops.object.html, we have the following: bpy.ops.object.bake(type='COMBINED', pass_filter={}, filepath='', width=512, height=512, margin=16, margin_type='EXTEND', use_selected_to_active=False, max_ray_distance=0.0, cage_extrusion=0.0, cage_object='', normal_space='TANGENT', normal_r='POS_X', normal_g='POS_Y', normal_b='POS_Z', target='IMAGE_TEXTURES', save_mode='INTERNAL', use_clear=False, use_cage=False, use_split_materials=False, use_automatic_name=False, uv_layer='') In the description of the parameters, nothing tells us that the "type" parameter works differently than all others. Write now we have to write the following: import bpy bpy.context.scene.cycles.bake_type = 'NORMAL' bpy.context.scene.render.bake.normal_g = 'NEG_Y' bpy.context.scene.render.bake.use_selected_to_active = True bpy.ops.object.bake(type=bpy.context.scene.cycles.bake_type) For example, "bpy.context.scene.render.bake.normal_g" modifies the UI dropdown, and it does not need to be written again inside bpy.ops.object.bake(). The paramater "type" is the only one that needs to be inside bpy.ops.object.bake(). Shouldn't it be "bpy.context.scene.render.bake_type" instead of "bpy.context.scene.cycles.bake_type"? So "render" instead of "cycles"? "bpy.context.scene.cycles" I think was used in older Blender versions, as you can see here: https://docs.blender.org/api/2.80/bpy.types.CyclesRenderSettings.html
Member

Hmm, I wouldn't have expected that calling bpy.ops.object.bake() would use any of the bpy.context.scene.render.bake.normal_g and similar properties because the default execution context for Operators called from code is 'EXEC_DEFAULT' which does not call invoke functions.

Looking at the source, both the execute and invoke functions call bake_set_props(). I think it may be a bug that the execute function also calls bake_set_props().

Rather than the type argument being the argument with odd behaviour, I think it would be better to say that all the other arguments are the ones with odd behaviour because their default values are ignored and instead replaced by values from bpy.context.scene.render.bake.

Call in execute: b6131cc338/source/blender/editors/object/object_bake_api.cc (L1868)
Call in invoke: b6131cc338/source/blender/editors/object/object_bake_api.cc (L2105)

Hmm, I wouldn't have expected that calling `bpy.ops.object.bake()` would use any of the `bpy.context.scene.render.bake.normal_g` and similar properties because the default execution context for Operators called from code is `'EXEC_DEFAULT'` which does not call `invoke` functions. Looking at the source, both the `execute` and `invoke` functions call `bake_set_props()`. I think it may be a bug that the `execute` function also calls `bake_set_props()`. Rather than the `type` argument being the argument with odd behaviour, I think it would be better to say that all the other arguments are the ones with odd behaviour because their default values are ignored and instead replaced by values from `bpy.context.scene.render.bake`. Call in `execute`: https://projects.blender.org/blender/blender/src/commit/b6131cc338ab05b5c59f34ae3fc174e4c45d73ba/source/blender/editors/object/object_bake_api.cc#L1868 Call in `invoke`: https://projects.blender.org/blender/blender/src/commit/b6131cc338ab05b5c59f34ae3fc174e4c45d73ba/source/blender/editors/object/object_bake_api.cc#L2105
Author

I agree.

And shouldn't bake_type be inside "bpy.context.scene.render" or "bpy.context.scene.render.bake" instead of "bpy.context.scene.cycles"?

Because CyclesRenderSettings is inside Blender 2.8 documentation: https://docs.blender.org/api/2.80/bpy.types.CyclesRenderSettings.html

But it is not inside Blender 3.6 documentation: https://docs.blender.org/api/current/bpy.types.html

I agree. And shouldn't bake_type be inside "bpy.context.scene.render" or "bpy.context.scene.render.bake" instead of "bpy.context.scene.cycles"? Because CyclesRenderSettings is inside Blender 2.8 documentation: https://docs.blender.org/api/2.80/bpy.types.CyclesRenderSettings.html But it is not inside Blender 3.6 documentation: https://docs.blender.org/api/current/bpy.types.html
Member

I think bpy.ops.object.bake() should bake with default arguments (i.e. type=combined and other default parameters)
@lichtwerk , any idea about the expected result?

I think `bpy.ops.object.bake()` should bake with default arguments (i.e. type=combined and other default parameters) @lichtwerk , any idea about the expected result?
Pratik Borhade added
Module
Python API
Interest
Render & Cycles
Status
Confirmed
and removed
Status
Needs Triage
labels 2023-10-16 11:25:31 +02:00
Member

I am not sure if there is a general rule in regards to fetching operator properties (in case they are not specifically provided in the operator call as argument) from elsewhere (e.g. scene properties / tool settings) or if this only meant to be done in ot->invoke()

Looking at code, !RNA_property_is_set is usually done in invoke, but there are occasions if it being done in ot->exec as well, e.g.:

  • bake_exec (as reported in #113732)
  • gpencil_convert_layer_exec
  • collection_instance_add_exec
  • move_to_collection_exec
  • edbm_select_similar_exec (has ot->invoke WM_menu_invoke, could be made specific)
  • uv_select_similar_exec (has ot->invoke WM_menu_invoke, could be made specific)
  • edbm_extrude_repeat_exec (doesnt have ot->invoke though)
  • text_jump_to_file_at_point_exec (doesnt have ot->invoke though)
  • edbm_dissolve_mode_exec (doesnt have ot->invoke though)
  • cube_project_exec (doesnt have ot->invoke though)

Executing one of the above operators will never really use defined defaults (when not specifying them specifically, but instead use e.g. scene properties / tool settings).
Not a big deal, but it would seem more consistent/correct to reserve for invoke

If there is the need to use the same function (e.g. bake_set_props() as reported in #113732) maybe we could check OP_IS_INVOKE flag and otherwise still get defaults with something like RNA_property_XXXtypeXXX_get_default?

Will ask around

I am not sure if there is a general rule in regards to fetching operator properties (in case they are not specifically provided in the operator call as argument) from elsewhere (e.g. scene properties / tool settings) or if this only meant to be done in `ot->invoke()` Looking at code, `!RNA_property_is_set` is usually done in invoke, but there are occasions if it being done in `ot->exec` as well, e.g.: - bake_exec (as reported in #113732) - gpencil_convert_layer_exec - collection_instance_add_exec - move_to_collection_exec - edbm_select_similar_exec (has `ot->invoke` WM_menu_invoke, could be made specific) - uv_select_similar_exec (has `ot->invoke` WM_menu_invoke, could be made specific) - edbm_extrude_repeat_exec (doesnt have `ot->invoke` though) - text_jump_to_file_at_point_exec (doesnt have `ot->invoke` though) - edbm_dissolve_mode_exec (doesnt have `ot->invoke` though) - cube_project_exec (doesnt have `ot->invoke` though) Executing one of the above operators will never really use defined defaults (when not specifying them specifically, but instead use e.g. scene properties / tool settings). Not a big deal, but it would seem more consistent/correct to reserve for `invoke` If there is the need to use the same function (e.g. `bake_set_props()` as reported in #113732) maybe we could check `OP_IS_INVOKE` flag and otherwise still get defaults with something like `RNA_property_XXXtypeXXX_get_default`? Will ask around

In the case of #113732 I think it makes sense to treat type the same as all other properties (initializing it from the scene).

Only initializing from tool-settings/scene properties from invoke & not exec seems like it might be a good rule of thumb although it would need to be tested.
It could be impractical at times:
There are cases when we use exec from menus for e.g (to avoid being prompted for options). This might end up forcing the menu item to copy properties from tool-settings in a way thats awkward and could even cause key-shortcuts not to show... so it could be OK to check OP_IS_INVOKE and only set properties in this case - it's more something to investigate as far as I can see.

In the case of #113732 I think it makes sense to treat `type` the same as all other properties (initializing it from the scene). Only initializing from tool-settings/scene properties from invoke & not exec seems like it might be a good rule of thumb although it would need to be tested. It could be impractical at times: There are cases when we use `exec` from menus for e.g (to avoid being prompted for options). This might end up forcing the menu item to copy properties from tool-settings in a way thats awkward and could even cause key-shortcuts not to show... so it could be OK to check `OP_IS_INVOKE` and only set properties in this case - it's more something to investigate as far as I can see.
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#113732
No description provided.