Page MenuHome

BGL_Wrap: disable calls on non-opengl backends.
ClosedPublic

Authored by Jeroen Bakker (jbakker) on Fri, Jan 13, 4:38 PM.

Details

Summary

Goal of this patch is to stop the invocation of OpenGL calls via the bgl module
on a none OpenGL GPU backend, report this as a python deprecation warning
and report this to the user.

Deprecation warning to developers

>>> import bgl
>>> bgl.glUseProgram(0)
<blender_console>:1: DeprecationWarning: 'bgl.glUseProgram' is deprecated and will be removed in Blender 3.7. Report or update your script to use 'gpu' module.

Deprecation message to users

The message to the user is shown as part of the Info Space and as a message box.


During implementation we tried several ideas:

  1. Use python warning as errors: This isn't fine grained enough and can show incorrect information to the user.
  2. Throw deprecation as error and use sys.excepthook to report the user message. This required a custom exception class to identify the bgl deprecation and a CPython handler function to be set during python initialization. Although this is the most flexible there was a disconnect between the exception class, exception function and the excepthook registration.
  3. A variant how we handle autoexec failures. A flag is stored in Global and when set the user message is reported. Not that flexible, but code is more connected to the boolean stored in the Global struct.

Although using Global struct isn't nice I chose this solution due to its traceability. It is clear to developers
reading the code how the mechanism works by using search all functionality of your IDE.

Diff Detail

Repository
rB Blender

Event Timeline

  • Show in log when bgl is imported without an OpenGL back-end being active.
  • Report deprecation warning.
Jeroen Bakker (jbakker) edited the summary of this revision. (Show Details)Mon, Jan 16, 2:43 PM
Jeroen Bakker (jbakker) edited the summary of this revision. (Show Details)
  • Fix system error when running with metal.
Jeroen Bakker (jbakker) edited the summary of this revision. (Show Details)Mon, Jan 16, 2:50 PM
Germano Cavalcante (mano-wii) added inline comments.
source/blender/python/generic/bgl.c
1122

The deprecated warning is often overlooked.
The error message cannot be ignored.
If we wanted to show something more informative, (instead of "returned NULL without setting an exception") we could use:
PyErr_Format or PyErr_SetString

  • Small tweaks to the error message.
  • Use error when running with metal.
source/blender/python/generic/bgl.c
1122

This would still not show to the user on a mac. I believe the best option is to load sys.excepthook with a custom callback that reports back to the user. Still investigating how to do this via CPython

  • WIP: exception handler for deprecation warnings.
  • Use similar mechanism as autoexec fail to show message to user.
Jeroen Bakker (jbakker) requested review of this revision.Tue, Jan 17, 8:55 AM
Jeroen Bakker (jbakker) edited the summary of this revision. (Show Details)

@Campbell Barton (campbellbarton) Although this patch is not yet finished. I would like an initial review on the approach as we discussed a different solution.

  • Show popup panel with message and mitigation.
Jeroen Bakker (jbakker) edited the summary of this revision. (Show Details)
Jeroen Bakker (jbakker) edited the summary of this revision. (Show Details)
Jeroen Bakker (jbakker) edited the summary of this revision. (Show Details)
Jeroen Bakker (jbakker) edited the summary of this revision. (Show Details)
  • Removed commented out code.
source/blender/windowmanager/intern/wm_window.c
1648

Add newline

source/blender/python/generic/bgl.c
1124

Is it possible to continue with the functions logic (even if the BGL call does nothing), as any function that is expected to return a non None value will error (although admittedly there aren't too many of these).

source/blender/windowmanager/intern/wm_window.c
1659–1668

It would be good to include the source of the error, otherwise it's up to the user to find which script caused the problem.

The Global could store the location of the error and report it here, suggest to use PyC_FileAndNum in report_deprecated_call_to_user to include the file and line the BGL call was made.

source/blender/python/generic/bgl.c
1124

The biggest issue is that on metal the opengl library isn't loaded, that will crash when continuing the logic. Adding logic to use the correct return type might be doable, For example -1 for integer types. Will have a look how to do this

source/blender/windowmanager/intern/wm_window.c
1659–1668

Note that this is already reported in the Python warning, just didn't want to clutter this information to the end-user. But no hard objections adding some hints here as well.

Jeroen Bakker (jbakker) marked 2 inline comments as done.
  • Code-style: Added empty line
  • Show hint of error to end user.
Jeroen Bakker (jbakker) edited the summary of this revision. (Show Details)Tue, Jan 17, 2:30 PM
Jeroen Bakker (jbakker) marked an inline comment as done.
Campbell Barton (campbellbarton) added inline comments.
source/blender/python/generic/bgl.c
52

Use (void), to prevent -Wstrict-prototypes warning.

source/blender/windowmanager/intern/wm_window.c
1659–1668

While I see your point - if an inexperienced user runs into this message they wont know who to report the issue to if there are no hints as to the cause of the warning.
An alternative could be to tell the user to run Blender from a terminal, but that feels like asking them to perform extra/unnecessary steps if the file/line is sufficient.

1661

*picky* perhaps this should say "Python script uses OpenGL for drawing" ... as it's possible the script is not an add-on.

This revision is now accepted and ready to land.Wed, Jan 18, 7:35 AM

This appears to work as expected through testing, thanks!
Blender no longer appears to crash when enabling add-ons via the Metal backend, but highlights useful error messages.

Jeroen Bakker (jbakker) edited the summary of this revision. (Show Details)Thu, Jan 19, 8:13 AM
Jeroen Bakker (jbakker) marked 3 inline comments as done.
Jeroen Bakker (jbakker) edited the summary of this revision. (Show Details)
  • Fix strict prototypes.
  • Improved title text.
Jeroen Bakker (jbakker) edited the summary of this revision. (Show Details)Thu, Jan 19, 8:22 AM