Isotropix Forums

Attribute flag toggle

Clarisse Scripting related topics

Attribute flag toggle

Unread postby stheroux » Mon Apr 03, 2017 4:55 pm

Hi guys,

I have a simple interface in python like so:

python code

def simple_ui():
interface = ix.create_generic_object('simple_ui')

# A
attr_a = ix.add_attribute(interface, 'A', 'bool', 'test')
attr_a.set_bool(False)

# B
attr_b = ix.add_attribute(interface, 'B', 'long', 'test')
attr_b.set_long(0)
attr_b.set_read_only(True)

if ix.application.inspect(interface, ix.api.AppDialog.ok(), ix.api.AppDialog.STYLE_OK_CANCEL).is_ok():
ret_dict = {'A': interface.get_attribute('A').get_bool(),
'B': interface.get_attribute('B').get_long()}
return ret_dict

else:
ix.log_warning('User canceled')
return None


simple_ui()


I would like to toggle the read-only flag of a attribute B when attribute A value is changed. Like the limited regions attributes an image get toggle by the Enable Limited Region attribute. Any simple way to do that ?


Thanks
Sylvain
stheroux
 
Posts: 42
Joined: Thu Jan 15, 2015 10:23 pm

Re: Attribute flag toggle

Unread postby stheroux » Mon Apr 03, 2017 5:39 pm

Looks like maybe scripted class is the way to go for that ?
stheroux
 
Posts: 42
Joined: Thu Jan 15, 2015 10:23 pm

Re: Attribute flag toggle

Unread postby bvaldes » Tue Apr 04, 2017 10:18 am

Hi,

There are several ways to do what you want. It depends on your needs:
- A custom UI in python: most powerful but long to write (the "python way").
- Scripted class: easy to set up but only for simple UI (like Attribute Editor not more !)

The custom UI:

python code

# This class will handle all events and run functions depending on wich event it recieve.
class EventRewire(ix.api.EventObject):

# This function get state of the checkbox and enable or disable the B attribute depending on its state
def update(self, sender, evtid):
if sender.get_value():
numfieldLabel.set_enable(False)
numfield.set_enable(False)
else:
numfieldLabel.set_enable(True)
numfield.set_enable(True)

# The needed line to make a UI
clarisse_win = ix.application.get_event_window() # Find the Clarisse main window
window = ix.api.GuiWindow(clarisse_win, 0, 0, 250, 400) # Create a window child of Clarisse win: GuiWindow(parnt, x, y, width, height)
window.set_title('My perfect Window') # Name of the generated window
panel = ix.api.GuiPanel(window, 0, 0, window.get_width(), window.get_height()) # The main widget that will contain all UI elements
panel.set_constraints(ix.api.GuiWidget.CONSTRAINT_LEFT, ix.api.GuiWidget.CONSTRAINT_TOP, ix.api.GuiWidget.CONSTRAINT_RIGHT, ix.api.GuiWidget.CONSTRAINT_BOTTOM) # The widget has the same size than the window

checkboxLabel = ix.api.GuiLabel(panel, 10, 10, 10, 20, "A") # The Label of the checkbox: GuiLabel(parent, x, y, width, height, labek)
checkBox = ix.api.GuiCheckbox(panel, checkboxLabel.get_x()+checkboxLabel.get_width()+ 10, checkboxLabel.get_y()+1, "") # the checkbox itself: GuiCheckbo(parent, x, y, label)


numfieldLabel = ix.api.GuiLabel(panel, 10, checkboxLabel.get_y()+checkboxLabel.get_height(), 10, 20, "B") # The label of the numberfield
numfield = ix.api.GuiNumberField(panel, numfieldLabel.get_x()+numfieldLabel.get_width()+10, numfieldLabel.get_y(), 200, "") # The numberfield itself: GuiNumberField(parent, x, y, width, label)


event_rewire = EventRewire() # Init the EvebtRewire class
event_rewire.connect(checkBox, "EVT_ID_CHECKBOX_CLICK", event_rewire.update) # Connect the checkbox event on the EventRewire class

# Display window and destroy it if it's hidden
window.show()
while window.is_shown():
ix.application.check_for_events()
window.destroy()

The Scripted Class:

python code

class MyScriptedClassAction(ix.api.ModuleScriptedClassEngine):
def __init__(self):
ix.api.ModuleScriptedClassEngine.__init__(self)

# This function will be executed if an attribute change
def on_attribute_change(self, object, attr, dirtiness, dirtiness_flags):
if attr.get_name() == "A": # If the attribute A change
if attr.get_bool():
object.get_attribute("B").set_read_only(False)
else:
object.get_attribute("B").set_read_only(True)

# This is a CID. It contain the attributes of the item.
# To add attribute it is very simple: type "Name"
# To have a name like "My Attribute" you have to write: type "my_attribute"
# You can add flags to attributes like read_only, texturable...
# Just write these flags between {}. You needn't go to line a simple space is enough
my_cid = """
bool "A"
long "B"{
read_only yes
}
"""

# Declare the class. You can add this script to your startup_script to be sure to have the item in your Clarisse on start
classe = ix.api.ModuleScriptedClass.register_scripted_class(ix.application, "My_scripted_class", MyScriptedClassAction(), my_cid)

Hope this help. If you have any question, feel free to ask. Regards
Benoit VALDES
Isotropix
Clarisse QA
User avatar
bvaldes
 
Posts: 141
Joined: Mon Sep 26, 2016 11:44 am

Re: Attribute flag toggle

Unread postby stheroux » Tue Apr 04, 2017 2:47 pm

Hi Benoit,

Thanks for the examples. Scripted class seems very nice for quick stuff, exactly what we need. But I do have a question about them:

We need to register our scripted class in a startup script for our items to appear. How should we handle the case of having to exchange projects with another vendor who does not have access to our startup scripts?

I guess we would need to write a function to transfer attribute values to native items ?

Thanks
Sylvain
stheroux
 
Posts: 42
Joined: Thu Jan 15, 2015 10:23 pm

Re: Attribute flag toggle

Unread postby stheroux » Tue Apr 11, 2017 10:41 pm

Hi again,

What would be the syntax to create dropdown menu attributes using CID ?

I have been using OfAttr.add_preset() with OfAttr.VISUAL_HINT_ENUM on my generic objects which work well. But i cant seem to find a working syntax for CIDs.

Also generally i cant seem to find documentation anywhere for CIDs. Does it exists yet?

Thanks
Sylvain
stheroux
 
Posts: 42
Joined: Thu Jan 15, 2015 10:23 pm

Re: Attribute flag toggle

Unread postby bvaldes » Wed Apr 12, 2017 10:30 am

Hi,

Sorry for the late reply. If the vendor doesn't have the class in the startup script, the item will be loaded but will contain nothing and will be disabled.

About transfer attribute, you could but you won't have the same result: when you check attribute A the attribute B will not become enable.

What would be the syntax to create dropdown menu attributes using CID ?

I have been using OfAttr.add_preset() with OfAttr.VISUAL_HINT_ENUM on my generic objects which work well. But i cant seem to find a working syntax for CIDs.

Could you describe more what you want? Is there an example in the attribute editor?

About documentation, it exists. You can find it by pressing F1 in Clarisse, then on the bottom of the menu click on Reference> Scripting API. In the opened page you can find a link called "Introduction to CID syntax".

Regards
Benoit VALDES
Isotropix
Clarisse QA
User avatar
bvaldes
 
Posts: 141
Joined: Mon Sep 26, 2016 11:44 am

Re: Attribute flag toggle

Unread postby stheroux » Wed Apr 12, 2017 2:49 pm

Nice! super complete doc. I completely missed that as i was searching directly in the python-wrapped C++ where most things are usually.

Looks like i can do a dropdown menu similar to what i had with something like:
Code: Select all
long "shapes" {
    value 0
    preset "Box" "0"
    preset "Sphere" "1"
    preset "Ellipsoid" "2"
    preset "Tetrahedron" "3"
}


Thanks for the help again.
Sylvain
stheroux
 
Posts: 42
Joined: Thu Jan 15, 2015 10:23 pm


Return to Scripting