Skip to main content

3DS to Maya Layers

Previously, 3DS Python Script to JSON external file but 3DS revised their API (i.e. removed the MaxPlus library). So workflow is:

  1. exporting FBX.
  2. importing FBX to Maya. The layers info are retained.
  3. importing FBX to C4D. The layers info are NOT retained.
  4. Rebuild the Layers Info to a Grouping in the Outliner.
# CREATE GROUPING FROM LAYERS
import pymel.core as pm

def create_groups_for_display_layers():
# Get all display layers in the scene
display_layers = pm.ls(type='displayLayer')

# Filter out the default display layer (named 'defaultLayer')
display_layers = [layer for layer in display_layers if layer != pm.PyNode('defaultLayer')]

# Create a group for each display layer and parent its members
for layer in display_layers:
# Create a group with a name similar to the display layer
group_name = layer.name() + "_grp"
if not pm.objExists(group_name):
pm.group(em=True, name=group_name)
print(f"Created group: {group_name}")

# Get members of the display layer
members = layer.listMembers()

if members:
# Parent members to the corresponding group
for member in members:
pm.parent(member, group_name)
print(f"Parented members of {layer} to {group_name}")

# Run the function
create_groups_for_display_layers()
# REMOVE PREFIX
import pymel.core as pm

def rename_objects_with_prefix():
# Define the prefix to remove
prefix = "female_anatomy_20240522_"

# Get all objects in the scene
all_objects = pm.ls()

# Loop through each object and check if it has the specified prefix
for obj in all_objects:
if obj.name().startswith(prefix):
# Get the new name by removing the prefix
new_name = obj.name()[len(prefix):]

print(new_name)
print(obj.name())
obj.rename(new_name)
#break # Note: The 'break' statement will stop the loop after the first rename

# Run the function
rename_objects_with_prefix()
# SIMPLE REPLACE OF ASCII
import pymel.core as pm

def replace_string_in_object_names(old_string, new_string):
# Define the string to be replaced and the replacement string


# Get all objects in the scene
all_objects = pm.ls()

# Loop through each object and replace the string in its name
for obj in all_objects:
old_name = obj.name()
if old_string in old_name:
new_name = old_name.replace(old_string, new_string)
obj.rename(new_name)
print(f"Renamed {old_name} to {new_name}")

dirty_string_list = ["FBXASC032", "FBXASC045", "FBXASC040XIFBXASC041", "FBXASC040", "FBXASC041", "__", " "]

for dirty_string in dirty_string_list:
replace_string_in_object_names(dirty_string, "__")


# Run the function
old_string = "FBXASC032"
new_string = "_"
replace_string_in_object_names(old_string, new_string)

old_string = "FBXASC045"
new_string = "_"
replace_string_in_object_names(old_string, new_string)

old_string = "FBXASC040XIFBXASC041"
new_string = "_"
replace_string_in_object_names(old_string, new_string)

old_string = "FBXASC040"
new_string = "_"
replace_string_in_object_names(old_string, new_string)

old_string = "FBXASC041"
new_string = "_"
replace_string_in_object_names(old_string, new_string)

old_string = "__"
new_string = "_"
replace_string_in_object_names(old_string, new_string)

dirty_string_list = ["FBXASC032", "FBXASC045", "FBXASC040XIFBXASC041", "FBXASC040", "FBXASC041", "__", " "]

for dirty_string in dirty_string_list:
replace_string_in_object_names(dirty_string, "__")


def string_cleanup(string_to_process):
# Define the string to be replaced and the replacement string




# Get all objects in the scene
all_objects = pm.ls()

# Loop through each object and replace the string in its name
for obj in all_objects:
old_name = obj.name()

for dirty_string in dirty_string_list:
if dirty_string in old_name:
new_name = old_name.strip().replace(dirty_string, "_")
obj.rename(new_name)

import maya.cmds as cmds
import json

# The JSON data (you can replace this with a file read if needed)
json_data = '''
[
{
"name": "sciepro_female_anatomy",
"children": [
{
"name": "Urinary_System",
"children": []
},
{
"name": "Membranous_Labyrinth",
"children": []
},
{
"name": "Mucous_Membranes",
"children": []
},
{
"name": "Female_Body",
"children": [
{
"name": "Eye_Anatomy",
"children": []
},
{
"name": "Integumentary_System",
"children": [
{
"name": "hair_femaleOriginal",
"children": []
},
{
"name": "hair_femaleGlobal",
"children": []
},
{
"name": "hair_femaleBodyVariant",
"children": []
}
]
}
]
}
]
}
]
'''

# Function to create the hierarchy
def create_hierarchy(parent, data):
for item in data:
# Create an empty group (null object)
name = None

if cmds.objExists(item["name"])
name = item["name"]

else:
null = cmds.group(empty=True, name=item["name"])

# Parent the null object to the parent if a parent is provided
if parent:
cmds.parent(null, parent)

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

# Main function
def main():
# Parse the JSON data
#data = json.loads(json_data)
param_path = r"C:/users/BT/Desktop/layers_hierarchy.json"
with open(param_path, "r") as read_file:
data = json.load(read_file)

# Create the hierarchy
create_hierarchy(None, data)

# Run the main function
main()
import maya.cmds as cmds
import json

# Function to check if an object with the given name already exists under the specified parent
def check_existing_child(parent, name):
children = cmds.listRelatives(parent, children=True, fullPath=True) or []
for child in children:
if cmds.nodeType(child) == 'transform' and cmds.ls(child, shortNames=True)[0] == name:
return True
return False


# Function to create the hierarchy
def create_hierarchy(parent_obj, data):
for item in data:

# Check if the object already exists under the parent
if parent_obj and check_existing_child(parent_obj, item["name"]):
continue

# Create an empty group (null object)
null = cmds.group(empty=True, name=item["name"])

# Parent the null object to the parent if a parent is provided
if parent_obj:
cmds.parent(null, parent_obj)

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

# Main function
def main():
# Parse the JSON data
#data = json.loads(json_data)
param_path = r"C:/users/BT/Desktop/layers_hierarchy.json"
with open(param_path, "r") as read_file:
data = json.load(read_file)

# Create the hierarchy
create_hierarchy(None, data)

# Run the main function
main()