0
$\begingroup$

I have an EnumProperty (a list) which enables the visibility of three different collections and now I want it to enable different Bools based on what's selected from the list. I can share my example - blend file if possible. For instance, below, I need the cube Bool to appear only when "skin1" is selected from the enum.

import math
import json
import collections
import traceback
from math import pi
from bpy.props import StringProperty
from mathutils import Euler, Matrix, Quaternion, Vector
from rna_prop_ui import rna_idprop_quote_path

rig_id = "Character"

#### CUSTOM PROPERTIES ####

#### SKIN LIST #####

character_skin_list = [('skin1','Skin 1',''),
                         ('skin2','Skin 2',''),
                         ('skin3','Skin 3','')
                        ]                   

def skin_show_update(self, context):
    
            
    for skin1 in character_skin_list:
        bpy.data.collections[skin1[0]].hide_viewport = not self.show_skin == skin1[0]
               
     
    return
 
 
bpy.types.Armature.show_skin = bpy.props.EnumProperty(items = character_skin_list,
                                                        name = "Skin",
                                                        description = "Select skin",
                                                        update = skin_show_update)


#### CUBE ####

def cube_show_update(self, context):
    
    bpy.data.objects["Cube"].hide_viewport = not self.show_cube
    bpy.data.objects["Cube"].hide_render = not self.show_cube

    
    return

bpy.types.Armature.show_cube = bpy.props.BoolProperty(default = True,
                                                            name = "Cube",
                                                            description = "Show cube",
                                                            update = cube_show_update)

#### SPHERE ####
                                                            
def sphere_show_update(self, context):
    
    bpy.data.objects["Sphere"].hide_viewport = not self.show_sphere
    bpy.data.objects["Sphere"].hide_render = not self.show_sphere

    
    return

bpy.types.Armature.show_sphere = bpy.props.BoolProperty(default = True,
                                                            name = "Sphere",
                                                            description = "Show sphere",
                                                            update = sphere_show_update)
                                                            
#### TORUS ####
                                                            
def torus_show_update(self, context):
    
    bpy.data.objects["Torus"].hide_viewport = not self.show_torus
    bpy.data.objects["Torus"].hide_render = not self.show_torus

    
    return

bpy.types.Armature.show_torus = bpy.props.BoolProperty(default = True,
                                                            name = "Torus",
                                                            description = "Show torus",
                                                            update = torus_show_update)                                                            

                                                            
#### TOGGLES ####

class RigLayersProperties(bpy.types.Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_label = "Properties"
    bl_idname = "VIEW3D_PT_rig_layers_properties_" + rig_id
    bl_category = 'Item'

    @classmethod
    def poll(self, context):
        try:
            return (context.active_object.data.get("rig_id") == rig_id)
        except (AttributeError, KeyError, TypeError):
            return False

    def draw(self, context):
        layout = self.layout
        col = layout.column()
        
        row = col.row()
        row.prop(context.active_object.data, 'show_skin', toggle=True)
        
        col.separator()

        row = col.row()
        row.prop(context.active_object.data, 'show_cube', toggle=True)
                
        row = col.row()
        row.prop(context.active_object.data, 'show_sphere', toggle=True)  
        
        row = col.row()
        row.prop(context.active_object.data, 'show_torus', toggle=True) 
        
def register():
    bpy.utils.register_class(RigLayersProperties)
def unregister():
    bpy.utils.unregister_class(RigLayersProperties)
register()                                                                  ```
$\endgroup$

1 Answer 1

0
$\begingroup$

I'm not exactly sure you're looking for that, but if each skin enum is linked to a bool property, you can do it like this. It's a little hacky usage of a dictionary, but it's basically a switch instruction.

def draw(self, context):
    data = context.active_object.data
    layout = self.layout
    col = layout.column()
    
    row = col.row()
    row.prop(data , 'show_skin', toggle=True)
    
    col.separator()

    bool_to_show = {
        "skin1": "show_cube",
        "skin2": "show_sphere",
        "skin3": "show_torus",
    }.get(data.show_skin)

    row = col.row()
    row.prop(data , bool_to_show , toggle=True)

However if the logic is more involved than that, eg if you want layout elements to be displayed when 2 different options are selected but not the other one, you can use conditional statements :

def draw(self, context):
    data = context.active_object.data
    layout = self.layout
    col = layout.column()
    
    row = col.row()
    row.prop(data , 'show_skin', toggle=True)
    
    col.separator()

    if data.show_skin in ("skin1", "skin2"):
        row = col.row()
        row.prop(data, 'show_cube', toggle=True)
            
    if data.show_skin in ("skin2", "skin3"):
        row = col.row()
        row.prop(data, 'show_sphere', toggle=True)  
    
    if data.show_skin in ("skin3", "skin1"):
        row = col.row()
        row.prop(data, 'show_torus', toggle=True) 
$\endgroup$
1
  • $\begingroup$ Ah nice, it works (both ways!) :) Thank you! (I'm new user, hence site won't let me upvote). $\endgroup$
    – mavix
    Commented Mar 17, 2022 at 10:52

You must log in to answer this question.

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