Sample Addons
From Light Forge Wiki
Revision as of 19:35, 9 May 2013 by Chris Monson (Talk | contribs)
Select Children
This script will get a list of all the groups that you currently have selected and then it will select all of their child groups and deselect the parent.
#called when the addon is selected in the addon's menu def onExecute(): #get list of selected groups groupList = api.groups.getSelectedGroups() if groupList != None: for group in groupList: selectChildren(group) #select all the children for a given group def selectChildren(groupPath): childCount = api.groups.getChildCount(groupPath) if childCount > 0: api.groups.deselectGroup(groupPath) for i in range(0, childCount): #build the group path for the next group newPath = groupPath if len(newPath) == 0: newPath = str(i) else: newPath = newPath + "," + str(i) #select group api.groups.selectGroup(newPath)
Lamps On
This addon allows you to specify a list of profile names and the value that the lamp channel must be assigned to in order to strike the lamp for that particular fixture. This addon runs automatically on startup asking if you'd like to turn the lamps on.
This example is a little more complicated, utilizing file operations for saving and loading settings, asynchronous processing, as well as using Windows Forms for dialog boxes to adjust the addon's settings in Lightforge.
import clr clr.AddReference("System.Drawing") clr.AddReference("System.Windows.Forms") from System.Drawing import Point from System.Windows.Forms import Application, Button, ListView, ListViewItem, View, Form, ContextMenu, TextBox, NumericUpDown, Label from System.Windows.Forms import MessageBox, MessageBoxButtons from System.Windows.Forms import FormBorderStyle, DialogResult profilesAndValues = [] #called when the script is loaded def onInitialize(): load() #called when the script settings are edited in the script settings dialog def onEdit(): dlg = LampOnDialog() if dlg.ShowDialog() == DialogResult.OK: save() #executes asyncronously when clicked from the Lightforge interface def onExecuteAsync(): onStartupAsync() #called as Lightforge is starting up def onStartupAsync(): global profilesAndValues if MessageBox.Show("Would you like to turn lamps on?", "Lamp Startup", MessageBoxButtons.YesNo) == DialogResult.Yes: print("") print("Turning Lamps On") #find profiles that match those in our profilesAndValues list and set values appropriately findProfiles("", profilesAndValues) #wait 5 seconds to allow the fixtures to process the command print("Waiting...") api.wait(5) #release the rig to remove the changes we made print("Releasing Fixtures") api.groups.releaseGroup("") #Search through rig to find the specified profiles and assign them the given values def findProfiles(groupPath, profileList): currentPath = groupPath #step through all children of the current group for i in range(0, api.groups.getChildCount(currentPath)): #build the group path for the next group if len(currentPath) == 0: currentPath = str(i) else: currentPath = groupPath + "," + str(i) found = False #step through all the profiles we've listed to see if this fixture's profile matches any of them for prf in profileList: if prf[0] in api.groups.getProfileName(currentPath): #set the value of this fixture to the value in the profile list api.groups.setAttribute(api.attributeTypes.lamp, int(prf[1]) * 256, currentPath) found = True #call this function on the tested group to work recursively through the groups if found == False and api.groups.getChildCount(currentPath) > 0: findProfiles(currentPath, profileList) #Save settings to file def save(): global profilesAndValues strOutput = "" for prf in profilesAndValues: strOutput += prf[0] + ";" + str(prf[1]) + "\n" f = open(addon.getDirectory() + "Lamps On.settings", "w+") f.write(strOutput) f.close() #Load settings in from file def load(): global profilesAndValues profilesAndValues = [] try: f = open(addon.getDirectory() + "Lamps On.settings", "r+") for line in f: profilesAndValues.append(line.rstrip().split(';')) f.close() except: pass #Dialog box for assigning values to profiles class LampOnDialog(Form): def __init__(self): global profilesAndValues self.Text = 'Lamp On Settings' self.FormBorderStyle = FormBorderStyle.FixedDialog self.MinimizeBox = False self.MaximizeBox = False margin = 10 #Cancel Button btnCancel = Button() btnCancel.Text = "Cancel" btnCancel.Location = Point(self.ClientRectangle.Right - (btnCancel.Width + margin), self.ClientRectangle.Bottom - (btnCancel.Height + margin)) btnCancel.Click += self.clickCancel self.Controls.Add(btnCancel) #Ok Button btnOk = Button() btnOk.Text = "Ok" btnOk.Location = Point(btnCancel.Left - (btnOk.Width + margin), self.ClientRectangle.Bottom - (btnOk.Height + margin)) btnOk.Click += self.clickOk self.Controls.Add(btnOk) #List Box self.lstProfiles = ListView() self.lstProfiles.Location = Point(margin, margin) self.lstProfiles.Width = self.ClientRectangle.Width - (margin * 2) self.lstProfiles.Height = btnOk.Top - (margin * 2) self.Controls.Add(self.lstProfiles) self.lstProfiles.View = View.Details self.lstProfiles.Columns.Add("Profile") self.lstProfiles.Columns.Add("Value") self.lstProfiles.Columns[0].Width = self.lstProfiles.Width * 0.7 self.refreshList() #Context Menu listContext = ContextMenu() listContext.MenuItems.Add("Add Profile") listContext.MenuItems[0].Click += self.addProfile listContext.MenuItems.Add("Edit") listContext.MenuItems[1].Click += self.editProfile listContext.MenuItems.Add("Remove") listContext.MenuItems[2].Click += self.removeProfile self.lstProfiles.ContextMenu = listContext #reload the profilesAndValues data into the ListView control def refreshList(self): global profilesAndValues self.lstProfiles.Items.Clear() for prf in profilesAndValues: lvItem = ListViewItem(prf[0]) lvItem.SubItems.Add(str(prf[1])) self.lstProfiles.Items.Add(lvItem) #handle Ok button click def clickOk(self, sender, args): self.DialogResult = DialogResult.OK self.Close #handle Cancel button click def clickCancel(self, sender, args): self.DialogResult = DialogResult.Cancel self.Close #add profile called from the context menu def addProfile(self, sender, args): global profilesAndValues dlg = ProfileValueDialog("",0) if dlg.ShowDialog() == DialogResult.OK: profilesAndValues.append([dlg.profile,dlg.value]) self.refreshList() #remove profile called from the context menu def removeProfile(self, sender, args): global profilesAndValues if self.lstProfiles.SelectedIndices.Count > 0: profilesAndValues.pop(self.lstProfiles.SelectedIndices[0]) self.refreshList() #edit profile called from the context menu def editProfile(self, sender, args): global profilesAndValues if self.lstProfiles.SelectedIndices.Count > 0: index = self.lstProfiles.SelectedIndices[0] dlg = ProfileValueDialog(profilesAndValues[index][0],profilesAndValues[index][1]) if dlg.ShowDialog() == DialogResult.OK: profilesAndValues[index] = [dlg.profile,dlg.value] self.refreshList() #Dialog box for assigning values to profiles class ProfileValueDialog(Form): profile = "" value = 0 def __init__(self, profileName, lampValue): global profilesAndValues self.Text = 'Profile Value Dialog' self.FormBorderStyle = FormBorderStyle.FixedDialog self.MinimizeBox = False self.MaximizeBox = False margin = 10 self.Width = 350 self.Height = 150 #Label lblPrompt = Label() lblPrompt.Text = "Enter profile name and 8 bit value for lamp on." lblPrompt.Location = Point(margin, margin) lblPrompt.Width = self.Width self.Controls.Add(lblPrompt) #Text Box self.txtProfile = TextBox() self.txtProfile.Location = Point(margin, margin * 4) self.txtProfile.Text = profileName self.txtProfile.Width = 200 self.Controls.Add(self.txtProfile) #NumericUpDown self.numValue = NumericUpDown() self.numValue.Maximum = 255 self.numValue.Value = lampValue self.numValue.Location = Point(self.txtProfile.Right + margin, self.txtProfile.Top) self.numValue.Width = (self.Width - margin) - (self.txtProfile.Right + (margin * 3)) self.Controls.Add(self.numValue) #Cancel Button btnCancel = Button() btnCancel.Text = "Cancel" btnCancel.Location = Point(self.ClientRectangle.Right - (btnCancel.Width + margin), self.ClientRectangle.Bottom - (btnCancel.Height + margin)) btnCancel.Click += self.clickCancel self.Controls.Add(btnCancel) #Ok Button btnOk = Button() btnOk.Text = "Ok" btnOk.Location = Point(btnCancel.Left - (btnOk.Width + margin), self.ClientRectangle.Bottom - (btnOk.Height + margin)) btnOk.Click += self.clickOk self.Controls.Add(btnOk) def clickOk(self, sender, args): self.profile = self.txtProfile.Text self.value = self.numValue.Value self.DialogResult = DialogResult.OK self.Close def clickCancel(self, sender, args): self.DialogResult = DialogResult.Cancel self.Close