0
$\begingroup$

The UI from UI.py generates all those check boxes

enter image description here

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
    cob = ob.cycles
    visibility = ob.cycles_visibility

    layout.label(text="Ray Visibility:")
    flow = layout.column_flow()

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

When I click camera I get this in the console read out:

bpy.context.object.cycles_visibility.camera = False

What would be a code to make all boxes be checked off or on.

The main function would be for making a light invisible or object invisible during render time

$\endgroup$
2
$\begingroup$

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. Here's a not-fancy 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

| improve this answer | |
$\endgroup$
  • $\begingroup$ Perfect! This works fantastic! $\endgroup$ – Claas Kuhnen Jan 6 '16 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.