[MaxScript] Clone, Weld, and Skin Wrap

I’m working on a script where you…

  1. Select any objects in your scene - say $head, $body, and $shirt
on buttonName pressed do
  1. Script will clone the selected objects, then attach and weld the clones
  2. Apply a Skin Wrap modifier to the unified clone
  3. Add the original objects to the Skin Wrap’s Control Objects list

What I’m missing is #4. I don’t know how to store those objects so I can access that list for a later function. I think I need to getCurrentSelection () and pass that into another variable, but I’m not sure how to do that and get Max to “remember” the objects I had selected at the beginning.

Here’s what I have so far that works:


on weldGeo pressed do
	(
		undo off
			(
				maxOps.cloneNodes $ cloneType:#copy newNodes:&nnl
				while selection.count > 1 do
				(
					selcount = selection.count
					for i = selcount to 2 by -2 do
						
					(
						macros.run "Modifier Stack" "Convert_to_Poly"
						polyop.attach selection[i] selection[i-1]
					)
				)
				update selection[1]
				$.name = "geo_welded"
			)
		subobjectLevel = 3
		max select all
		macros.run "Editable Polygon Object" "EPoly_Convert_Sel_To_Vertex"
		$.weldThreshold = 0.01
		$.EditablePoly.weldFlaggedVertices ()
		subobjectLevel = 0
		$.wirecolor = color (random 0 255) (random 0 255) (random 0 255)
		--APPLY SKIN WRAP MODIFIER
		max modify mode
		select $geo_welded
		modPanel.addModToSelection (Skin_Wrap engine:0 weightAllVerts:true) ui:on
	)

In the beginning of your button event, you can define an array to store all of your currently selected objects in. Here’s a tiny test script to demonstrate this functionality.


fn selTest = 
(
	local objs = getCurrentSelection() 
	clearSelection()
	print objs
	
)
selTest()

Also, you probably should not be cloning nodes within an “undo off” bracket. Here’s what the all-mighty documentation says on the topic:

WARNING!

You may not use maxOps.cloneNodes inside an undo off context. Doing so will crash 3ds Max when using the Undo function!

Hope this helps and good luck!

Thanks. I’m learning here, so please bear with me. If that’s really storing the selected objects in an array, I’d think I should be able to run that then type “select objs” in the listener and those same objects should get selected again. That isn’t happening.

The keyword “local” in front of the var “objs” tells MaxScript that the var is only available within the scope of the function. Once the function has finished running, the local variable is effectively removed from play.

I.e, it can’t be accessed at a global, listener level.

Ah, thanks that makes sense. I ended up using


global selObjs = selection as array

I would not use a global.
Just forward declare the variable in your rollout scope and then define it in the event.


(


rollout rolName ""
(
local somevar

button btName ""

on btName pressed do
(
somevar = getcurrentselection()
)


You can also forward declare the variable outside the rollout scope so you can use the variable in multiple rollouts/functions/…