ScriptSpot

Forum topic · General Scripting

Apply uvwmapping to multiple object problem

By jackieteh · 2016-11-25

Description

Hi all, i want to make a mxscript that can apply box uvw mapping to multiple objects in my scene, the code i use below:


for i in selection do
				(
					addModifier i (uvwmap())
					i.modifiers[#UVW_Map].maptype = 4
					i.modifiers[#UVW_Map].length = spn1.value
					i.modifiers[#UVW_Map].width = spn2.value
					i.modifiers[#UVW_Map].height = spn3.value
				)

it work fine for one object selected, but not working for more object selection, especially got instance object in the selction, it add more than one uvwmapping modifier on one object, please help! how can i apply just one uvwmapping to one object only even it is instance object with my script?

i hope you understand with my poor english.

Best regards,
Jackieteh

Comments (7)

jackieteh · 2016-12-06

Hi miauu, the script work on the following situation:

1) select several instances objects
2) select several instances and not instances objects

but not working on

1) if i group several instances and not instances objects
2) if i group several instances objects only
3) if i group several not instance objects only

so the script only working on selected objects but not working on a group objects.

.

miauu · 2016-12-06

Try to optimize the script in the part where the instanced objets are processed.




(
	function GetUiqueNodes nodes: = 
	(
 
		local handles = #{}
		for n in nodes where ( (isGroupHead n == false) and (isOpenGroupHead n == false) ) collect
		(
			if (not handles[GetHandleByAnim n]) do
			(
				InstanceMgr.GetInstances n &inst
				for i in inst do handles[GetHandleByAnim i] = on
				n
			)
		)
	)
	selObjsArr = GetUiqueNodes nodes:(selection as array)
	
	for i in selObjsArr where i != undefined do
	(
 
		addModifier i (uvwmap())
		i.modifiers[#UVW_Map].maptype = 4
		i.modifiers[#UVW_Map].length = 100
		i.modifiers[#UVW_Map].width = 100
		i.modifiers[#UVW_Map].height = 100
	)
)

Alternatif solution

fajar · 2016-12-05



fn addUVWModifiers sel:selection l:100 w:100 h:100 =
(
	local jn = join, addMod = addModifier, find = findItem, getInstance = InstanceMgr.GetInstances
	local uvMap=uvwmap maptype:4 length:l width:w height:h
	local allObjs=#()
	with redraw off 
	(
		for i in sel where find allObjs i == 0 do 
		(
		   getInstance  i &inst
		   jn allObjs inst
		   addMod i uvMap
		)
		free allObjs
	)
OK;
	
)
/* usage 
addUVWModifiers ()
-- or 
addUVWModifiers l:spn1.value w:spn2.value h:spn3.value
*/

jackieteh · 2016-12-06

Hi fajar,

i will try your script and post the result here later, thank you for your help :)

.

miauu · 2016-11-28


(
	function GetUiqueNodes nodes: = 
	(

		local handles = #{}
		for n in nodes where not handles[GetHandleByAnim n] collect
		(
			InstanceMgr.GetInstances n &inst
			for i in inst do handles[GetHandleByAnim i] = on
			n
		)
	)
	
	selObjsArr = GetUiqueNodes nodes:(selection as array)
	for i in selObjsArr do
	(
		
		addModifier i (uvwmap())
		i.modifiers[#UVW_Map].maptype = 4
		i.modifiers[#UVW_Map].length = spn1.value
		i.modifiers[#UVW_Map].width = spn2.value
		i.modifiers[#UVW_Map].height = spn3.value
	)
)

jackieteh · 2016-12-01

Hi miauu, it work better than what i expected.
i wonder why my first don't work as what i want?
and what is the function in your script work for?

.

miauu · 2016-12-01

Your code not works properly, beause you use:

for o in selection do

selection

contains alls elected objects and when you select another object the

selection

is changed.
When you apply modifier to an object this object become selected and the for loop stops working, because

selection

contains the object to which the modifier was applied.

The function is to get only one object from all instances of this object. So, when you add the modifier to this object, all instances will receive the same modifier. This way the instances do not have several modifiers applied one after another.