Blender Python (overall Python) noob here. I simply want to make a Custom UI that toggles my models accessories by enabling and disabling their viewport and render visibility. Generally, I want to know how to make customizeable input for operators but I simply can't wrap my head around it. I'm trying to avoid using Custom Drivers since I need a more user friendly input and not just a float property.
I don't exactly have a full script since I'm not sure how to approach it.
Here's a script that toggles the visibility of both the viewport and render but isn't a toggle:
import bpy
class UiPanel(bpy.types.Panel):
bl_label = "Custom UI Visibility Toggles"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = 'Item'
def draw(self, context):
layout = self.layout
row = layout.row()
layout.operator("cube.button")
class CubeVisibility(bpy.types.Operator):
bl_idname = "cube.button"
bl_label = "Cube Visibility"
def execute(self, context):
if bpy.data.objects['Cube'].hide_viewport == True:
bpy.data.objects['Cube'].hide_viewport = False
else:
bpy.data.objects['Cube'].hide_viewport = True
if bpy.data.objects['Cube'].hide_render == True:
bpy.data.objects['Cube'].hide_render = False
else:
bpy.data.objects['Cube'].hide_render = True
return{'FINISHED'}
def register():
bpy.utils.register_class(UiPanel)
bpy.utils.register_class(CubeVisibility)
def unregister():
bpy.utils.unregister_class(UiPanel)
bpy.utils.unregister_class(CubeVisibility)
if __name__ == "__main__":
register()
This one has the toggle feature but are on separate buttons (needs to be only one):
import bpy
class customMenu(bpy.types.Panel):
"""Sample Toggle"""
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_label = "Toggle"
bl_idname = "custom_ui"
bl_category = 'Item'
def draw(self, context):
layout = self.layout
row = layout.row(align=True)
row.prop(bpy.data.objects['Cube'], "hide_viewport",text="Viewport")
row.prop(bpy.context.object, "hide_render",text="View")
def register():
bpy.utils.register_class(customMenu)
def unregister():
bpy.utils.unregister_class(customMenu)
if __name__ == "__main__":
register()
"Cube"
doesn't exist? What you can do is providing the user a list of objects in the scene to choose from (via pointer property) otherwise this script will always return an error if the cube can't be found... $\endgroup$"Cube"
is just an example what's important is to have UI that toggles an object's visibility while being a bool property. $\endgroup$