0

How can I change a viewport shading settings from one button? I am creating a toolbox I put in the Right panel. Everything's Ok exept for this. I'd like to get my settings in one click. Anyone?

bpy.data.screens["Scripting"].shading.light = 'MATCAP'
bpy.data.screens["Scripting"].shading.color_type = 'SINGLE'
bpy.data.screens["Scripting"].shading.show_xray = False
bpy.data.screens["Scripting"].shading.show_cavity = True
bpy.data.screens["Scripting"].shading.show_object_outline = False
Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125
  • Are you specifically looking to do this with Python or did you just include that code as a reference to what settings you want to change? – Ray Mairlot Dec 14 '18 at 15:45
  • yes I want it in python because I want to use it for my addon. The code here is the one given in Blender for that but it does not work probably because it's 3d view dependant. I don't know how to do that – Loranozor Graphitz Dec 18 '18 at 22:13

2 Answers2

1

Make an operator

For the most part a button in blender calls an operator.

Using the simple operator template, here is a quick edit to hardcode in some settings.

  • I have made the operator poll only in the 3d view area.
  • Haven't sanitized name.

Since your addon panel also resides in this area, it is simply a matter of adding

self.layout.operator("object.simple_operator")

into your panel's draw method.

import bpy


def main(context):
    space = context.space_data
    space.shading.light = 'MATCAP'
    space.shading.color_type = 'SINGLE'
    space.shading.show_xray = False
    space.shading.show_cavity = True
    space.shading.show_object_outline = False


class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.area.type == 'VIEW_3D'

    def execute(self, context):
        main(context)
        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()

Further to this, for saving multiple settings

  • there is the preset system for saving an operators, or some tools settings to a text file. How to use presets for operator properties?
  • A collection of settings can be defined as a property group. Multiple can be saved as a CollectionPropery the "current" as a PointerProperty
batFINGER
  • 84,216
  • 10
  • 108
  • 233
0

Basic guide script considering Blender 2.80 is still evolving. (Tested on 2.80.35)

import bpy

my_areas = bpy.context.workspace.screens[0].areas
my_shading = 'SOLID'
my_light = 'MATCAP'
my_color_type = 'SINGLE'
my_show_xray = False
my_show_cavity = True
my_show_object_outline = False


def chngviewptshader():
    for area in my_areas:
        for space in area.spaces:
            if space.type == 'VIEW_3D':
                try:
                    space.shading.type = my_shading
                    space.shading.light = my_light
                    space.shading.color_type = my_color_type
                    space.shading.show_xray = my_show_xray
                    space.shading.show_cavity = my_show_cavity
                    space.shading.show_object_outline = my_show_object_outline
                except:
                    print('Setting invalid')

chngviewptshader()
Ratt
  • 2,126
  • 1
  • 10
  • 17
  • thank you! What if I want it to affect only the area where the button is clicked? – Loranozor Graphitz Dec 30 '18 at 17:57
  • @LoranozorGraphitz batfinger's answer is already setup for only affecting the area in which the operator is called. You may want to include the shading type being set or checking for it to be equal to solid in the poll since some of the settings like shading.light = 'MATCAP' are not available in all shading types. – Ratt Dec 30 '18 at 18:06