Modify existing script: apply it ONLY to selected objects?

Hi,

I have a script that maps bitmap textures to objects in scene based on object name. My problem is that when I run it, ALL objects in the scene with the name "PlaneXXX" get remapped with images in the script's current path. I would like to modify it so that only the selected ones get remapped.

(I have played with "for obj in selection do" - such as putting it outside the "for object in selection" loop and integrating it into the "for p in $Plane*" line, but no luck.)

(
    local img_path = "C:/Users/Stenrik/rest/of/the/path/"
 
    for p in $Plane* do (
        addModifier p (uvwMap())        
        convertToMesh p                 
 
        local num = substring p.name (p.name.count - 2) p.name.count
        local mat = standardMaterial()
 
        mat.diffuseMap = bitmapTexture filename:(img_path + "Diffuse" + num + ".png")
        mat.opacityMap = bitmapTexture filename:(img_path + "Alpha" + num + ".png")
 
        p.material = mat
    )
)

Many thanks,
-Sten

Comments

Comment viewing options

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

Still affecting all in scene

Hi Fajar, thanks for the reply. Definitely looks like a simple fix.

However, when I test it (by creating two identically-named plane stacks and selecting only one of them before running the script), BOTH still end up being remapped.

Below is the modified code:

(
	    local img_path = "C:/Users/Kirsten/Dropbox/() 2D Assets/() Master MRI Texture/Front - All - 500/"
 
	    for p in selection do (
	        addModifier p (uvwMap())        -- remove these 2 lines if the planes
	        convertToMesh p                 -- already have texture coordinates
 
	        local num = substring p.name (p.name.count - 2) p.name.count
	        local mat = standardMaterial()
 
	        mat.diffuseMap = bitmapTexture filename:(img_path + "Diffuse" + num + ".png")
	        mat.opacityMap = bitmapTexture filename:(img_path + "Alpha" + num + ".png")
 
 
	        p.material = mat
	    )
	)
miauu's picture

.

I've tested your code and it works properly. Only the selected objects receives the material.
Are you sure that the planes that you use are not instances?

Or, you can provide the scene that you use for testing.

Stenrik's picture

Editing from custom toolbar buttons...

So here's what I learned: Changes made by right-clicking the script in a custom toolbar and saving do not seem to apply without restarting Max. That first time, I edited the script from its toolbar button, saved it, then hit the button to run. I thought the changes had applied, as I saw them there when I edited the script again.

After restarting Max and making a new scene, it worked! BTW, I tried cloning the stack, copying, and instancing. Interestingly, the script worked with the instanced version in addition to the others.

fajar's picture

try it

simply change

$Plane*
 
-- to
 
selection 
 
-- => to like this 
 
for i in selection 
(
-----bla
)

Comment viewing options

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