From a5aec7f5a5558bf0c08b9c5a75eb4272f586dfa8 Mon Sep 17 00:00:00 2001
From: mano-wii <germano.costa@ig.com.br>
Date: Mon, 11 Feb 2019 00:21:05 -0200
Subject: [PATCH] mesh_snap_utilities_line: New solution for creating
 framebuffer out of the right rendering context

The way was to delay the initialization of the framebuffer for the test_select method.
(This rendering context issue gives a lot of headache)!
---
 mesh_snap_utilities_line/__init__.py               |  9 +--------
 .../snap_context_l/__init__.py                     | 12 ++----------
 mesh_snap_utilities_line/widgets.py                | 14 +++++++++++---
 3 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/mesh_snap_utilities_line/__init__.py b/mesh_snap_utilities_line/__init__.py
index 40194f1db..604c1b155 100644
--- a/mesh_snap_utilities_line/__init__.py
+++ b/mesh_snap_utilities_line/__init__.py
@@ -22,7 +22,7 @@
 bl_info = {
     "name": "Snap_Utilities_Line",
     "author": "Germano Cavalcante",
-    "version": (5, 9, 2),
+    "version": (5, 9, 3),
     "blender": (2, 80, 0),
     "location": "View3D > TOOLS > Line Tool",
     "description": "Extends Blender Snap controls",
@@ -154,10 +154,6 @@ classes = (
 )
 
 def register():
-    from .snap_context_l import global_snap_context_init
-    # This makes sure that the framebuffer is created in the correct context
-    global_snap_context_init(None, None, None)
-
     for cls in classes:
         bpy.utils.register_class(cls)
 
@@ -165,9 +161,6 @@ def register():
 
 
 def unregister():
-    from .snap_context_l import global_snap_context_destroy
-    global_snap_context_destroy()
-
     unregister_snap_tools()
 
     for cls in reversed(classes):
diff --git a/mesh_snap_utilities_line/snap_context_l/__init__.py b/mesh_snap_utilities_line/snap_context_l/__init__.py
index 739500e1d..62a39bb33 100644
--- a/mesh_snap_utilities_line/snap_context_l/__init__.py
+++ b/mesh_snap_utilities_line/snap_context_l/__init__.py
@@ -548,7 +548,7 @@ class SnapContext():
         self.freed = True
 
 
-def global_snap_context_init(depsgraph, region, space):
+def global_snap_context_get(depsgraph, region, space):
     if _Internal.global_snap_context == None:
         import atexit
 
@@ -558,15 +558,7 @@ def global_snap_context_init(depsgraph, region, space):
         atexit.unregister(_Internal.snap_context_free)
         atexit.register(_Internal.snap_context_free)
 
-
-def global_snap_context_get(depsgraph, region, space):
-    if (depsgraph and region and space):
+    elif (depsgraph and region and space):
         _Internal.global_snap_context.update_viewport_context(depsgraph, region, space, True)
 
     return _Internal.global_snap_context
-
-
-def global_snap_context_destroy():
-    if _Internal.global_snap_context != None:
-        _Internal.global_snap_context.free()
-        _Internal.global_snap_context = None
diff --git a/mesh_snap_utilities_line/widgets.py b/mesh_snap_utilities_line/widgets.py
index 92256f814..2849075ce 100644
--- a/mesh_snap_utilities_line/widgets.py
+++ b/mesh_snap_utilities_line/widgets.py
@@ -53,7 +53,12 @@ class SnapWidgetCommon(SnapUtilities):
 
         self.draw_cache.draw(self.type, self.location, None, None, None)
 
+    def init_delayed(self):
+        self.inited = False
+
     def init_snapwidget(self, context, snap_edge_and_vert = True):
+        self.inited = True
+
         self.snap_context_init(context, snap_edge_and_vert)
         self.snap_context_update(context)
         self.mode = context.mode
@@ -118,6 +123,9 @@ class SnapPointWidget(SnapWidgetCommon, bpy.types.Gizmo):
     bl_idname = "VIEW3D_GT_snap_point"
 
     def test_select(self, context, mval):
+        if not self.inited:
+            self.init_snapwidget(context)
+
         self.update_snap(context, mval)
         self.snap_to_grid()
 
@@ -125,11 +133,11 @@ class SnapPointWidget(SnapWidgetCommon, bpy.types.Gizmo):
         return -1
 
     def draw(self, context):
-        self.draw_point_and_elem()
+        if self.inited:
+            self.draw_point_and_elem()
 
     def setup(self):
-        self.init_snapwidget(bpy.context)
-
+        self.init_delayed()
 
 def context_mode_check(context, widget_group):
     workspace = context.workspace
-- 
GitLab