How do I explicitly go into wireframe mode

9

I’m developing a function for practice and one of the features I’d like to include in this addon requires modes to be set explicitly.

Here’s an example of the type of function that I’m looking for.

if bpy.context.mode != 'WIREFRAME':
    bpy.ops.set_mode('WIREFRAME')

(but how do I get back by pressing the same menu item I can’t put a toggle button on this menu So the menu item has to act as a toggle By the way I write the code… so activating the function the first time goes into wireframe no matter what and activating the function the second time goes into I know the above code is wrong but I need help on the logic of that too… it’s just a wireframe different because it’s like a mode that accentuates other modes instead of being a mode of itself)

Is there a method like what I was describing that would do the job?

If you press the wireframe button on the 3D View you will see this:
bpy.context.space_data.shading.type = 'WIREFRAME'
(Because when you are in 3D View and you press the button, you assume that the context is already 3D_View) (However this happens at the GUI level internally, from the programmer using the bpy is totally different).

This means that you have to get the 3D View area and then access the space data and then change the shading type.

You would typically start from here, and then search bit by bit each class.
https://docs.blender.org/api/current/bpy.context.html#bpy.context.area

# to get the 3D area
# [list comprehension returns an array of areas]                -> [you get only the first result]
area = [a for a in bpy.context.screen.areas if a.type == 'VIEW_3D'][0]
# to get the space data
# (looks like 3D View has only one space)
>>> area.spaces[0].type
'VIEW_3D'
# to see the shading type and change it
>>> area.spaces[0].shading.type
'SOLID'
>>> area.spaces[0].shading.type = 'WIREFRAME'