Skip to content

Instantly share code, notes, and snippets.

@theawesomecoder61
Last active August 10, 2021 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theawesomecoder61/0bcf34f9583ad2d2c74fd2bcefaf6af9 to your computer and use it in GitHub Desktop.
Save theawesomecoder61/0bcf34f9583ad2d2c74fd2bcefaf6af9 to your computer and use it in GitHub Desktop.
compare_hierachies.py: a Cinema 4D script with GUI that compares two hierarchies by name
# compare hierarchies by pineapples721
# tested on C4D R21, should work on >= R19
import c4d
from c4d import gui
dialog = None
def PickSessionEnd(self, active, multi):
global dialog
if dialog.hier == "a":
dialog.ha = active[0]
dialog.WriteLog("Set root of hier. A: {0}".format(active[0].GetName()))
if dialog.hier == "b":
dialog.hb = active[0]
dialog.WriteLog("Set root of hier. B: {0}".format(active[0].GetName()))
doc.StopPickSession(False)
class CHDialog(gui.GeDialog):
BTN_SELECTA = 1000
BTN_SELECTB = 1001
BTN_COMPARE = 1002
BTN_CLEAR = 1003
MLE_RESULTS = 1004
def CreateLayout(self):
self.hier = ""
self.ha = None
self.hb = None
self.log = ""
self.SetTitle("Compare hierarchies")
self.AddStaticText(0, c4d.BFH_LEFT, 0, 0, "Compare hierarchies", 0)
self.AddButton(self.BTN_SELECTA, c4d.BFH_SCALEFIT, 120, 20, "Select hier. A")
self.AddButton(self.BTN_SELECTB, c4d.BFH_SCALEFIT, 120, 20, "Select hier. B")
self.AddButton(self.BTN_COMPARE, c4d.BFH_SCALEFIT, 120, 20, "Compare them")
self.AddButton(self.BTN_CLEAR, c4d.BFH_SCALEFIT, 120, 12, "Clear log")
self.AddMultiLineEditText(self.MLE_RESULTS, c4d.BFH_SCALEFIT, 200, 190)
self.AddStaticText(0, c4d.BFH_SCALEFIT, 0, 0, "by pineapples721", 0)
return True
def Command(self, id, msg):
if id == self.BTN_SELECTA:
self.hier = "a"
doc.StartPickSession(PickSessionEnd, False)
if id == self.BTN_SELECTB:
self.hier = "b"
doc.StartPickSession(PickSessionEnd, False)
if id == self.BTN_COMPARE:
self.Compare()
if id == self.BTN_CLEAR:
self.ClearLog()
return True
def IterateHier(self, h, objs):
for o in h.GetChildren():
if o.GetDown() is not None:
self.IterateHier(o, objs)
objs.append(o.GetName())
def Compare(self):
if self.ha is not None and self.hb is not None:
haObjs = []
hbObjs = []
self.IterateHier(self.ha, haObjs)
self.IterateHier(self.hb, hbObjs)
self.WriteLog("Hier. A: {0}\t|\tHier. B: {1}\n".format(len(haObjs), len(hbObjs)))
self.WriteLog("Unique objects in hier. A:")
for o in haObjs:
if o not in hbObjs:
self.WriteLog("\t" + o)
self.WriteLog("Unique objects in hier. B:")
for o in hbObjs:
if o not in haObjs:
self.WriteLog("\t" + o)
self.SetString(self.MLE_RESULTS, self.log)
def WriteLog(self, txt):
self.log = self.log + txt + "\n"
self.SetString(self.MLE_RESULTS, self.log)
def ClearLog(self):
self.log = ""
self.SetString(self.MLE_RESULTS, self.log)
if __name__ == "__main__":
global dialog
dialog = CHDialog()
dialog.Open(dlgtype=c4d.DLG_TYPE_ASYNC, pluginid=1645293, defaultw=200, defaulth=300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment