Add a node to a layer using MaxPlus

Add a node to a layer using MaxPlus

Anonymous
Not applicable
1,764 Views
4 Replies
Message 1 of 5

Add a node to a layer using MaxPlus

Anonymous
Not applicable

Hi,
I figured out how to create a layer, make it current and that works fine but I would like to be able to add a node to a layer and I can't find a function in either INode or LayerManager for that, any clue ?

Thanks !
Chris

0 Likes
Accepted solutions (1)
1,765 Views
4 Replies
Replies (4)
Message 2 of 5

malcomarmstrong
Advocate
Advocate

Hi Chris, MaxPlus can be a bit tricky and wI am not making any assumptions about your skill level as I have been scripting a long time and get a little confused with some of the Python code in Max. below is an example of how to create some layers and add a selection of nodes to the current layer. I believe that if you look at and adapt what I have shown, you will be able to broaden its scope. Hope it works for you.

import os, sys
import MaxPlus as mxp

# List of layers to make
layerName_list = ["Rig", "ctrls", "geom"]

# For loop to make layers from list
for i in range(len(layerName_list)):
	new_Layer = mxp.LayerManager.CreateLayer(layerName_list[i])
	
# Print number of layers
print mxp.LayerManager.GetNumLayers()

# Set the current layer to one from the list
mxp.LayerManager.SetCurrentLayer("Rig")

# Get the current layer
curLay = mxp.LayerManager.GetCurrentLayer()

# Get current layer name
curLayName = mxp.Layer.GetName(curLay)

# Get Current selection
currentSel = MaxPlus.SelectionManager.Nodes

# For loop to add the selected objects to the layer
for node in currentSel:
	print node.Name
	mxp.Layer.AddToLayer(curLay, node)
0 Likes
Message 3 of 5

malcomarmstrong
Advocate
Advocate

No, wait, something is not quite right with the selection. I will post back.

 

Ok. So the code WAS ok, I had added a check for the validity of the selection and had it the wrong way round. Code below should be fine. If i get a chance, I will try and wrap it in a function, but it might be a good idea to try that yourself.

import os, sys
import MaxPlus as mxp

# List of layers to make
layerName_list = ["Rig", "ctrls", "geom"]

# For loop to make layers from list
for i in range(len(layerName_list)):
	new_Layer = mxp.LayerManager.CreateLayer(layerName_list[i])
	
# Print number of layers
print mxp.LayerManager.GetNumLayers()

# Set the current layer to one from the list
mxp.LayerManager.SetCurrentLayer("Rig")

# Get the current layer
curLay = mxp.LayerManager.GetCurrentLayer()

# Get current layer name
curLayName = mxp.Layer.GetName(curLay)

# Get Current selection
currentSel = MaxPlus.SelectionManager.Nodes

# For loop to add the selected objects to the layer
if currentSel:
	for node in currentSel:
		print node.Name
		mxp.Layer.AddToLayer(curLay, node)
else:
	print "no current selection"
0 Likes
Message 4 of 5

malcomarmstrong
Advocate
Advocate
Accepted solution

As a mini project, I thought I would write a function that would allow a user to get a selected set of nodes and add them to the current OR a new layer. I know this can be done visually in the layer manager and its probably overkill in that context, but the code here could be useful if you are creating large object sets and then need to put them on a layer as part of a process in a class.

Feel free to comment and critique it, I have put some notes at the top of the pasted code block. It has some scope for improvement, both in its execution and in its format, but the general idea is sound. (I think!) Smiley Wink

 

"""
Written by: Malcom Armstrong
Date: 08/05/2018

Description:
This will allow a user to add a set of selected nodes to the current layer (curLayer=True) or a new layer (newLayer=True, newLayerName="SomethingOriginal)"

Usage:

To add selected nodes to new layer: addSelectedToLayer(theObjs, curLayer=False, newLayer=True, newLayerName="myNewLayer")
To add nodes to current layer: addSelectedToLayer(theObjs, curLayer=True, newLayer=False, newLayerName="")

Current limitations:
No check for existing layer name
Only prints warnings
Format could probably be more 'Pythonc'
try ecxept?
clunky way to get the selection count, probably need selection cast to maxPlus.ObjectList() and then append the selection
"""

import os, sys
import MaxPlus as mxp

def addSelectedToLayer(objectsToAdd, curLayer, newLayer, newLayerName):
	objectsCountList = [i for i in MaxPlus.SelectionManager.Nodes]
	if len(objectsCountList) > 0:
		if curLayer and not newLayer :
			cur_layer = mxp.LayerManager.GetCurrentLayer()
			for node in objectsToAdd:
				print "Adding {} to the current layer".format(node.Name)
				mxp.Layer.AddToLayer(cur_layer, node)		
		elif not curLayer and newLayer:
			print "Creating a new layer and adding selected nodes to it"		
			if newLayerName != "defaultLayerName":
				print "New layer name {} is set and is valid as the value for the new layer".format(newLayerName)
				new_Layer = mxp.LayerManager.CreateLayer(newLayerName)
				for node in objectsToAdd:
					print "Adding {} to the new layer: {}".format(node.Name, newLayerName)
					mxp.Layer.AddToLayer(new_Layer, node)
			else:
				print "New layer name is required"			
		elif curLayer and newLayer:
			print "Not possible to add nodes to current and new layer"
		else:
			print "At least one argument (curLayer or newLayer) needs to be passed as True"
	else:
		print "Nothing is currently selected" 
		
theObjs = mxp.SelectionManager.Nodes
addSelectedToLayer(theObjs, curLayer=False, newLayer=True, newLayerName="myNewLayer")
Message 5 of 5

Anonymous
Not applicable

Thanks Malcom ! That's way more that I wanted 🙂