MotionBuilder Forum
Welcome to Autodesk’s MotionBuilder Forums. Share your knowledge, ask questions, and explore popular MotionBuilder topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Toggle visibility of a Group

4 REPLIES 4
Reply
Message 1 of 5
agnisen07
445 Views, 4 Replies

Toggle visibility of a Group

Hey there,

I am trying a simple script to find a group by it's name and simply toggle the 'Show' property On and Off.

Here is the function snippet for that:

 

 

def toggle():
    for comp in foundComponents:
        if comp.PropertyList.Find('Show').Data == 0:
            comp.PropertyList.Find('Show').Data = 1
        else:
            comp.PropertyList.Find('Show').Data = 0

toggle()

 

 

 

PROBLEM: The script works only when the group is visible. When toggled, the script turns off the visibility of the group without errors. It's when I want to turn it back on, it just doesn't work.

 

WORKAROUNDS I TRIED: I tried an elif statement as well, but the same issue persisted. Edit: Didn't mention earlier, but I did try out boolean values instead of int for the Show value. The issue persisted.

 

I am pretty sure I am missing a very fundamental logic theory here (apologies, I am still learning scripting). Has anyone tried something similar? Can anyone please point out the mistake I am making here?

 

Thanks a lot for any pointers on this.

 

Tagging members who have helped me previously : @Mocappy 

4 REPLIES 4
Message 2 of 5
damian.pajda
in reply to: agnisen07

First please try using bolean type instead of int. Maybe this is the case here. So:

 

def toggle():
    for comp in foundComponents:
        if comp.PropertyList.Find('Show').Data == False:
            comp.PropertyList.Find('Show').Data = True
        else:
            comp.PropertyList.Find('Show').Data = False
toggle()

 

 

BTW. if this won't help, your function iterate through foundComponents variable

for comp in foundComponents:

maybe issue is somewhere ealier in function that is getting those components from scene, could you also show how you find those components?

 

BTW. for accessing Show property, you can also use shorter form i.e. :

comp.Show = False

 

 

Message 3 of 5
agnisen07
in reply to: damian.pajda

Hi, 

Thanks for your reply.

Apologies for not pointing it out earlier, I tried boolean right at the beginning but with the same result. 

This is how I am trying to find the group in the scene. I think at least this portion works, because I am able to access the specific group.

foundComponents = FBComponentList()
includeNamespace = True
modelsOnly = False
comp = FBFindObjectsByName('ENV:Geometry', foundComponents, includeNamespace, modelsOnly)

 

Yes, even I thought that the issue might not be in the loop. I am not quite sure about it, since it is mostly just following the generic way of finding a group.

 

Thanks a lot for the:

comp.Show = False

I was planning to try out a form in this manner to see if it can shorten it.

 

I will keep checking on my end. Let me know if you find anything wrong in the section where I am finding the group.

 

Peace!

Message 4 of 5
damian.pajda
in reply to: agnisen07

hmm... I just tested this and it works fine in my simple test scene.

*Boolean or int doesn't matter, works in both cases (I was sure it should work but sometimes it is worth to try different things in case of issues... :D)

I combined your code like this:

 

from pyfbsdk import *
from pyfbsdk_additions import *


foundComponents = FBComponentList()
includeNamespace = True
modelsOnly = False
comp = FBFindObjectsByName('ENV:Geometry', foundComponents, includeNamespace, modelsOnly)

def toggle():
    for comp in foundComponents:
        if comp.PropertyList.Find('Show').Data == 0:
            comp.PropertyList.Find('Show').Data = 1
        else:
            comp.PropertyList.Find('Show').Data = 0
toggle()

 

damianpajda_0-1701314565578.png

 

I also tested it in some QT ui with button on Mobu 2020 and 2024.

Not sure where could be issue in your case 😕

Message 5 of 5
Mocappy
in reply to: agnisen07

Hello,

 

Think this will do what you are describing?

 

If you're using "Groups", you can narrow your search to the "Groups" in your FBScene and then change the "Show" attribute on them...something like this:

def find_group_by_long_name(group_long_name):
    group = None
    for scnGroup in FBSystem().Scene.Groups:
        if scnGroup.LongName == group_long_name:
            group = scnGroup
            continue
    
    return group

def toggle_group_show(group_obj):
    if group_obj.Show == True:
        group_obj.Show = False
    else:
        group_obj.Show = True
        
env_geo_group = find_group_by_long_name("ENV:Geometry")
if env_geo_group:
    toggle_group_show(env_geo_group)
else:
    print ("ERROR:group not found")

 Searching "Components" will search everything in your scene, which might be causing conflicts on the "Show" attributes. I've used "LongName" to make sure it searches using the namespace, but you can change as needed.

 

Hope this helps,

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report