Notifications
Clear all

[Closed] progressive morpher via maxscript…?

hey guys, I’m trying something in maxscript but i don’t even know how to approach it, let’s see if i can explain what I’m trying to do…:

let’s say I have object “A” with a morpher modifier on it.
now I have 2 more objects “B_100” and “B_050”

how can I load those 2 objects into the morpher automatically, being B_100 in a channel, and B_050 in the same channel as a progressive morpher with the target% at 50? so far I managed to load them but each of them in its own channel when what I want is load the objects with the same name in the same channel and adjust their target% using the sufix i’m giving in the name…

any help really appreciated! thanks!

Alex

2 Replies

You should look into morpher’s WM3_AddProgressiveMorphNode and WM3_SetProgressiveMorphWeight for how to add progressive morphs and set their weights.

If you follow a strict naming rule of having the percentage digit the last element in the name, you could get the weight from the name very simply:

exampleName = "someNameMT_066"
filteredNameArr = filterString exampleName "_"
MTweight = filteredNameArr[filteredNameArr.count] as float

To figure out which names end with a number, I’d probably do something dirty, like test MTweight above for undefined. Nonsense like “potato” as float returns undefined. Perhaps someone else has a more elegant solution to this bit.

Afterwards you can figure out which objects have similar names by using matchPattern.

I thought I remember something being weird about progressive morphs, so later I came back to test it a little. Yeah, there’s a catch… You can’t add MTs to a channel using WM3_AddProgressiveMorphNode if you don’t have a target there already. So that part needs a workaround.

Here’s a little quick and dirty code scrap of one way to do it:
(select the morph object(s) and morph targets, then run the script)

undo on 
(
	local morpherArr = #()
	local directMTarr = #() 
	local progMTarr = #() 
	local progMTarr_nameArr = #()
	
	morpherArr = for obj in selection where obj.modifiers[#morpher] != undefined collect obj.modifiers[#morpher]
	deselect (for obj in selection where obj.modifiers[#morpher] != undefined collect obj)	-- deselect objects with morpher; for convenience
	
	-- dividing direct and progressive MTs into different arrays
	local filteredNameArr, MTweight, nameSansNum
	for obj in selection do 
	(
		filteredNameArr = filterString obj.name "_ -"
		MTweight = undefined
		MTweight = filteredNameArr[filteredNameArr.count] as float
		if MTweight != undefined then 
			(
				-- getting the name without number and without last delimiter character:
				nameSansNum = substring obj.name 1 (obj.name.count - filteredNameArr[filteredNameArr.count].count - 1)
				append progMTarr #(obj, MTweight, nameSansNum)   -- two-dimensional array, containing objects, weights and names_minus_number
				appendifunique progMTarr_nameArr nameSansNum		-- for convenience, an array of unique names only
			)
			else append directMTarr obj
	)
	
	-- go through every collected morpher and add targets; no foolproofing included, will *mostly* silently fail if not valid
	for MP in morpherArr do
	(
		local id = 1	-- keeping track of which channel we're modifying
		
		-- first adding direct morh targets
		for mt in directMTarr do
		(
			WM3_MC_BuildFromNode MP id mt
			id += 1
		)
		
		-- ...now the progressive mess
		local isFirstPMT = true
		local tempFirstPMT
		for pmtName in progMTarr_nameArr do
		(
			isFirstPMT = true
			WM3_MC_SetName MP id pmtName
			for pmt in progMTarr where pmt[3] == pmtName do
			(
				if isFirstPMT then
				(
					-- WM3_AddProgressiveMorphNode doesn't work on empty channel
					-- can't set weight if one target exists, either, so:
					-- adding first MT the regular way... 
					WM3_MC_BuildFromNode MP id pmt[1]
					tempFirstPMT = pmt		-- ...storing the entry for later use...
					isFirstPMT = false
				)
				else
				(
					WM3_AddProgressiveMorphNode MP id pmt[1]
					WM3_SetProgressiveMorphWeight MP id pmt[1] pmt[2]
				)
			)
			-- ...returning to it after we're done with the others and setting the weight
			WM3_SetProgressiveMorphWeight MP id tempFirstPMT[1] tempFirstPMT[2] 
			-- (could skip this part with a sorted array and if there's always a 100% target)
			
			id += 1
		)
	)
)
	

(Obviously needs some fool-proofing, error checking, dividing neatly into functions, and changing the way morpher object and MT objects are designated (so it works when the MT has a morpher on it), but the basic idea is here, perhaps it’s of some help to you.)