Isotropix Forums

Creating dropdown attribute

Clarisse Scripting related topics

Creating dropdown attribute

Unread postby jboissinot » Thu Sep 05, 2019 10:12 pm

Hi,

I was wondering if it'd be possible to create a custom attribute on an object as a dropdown list to let the user select a string item which could then be used to drive a value in an expression for instance?

Thanks,
Jeremy
jboissinot
 
Posts: 85
Joined: Tue Jan 29, 2019 10:36 pm

Re: Creating dropdown attribute

Unread postby dcourtois » Fri Sep 06, 2019 9:55 am

Hi,

You can create a dropdown attribute like this:

python code

config = ix.create_object("config", "ProjectItem")
attr = config.add_attribute("some_enum", ix.api.OfAttr.TYPE_STRING)
attr.add_preset("label A", "0")
attr.add_preset("label B", "1")
attr.set_string("0")
User avatar
dcourtois
 
Posts: 100
Joined: Tue Jul 25, 2017 3:15 pm

Re: Creating dropdown attribute

Unread postby jboissinot » Fri Sep 06, 2019 7:12 pm

Hi,

Ok good to know that this is possible, tried it and does work fine, so thanks for the info.

That being said, is this possible only in scripting cause I could not find a way to do it manually through the create custom attribute editor while investigating before to try in scripting?

Thanks,
Jeremy
jboissinot
 
Posts: 85
Joined: Tue Jan 29, 2019 10:36 pm

Re: Creating dropdown attribute

Unread postby anemoff » Mon Sep 09, 2019 10:43 am

Hi Jeremy,

Currently, the Create Custom Attribute editor doesn't allow creating custom attributes with presets.
But you could create a shelf tool that runs a script with a UI to create such an attribute.
Here is a basic UI example:

python code

class EventRewire(ix.api.EventObject):

window = None
target_item = None
attr_name = ''
presets = ''

def attr_name_changed(self, sender, evtid):
self.attr_name = sender.get_text()

def presets_changed(self, sender, evtid):
self.presets = sender.get_text()

def create(self, sender, evtid):
if not self.target_item:
ix.log_warning('No item selected.')
else:
if ix.api.CoreString(self.attr_name).is_identifier() == False:
ix.log_warning('Attribute name "{}" is invalid.'.format(self.attr_name))
else:
# create the attribute
attr = self.target_item.add_attribute(self.attr_name, ix.api.OfAttr.TYPE_STRING)
if not attr:
ix.log_warning('Failed to create attribute "{}" in item "{}"'.format(attr_name, self.target_item))
else:
# create the presets
assert len(self.presets) > 0
first_value = None
for preset in self.presets.split(';'):
parts = preset.split('|')
if len(parts) != 2:
ix.log_warning('Invalid preset "{}".'.format(preset))
else:
label = parts[0]
value = parts[1]
attr.add_preset(label, value)

if first_value == None:
first_value = value

attr.set_string(first_value)

window.hide()


if ix.selection.get_count() != 1:
ix.log_warning("Please select one object.")
else:
target_item = ix.selection[0]

h = 22
w = 400
pad = 2

clarisse_win = ix.application.get_event_window()
window = ix.api.GuiWindow(clarisse_win, clarisse_win.get_x() + clarisse_win.get_width() / 2, clarisse_win.get_y() + clarisse_win.get_height() / 2, w, 100)
window.set_title('Create Attribute With Presets')

panel = ix.api.GuiPanel(window, 0, 0, window.get_width(), window.get_height())
panel.set_constraints(ix.api.GuiWidget.CONSTRAINT_LEFT, ix.api.GuiWidget.CONSTRAINT_TOP,
ix.api.GuiWidget.CONSTRAINT_RIGHT, ix.api.GuiWidget.CONSTRAINT_BOTTOM)

y = 0
item_name_label = ix.api.GuiLineEdit(panel, pad, y, w - pad, h, "Target item:")
item_name_label.set_read_only(True)
item_name_label.set_text(target_item.get_full_name())
y += h + pad

attr_name_edit = ix.api.GuiLineEdit(panel, pad, y, w - pad, h, "New Attribute:")
y += h + pad

presets_edit = ix.api.GuiLineEdit(panel, pad, y, w - pad, h, "Presets:")
# expected syntax: "label1|value1;label2|value2|..."
y += h + pad

create_btn = ix.api.GuiPushButton(panel, pad, y, w - pad, h, "Create")

event_rewire = EventRewire()
event_rewire.window = window
event_rewire.target_item = target_item

# connect events: connect(item_to_listen, what_we_are_listening, function_called)
event_rewire.connect(attr_name_edit, 'EVT_ID_LINE_EDIT_VALUE_EDITED', event_rewire.attr_name_changed)
event_rewire.connect(presets_edit, 'EVT_ID_LINE_EDIT_VALUE_EDITED', event_rewire.presets_changed)
event_rewire.connect(create_btn, 'EVT_ID_PUSH_BUTTON_CLICK', event_rewire.create)

window.show()
while window.is_shown():
ix.application.check_for_events()

window.destroy()
Anthony Nemoff
Isotropix
R&D Engineer
User avatar
anemoff
 
Posts: 369
Joined: Wed Jan 13, 2016 10:10 am

Re: Creating dropdown attribute

Unread postby jboissinot » Tue Sep 10, 2019 2:21 pm

Ok thanks for the info Anthony, I'll take a look at this.
jboissinot
 
Posts: 85
Joined: Tue Jan 29, 2019 10:36 pm


Return to Scripting