Solved Treeview adding a new item as an child

I have seen the excellent post of Maxime about creating a Treeview.

There is an Add button, but now I want to add it as a child.

I added a Insert Under button with following code in Command().

        if id == 1002:
            # Insert Under
            tex = TextureObject("Inserted under first item.")
            first = self._listView.GetFirst(self._treegui, self._listView)
            #InsertObject(self, root, userdata, obj, dragtype, dragobject, insertmode, bCopy):
            self._listView.InsertObject(self._treegui, self._listView, first, c4d.DRAGTYPE_FILENAME_OTHER, tex, c4d.INSERT_UNDER, True)
            # Refresh the TreeView

And in class ListView(c4d.gui.TreeViewFunctions) I added

    def GetFirst(self, root, userdata):
        rValue = None if not self.listOfTexture else self.listOfTexture[0]
        return rValue

    def InsertObject(self, root, userdata, obj, dragtype, dragobject, insertmode, bCopy):
        return True

But nothing is inserted.
I think it is because of InsertObject(), because nothing is inserted in the _listView.
So, how to insert it as a child?


Hi Pim thanks for reaching out us.

With regard to your request, rather than implementing TreeViewFunctions::InsertObject() you should take care of implementing a TreeViewFunctions::GetDown() and also to redesign the data type being responsible to store the TreeView items and their children.

In the case presented by @m_adam a simple list was used and this was sufficient for the scope of representing a one-level tree. Also accessing the next/prev items was done by searching for the index of a given item in the list which turned to be quick and slick.

Last but not least it's also relevant to implement a TreeViewFunctions::Open()and TreeViewFunctions::IsOpened() in order to properly manage the folding/unfolding functionality otherwise your tree might appear always folded

Hope these few notes can lead you through the right direction.

Best, Riccardo

I'd like to add that using GeListNode and GeListHead is convenient since they already come with all functions you need.

@mp5gosu this completely makes sense and it's indeed a valuable recommendation but I wasn't sure about Pim's requirement here on deriving from Cinema 4D base classes.

Cheers, R