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.
context.object
instead add an operator button to layoutrow.operator("object.simple_operator")
to invoke the operator. \ $\endgroup$ob ^= True
$\endgroup$