How to know if a node is visible in the scene by script

How to know if a node is visible in the scene by script

Anonymous
Not applicable
1,320 Views
6 Replies
Message 1 of 7

How to know if a node is visible in the scene by script

Anonymous
Not applicable

Hi all,
My workmates and I, have been looking for a way to know if a node is visible in the scene via script

Groups.PNG

How we can know if the "Sphere" is visible in the scene? I mean, if it is gray or if it has color in the sceenegraph tree.
Thanks a lot 🙂

Pol.

Groups.PNG

0 Likes
1,321 Views
6 Replies
Replies (6)
Message 2 of 7

dlincol1
Advocate
Advocate

Here is a way that you can build on.

s = findNode('Sphere')

print s.getActive()

 

sP = s.getParent()

print sP.getActive()

 

b = findNode('Box')

print b.getActive()

 

bP = b.getParent()

print bP.getActive()

Regards,
Dan
0 Likes
Message 3 of 7

Anonymous
Not applicable

Thank you Dan,
After looking your script, I understand that your are suggesting us to look if the father of the node is active. However, this is what we are trying to avoid. We use to work with an extense tree and if the node is under a "Switch", the function  .getActive() will return True even the node is in color gray (what we expect to  be False) what means that the node is not visible in the scene.

 

In conclusion, we are looking for a way to know if a node is visible in the scene only looking to it. We would like to ask the node if it is gray or orange in the scenegraph tree.

 

Regards,

Pol.

0 Likes
Message 4 of 7

michael_nikelsky
Autodesk
Autodesk

Hi,

this should do it:

 

def isVisible( node ):
    if not node.isValid():
        return true
    if not node.getActive():
        return false
    nodeParent = node.getParent()
    if nodeParent.isValid():
        if nodeParent.getType() == "Switch":
            currentChoice = nodeParent.fields().getInt32("choice")
            if currentChoice == -1:
                return false
            if currentChoice == -2:
                return true
            activeNode = nodeParent.getChild(currentChoice)
            if activeNode.getID() != node.getID():
                return false  
    return isVisible( node.getParent())

Kind regards

Michael

 



Michael Nikelsky
Sr. Principal Engineer
Message 5 of 7

hobbotuk
Contributor
Contributor

Hi!

 

You may try with a recursive function, sorry the comments are in spanish

 

 

 

#------------------------------------------------------------#

def IsVisible(Nodo):

    if Nodo.getActive():

        #Tomar el nodo padre
        Padre = Nodo.getParent()
        
        if Padre.getType() == "Switch":
            #Mirar si es el hijo seleccionado del Switch
            if Padre.getChild(Padre.fields().getInt32("choice")) != Nodo:
                return False #"NO VISIBLE"

        #Comprobar si el padre es el Root
        if Padre.getID() == getRootNode().getID() :
            return True #"VISIBLE"

        #Lanzar la funcion de nuevo
        return  IsVisible(Padre)

    else:
        #Si no esta activo, es "NO VISIBLE"
        return False #"NO VISIBLE"

 

#------------------------------------------------------------#

 

IsVisible(getSelectedNode())

#or

IsVisible(findNode("Sphere"))

 

#------------------------------------------------------------#

 

 

 

Best Regards!

Message 6 of 7

michael_nikelsky
Autodesk
Autodesk

The -2 case was bogus:

def isVisible( node ):
    if not node.isValid():
        return true
    if not node.getActive():
        return false
    nodeParent = node.getParent()
    if nodeParent.isValid():
        if nodeParent.getType() == "Switch":
            currentChoice = nodeParent.fields().getInt32("choice")
            if currentChoice == -1:
                return false
            if currentChoice >= 0 and currentChoice < nodeParent.getNumChildren():
                activeNode = nodeParent.getChild(currentChoice)
                if activeNode.getID() != node.getID():
                    return false  
    return isVisible( node.getParent())
 

 



Michael Nikelsky
Sr. Principal Engineer
0 Likes
Message 7 of 7

Anonymous
Not applicable

I've previously asked for a script to "select hidden in subtree" (script below)

From there you can select any parent node (including Root itself)...use the script... then Ctrl+Shift+F to Scroll to selected (or just isolate/delete etc. as you wish)

Hope this helps.

def getHidden(node, hidden):
    if(not node.getActive()):
        hidden.append(node)
    for i in range(0, node.getNChildren()):
        getHidden(node.getChild(i), hidden)

def selectHidden():
   node = getSelectedNode()
    hidden = []
    getHidden(node, hidden)
    selectNodes(hidden)
    
KeyH = vrKey(Key_H)
KeyH.connect(selectHidden)
KeyH.setDescription("Select hidden nodes of selected subtree")
0 Likes