1
$\begingroup$
import bpy

from bpy.props import (StringProperty,
                       PointerProperty,
                       )

from bpy.types import (Panel,
                       Operator,
                       PropertyGroup,
                       )

class MySettings(PropertyGroup):

    my_objs: StringProperty(
        name="Object Name",
        description="Helper property to store an object name",
        )


class SimpleOperator(Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Viewports and Renders"

    @classmethod
    def poll(cls, context):
        return len(context.selected_objects)
    

    def execute(self, context):
        for objs in bpy.context.selected_objects:
            objs.hide_render = False

        return {'FINISHED'}


class OBJECT_PT_CustomPanel(Panel):
    bl_label = "My Panel"
    bl_idname = "OBJECT_PT_custom_panel"
    bl_space_type = "VIEW_3D"   
    bl_region_type = "UI"
    bl_category = "My_test"
    bl_context = "objectmode"   

    @classmethod
    def poll(cls, context):
        return len(context.selected_objects)

    def draw(self, context):
        layout = self.layout
        row = layout.row(align=True)
        row.prop(context.object, "hide_render", text='')

classes = (
    MySettings,
    SimpleOperator,
    OBJECT_PT_CustomPanel,
)

def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)

    bpy.types.Scene.my_tool = PointerProperty(type=MySettings)

def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
    del bpy.types.Scene.my_tool


if __name__ == "__main__":
    register()

I'm trying to run the script in Blender 2.93.5 for selected objects, but it is only working for the active object.

UPDATED:

If I change as suggested the property for a operator, the script works as it should. The thing is that I would like to keep the property because the icon change it's "looks" when clicked as you see in the current UI, but if I change it for an operator the button is not the same.

Some extra details in the question, what if half of the objects are hidden?, since the default function is to set all selected objects hidden from render, the script should check which ones are hidden already and hide the ones that are not hidden, when clicking the button again, all selected should become visible then.

$\endgroup$
9
  • 2
    $\begingroup$ You are displaying the property of the context.object instead add an operator button to layout row.operator("object.simple_operator") to invoke the operator. \ $\endgroup$
    – batFINGER
    Oct 29, 2021 at 10:46
  • $\begingroup$ Thanks, but the problem with it is that is not toggling the hide_render of course I can add this ``` for objs in bpy.context.selected_objects: if objs.hide_render == False: objs.hide_render = True else: objs.hide_render = False ``` in the operator, but then the icon toggle property is missing. $\endgroup$
    – pekkuskär
    Oct 29, 2021 at 10:54
  • $\begingroup$ so what do you want now? a button which just does all selected object to "not hide in render"? or a toggle button? In your old code you just set hide_render to false - there was no toggle functionality at all $\endgroup$
    – Chris
    Oct 29, 2021 at 11:00
  • $\begingroup$ or with exclusive or ob ^= True $\endgroup$
    – batFINGER
    Oct 29, 2021 at 11:02
  • $\begingroup$ @Chris, so that's where my problem resides then, I'm going after having the current behavior toggle button but acting on all selected objects and not only on the active one. So there is some code missing which I don't know. $\endgroup$
    – pekkuskär
    Oct 29, 2021 at 11:08

1 Answer 1

1
$\begingroup$

so if i understood you right, you want this:

enter image description here

which you can get by this:

import bpy

from bpy.props import (StringProperty,
                       PointerProperty,
                       )

from bpy.types import (Panel,
                       Operator,
                       PropertyGroup,
                       )

class MySettings(PropertyGroup):

    my_objs: StringProperty(
        name="Object Name",
        description="Helper property to store an object name",
        )


class SimpleOperator(Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Toggle Render"

    @classmethod
    def poll(cls, context):
        return len(context.selected_objects)
    

    def execute(self, context):
        print("in execute")
        for objs in bpy.context.selected_objects:
            print(objs)
            objs.hide_render = not objs.hide_render
#            objs.hide_render = False

        return {'FINISHED'}


class OBJECT_PT_CustomPanel(Panel):
    bl_label = "My Panel"
    bl_idname = "OBJECT_PT_custom_panel"
    bl_space_type = "VIEW_3D"   
    bl_region_type = "UI"
    bl_category = "My_test"
    bl_context = "objectmode"   

    @classmethod
    def poll(cls, context):
        print("in poll")
        return len(context.selected_objects)

    def draw(self, context):
        print("in draw")
        layout = self.layout
        row = layout.row(align=True)
        row.operator("object.simple_operator")
 #       row.prop(context.object, "hide_render", text='')

classes = (
    MySettings,
    SimpleOperator,
    OBJECT_PT_CustomPanel,
)

def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)

    bpy.types.Scene.my_tool = PointerProperty(type=MySettings)

def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
    del bpy.types.Scene.my_tool


if __name__ == "__main__":
    print("in main")
    register()
$\endgroup$
3
  • $\begingroup$ Thank you for the example it could solve some other things. I may have explained myself not clear enough, what I'm going after is to be bale to change the toggle icon depending if the selected objects are hidden or not from render. I'm trying to disect the code to use it on a UIList so I can use the icon changing (toggling) per list index. I hope I explained myself more clear now. $\endgroup$
    – pekkuskär
    Oct 29, 2021 at 11:29
  • 3
    $\begingroup$ Yeah recommend edit question with all extra details. ... what if half selected are hidden? $\endgroup$
    – batFINGER
    Oct 29, 2021 at 11:40
  • $\begingroup$ Question has been updated with further details. $\endgroup$
    – pekkuskär
    Nov 1, 2021 at 6:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .