Skip to content

Instantly share code, notes, and snippets.

@Andrej730
Last active May 14, 2025 05:52
Show Gist options
  • Save Andrej730/dd422a4fc7c1aa39cfa17e83cc512bb7 to your computer and use it in GitHub Desktop.
Save Andrej730/dd422a4fc7c1aa39cfa17e83cc512bb7 to your computer and use it in GitHub Desktop.
rawHide - numpy 1.20+ fix
bl_info = {
"name": "rawHide",
"description": "toggle hide solution",
"author": "luiSSeyfer",
"version": (0, 0, 1),
"category": "View 3D",
"location": "View 3D",
"blender": (2, 80, 0)
}
import bpy
import numpy as np
############### get active object ###################
def get_osel():
bpy.context.view_layer.update()
layer = bpy.context.view_layer
layer.update()
osel = bpy.context.view_layer.objects.selected
return osel
def get_so():
bpy.context.view_layer.update()
layer = bpy.context.view_layer
layer.update()
so = bpy.context.active_object
return so
#######################################################
def get_vtx(so): ##index
count = len(so.data.vertices)
vtx = np.empty(count, dtype=np.int32) # check dtype!!!
so.data.vertices.foreach_get("index", vtx)
return vtx
def get_slvtx(so): ##selection
count = len(so.data.vertices)
slvtx = np.zeros(count, dtype=bool)
so.data.vertices.foreach_get("select", slvtx)
return slvtx
def get_vsvtx(so): ##visibility
count = len(so.data.vertices)
vsvtx = np.zeros(count, dtype=bool)
so.data.vertices.foreach_get("hide", vsvtx) # visibility
return vsvtx
def get_ed(so): ##index
count = len(so.data.edges)
ed = np.empty(count, dtype=np.int32)
so.data.edges.foreach_get("index", ed)
return ed
def get_sled(so): ##selection
count = len(so.data.edges)
sled = np.zeros(count, dtype=bool)
so.data.edges.foreach_get("select", sled)
return sled
def get_vsed(so): ##visibility
count = len(so.data.edges)
vsed = np.zeros(count, dtype=bool)
so.data.edges.foreach_get("hide", vsed) # visibility
return vsed
def get_edvtx(so): ##edge vertices
count = len(so.data.edges)
edvtx = np.empty(count * 2, dtype=np.int32)
so.data.edges.foreach_get("vertices", edvtx)
edvtx.shape = (count, 2)
return edvtx#,edvtxsl
def get_fc(so): ##index
count = len(so.data.polygons)
fc = np.empty(count, dtype=np.int32)
so.data.polygons.foreach_get("index", fc)
return fc
def get_slfc(so): ##selection
count = len(so.data.vertices)
slfc = np.zeros(count, dtype=bool)
so.data.vertices.foreach_get("select", slfc)
return slfc
def get_vsfc(so): ##visibility
count = len(so.data.polygons)
vsfc = np.zeros(count, dtype=bool)
so.data.polygons.foreach_get("hide", vsfc) # visibility
return vsfc
def get_fclps(so): ##face loops
count = len(so.data.polygons)
fclps = np.empty(count, dtype=np.int32)
so.data.polygons.foreach_get("loop_total", fclps)
return fclps
def get_fcvtx(so,fclps): ##face vertices
count = np.sum(fclps)
fcvtx = np.empty(count, dtype=np.int32)
so.data.polygons.foreach_get("vertices", fcvtx)
return fcvtx
def get_fclstrt(so): ##face loop_start
count = len(so.data.polygons)
fclstrt = np.empty(count, dtype=np.int32)
so.data.polygons.foreach_get("loop_start", fclstrt)
return fclstrt
################## OP HIDE SELECTED ######################
class HIDE_OT_selvis(bpy.types.Operator):
'''Hide Selected'''
bl_idname = "hide.selvis"
bl_label = "hide Selvis"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
if (bpy.context.area.type == "VIEW_3D" or bpy.context.area.type == "OUTLINER"):
return True
def execute(self, context):
if bpy.context.mode == 'OBJECT':
osel = get_osel()
for so in osel:
so.hide_set(True)
elif bpy.context.mode == 'EDIT_MESH':
so = get_so()
if so.type == "MESH":
bpy.ops.object.mode_set(mode='OBJECT') ## update selection
vtx = get_vtx(so) ##index
slvtx = get_slvtx(so) ##selection
if slvtx is None:
bpy.ops.object.mode_set(mode='EDIT') ## update selection
return {'FINISHED'}
vtxsel = vtx[slvtx]
edvtx = get_edvtx(so) ##edge vertices
edsel = np.isin(edvtx, vtxsel)
edsel = np.any(edsel, axis = 1)
fclps = get_fclps(so) ##face loops
fcvtx = get_fcvtx(so,fclps) ##face vertices
fclstrt = get_fclstrt(so) ##face loop_start
xxxx = np.asarray(np.max(fclps)+1-fclps)
xxxx[xxxx==0] = 1
oooo = np.ones(np.sum(fclps), dtype=np.int32)
np.put(oooo, [fclstrt], [xxxx])
fcvs = np.repeat(fcvtx, oooo)
fcvs = fcvs.reshape(-1, np.max(fclps))
fcsel = np.isin(fcvs, vtxsel)
fcsel = np.any(fcsel, axis = 1)
so.data.vertices.foreach_set("hide", slvtx)
so.data.edges.foreach_set("hide", edsel)
so.data.polygons.foreach_set("hide", fcsel)
bpy.ops.object.mode_set(mode='EDIT') ## update selection
return {'FINISHED'}
################## OP HIDE UNSELECTED ######################
class HIDE_OT_unselvis(bpy.types.Operator):
'''Hide Unselected'''
bl_idname = "hide.unselvis"
bl_label = "hide Unselvis"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
if (bpy.context.area.type == "VIEW_3D" or bpy.context.area.type == "OUTLINER"):
return True
def execute(self, context):
if bpy.context.mode == 'OBJECT':
osel = get_osel()
for obj in bpy.data.objects:
if obj in bpy.context.selected_objects:
obj.hide_set(False)
else:
obj.hide_set(True)
elif bpy.context.mode == 'EDIT_MESH':
so = get_so()
if so.type == "MESH":
bpy.ops.object.mode_set(mode='OBJECT') ## update selection
vtx = get_vtx(so) ##index
slvtx = np.invert(get_slvtx(so)) ##selection
if slvtx is None:
bpy.ops.object.mode_set(mode='EDIT') ## update selection
return {'FINISHED'}
vtxsel = vtx[slvtx]
edvtx = get_edvtx(so) ##edge vertices
edsel = np.isin(edvtx, vtxsel)
edsel = np.any(edsel, axis = 1)
#####################################################
fclps = get_fclps(so) ##face loops
fcvtx = get_fcvtx(so,fclps) ##face vertices
fclstrt = get_fclstrt(so) ##face loop_start
xxxx = np.asarray(np.max(fclps)+1-fclps)
xxxx[xxxx==0] = 1
oooo = np.ones(np.sum(fclps), dtype=np.int32)
np.put(oooo, [fclstrt], [xxxx])
fcvs = np.repeat(fcvtx, oooo)
fcvs = fcvs.reshape(-1, np.max(fclps))
#####################################################
fcsel = np.isin(fcvs, vtxsel)
fcsel = np.any(fcsel, axis = 1)
so.data.vertices.foreach_set("hide", slvtx)
so.data.edges.foreach_set("hide", edsel)
so.data.polygons.foreach_set("hide", fcsel)
bpy.ops.object.mode_set(mode='EDIT') ## update selection
return {'FINISHED'}
################## OP REVEAL ######################
class REVEAL_OT_allvis(bpy.types.Operator):
'''Reveal All'''
bl_idname = "reveal.allvis"
bl_label = "reveal Allvis"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
if (bpy.context.area.type == "VIEW_3D" or bpy.context.area.type == "OUTLINER"):
return True
def execute(self, context):
if bpy.context.mode == 'OBJECT':
for obj in bpy.data.objects:
obj.hide_set(False)
elif bpy.context.mode == 'EDIT_MESH':
for so in bpy.data.objects:
if so.type == "MESH":
bpy.ops.object.mode_set(mode='OBJECT') ## update selection
vsvtx = get_vsvtx(so) ##visibility
vsed = get_vsed(so) ##visibility
vsfc = get_vsfc(so) ##visibility
allvis_vtx = np.zeros((np.size(vsvtx)), dtype=bool)
allvis_ed = np.zeros((np.size(vsed)), dtype=bool)
allvis_fc = np.zeros((np.size(vsfc)), dtype=bool)
so.data.vertices.foreach_set("hide", allvis_vtx)
so.data.edges.foreach_set("hide", allvis_ed)
so.data.polygons.foreach_set("hide", allvis_fc)
bpy.ops.object.mode_set(mode='EDIT') ## update selection
return {'FINISHED'}
################## OP TOGGLE ######################
class TOGGLE_OT_allvis(bpy.types.Operator):
'''Toggle Visible'''
bl_idname = "toggle.allvis"
bl_label = "toggle Allvis"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
if (bpy.context.area.type == "VIEW_3D" or bpy.context.area.type == "OUTLINER"):
return True
def execute(self, context):
if bpy.context.mode == 'OBJECT':
for obj in bpy.data.objects:
if obj:
obj.hide_set(not obj.hide_get())
elif bpy.context.mode == 'EDIT_MESH':
so = get_so()
if so.type == "MESH":
bpy.ops.object.mode_set(mode='OBJECT') ## update selection
vsvtx = np.invert(get_vsvtx(so)) ##visibility
print("\n vsvtx: \n", vsvtx,)
vsed = np.invert(get_vsed(so)) ##visibility
vsfc = np.invert(get_vsfc(so)) ##visibility
so.data.vertices.foreach_set("hide", vsvtx)
so.data.edges.foreach_set("hide", vsed)
so.data.polygons.foreach_set("hide", vsfc)
bpy.ops.object.mode_set(mode='EDIT') ## update selection
return {'FINISHED'}
###########################################################
# store keymaps here to access after registration
addon_keymaps = []
def register():
bpy.utils.register_class(HIDE_OT_selvis)
bpy.utils.register_class(HIDE_OT_unselvis)
bpy.utils.register_class(REVEAL_OT_allvis)
bpy.utils.register_class(TOGGLE_OT_allvis)
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
if kc:
km = kc.keymaps.new(name='3D View', space_type='VIEW_3D', region_type='WINDOW') ## Object & Edit Mode both
kmi = km.keymap_items.new('hide.selvis', 'H', 'PRESS')
addon_keymaps.append((km, kmi))
kmi = km.keymap_items.new('hide.unselvis', 'H', 'PRESS', shift=True)
addon_keymaps.append((km, kmi))
kmi = km.keymap_items.new('reveal.allvis', 'H', 'PRESS', alt=True)
addon_keymaps.append((km, kmi))
kmi = km.keymap_items.new('toggle.allvis', 'H', 'PRESS', alt=True, shift=True)
addon_keymaps.append((km, kmi))
def unregister():
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
bpy.utils.unregister_class(HIDE_OT_selvis)
bpy.utils.unregister_class(HIDE_OT_unselvis)
bpy.utils.unregister_class(REVEAL_OT_allvis)
bpy.utils.unregister_class(TOGGLE_OT_allvis)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment