1
$\begingroup$

I have an Enum which controls the visibility of different collections and certain bools. If I select a different item from the list then different bools appear.

So one of those Bools controls the visibility of a mask modifier of a cylinder object. I want though the mask modifier to be deactivated from viewport if I choose a different item from my enum property.

I tried something like bpy.data.objects["Cylinder"].modifiers["Mask_top"].show_viewport = (self.show_cube) + int(self.show_skin == 1) but it doesn't work. The code maybe can explain it more clearly:

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
    bpy.data.objects["Cylinder"].modifiers["Mask_top"].show_viewport = (self.show_cube) + int(self.show_skin == 1)  ### HERE IS MY PROBLEM ###

    
    return

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

def circle_show_update(self, context):
    
    bpy.data.objects["Circle"].hide_viewport = not self.show_circle
    bpy.data.objects["Circle"].hide_render = not self.show_circle

    
    return

bpy.types.Armature.show_circle = bpy.props.BoolProperty(default = True,
                                                            name = "Circle",
                                                            description = "Show circle",
                                                            update = circle_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):
        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"):
            row = col.row()
            row.prop(data, 'show_cube', toggle=True)
            row = col.row()
            row.prop(data, 'show_circle', toggle=True)
            
        if data.show_skin in ("skin2"):
            row = col.row()
            row.prop(data, 'show_sphere', toggle=True)  
    
        if data.show_skin in ("skin3"):
            row = col.row()
            row.prop(data, 'show_torus', toggle=True)  
        
def register():
    bpy.utils.register_class(RigLayersProperties)
def unregister():
    bpy.utils.unregister_class(RigLayersProperties)
register()                                                                  ```
$\endgroup$
6
  • $\begingroup$ What exactly doesn't work ? It throws an error ? It doesn't do anything ? Also you can use special operators when you're doing operation with booleans. I think you want to use = self.show_skin or self.show_cube for instance. Note self.show_skin and self.show_skin == 1 is a tautology if self.show_skin is a boolean $\endgroup$
    – Gorgious
    Mar 22, 2022 at 11:34
  • $\begingroup$ @Gorgious it doesn't throw an errow. I'm only trying to make the modifier work only when both the bool and the skin1 are selected. If skin2 is selected from the list then the mask shoud be deactivated (even if the bool is still on, I mean. That's why I used "+"). $\endgroup$
    – mavix
    Mar 22, 2022 at 11:48
  • $\begingroup$ Then you should use and, not or. Adding two bools is the same thing as checking if either of them is True, not if both of them are True. $\endgroup$
    – Gorgious
    Mar 22, 2022 at 11:55
  • $\begingroup$ @Gorgious ok my bad, but blender still doesn't do anything, and even more: the bool doesnt affect the modifier at all now. It's like it doesn't recognize that self.show_skin == 1 is skin1. The problem is with the Enum I think. It always gives me trouble. I can send my blend if you want. $\endgroup$
    – mavix
    Mar 22, 2022 at 12:03
  • $\begingroup$ oh my bad, show_skin isn't a boolean. However I think what you want is self.show_skin == "skin1". Enum properties access is done with a string identifier, not an index $\endgroup$
    – Gorgious
    Mar 22, 2022 at 14:33

0

You must log in to answer this question.

Browse other questions tagged .