/

Maya – Import all references

Here we will import all references starting with the top most nodes, we then collect all nested children and continue. It is recommended you run the “load_all_references” before this to make sure everything will indeed be imported.

import logging
from maya import cmds

# Would be a good idea to use the "load_all_references" from my other post before this.

# Get all references top nodes. Use set() here to remove duplicates.
all_references = list(
	set(
		[
			cmds.referenceQuery(x, referenceNode=True, topReference=True)
			for x in cmds.ls(references=True)
		]
	)
)
# Start importing
logging.info("Starting to import asset references..")
for reference in all_references:
	# Can't import a reference that is not loaded.
	if cmds.referenceQuery(reference, isLoaded=True):
		reference_file_name = cmds.referenceQuery(reference, filename=True)
		logging.info("Importing {}...".format(reference))
		cmds.file(reference_file_name, importReference=True)
		# Check for nests
		potential_refs = list(
			set(
				[
					cmds.referenceQuery(x, referenceNode=True, topReference=True)
					for x in cmds.ls(references=True)
				]
			)
		)
		if potential_refs:
			for nested_ref in potential_refs:
				if nested_ref not in all_references:
					logging.info("Adding nested ref {} to top nodes for import.".format(nested_ref))
					all_references.append(nested_ref)

logging.info("All asset references imported!")

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *