1 # SPDX-License-Identifier: GPL-2.0-or-later
5 from .common_classes
import SnapUtilities
6 from .common_utilities
import snap_utilities
9 #def mesh_runtime_batchcache_isdirty(me):
11 # batch_cache = ctypes.c_void_p.from_address(me.as_pointer() + 1440)
13 # return ctypes.c_bool.from_address(batch_cache.value + 549).value
17 class SnapWidgetCommon(SnapUtilities
, bpy
.types
.Gizmo
):
18 # __slots__ = ('inited', 'mode', 'last_mval')
20 snap_to_update
= False
22 def handler(self
, scene
, depsgraph
):
23 cls
= SnapWidgetCommon
24 if cls
.snap_to_update
is False:
25 last_operator
= self
.wm_operators
[-1] if self
.wm_operators
else None
26 if (not last_operator
or
27 last_operator
.name
not in {'Select', 'Loop Select', '(De)select All'}):
28 cls
.snap_to_update
= depsgraph
.id_type_updated('MESH') or \
29 depsgraph
.id_type_updated('OBJECT')
31 def draw_point_and_elem(self
):
33 if self
.bm
.is_valid
and self
.geom
.is_valid
:
34 self
.draw_cache
.draw_elem(self
.snap_obj
, self
.bm
, self
.geom
)
39 self
.draw_cache
.draw(self
.type, self
.location
, None, None, None)
41 def init_delayed(self
):
44 def init_snapwidget(self
, context
, snap_edge_and_vert
=True):
47 self
.snap_context_init(context
, snap_edge_and_vert
)
48 self
.snap_context_update(context
)
49 self
.mode
= context
.mode
52 self
.wm_operators
= context
.window_manager
.operators
53 bpy
.app
.handlers
.depsgraph_update_post
.append(self
.handler
)
54 SnapWidgetCommon
.snap_to_update
= False
56 SnapUtilities
.snapwidgets
.append(self
)
58 def end_snapwidget(self
):
59 if self
in SnapUtilities
.snapwidgets
:
60 SnapUtilities
.snapwidgets
.remove(self
)
62 #from .snap_context_l import global_snap_context_get
63 #sctx = global_snap_context_get(None, None, None)
65 sctx
= object.__getattribute
__(self
, 'sctx')
66 if sctx
and not SnapUtilities
.snapwidgets
:
67 sctx
.clear_snap_objects()
69 handler
= object.__getattribute
__(self
, 'handler')
70 bpy
.app
.handlers
.depsgraph_update_post
.remove(handler
)
72 def update_snap(self
, context
, mval
):
73 if self
.last_mval
== mval
:
78 if (SnapWidgetCommon
.snap_to_update
):
79 ## Something has changed since the last time.
80 # Has the mesh been changed?
81 # In the doubt lets clear the snap context.
82 self
.snap_context_update(context
)
83 SnapWidgetCommon
.snap_to_update
= False
85 #print('test_select', mval)
86 space
= context
.space_data
87 self
.sctx
.update_viewport_context(context
.evaluated_depsgraph_get(), context
.region
, space
, True)
89 shading
= space
.shading
90 snap_face
= not ((self
.snap_vert
or self
.snap_edge
) and
91 (shading
.show_xray
or shading
.type == 'WIREFRAME'))
93 if snap_face
!= self
.snap_face
:
94 self
.snap_face
= snap_face
95 self
.sctx
.set_snap_mode(self
.snap_vert
, self
.snap_edge
, self
.snap_face
)
97 snap_utilities
.cache
.clear()
98 self
.snap_obj
, prev_loc
, self
.location
, self
.type, self
.bm
, self
.geom
, len = snap_utilities(
102 increment
=self
.incremental
106 class SnapPointWidget(SnapWidgetCommon
):
107 bl_idname
= "VIEW3D_GT_snap_point"
111 def test_select(self
, context
, mval
):
113 self
.init_snapwidget(context
)
115 self
.update_snap(context
, mval
)
118 context
.area
.tag_redraw()
121 def draw(self
, context
):
123 self
.draw_point_and_elem()
129 def context_mode_check(context
, widget_group
):
130 tools
= context
.workspace
.tools
133 if (tool
.widget
== widget_group
) and (tool
.mode
== mode
):
136 context
.window_manager
.gizmo_group_type_unlink_delayed(widget_group
)
141 class SnapWidgetGroupCommon(bpy
.types
.GizmoGroup
):
142 bl_space_type
= 'VIEW_3D'
143 bl_region_type
= 'WINDOW'
147 def poll(cls
, context
):
148 return context_mode_check(context
, cls
.bl_idname
)
150 def init_tool(self
, context
, gizmo_name
):
151 self
._widget
= self
.gizmos
.new(gizmo_name
)
154 if hasattr(self
, "_widget"):
155 object.__getattribute
__(self
._widget
, 'end_snapwidget')()
158 class SnapPointWidgetGroup(SnapWidgetGroupCommon
):
159 bl_idname
= "MESH_GGT_snap_point"
160 bl_label
= "Draw Snap Point"
162 def setup(self
, context
):
163 self
.init_tool(context
, SnapPointWidget
.bl_idname
)