Skip to main content

recreate_layers_from_3ds_to_c4d

import c4d
import json

def create_hierarchy(parent, data, doc):
for item in data:
name = item["name"] + "_grp"


# Check if the object already exists
obj = doc.SearchObject(name)

# If it does not exist, create a new null object
if obj is None:
obj = c4d.BaseObject(c4d.Onull)
obj.SetName(name)
doc.InsertObject(obj)

# If a parent is provided, set the parent of the new object
if parent:
obj.InsertUnder(parent)

# Recursively add children
if "children" in item and item["children"]:
create_hierarchy(obj, item["children"], doc)

def create_layers():
# Path to the JSON file
param_path = r"C:/users/BT/Desktop/layers_hierarchy.json"

with open(param_path, "r") as read_file:
data = json.load(read_file)

# Get the active document
doc = c4d.documents.GetActiveDocument()

# Start an undo block
doc.StartUndo()

try:
# Create the hierarchy
create_hierarchy(None, data, doc)
finally:
# End the undo block
doc.EndUndo()

# Refresh Cinema 4D to show the new objects
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()

# Execute the main function
if __name__ == '__main__':
reparent_objects()
create_layers()