When developing add-ons for Blender, we may need to programmatically switch 3D viewport modes – for example, enable the WIREFRAME grid display mode.
To switch the 3D viewport to the desired mode, we need to change the “shading.type” property of the viewport area.
List of modes and values to enable it:
- Wireframe grid mode – “WIREFRAME” value
- Common mode for working with meshes – “SOLID” value
- Display of textures and materials – “MATERIAL” value
- The same as in the final render – “RENDERED” value
For example, to enable the wireframe grid display mode in the 3D viewport, we need to set the “WIREFRAME” value to the “shading.type” property.
1 |
bpy.context.space_data.shading.type = 'WIREFRAME' |
Don’t forget about the need to redefine the context if we execute this code in another area, for example, in the Text Editor.
1 2 3 4 |
area = [area for area in bpy.context.screen.areas if area.type == "VIEW_3D"][0] with bpy.context.temp_override(area=area): bpy.context.space_data.shading.type = 'WIREFRAME' |
We can get which display mode is currently enabled in the 3D viewport by checking this property:
1 2 3 4 |
with bpy.context.temp_override(area=area): print(bpy.context.space_data.shading.type) # SOLID |
We can place the buttons for switching the 3D viewport display modes on a custom user’s panel.
First define a test operator that will enable, for example, the wireframe grid mode.
1 2 3 4 5 6 7 8 |
class TEST_OT_op(bpy.types.Operator): bl_idname = 'test.op' bl_label = 'TEST OPERATOR' bl_options = {'REGISTER'} def execute(self, context): context.space_data.shading.type = 'WIREFRAME' return {'FINISHED'} |
Here, context overriding is not required, since the operator will be executed in the context of the 3D viewport itself.
Now, define a test panel on which we will place the button to call our operator.
1 2 3 4 5 6 7 8 9 10 11 12 |
class TEST_PT_panel(bpy.types.Panel): bl_idname = 'TEST_PT_panel' bl_label = 'TEST PANEL' bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = 'TEST' def draw(self, context): self.layout.operator( operator='test.op', text='WIREFRAME MODE' ) |
And don’t forget to register both of our classes in Blender Python API.
1 2 |
bpy.utils.register_class(TEST_OT_op) bpy.utils.register_class(TEST_PT_panel) |
After executing this code, a custom panel with a “WIREFRAME MODE” button will be created in the N-panel, by clicking on which the viewport mode will be switched to the wireframe grid display mode.
- ВКонтакте
- LiveJournal
- РћРТвЂВВВВВВВВнокласснРСвЂВВВВВВВВРєРСвЂВВВВВВВВ
- Telegram
- Viber
- Evernote
- Skype