Notifications
Clear all

[Closed] Creating and Naming Layers

I’m a maxscript recorder kind a guy … I usually just do the tasks and copy and paste the functions into a tiny script. This time, however, I realized that my actions in the layers area of max revealed no code. Anyone know what I need to do to:

  1. Check to see if a layer already exists with a particular name
  2. If not, create layer with the specifc name

I am attempting to create a script that will hopefully save me some time. I need to lay down some objects and name them. Afterwards, I would like to collect all files that have a particular name (such as foo_01, foo_02, etc) to be scooped up and put on a Layer. Furthermore, I would like this Layer to have a specific name.

Any help would be appreciated – I seem to only get so far with the maxscript help.

4 Replies

I’m still having troubles making the Maxscript reference meaningful – but with some help the answer was using the following command:

LayerManager.newLayerFromName

Now I”m looking for a way to select objects and place it onto this newly created layer. While I feel like the scripting is powerful, I’m having a lot of troubles making the most out of this reference. Any tips or suggestions would be helpful.

Jason

Here you go:

function addNodesToLayer str_layerName inputNodes =
   	(
   	if str_layerName == undefined or inputNodes == undefined then return undefined;
   	if (classof inputNodes) != array then inputNodes = #(inputNodes);
   	
   	local inputLayer = (LayerManager.getLayerFromName str_layerName);
   	
   	for eachObj in inputNodes do
   		(
   		inputLayer.addnode eachObj;
   		);
   	);

And use the code:

LayerManager.newLayerFromName "testLayer";
   addNodesToLayer "testLayer" (selection as array);

To create a new layer called ‘testLayer’ and add the current selection to that layer.

Hey Impus,

Thanks a lot, your code works. I’m wondering if I could ask a few questions though to better understand what is going on.

You are creating a function to make it easier to call the command to add nodes to a particular layer, but I am unsure why an array is needed. Why can’t I break this down to the following without creating an error.

LayerName = “test”;
LayerManager.newLayerFromName LayerName;
LayerName.addnode $box01;

As long as Box01 exists in my scene, why doesn’t the above work? Instead I receive “– Unknown property: “AddNode” in “test””

Sorry if this is overly simplistic, but I want to better understand what I’m doing for “the next time.”

The array is used so you can add 1 or more objects at once. It’s not efficient to keep recalling the function which then re-finds the layer and adds the object. It’s better to find the layer once then add each node after. It’s basically a problem where you know you have to iterate but the issue is where you iterate. Generally it’s best to iterate in a position where you can lessen the number of commands used in the iteration, i.e. setup the variables (in this case the layer) then iterate on the objects.

And to address the problem with your script – it seems you are missing the context of the ‘layerManager’ structure. I’ll step through what you are doing:

 LayerName = "test";
   OK, here you are establishing a variable called 'LayerName' with the string value of "test".
LayerManager.newLayerFromName LayerName;
    Here you are creating a new layer from a string using the variable 'LayerName' and it's value
   "test" using the function 'LayerManager.newLayerFromName' (technically it's actually a
   structure 'LayerManager' which contains a function 'newLayerFromName' which you are using) 
      This is equal to doing:
   LayerManager.newLayerFromName "test";
LayerName.addnode $box01;
    Here you are trying to apply the method 'addnode' to the structure 'LayerName'. LayerName
   it's just a string variable and doesn't contain any methods (functions) to do with layers.
    So as a solution you should be doing:
 LayerName = "test";
      NewLayerObject = (LayerManager.newLayerFromName LayerName);
      NewLayerObject.addnode $box01;
  This broken down is as follows:
 LayerName = "test";
        Same as above.
NewLayerObject = (LayerManager.newLayerFromName LayerName);
    Here we are taking the 'returned' layer object, it's basically an internal reference to the
   layer, and setting it to be the value of the variable 'NewLayerObject'.
NewLayerObject.addnode $box01;
        And now we are able to use this link to the layer to execute methods and modify
   properties.
                                                                                   Now this all may go completely over your head but it hopefully will give you some insight into how to use systems like this. Basically this is the main architecture of a 'object' oriented language. I'm sure there is heaps of information on the web about how to understand such systems in a generic sense.