Apply uvwmapping to multiple object problem

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jackieteh's picture

Hi miauu, the script work on

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.

Regards,

Jackie Teh
website: https://www.sporadicstudio.com/

miauu's picture

.

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
	)
)
fajar's picture

Alternatif solution

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's picture

Hi fajar, i will try your

Hi fajar,

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

Regards,

Jackie Teh
website: https://www.sporadicstudio.com/

miauu's picture

.

(
	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's picture

Hi miauu, it work better than

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?

Regards,

Jackie Teh
website: https://www.sporadicstudio.com/

miauu's picture

.

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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.