Skip to content

Instantly share code, notes, and snippets.

@donovankeith
Last active June 15, 2016 18:42
Show Gist options
  • Save donovankeith/1a560d194f6374656187dca8693592ae to your computer and use it in GitHub Desktop.
Save donovankeith/1a560d194f6374656187dca8693592ae to your computer and use it in GitHub Desktop.
"""Treeview Dialog
UltraSimple Treeview Example. Displays a list of Greek letters you can select and delete.
MIT License
Copyright (c) 2016 Donovan Keith
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import c4d
import os
PLUGIN_ID = 1037559 #TODO: Get your own Plugin ID from: http://www.plugincafe.com/forum/developer.asp
PLUGIN_NAME = "TreeView Dialog"
PLUGIN_HELP = "A simple treeview example"
class ListItem():
"""A simple class for storing an item with a name, selection, & open status.
"""
def __init__(self, name=""):
self.name = name
self.selected = False
self.opened = True
class TreeData(c4d.gui.TreeViewFunctions):
"""A one-dimensional list."""
def __init__(self, dlg):
"""Build a simple list."""
self.items = []
self.DummyData() # Fill the treeview with some dummy data
def DummyData(self):
"""Make a dummy list"""
greek_letters = [
"Alpha",
"Beta",
"Gamma",
"Delta",
"Epsilon",
"Zeta",
"Eta",
"Theta",
"Iota",
"Kappa",
"Lambda",
"Mu",
"Nu",
"Xi",
"Omicron",
"Pi",
"Rho",
"Sigma",
"Tau",
"Upsilon",
"Phi",
"Chi",
"Psi",
"Omega"
]
for letter_name in greek_letters:
letter = ListItem(letter_name)
self.items.append(letter)
def GetFirst(self, root, userdata):
if self.items:
return self.items[0]
def GetDown(self, root, userdata, obj):
return None
def GetNext(self, root, userdata, obj):
"""Return the next item in the list after obj."""
if obj in self.items:
obj_index = self.items.index(obj)
next_index = obj_index + 1
if next_index < len(self.items):
return self.items[next_index]
def GetPred(self, root, userdata, obj):
if obj in self.items:
obj_index = self.items.index(obj)
prev_index = obj_index - 1
if prev_index > 0:
return self.items[prev_index]
def GetName(self, root, userdata, obj):
return obj.name
def IsOpened(self, root, userdata, obj):
return obj.opened
def IsSelected(self, root, userdata, obj):
return obj.selected
def Select(self, root, userdata, obj, mode):
if mode == c4d.SELECTION_NEW:
for item in self.items:
item.selected = False
if item == obj:
item.selected = True
elif mode == c4d.SELECTION_ADD:
obj.selected = True
elif mode == c4d.SELECTION_SUB:
obj.selected = False
def DeletePressed(self, root, userdata):
self.items = [x for x in self.items if not x.selected]
class TreeviewDialog(c4d.gui.GeDialog):
"""A simple treeview dialog.
"""
_tree_gui = None
def CreateLayout(self):
self.SetTitle(PLUGIN_NAME)
tree_gui_settings = c4d.BaseContainer()
tree_gui_settings.SetLong(c4d.TREEVIEW_BORDER, c4d.BORDER_THIN_IN)
tree_gui_settings.SetBool(c4d.TREEVIEW_HAS_HEADER, True)
tree_gui_settings.SetBool(c4d.TREEVIEW_HIDE_LINES, False)
tree_gui_settings.SetBool(c4d.TREEVIEW_MOVE_COLUMN, True)
tree_gui_settings.SetBool(c4d.TREEVIEW_RESIZE_HEADER, True)
tree_gui_settings.SetBool(c4d.TREEVIEW_FIXED_LAYOUT, True) # Don't allow Columns to be re-ordered
tree_gui_settings.SetBool(c4d.TREEVIEW_ALTERNATE_BG, True) # Alternate Light/Dark Gray BG
tree_gui_settings.SetBool(c4d.TREEVIEW_CURSORKEYS, True) # Process Up/Down Arrow Keys
self._tree_gui = self.AddCustomGui(
0,
c4d.CUSTOMGUI_TREEVIEW,
"",
c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT,
300,
300,
tree_gui_settings
)
return True
def InitValues(self):
tree_data = TreeData(self)
self._tree_gui.SetRoot(None, tree_data, None)
return True
class MenuCommand(c4d.plugins.CommandData):
""" Command to invoke the TreeView dialog.
"""
dialog = None
def Execute(self, doc):
""" Open the dialog when the user call the command
:param doc:
:return:
"""
if self.dialog is None:
self.dialog = TreeviewDialog()
return self.dialog.Open(
c4d.DLG_TYPE_ASYNC,
PLUGIN_ID,
defaulth=300,
defaultw=300
)
def RestoreLayout(self, sec_ref):
""" Restore the dialog if it's closed.
:param sec_ref:
:return:
"""
if self.dialog is None:
self.dialog = TreeviewDialog()
return self.dialog.Restore(PLUGIN_ID, secret=sec_ref)
def main():
"""Register the plugin with Cinema 4D."""
c4d.plugins.RegisterCommandPlugin(
PLUGIN_ID,
PLUGIN_NAME,
0,
None,
PLUGIN_HELP,
MenuCommand()
)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment