SHARE
TWEET

C4D TreeView Example

gr4ph0s Feb 26th, 2018 (edited) 182 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #More information http://www.plugincafe.com/forum/forum_posts.asp?TID=14102&PID=56287#56287
  2. import c4d
  3.  
  4. # Be sure to use a unique ID obtained from http://www.plugincafe.com/.
  5. PLUGIN_ID = 1000010 # TEST ID ONLY
  6.  
  7. # TreeView Column IDs.
  8. ID_CHECKBOX = 1
  9. ID_NAME = 2
  10. ID_OTHER = 3
  11.  
  12. class TextureObject(object):
  13.     """
  14.    Class which represent a texture, aka an Item in our list
  15.    """
  16.     texturePath = "TexPath"
  17.     otherData = "OtherData"
  18.     _selected = False
  19.  
  20.     def __init__(self, texturePath):
  21.         self.texturePath = texturePath
  22.         self.otherData += texturePath
  23.  
  24.     @property
  25.     def IsSelected(self):
  26.         return self._selected
  27.  
  28.     def Select(self):
  29.         self._selected = True
  30.  
  31.     def Deselect(self):
  32.         self._selected = False
  33.  
  34.     def __repr__(self):
  35.         return str(self)
  36.  
  37.     def __str__(self):
  38.         return self.texturePath
  39.  
  40.  
  41. class ListView(c4d.gui.TreeViewFunctions):
  42.  
  43.     def __init__(self):
  44.         self.listOfTexture = list() # Store all objects we need to display in this list
  45.  
  46.         # Add some defaults values
  47.         t1 = TextureObject("T1")
  48.         t2 = TextureObject("T2")
  49.         t3 = TextureObject("T3")
  50.         t4 = TextureObject("T4")
  51.  
  52.         self.listOfTexture.extend([t1, t2, t3, t4])
  53.  
  54.  
  55.     def IsResizeColAllowed(self, root, userdata, lColID):
  56.         return True
  57.  
  58.     def IsTristate(self, root, userdata):
  59.         return False
  60.  
  61.     def GetColumnWidth(self, root, userdata, obj, col, area):
  62.         return 80  # All have the same initial width
  63.  
  64.     def IsMoveColAllowed(self, root, userdata, lColID):
  65.         # The user is allowed to move all columns.
  66.         # TREEVIEW_MOVE_COLUMN must be set in the container of AddCustomGui.
  67.         return True
  68.  
  69.     def GetFirst(self, root, userdata):
  70.         """
  71.        Return the first element in the hierarchy, or None if there is no element.
  72.        """
  73.         rValue = None if not self.listOfTexture else self.listOfTexture[0]
  74.         return rValue
  75.  
  76.     def GetDown(self, root, userdata, obj):
  77.         """
  78.        Return a child of a node, since we only want a list, we return None everytime
  79.        """
  80.         return None
  81.  
  82.     def GetNext(self, root, userdata, obj):
  83.         """
  84.        Returns the next Object to display after arg:'obj'
  85.        """
  86.         rValue = None
  87.         currentObjIndex = self.listOfTexture.index(obj)
  88.         nextIndex = currentObjIndex + 1
  89.         if nextIndex < len(self.listOfTexture):
  90.             rValue = self.listOfTexture[nextIndex]
  91.  
  92.         return rValue
  93.  
  94.     def GetPred(self, root, userdata, obj):
  95.         """
  96.        Returns the previous Object to display before arg:'obj'
  97.        """
  98.         rValue = None
  99.         currentObjIndex = self.listOfTexture.index(obj)
  100.         predIndex = currentObjIndex - 1
  101.         if 0 <= predIndex < len(self.listOfTexture):
  102.             rValue = self.listOfTexture[predIndex]
  103.  
  104.         return rValue
  105.  
  106.     def GetId(self, root, userdata, obj):
  107.         """
  108.        Return a unique ID for the element in the TreeView.
  109.        """
  110.         return hash(obj)
  111.  
  112.     def Select(self, root, userdata, obj, mode):
  113.         """
  114.        Called when the user selects an element.
  115.        """
  116.         if mode == c4d.SELECTION_NEW:
  117.             for tex in self.listOfTexture:
  118.                 tex.Deselect()
  119.             obj.Select()
  120.         elif mode == c4d.SELECTION_ADD:
  121.             obj.Select()
  122.         elif mode == c4d.SELECTION_SUB:
  123.             obj.Deselect()
  124.  
  125.     def IsSelected(self, root, userdata, obj):
  126.         """
  127.        Returns: True if *obj* is selected, False if not.
  128.        """
  129.         return obj.IsSelected
  130.  
  131.     def SetCheck(self, root, userdata, obj, column, checked, msg):
  132.         """
  133.        Called when the user clicks on a checkbox for an object in a
  134.        `c4d.LV_CHECKBOX` column.
  135.        """
  136.         if checked:
  137.             obj.Select()
  138.         else:
  139.             obj.Deselect()
  140.  
  141.     def IsChecked(self, root, userdata, obj, column):
  142.         """
  143.        Returns: (int): Status of the checkbox in the specified *column* for *obj*.
  144.        """
  145.         if obj.IsSelected:
  146.             return c4d.LV_CHECKBOX_CHECKED | c4d.LV_CHECKBOX_ENABLED
  147.         else:
  148.             return c4d.LV_CHECKBOX_ENABLED
  149.  
  150.     def GetName(self, root, userdata, obj):
  151.         """
  152.        Returns the name to display for arg:'obj', only called for column of type LV_TREE
  153.        """
  154.         return str(obj) # Or obj.texturePath
  155.  
  156.     def DrawCell(self, root, userdata, obj, col, drawinfo, bgColor):
  157.         """
  158.        Draw into a Cell, only called for column of type LV_USER
  159.        """
  160.         if col == ID_OTHER:
  161.             name = obj.otherData
  162.             geUserArea = drawinfo["frame"]
  163.             w = geUserArea.DrawGetTextWidth(name)
  164.             h = geUserArea.DrawGetFontHeight()
  165.             xpos = drawinfo["xpos"]
  166.             ypos = drawinfo["ypos"] + drawinfo["height"]
  167.             drawinfo["frame"].DrawText(name, xpos, ypos - h * 1.1)
  168.  
  169.     def DoubleClick(self, root, userdata, obj, col, mouseinfo):
  170.         """
  171.        Called when the user double-clicks on an entry in the TreeView.
  172.  
  173.        Returns:
  174.          (bool): True if the double-click was handled, False if the
  175.            default action should kick in. The default action will invoke
  176.            the rename procedure for the object, causing `SetName()` to be
  177.            called.
  178.        """
  179.         c4d.gui.MessageDialog("You clicked on " + str(obj))
  180.         return True
  181.  
  182.     def DeletePressed(self, root, userdata):
  183.         "Called when a delete event is received."
  184.         for tex in reversed(self.listOfTexture):
  185.             if tex.IsSelected:
  186.                 self.listOfTexture.remove(tex)
  187.  
  188. class TestDialog(c4d.gui.GeDialog):
  189.     _treegui = None # Our CustomGui TreeView
  190.     _listView = ListView() # Our Instance of c4d.gui.TreeViewFunctions
  191.  
  192.     def CreateLayout(self):
  193.         # Create the TreeView GUI.
  194.         customgui = c4d.BaseContainer()
  195.         customgui.SetBool(c4d.TREEVIEW_BORDER, c4d.BORDER_THIN_IN)
  196.         customgui.SetBool(c4d.TREEVIEW_HAS_HEADER, True) # True if the tree view may have a header line.
  197.         customgui.SetBool(c4d.TREEVIEW_HIDE_LINES, False) # True if no lines should be drawn.
  198.         customgui.SetBool(c4d.TREEVIEW_MOVE_COLUMN, True) # True if the user can move the columns.
  199.         customgui.SetBool(c4d.TREEVIEW_RESIZE_HEADER, True) # True if the column width can be changed by the user.
  200.         customgui.SetBool(c4d.TREEVIEW_FIXED_LAYOUT, True) # True if all lines have the same height.
  201.         customgui.SetBool(c4d.TREEVIEW_ALTERNATE_BG, True) # Alternate background per line.
  202.         customgui.SetBool(c4d.TREEVIEW_CURSORKEYS, True) # True if cursor keys should be processed.
  203.         customgui.SetBool(c4d.TREEVIEW_NOENTERRENAME, False) # Suppresses the rename popup when the user presses enter.
  204.  
  205.         self._treegui = self.AddCustomGui( 1000, c4d.CUSTOMGUI_TREEVIEW, "", c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT, 300, 300, customgui)
  206.         if not self._treegui:
  207.             print "[ERROR]: Could not create TreeView"
  208.             return False
  209.  
  210.         self.AddButton(1001, c4d.BFH_CENTER, name="Add")
  211.         return True
  212.  
  213.     def InitValues(self):
  214.         # Initialize the column layout for the TreeView.
  215.         layout = c4d.BaseContainer()
  216.         layout.SetLong(ID_CHECKBOX, c4d.LV_CHECKBOX)
  217.         layout.SetLong(ID_NAME, c4d.LV_TREE)
  218.         layout.SetLong(ID_OTHER, c4d.LV_USER)
  219.         self._treegui.SetLayout(3, layout)
  220.  
  221.         # Set the header titles.
  222.         self._treegui.SetHeaderText(ID_CHECKBOX, "Check")
  223.         self._treegui.SetHeaderText(ID_NAME, "Name")
  224.         self._treegui.SetHeaderText(ID_OTHER, "Other")
  225.         self._treegui.Refresh()
  226.  
  227.         # Set TreeViewFunctions instance used by our CUSTOMGUI_TREEVIEW
  228.         self._treegui.SetRoot(self._treegui, self._listView, None)
  229.         return True
  230.  
  231.     def Command(self, id, msg):
  232.         # Click on button
  233.         if id == 1001:
  234.             # Add data to our DataStructure (ListView)
  235.             newID = len(self._listView.listOfTexture) + 1
  236.             tex = TextureObject("T{}".format(newID))
  237.             self._listView.listOfTexture.append(tex)
  238.  
  239.             # Refresh the TreeView
  240.             self._treegui.Refresh()
  241.  
  242.         return True
  243.  
  244.  
  245. class MenuCommand(c4d.plugins.CommandData):
  246.     dialog = None
  247.  
  248.     def Execute(self, doc):
  249.         if self.dialog is None:
  250.             self.dialog = TestDialog()
  251.         return self.dialog.Open(c4d.DLG_TYPE_ASYNC, PLUGIN_ID, defaulth=600, defaultw=600)
  252.  
  253.     def RestoreLayout(self, sec_ref):
  254.         if self.dialog is None:
  255.             self.dialog = TestDialog()
  256.         return self.dialog.Restore(PLUGIN_ID, secret=sec_ref)
  257.  
  258.  
  259. def main():
  260.     c4d.plugins.RegisterCommandPlugin(
  261.         PLUGIN_ID, "Python TreeView Example", 0, None, "Python TreeView Example", MenuCommand())
  262.  
  263.  
  264. if __name__ == "__main__":
  265.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top