Skip to main content
added 84 characters in body
Source Link
zeffii
  • 39.9k
  • 9
  • 106
  • 189

I imagine you might want a single button that has states, like a BoolProperty. Here's an undecorated / unadorneda not-fancy version using a scene Boolean.

making it look nice with a different Icon for each state should be a separate questionseparate question.

I imagine you might want a single button that has states, like a BoolProperty. Here's an undecorated / unadorned version using a scene Boolean.

making it look nice with a different Icon for each state should be a separate question.

I imagine you might want a single button that has states, like a BoolProperty. Here's a not-fancy version using a scene Boolean.

making it look nice with a different Icon for each state should be a separate question.

added 2044 characters in body
Source Link
zeffii
  • 39.9k
  • 9
  • 106
  • 189
 

I imagine you might want a single button that has states, like a BoolProperty. Here's an undecorated / unadorned version using a scene Boolean.

import bpy

def bool_updates_scene(self, context):
    ob = context.object
    visibility = ob.cycles_visibility

    attributes = ["camera", "diffuse", "glossy", "transmission", "scatter"]
    state = bool(context.scene.claas_visibility_switch)
    for attr in attributes:        
        setattr(visibility, attr, state)

class ObjectModifier(bpy.types.Panel):
    bl_label = "ObjectModifier"
    bl_idname = "ObjectModifier"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Claas"

    def draw(self, context):
        layout = self.layout

        scene = context.scene
        cscene = scene.cycles
        ob = context.object
        
        if not context.object:
            layout.row().label('no active objects')
            # finish drawing early
            return
        
        cob = ob.cycles
        visibility = ob.cycles_visibility

        layout.label(text="Ray Visibility:")
        flow = layout.column_flow()
        flow.prop(scene, "claas_visibility_switch", text='All')
        flow.separator()
        flow.prop(visibility, "camera")
        flow.prop(visibility, "diffuse")
        flow.prop(visibility, "glossy")
        flow.prop(visibility, "transmission")
        flow.prop(visibility, "scatter")



def register():
    bpy.types.Scene.claas_visibility_switch = bpy.props.BoolProperty(
        update=bool_updates_scene
    )
    bpy.utils.register_module(__name__)


def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.claas_visibility_switch


if __name__ == "__main__":
    register()

making it look nice with a different Icon for each state should be a separate question.

enter image description here

I imagine you might want a single button that has states, like a BoolProperty.

 

I imagine you might want a single button that has states, like a BoolProperty. Here's an undecorated / unadorned version using a scene Boolean.

import bpy

def bool_updates_scene(self, context):
    ob = context.object
    visibility = ob.cycles_visibility

    attributes = ["camera", "diffuse", "glossy", "transmission", "scatter"]
    state = bool(context.scene.claas_visibility_switch)
    for attr in attributes:        
        setattr(visibility, attr, state)

class ObjectModifier(bpy.types.Panel):
    bl_label = "ObjectModifier"
    bl_idname = "ObjectModifier"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Claas"

    def draw(self, context):
        layout = self.layout

        scene = context.scene
        cscene = scene.cycles
        ob = context.object
        
        if not context.object:
            layout.row().label('no active objects')
            # finish drawing early
            return
        
        cob = ob.cycles
        visibility = ob.cycles_visibility

        layout.label(text="Ray Visibility:")
        flow = layout.column_flow()
        flow.prop(scene, "claas_visibility_switch", text='All')
        flow.separator()
        flow.prop(visibility, "camera")
        flow.prop(visibility, "diffuse")
        flow.prop(visibility, "glossy")
        flow.prop(visibility, "transmission")
        flow.prop(visibility, "scatter")



def register():
    bpy.types.Scene.claas_visibility_switch = bpy.props.BoolProperty(
        update=bool_updates_scene
    )
    bpy.utils.register_module(__name__)


def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.claas_visibility_switch


if __name__ == "__main__":
    register()

making it look nice with a different Icon for each state should be a separate question.

enter image description here

Source Link
zeffii
  • 39.9k
  • 9
  • 106
  • 189

There are a bunch of ways to do this. The TextEditor->Templates->Python->Operator simple has an example of the Operator syntax.

Here's a rather brutish interpretation of what you want:

import bpy

class ClaasVisibilitySwitch(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "claas.visibility_switch"
    bl_label = "Simple Object Operator"
    
    func = bpy.props.IntProperty(description='0 is off, 1 is on')
 
    def execute(self, context):
        ob = context.object
        visibility = ob.cycles_visibility

        attributes = ["camera", "diffuse", "glossy", "transmission", "scatter"]
        for attr in attributes:        
            setattr(visibility, attr, bool(self.func))

        return {'FINISHED'}

class ObjectModifier(bpy.types.Panel):
    bl_label = "ObjectModifier"
    bl_idname = "ObjectModifier"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Claas"
    
    # uncommenting this will make the panel be invisible if no
    # objects are selected/active.
    # @classmethod
    # def poll(cls, context):
    #     return context.active_object is not None    

    def draw(self, context):
        layout = self.layout

        scene = context.scene
        cscene = scene.cycles
        ob = context.object
        
        # if you have a poll function like above, then early returns
        # like this aren't needed.
        if not context.object:
            layout.row().label('no active objects')
            # finish drawing early
            return
                    
        cob = ob.cycles
        visibility = ob.cycles_visibility

        layout.label(text="Ray Visibility:")
        row = layout.row()
        row.operator('claas.visibility_switch',text='on').func = 1
        row.operator('claas.visibility_switch',text='off').func = 0
        
        flow = layout.column_flow()

        flow.prop(visibility, "camera")
        flow.prop(visibility, "diffuse")
        flow.prop(visibility, "glossy")
        flow.prop(visibility, "transmission")
        flow.prop(visibility, "scatter")


def register(): bpy.utils.register_module(__name__)
def unregister(): bpy.utils.unregister_module(__name__)
if __name__ == "__main__": register()

enter image description here

I imagine you might want a single button that has states, like a BoolProperty.