Anyone with Plugin Experience?

Do you have questions about writing plugins or scripts in Python? Meet the coders here.

Anyone with Plugin Experience?

Postby yokel22 » Sat Dec 17, 2016 8:48 pm

I was wondering if anyone with some experience writing EG plugins has time to answer a few questions i had?
yokel22
Experienced User
 
Posts: 98
Joined: Thu Feb 05, 2015 5:56 pm
Location: U.S. - Kansas city

Re: Anyone with Plugin Experience?

Postby kgschlosser » Sat Dec 17, 2016 10:39 pm

shoot
If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
 
Posts: 2111
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Anyone with Plugin Experience?

Postby yokel22 » Sat Dec 17, 2016 11:11 pm

You may want to look at the init, if so i'll pm it too ya. First question is, how to go about taking a list or dictionary and append each items key/value to a dropdown menu for an actions config menu?
yokel22
Experienced User
 
Posts: 98
Joined: Thu Feb 05, 2015 5:56 pm
Location: U.S. - Kansas city

Re: Anyone with Plugin Experience?

Postby kgschlosser » Sun Dec 18, 2016 7:47 am

well that's kind of an illusive question.

because if you are using the convenience class eg.Choice or panel.Choice

creation of a new control

list
Code: Select all
choiceList = ['1', '2', '3', '4']
selection = '3'
panel = eg.ConfigPanel()
choiceCtrl = panel.Choice(value=choiceList.index(selection), choices=choiceList)


dict
this will show you getting the keys and the values
Code: Select all
choiceDict = {
    '1': 'One',
    '2': 'Two',
    '3': 'Three',
    '4': 'Four'
}
keyChoicesList = sorted(choiceDict.keys())
valueChoicesList = sorted(choiceDict.values())
keySelection = '3'
valueSelection = 'Two'

panel = eg.ConfigPanel()

choiceKeyCtrl = panel.Choice(value=keyChoicesList.index(keySelection), choices=keyChoicesList)
choiceValueCtrl = panel.Choice(value=valueChoicesList.index(valueSelection), choices=valueChoicesList)



so now if you want to append to the list.
and i am going to throw in a little bit of a twist

Code: Select all
newChoices = ['7', '9', '8', '0']
choiceCtrl.AppendItems(newChoices)


this is great if you wanted your choice list to look like this
1
2
3
4
7
9
8
0

Code: Select all
newChoices = ['7', '9', '8', '0']
choiceCtrl.AppendItems(sorted(newChoices))


almost
1
2
3
4
0
7
8
9
Code: Select all
newChoices = ['7', '9', '8', '0']
newchoices.append(choiceCtrl.GetItems())
choiceCtrl.SetItems(sorted(newChoices))

that's the one

NOTE: if you are going to sort a list of strings are are numbers (ints) then you should use sorted(somevariable, key=int)
because of how it will sort the list because 10 actually comes before 2


now the reason why i stated something about using the eg.choice is because the keywords are different
a wxChoice doesn't have value as a keyword. or a GetValue() method.

more on the control is here
https://wxpython.org/docs/api/wx.Choice-class.html

but also because it is a subclass of wxControlWithItems
there is nothing in this class but here is the link anyways
https://wxpython.org/docs/api/wx.Contro ... class.html

but because ControlWithItems is a subclass of wxItemContainer
this is where all the method desctiptions are
https://wxpython.org/docs/api/wx.ItemCo ... l#GetItems
If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
 
Posts: 2111
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Anyone with Plugin Experience?

Postby yokel22 » Sun Dec 18, 2016 12:52 pm

Thank you. That cleared up a few things for me. I think i'm following, I'll see if i can't get one working. I had a few other ?'s but am kinda blanking on them right now.
yokel22
Experienced User
 
Posts: 98
Joined: Thu Feb 05, 2015 5:56 pm
Location: U.S. - Kansas city

Re: Anyone with Plugin Experience?

Postby yokel22 » Mon Dec 26, 2016 10:40 pm

Okay, so i played around with your suggestions a bit. I Think i'm doing something wrong. I keep getting this error when EG loads and tries to create the choice menu.
Code: Select all
choiceCtrl = panel.Choice(value=choiceList.index(selection), choiceList)
SyntaxError: non-keyword arg after keyword arg


This is the action i'm working with:
Code: Select all
class TurnSwitchOn(eg.ActionBase):
    class text:
        label_tree="Turn Switch On: "
        label_conf="Switch Name"

    def __call__(self, device):
        call = "omitted"
        eg.StopMacro(ignoreReturn=False)   

    def GetLabel(self, device):
        return self.text.label_tree
   
    def Configure(self, device="Device ID"):
       
        st1 = panel.StaticText("")
        choiceList = ['1', '2', '3', '4']
        selection = '3'
        panel = eg.ConfigPanel()
        choiceCtrl = panel.Choice(value=choiceList.index(selection), choiceList)
        while panel.Affirmed():
            panel.SetResult(
                choiceCtrl.GetCurrentSelection(),
            )


after playing with it some more i was able to get it to work like this:
Code: Select all
class TurnSwitchOn(eg.ActionBase):
    class text:
        label_tree="Turn Device On: "
        label_conf="Device Name"

    def __call__(self, device):
        call = "omitted"
        eg.StopMacro(ignoreReturn=False)   

    def GetLabel(self, device):
        return self.text.label_tree
   
    def Configure(self, device="Device ID"):
        choiceDict = {
            '1': 'One',
            '2': 'Two',
            '3': 'Three',
            '4': 'Four'
        }
        keyChoicesList = sorted(choiceDict.keys())
        valueChoicesList = sorted(choiceDict.values())
        keySelection = '3'
        valueSelection = 'Two'
        panel = eg.ConfigPanel()
        st1 = panel.StaticText("")
        textCtrl = panel.TextCtrl(device)
        choiceKeyCtrl = panel.Choice(keyChoicesList.index(keySelection), keyChoicesList)
        choiceValueCtrl = panel.Choice(valueChoicesList.index(valueSelection), valueChoicesList)
        panel.AddLine(st1)
        panel.AddLine("ID: ", textCtrl)
        panel.AddLine("Light: ", choiceKeyCtrl)
        panel.AddLine("value: ", choiceValueCtrl)
        while panel.Affirmed():
            panel.SetResult(
                choiceKeyCtrl.GetStringSelection(),
            )


This gets me almost there, but i'm trying to get the selected dictionaries key(value), not the key itself. Or more specifically a dictionary value within the first dictionary.
yokel22
Experienced User
 
Posts: 98
Joined: Thu Feb 05, 2015 5:56 pm
Location: U.S. - Kansas city

Re: Anyone with Plugin Experience?

Postby jonib » Tue Dec 27, 2016 6:52 pm

yokel22 wrote:
Code: Select all
choiceCtrl = panel.Choice(value=choiceList.index(selection), choiceList)
SyntaxError: non-keyword arg after keyword arg

Do it like this:
Code: Select all
choiceCtrl = panel.Choice(value=choiceList.index(selection), choices=choiceList)
arguments with keywords need to be at the end.

Sorry did not look at your other code.

jonib
XBMC2 plugin to control XBMC. If you want to flatter me Image
jonib
Plugin Developer
 
Posts: 1220
Joined: Thu Mar 26, 2009 9:33 pm
Location: Sweden

Re: Anyone with Plugin Experience?

Postby kgschlosser » Tue Dec 27, 2016 7:02 pm

that was my mistake...
i forgot to add the choices keyword to my example...


sorry about that. I will edit it and fix the post.
If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
 
Posts: 2111
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Anyone with Plugin Experience?

Postby yokel22 » Tue Dec 27, 2016 10:58 pm

Thanks to both of you. I think i'm getting the hang of the config stuff now. To get the value of the selected key I have it setup like the code below, with an onChoice event setting the textCtrl value. Is this the best way to go about this?

Code: Select all
def Configure(self, device="Device ID"):
        keyChoicesList = sorted(self.plugin.switchdict.keys())
        keySelection = keyChoicesList[0]
        panel = eg.ConfigPanel()
        st1 = panel.StaticText("Choose Switch")
        textCtrl = panel.TextCtrl(device)
        choiceKeyCtrl = panel.Choice(keyChoicesList.index(keySelection), keyChoicesList)
        panel.AddLine(st1)
        panel.AddLine("Name: ", choiceKeyCtrl)
        panel.AddLine("deviceID: ", textCtrl)
        def OnChoice(event):
            name = choiceKeyCtrl.GetStringSelection()
            deviceid = self.plugin.switchdict[name]['deviceID']
            textCtrl.SetValue(deviceid)
            event.Skip()

        choiceKeyCtrl.Bind(wx.EVT_CHOICE, OnChoice)
        textCtrl.SetValue('')
        while panel.Affirmed():
            panel.SetResult(textCtrl.GetValue())
yokel22
Experienced User
 
Posts: 98
Joined: Thu Feb 05, 2015 5:56 pm
Location: U.S. - Kansas city

Re: Anyone with Plugin Experience?

Postby kgschlosser » Tue Dec 27, 2016 11:33 pm

it is really hard to know what is going on without seeing the entire plugin.

The code below is not tested. but is a little easier to read.. blank lines are your friend. use them to separate chunks of code that do a specific thing. you will understand in the code below. variable names are important for readability. textCtrl does not really tell us about the control except is has to deal with text kinda vague now deviceIDCtrl on the other hand tells us what it is specifically for.


I personally hate the use of the AddLine. I think it produces an ugly Config panel. so I changed that about a bit. You will see. give the code below a shot and let me know if it works or not.

I could have missed or typo'd some things. it was a quick once over. But feel free to ask questions. and let me know if it is not working

Code: Select all
def Configure(self, deviceName="Device ID"):
    panel = eg.ConfigPanel()
   
    switchDict = self.plugin.switchdict
    deviceNameChoices = sorted(switchDict.keys())

    if deviceName in deviceNameChoices:
        deviceSelection = deviceNameChoices.index(deviceName)
        deviceName = switchDict[deviceName]['deviceID']
    else:
        deviceSelection = 0

    deviceNameCtrl = panel.Choice(
        deviceSelection,
        deviceNameChoices
    )
    deviceIDCtrl = panel.TextCtrl(deviceName)

    deviceBox = panel.BoxedGroup(
        "Choose Switch",
        ("Name: ", deviceNameCtrl),
        ("device ID: ", deviceIDCtrl)
    )

    def OnChoice(event):
        deviceIDCtrl.SetValue(
            switchDict[deviceNameCtrl.GetStringSelection()]['deviceID']
        )
        event.Skip()
    deviceNameCtrl.Bind(wx.EVT_CHOICE, OnChoice)

    eg.EqualizeWidths(tuple(deviceBox.GetColumnItems(0)))
    panel.sizer.Add(deviceBox, 0, wx.EXPAND | wx.ALL, 10)

    while panel.Affirmed():
        panel.SetResult(deviceIDCtrl.GetValue())
If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
 
Posts: 2111
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Anyone with Plugin Experience?

Postby yokel22 » Wed Dec 28, 2016 8:40 am

Thanks, that worked without issue. Gave me a better understanding of some of the other panel functions too. Doing it by setting the textCtrl to the keys(value) still seems a bit cludgy way to grab that value. Since seeing the keys(value) is unimportant to the end user. This is more how i'd like it to function(it's from the iTunes plugin), but i can probably work that out myself.
Code: Select all
class GetUniversal(eg.ActionClass):
    name = "Get Universal"
    description = "Get Universal."
    class text:
        label="Select requested property:"
        get = "Get"
        class Properties:
            Name = "Song Name"
            Album = "Album Name "
            Artist = "Artist Name"
            Year = "Year Released"
            Rating = "Rating"
            Genre = "Genre"
            Duration = "Duration"
            Time = "Time"
            Artwork = "Artwork"
            Start = "Start Time"
            Finish = "Finish Time"
            TrackCount = "Track count"
            TrackNumber = "Track number"
            PlayedCount = "Track Times Played"
            PlayedDate = "Track Last Played Date/Time"
            DiscCount = "Number Of Discs"
            DiscNumber = "Disc Number"
            PlayedCount = "Times Played"
            PlayOrderIndex = "Playlist Track Order Number"
            KindAsString = "Track Type"
            SampleRate = "Sample Rate"
            BitRate = "BitRate"
            BPM = "BPM"
            Comment = "Comment"
            Compilation = "Compilation"
            Composer = "Composer"
            DateAdded = "DateAdded"

    def __init__(self):
        text=self.text
        self.propertiesList=(
            ("Name","Name"),
            ("Album","Album"),
            ("Artist","Artist"),
            ("Year","Year"),
            ("Rating","Rating"),
            ("Genre","Genre"),
            ("Duration","Duration"),
            ("Time","Time"),
            ("Artwork","Artwork"),
            ("Start","Start"),
            ("Finish","Finish"),
            ("TrackCount","TrackCount"),
            ("TrackNumber","TrackNumber"),
            ("PlayedCount","PlayedCount"),
            ("PlayedDate","PlayedDate"),
            ("DiscCount","DiscCount"),
            ("DiscNumber","DiscNumber"),
            ("PlayedCount","PlayedCount"),
            ("PlayOrderIndex","PlayOrderIndex"),
            ("KindAsString","KindAsString"),
            ("SampleRate","SampleRate"),
            ("BitRate","BitRate"),
            ("BPM","BPM"),
            ("Comment","Comment"),
            ("Compilation","Compilation"),
            ("Composer","Composer"),
            ("DateAdded","DateAdded"),         
        )
    def __call__(self, i):
        try:
            return self.plugin.workerThread.CallWait(partial(self.plugin.workerThread.GetProperty,self.propertiesList[i][0]))
        except:
            #This should mean that the COM server was active, then shutdown and restarted. 
            #The COM instance is from the old thread, so restart
            self.plugin.workerThread.Stop(1)
            self.plugin.workerThread = None
            if self.plugin.ComActive():
                return self.plugin.workerThread.CallWait(partial(self.plugin.workerThread.GetProperty,self.propertiesList[i][0]))


    def GetLabel(self, i):
        return self.text.get+" "+eval("self.text.Properties."+self.propertiesList[i][1])
#        exec 'from %s import Demo' % demo
    def Configure(self, i=0):
        #text=self.text
        #txt=Text.ListProperties
        choices=[eval("self.text.Properties."+tpl[1]) for tpl in self.propertiesList]
        panel=eg.ConfigPanel(self)
        panel.AddLabel(self.text.label)
        infoCtrl=wx.Choice(
            panel,
            choices=choices,
        )
        infoCtrl.SetSelection(i)
        panel.AddCtrl(infoCtrl)
        while panel.Affirmed():
            panel.SetResult(infoCtrl.GetSelection())


Anyhow, i did get the basic plugin fully functioning now :) . Probably not real pretty though. I'd planned on posting before the years end, but if someone that uses AxialControl http://www.axialcontrol.com/ stumbles upon this thread sooner, it should work fine.
Attachments
__init__.py
AxialControl Plugin
(27.54 KiB) Downloaded 31 times
yokel22
Experienced User
 
Posts: 98
Joined: Thu Feb 05, 2015 5:56 pm
Location: U.S. - Kansas city


Return to Coding Corner

Who is online

Users browsing this forum: No registered users and 2 guests