1
$\begingroup$

When a custom property is updated, this function is triggered and changes the boolean value of all the 10 others. It's a long and dirty code... is there better way to do it ?

def swap(prop):
    s = bpy.context.scene
    if prop == 'meshes':
        v = not s.meshes
        s.meshes = not v
        s.lights = v
        s.cameras = v
        s.nurbs = v
        s.lattices = v
        s.empties = v
        s.texts = v
        s.bones = v
        s.surfaces = v
        s.metaballs = v
        s.fields = v
    elif prop == 'lights':
        v = not s.lights
        s.meshes = v
        s.lights = not v
        s.cameras = v
        s.nurbs = v
        s.lattices = v
        s.empties = v
        s.texts = v
        s.bones = v
        s.surfaces = v
        s.metaballs = v
        s.fields = v
    elif prop == 'cameras':
        v = not s.cameras
        s.meshes = v
        s.lights = v
        s.cameras = not v
        s.nurbs = v
        ... and so on (11 times)
$\endgroup$
  • 3
    $\begingroup$ Have a look at this set up for custom properties. blender.stackexchange.com/a/39624/15543 $\endgroup$ – batFINGER Jan 21 '17 at 10:14
  • $\begingroup$ Wow, that's super smart ! With Zeffii's answer I already saved 100+ lines. With yours, I save 100 more ! What a treat ! $\endgroup$ – Nikos_VSE Jan 21 '17 at 12:33
3
$\begingroup$

something like this

def swap(prop):
    s = bpy.context.scene
    v = not getattr(s, prop)

    attributes = 'meshes lights cameras nurbs lattices empties texts bones surfaces metaballs fields'.split(' ')
    for attribute in attributes:
        setattr(s, attribute, not v if prop == attribute else v)

but achieving pythonic code is a quest for a different stack exchange site, like stack overflow

$\endgroup$
  • 1
    $\begingroup$ Thanks zeffii ! I am ashamed but greatly happy ! I learnt a lot today !! $\endgroup$ – Nikos_VSE Jan 21 '17 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.