Skip to main content
Participant
Jul 23, 2021 3:29 AM UTC
Question

Check selection type with python

Hi, 

let's say I make a selection with a python function like:

 

def Select(D):
    ctx = sd.getContext()
    app = ctx.getSDApplication()
    uiMgr = app.getQtForPythonUIMgr() 
    selection = uiMgr.getCurrentGraphSelection()
       
    return selection
 

 

How do I check the type of this selection??
I use to do it with type() but now I can't.. I check the sdtype docs but I didn't understand the use of it,
I just want to get a bool value to do an if (check if is array):
This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

Luca Giarrizzo
Community Manager
Community Manager
Jul 23, 2021 10:33 AM UTC

Hello,

 

First, please note the getCurrentGraphSelection() method of the SDUIMgr and QtForPythonUIMgrWrapper classes are deprecated. Use the getCurrentGraphSelectedNodes() and getCurrentGraphSelectedObjects() methods instead.

 

Similarly to getCurrentGraphSelection(), these methods return array objects, which you can go through with a simple for loop.

 

And to clarify, you may get the type of an SDProperty's value, however array objects and items selected in a graph do not have a type, but a class name which you can access using the getClassName() method.

 

Here is an example:

 

import sd

ctx = sd.getContext()
app = ctx.getSDApplication()
uiMgr = app.getUIMgr() 

selected_objects = uiMgr.getCurrentGraphSelectedObjects()
selected_nodes = uiMgr.getCurrentGraphSelectedNodes()


print('Object selection class= ' + selected_objects.getClassName())

# print class for graph objects (comments, frames, pins)
for obj in selected_objects:
  print('Class= ' + obj.getClassName())


print('Node selection class= ' + selected_nodes.getClassName())

# print if the selection object is an array
if selected_nodes.getClassName() == 'SDArray':
	print('This is indeed an array.')

# print if the array is empty
if not selected_nodes:
	print('...but nothing is selected!')
else:
  # print class and id/label for graph nodes
  for node in selected_nodes:
    print('Class= ' + node.getClassName())
    print('Node= {} / {}'.format(
      node.getDefinition().getId(), 
      node.getDefinition().getLabel()
      ))
	

 

 

And an example of what this code does:

 

I hope this is helpful!

 

Best regards.

 

Translate
Luca Giarrizzo | Quality Engineer - Substance 3D Designer | Adobe
ParanaxusAuthor
Participant
Jul 23, 2021 2:35 PM UTC

thanks a lot for the fast answer!, I'll try that then!.

Translate
Luca Giarrizzo
Community Manager
Community Manager
Jul 26, 2021 7:06 AM UTC

Happy to help!

 

Translate
Luca Giarrizzo | Quality Engineer - Substance 3D Designer | Adobe