Isotropix Forums

how to make a ui interactive with scene selection?

Clarisse Scripting related topics

how to make a ui interactive with scene selection?

Unread postby nourseeeee » Thu Feb 13, 2020 9:43 pm

Hi there, I'm currently working on a script in clarisse and I'm struggling on making my ui interactive with my scene.

My script is creating a window that populate buttons and labels based on a list of a selection of objects. This is all working fine, however I would like to have my window updating with a new list when I make a different selection in clarisse without having to close the window and relaunch the script.

Any help of clue?
Thanks!
nourseeeee
 
Posts: 3
Joined: Mon Dec 17, 2018 8:04 pm

Re: how to make a ui interactive with scene selection?

Unread postby anemoff » Mon Feb 17, 2020 2:32 pm

Hi,

I've made this very basic example to illustrate the usage.
It shows a GuiWindow with a GuiTextEdit, and the GuiTextEdit shows the current selection as text and reacts to selection change.

python code

class MyWidget(ix.api.GuiWindow):

def __init__(self, app, x, y, w, h):
# init the window
ix.api.GuiWindow.__init__(self, app, x, y, w, h, 'My Widget')
self.app = app

# connect to app selection change event
self.connect(app, 'EVT_ID_SELECTION_CHANGED', self.on_selection_changed)

# create ui
self.text_edit = ix.api.GuiTextEdit(self, 0, 0, w, h)
self.text_edit.set_text(self.get_selection_as_text())

# show it
self.show()
# listen to application events
# note: this loop is blocking until the widget is closed (manually or via a custom button, for example)
while self.is_shown():
ix.application.check_for_events()

def on_selection_changed(self, sender, event_id):
try:
self.text_edit.set_text(self.get_selection_as_text())
self.redraw()
except Exception as e:
ix.log_warning('Exception in MyWidget.on_selection_changed:\n{}'.format(e))

def get_selection_as_text(self):
text = 'Selection:\n'
if ix.selection.is_empty():
text += '<empty>'
else:
for i, item in enumerate(ix.selection):
text += '{} - {}\n'.format(i + 1, item)
return text

# Run it
try:
w = MyWidget(ix.application, 0, 0, 400, 300)
except Exception as e:
ix.log_error('Script error:\n{}'.format(e))

Note: you can derive directly fromix.api EventObject if you don't want your main class to derive from a Gui class. However, deriving from EventObject is mandatory to allow your class to listen to events and use "connect".
For example, you could have "class MyTool(ix.api.EventObject)", which then creates a GuiWindow. It's just a different approach.

I hope this gets you started with what you want to do.

Cheers,
Anthony Nemoff
Isotropix
R&D Engineer
User avatar
anemoff
 
Posts: 328
Joined: Wed Jan 13, 2016 10:10 am

Re: how to make a ui interactive with scene selection?

Unread postby nourseeeee » Wed Feb 19, 2020 12:02 am

Thanks for the answer that's really helpful!!
nourseeeee
 
Posts: 3
Joined: Mon Dec 17, 2018 8:04 pm


Return to Scripting