I want to hotkey the display as wireframe for an individual mesh with a keystroke, and then on the next press, it will go back to solid. I've tried to create a shortcut, but all I get is the "Display As" selection menu at the bottom. I know so little and have tried everything I could think of, from looking at the code editor and switching the menus, to copying and pasting the information from there, but to no avail.
1 Answer
It looks like you have to script a custom operator for this. It's just one line that changes the Viewport Display > Display As setting: bpy.context.object.display_type = 'WIRE'
To execute this with a shortcut you need to create a custom operator (class ToggleDisplayAsOperator(...):
) and assign a shortcut to it. Since you don't want to run the script for the operator every time you start Blender, you have to create a little add-on. (It's the bl_info = { ... }
part.). The add-on can also register the shortcut for you. (It's done at the end of the script.)
Once the operator is registered (ID object.toggle_display_as_operator
, display name "Toggle Display As Operator") you can find it using the search (main menu Edit > Operator Search ... and Menu Search ... F3). In the User Preferences you can change the assigned shortcut (Shift+V) anytime.
Here is the complete little add-on. It's based on the related question How do I give operators of my addon the "Assign Shortcut" button?
import bpy
bl_info = {
"name": "Your Addon Name",
"author": "Author Name",
"version": (0, 1),
"blender" : (2, 80, 0),
"location": "",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": ""
}
class ToggleDisplayAsOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.toggle_display_as_operator"
bl_label = "Toggle Display As Operator"
def execute(self, context):
# toggle the 'Displays As' setting between textured and wire
if bpy.context.object.display_type == 'TEXTURED':
bpy.context.object.display_type = 'WIRE'
elif bpy.context.object.display_type == 'WIRE':
bpy.context.object.display_type = 'TEXTURED'
return {'FINISHED'}
addon_keymaps = []
def register():
bpy.utils.register_class(ToggleDisplayAsOperator)
# Add the hotkey
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
if kc:
km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
# shortcut SHIFT+V
kmi = km.keymap_items.new(ToggleDisplayAsOperator.bl_idname, type='V', value='PRESS', shift=True)
addon_keymaps.append((km, kmi))
def unregister():
bpy.utils.unregister_class(ToggleDisplayAsOperator)
# Remove the hotkey
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
if __name__ == "__main__":
register()
(copy the script into a text file, change the file extension to *.py, and you can install it.)
-
$\begingroup$ I read through your instruction at least 10 times and they make complete sense i was able to install successfully but when i execute the command nothing updates in my script editor i even changed the keymapping to ctrl + \ so theres no conflicts note when i f3 and look for Toggle Display As Operator nothing appears in the list. not sure what i am missing $\endgroup$ Commented Jun 5 at 22:24
-
$\begingroup$ Sorry, here was a mistake. The script tried to toggle between the display modes 'solid' and 'wire' but the default mode is 'textured'. So it does nothing. I've changed it so that it now toggles between 'textured' and 'wire'. Although, you should be able to see the operator in the search not matter of this change. $\endgroup$– BlunderCommented Jun 9 at 21:26
-
$\begingroup$ It definitely Works!! in all blenders $\endgroup$ Commented Jun 11 at 2:52