Skip to content

Instantly share code, notes, and snippets.

@mp5gosu
Last active Nov 26, 2019
Embed
What would you like to do?
import c4d
from c4d import gui
from c4d import plugins
from os import path
PLUGIN_ID = 1000001 # Test plugin id
class ListItem:
"""
class ListItem:
A simple class for storing a single string
Implements equality operator to be able to easily compare with Filename string BEFORE instantiation,
see InsertObject()
"""
def __init__(self, filename):
self.__filename = filename # The simple String/Filename
@property
def filename(self):
return self.__filename
def __eq__(self, other):
return self.__filename == other # compares the Filename/String of an instance with a given Filename/String
class Tvf(gui.TreeViewFunctions):
def GetFirst(self, root, userdata): # Bare minimum, neccessary to insert ListItems
if not root:
return None
return root[0]
def GetNext(self, root, userdata, obj): # Bare minimum, neccessary to insert ListItems
"""
Get next or None
Args:
root (list): The list
obj (ListItem): The ListItem to get the next element from the list
Returns: None or next ListItem
"""
if not root:
return None
next_idx = root.index(obj) + 1
return root[next_idx] if next_idx < len(root) else None
def AcceptDragObject(self, root, userdata, obj, dragtype, dragobject):
"""
Checks if the dragged object is allowed.
Returns NOTOK, to prevent Cinema 4Ds default action when dragobject is not accepted
"""
if dragtype == c4d.DRAGTYPE_FILENAME_OTHER:
return c4d.INSERT_AFTER, False
return c4d.NOTOK, False
def InsertObject(self, root, userdata, obj, dragtype, dragobject, insertmode, bCopy):
"""
Creates a ListItem and initializes filename property, then adds it to the list (root)
"""
root.append(ListItem(dragobject))
def GetName(self, root, userdata, obj):
return obj.filename
class TestDialog(gui.GeDialog):
IDD_TREEVIEW = 10000
TV_FILENAME = 11001
def __init__(self, items):
self.__items = items # Hold ref to the Filename List
self.__treegui = None # the TreeViewCustomGui
self.__tv = Tvf() # instantiate TreeViewFunctions
def CreateLayout(self):
settings = c4d.BaseContainer()
settings.SetBool(c4d.TREEVIEW_OUTSIDE_DROP, True) # Allows dragging files from outside Cinema 4D
settings.SetBool(c4d.TREEVIEW_HAS_HEADER, True)
self.__treegui = self.AddCustomGui(self.IDD_TREEVIEW, c4d.CUSTOMGUI_TREEVIEW, "",
c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT,
300,
300,
settings)
return True
def InitValues(self):
layout_bc = c4d.BaseContainer()
layout_bc.SetLong(self.TV_FILENAME, c4d.LV_TREE)
self.__treegui.SetLayout(1, layout_bc)
self.__treegui.SetHeaderText(self.TV_FILENAME, "Filename")
self.__treegui.Refresh()
self.__treegui.SetRoot(self.__items, self.__tv, None)
return True
class TestCommand(plugins.CommandData):
def __init__(self):
self.__dlg = None
self.__items = list()
def Register(self):
return plugins.RegisterCommandPlugin(PLUGIN_ID, "Test-Plugin", 0, None, None, self)
def Execute(self, doc):
if self.__dlg is None:
self.__dlg = TestDialog(self.__items)
return self.__dlg.Open(dlgtype=c4d.DLG_TYPE_ASYNC, pluginid=PLUGIN_ID, defaulth=400, defaultw=400)
def RestoreLayout(self, sec_ref):
if self.__dlg is None:
self.__dlg = TestDialog(self.__items)
return self.__dlg.Restore(pluginid=PLUGIN_ID, secret=sec_ref)
if __name__ == '__main__':
TestCommand().Register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment