Skip to main content
Participant
July 23, 2021
Question

Check selection type with python

  • July 23, 2021
  • 1 reply
  • 426 views

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 has been closed for replies.

1 reply

Luca Giarrizzo
Community Manager
Community Manager
July 23, 2021

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.

 

Luca Giarrizzo | Quality Engineer - Substance 3D Designer | Adobe
ParanaxusAuthor
Participant
July 23, 2021

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

Luca Giarrizzo
Community Manager
Community Manager
July 26, 2021

Happy to help!

 

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