import c4d
import json
def create_hierarchy(parent, data, doc):
for item in data:
name = item["name"] + "_grp"
obj = doc.SearchObject(name)
if obj is None:
obj = c4d.BaseObject(c4d.Onull)
obj.SetName(name)
doc.InsertObject(obj)
if parent:
obj.InsertUnder(parent)
if "children" in item and item["children"]:
create_hierarchy(obj, item["children"], doc)
def create_layers():
param_path = r"C:/users/BT/Desktop/layers_hierarchy.json"
with open(param_path, "r") as read_file:
data = json.load(read_file)
doc = c4d.documents.GetActiveDocument()
doc.StartUndo()
try:
create_hierarchy(None, data, doc)
finally:
doc.EndUndo()
c4d.EventAdd()
def reparent_objects():
param_path = r"C:/users/BT/Desktop/layers_hierarchy_with_geo.json"
doc = c4d.documents.GetActiveDocument()
with open(param_path, "r") as read_file:
data = json.load(read_file)
doc.StartUndo()
for d, k in data.items():
print ("group_name", d)
null_obj = c4d.BaseObject(c4d.Onull)
null_obj.SetName(d + "_grp")
doc.InsertObject(null_obj)
for obj_name in k[0]:
scene_obj = doc.SearchObject(obj_name)
result = False
if scene_obj:
result = True
scene_obj.InsertUnder(null_obj)
if not result:
print (obj_name, "does not exist")
print ("=====")
doc.EndUndo()
if __name__ == '__main__':
reparent_objects()
create_layers()