bitmap switcher

Another script...another newbie problem!

I'm trying to develop a script that let me change diffuse texture of a certain material.
I started studying Joe Gunn old script "face switcher": this script change material ID (but first we need to create a multiamterial with a lot of submaterials with different textures), while I think it's faster to have only one material and change its texture.
The script let you choose a directory to sort bitmap from, create thumbnails and...nothing.
The scope is to change material texture clicking on the corresponding thumbnail and to create a keyframe for material diffuse slot.

This is my result:

try(destroyDialog ::texchange)catch()
rollout texchange"TEXTURE CHANGE"
 
(	
--interface
	button btnSetD "Set Dir:" pos:[5,5] width:50 height:20 toolTip:"directory immagini"  
	edittext Fdir "" pos:[55,5] width:100 height:20
	button btn01 "01" pos:[5,30] width:30 height:30  
	button btn02 "02" offset:[-30,-35] width:30 height:30
	button btn03 "03" offset:[0,-35] width:30 height:30  
	button btn04 "04" offset:[30,-35] width:30 height:30
	button btn05 "05" offset:[60,-35] width:30 height:30
--choose bitmap path
	on btnSetD pressed do
	(
		local curpath = Fdir.text 
		local newPath = getSavePath initialDir:(curpath) caption:"Select the path where your Face Images are located" 
		if newPath != undefined then	
--create thumbnails
		(
			Fdir.text = newpath
			btn01.images = #(Fdir.text + "\01.png", Fdir.text + "\01.png", 1, 1, 1, 1, 1)
			btn02.images = #(Fdir.text + "\02.png", Fdir.text + "\02.png", 1, 1, 1, 1, 1)
			btn03.images = #(Fdir.text + "\03.png", Fdir.text + "\03.png", 1, 1, 1, 1, 1)
			btn04.images = #(Fdir.text + "\04.png", Fdir.text + "\04.png", 1, 1, 1, 1, 1)
			btn05.images = #(Fdir.text + "\05.png", Fdir.text + "\05.png", 1, 1, 1, 1, 1)
		)
	)
--actions
	on btn01 pressed do
	(	
--and here the problem... load 1st thumbnail image to object material
	)
	on btn02 pressed do
	(	
--same, and so on...
	)	
)
createdialog texchange 160 200 10 110 style:#(#style_titlebar, #style_sysmenu, #style_toolwindow)

I hope to have been clear enought... any idea?

Comments

Comment viewing options

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

This work fine for me!!!

This work fine for me!!! THANKS!!!

You are Awasome!

dalmirobu's picture

Great job!!!

Could you please share this script? I would be very useful.

thank you very much

ste's picture

Thanks!

It could be a good tool, but I really need some help to finish it because it's full of bugs!!

Here the complete and commented code (I forget something... but some time has passed, sorry).

I have attacherd a zip file containing the script, a simple scene (max 2010) and 2 textures just to speed up testing process.

----- FACE SWITCHER V.0.1
----- This is a scripted modifier
----- It allows you to change object texture editing bitmap cropping and placement
 
----- INSTALLATION:
----- put this file in ...maxfolder\stdplugs\stdscripts\ before to open max
 
----- USE:
----- Create your bitmap with aspect ratio 10:1. This must contain 10 square images side to side
----- Put it in diffuse channel of your material (that must be a VrayMtl, and must be ID#1 of a Multi/Sub-Object material)
----- Set bitmap cropping to W:0.1 H:1.0 (so only the first image is used)
----- Assign Multi/Sub-Object material to your object
----- Add "Face switcher DEV" modifier to your object
----- Click on "Pick Obj": bitmap name should appear in text box and thumbnails in numbered boxes (in this step the script create a lowres version of your bitmap to use in UI, same folder, same format, same name with suffix "_lowres")
----- Done! Now you can change your texture clicking on each thumbnail
----- If you move the time slider and change the texture, keyframes are created automatically (stepped)
 
----- PROBLEMS/BUGS:
----- Works only with multisubmat
----- Material with bitmap to switch must have ID#1
----- If you use a bitmap with alpha the small one created for UI is terrible (tried jpg, bmp, tiff, png... no way, but this is a max problem I think)
----- If you use it on more than one object with different materials (and bitmap of course) UI icons are always the same, even if texture name displayed is right
----- Something that I forgot for sure...
 
----- Probably there are errors and inaccuracies in this script, but this is one of my first tries and I know I miss the basics
 
 
plugin modifier Face_switcher_DEV
name:"Face switcher DEV"
classID:#(0x133078, 0x54409)
version:1
 
 
--- CREATE UI
(	
	parameters main rollout:test
	(
		obj type: #string  ui:obj animatable:false
	)
 
	rollout test "FACE SWITCHER" width:162
 
	(	
	button pickObj "Pick Obj" pos:[5,5] width:50 height:20
	edittext obj "" pos:[55,5] width:100 height:20
 
	button btn01 "01" pos:[5,30] width:50 height:50  
	button btn02 "02" offset:[-1,-55] width:50 height:50
	button btn03 "03" offset:[49,-55] width:50 height:50  
	button btn04 "04" offset:[-51,-5] width:50 height:50
	button btn05 "05" offset:[-1,-55] width:50 height:50
	button btn06 "06" offset:[49,-55] width:50 height:50
	button btn07 "07" offset:[-51,-5] width:50 height:50
	button btn08 "08" offset:[-1,-55] width:50 height:50
	button btn09 "09" offset:[49,-55] width:50 height:50  
	button btn10 "10" offset:[-51,-5] width:50 height:50
 
 
--- PICK OBJECT TEXTURE, CREATE LOWRES VERSION, SAVE IT AND PUT IT IN UI
	on pickObj pressed do
	(
		--- GET MATERIAL TEXTURE
		local TEXTURE_NAME = $.material[1].TEXMAP_diffuse.filename
 
		--- FUNCTION TO OPEN TEXTURE
		local TEXTURE = openbitmap $.material[1].TEXMAP_diffuse.filename
 
		--- CANT'T REMEMBER SORRY
		local TEXMAP = bitmaptexture bitmap:(openBitmap TEXTURE_NAME)
 
		--- TEXTURE WIDTH, HEIGHT AND ASPECT RATIO
		w = TEXTURE.width
		h = TEXTURE.height
		c = w/h
 
		--- THUMBNAIL SIZE, BASED ON ORIGINAL TEXTURE ASPECT RATIO
		local THUMB_SIZE = [c*50, 50]
 
		--- GENERATE THUMBNAIL
		local THUMBNAIL = renderMap TEXMAP size:THUMB_SIZE filter:on
		THUMBNAIL.filename = (getFilenamePath TEXTURE_NAME + getFilenameFile TEXTURE_NAME + "_lowres" + getFilenameType TEXTURE_NAME)   
 
		--- SAVE THUMBNAIL
		save THUMBNAIL
 
		--- CLOSE THUMBNAIL
		close THUMBNAIL
 
		--- CANT'T REMEMBER SORRY AGAIN
		filename = filterstring TEXTURE_NAME "\\"
		filename = filename[filename.count]
 
		--- SUBDIVIDE THUMBNAIL OVER 10 UI BUTTONS
		btn01.images = #(THUMBNAIL, THUMBNAIL, c, 1, 1, 1, 1)
		btn02.images = #(THUMBNAIL, THUMBNAIL, c, 2, 2, 1, 1)
		btn03.images = #(THUMBNAIL, THUMBNAIL, c, 3, 3, 1, 1)
		btn04.images = #(THUMBNAIL, THUMBNAIL, c, 4, 4, 1, 1)
		btn05.images = #(THUMBNAIL, THUMBNAIL, c, 5, 5, 1, 1)
		btn06.images = #(THUMBNAIL, THUMBNAIL, c, 6, 6, 1, 1)
		btn07.images = #(THUMBNAIL, THUMBNAIL, c, 7, 7, 1, 1)
		btn08.images = #(THUMBNAIL, THUMBNAIL, c, 8, 8, 1, 1)
		btn09.images = #(THUMBNAIL, THUMBNAIL, c, 9, 9, 1, 1)
		btn10.images = #(THUMBNAIL, THUMBNAIL, c, 10, 10, 1, 1)
		obj.text = filename
	)
 
 
--- ACTION ON BUTTON PRESSED, SAME FOR ALL
	on btn01 pressed do if selection.count != 0 do
	(
		if (mtl = selection[1].mat) != undefined do
		(
			mtl = getClassInstances VRayMtl target:mtl
			if mtl.count != 0 do if isKindOf (map = mtl[1].TEXMAP_diffuse) bitmaptex do
			(
 
				--- SET AUTOKEY ON AND DEFINE BITMAP PLACEMENT
				with animate on (map.clipu = 0)
 
				--- CREATE KEYFRAME - SET IN & OUT TANGENT TO STEP
				map.clipu.controller.keys.inTangentType = map.clipu.controller.keys.outTangentType = #step
			)
		)
	)
 
	on btn02 pressed do if selection.count != 0 do
	(
		if (mtl = selection[1].mat) != undefined do
		(
			mtl = getClassInstances VRayMtl target:mtl
			if mtl.count != 0 do if isKindOf (map = mtl[1].TEXMAP_diffuse) bitmaptex do
			(
				with animate on (map.clipu = 0.1)
				map.clipu.controller.keys.inTangentType = map.clipu.controller.keys.outTangentType = #step
			)
		)
	)
 
	on btn03 pressed do if selection.count != 0 do
	(
		if (mtl = selection[1].mat) != undefined do
		(
			mtl = getClassInstances VRayMtl target:mtl
			if mtl.count != 0 do if isKindOf (map = mtl[1].TEXMAP_diffuse) bitmaptex do
			(
				with animate on (map.clipu = 0.2)
				map.clipu.controller.keys.inTangentType = map.clipu.controller.keys.outTangentType = #step
			)
		)
	)
 
	on btn04 pressed do if selection.count != 0 do
	(
		if (mtl = selection[1].mat) != undefined do
		(
			mtl = getClassInstances VRayMtl target:mtl
			if mtl.count != 0 do if isKindOf (map = mtl[1].TEXMAP_diffuse) bitmaptex do
			(
				with animate on (map.clipu = 0.3)
				map.clipu.controller.keys.inTangentType = map.clipu.controller.keys.outTangentType = #step
			)
		)
	)
 
	on btn05 pressed do if selection.count != 0 do
	(
		if (mtl = selection[1].mat) != undefined do
		(
			mtl = getClassInstances VRayMtl target:mtl
			if mtl.count != 0 do if isKindOf (map = mtl[1].TEXMAP_diffuse) bitmaptex do
			(
				with animate on (map.clipu = 0.4)
				map.clipu.controller.keys.inTangentType = map.clipu.controller.keys.outTangentType = #step
			)
		)
	)
 
	on btn06 pressed do if selection.count != 0 do
	(
		if (mtl = selection[1].mat) != undefined do
		(
			mtl = getClassInstances VRayMtl target:mtl
			if mtl.count != 0 do if isKindOf (map = mtl[1].TEXMAP_diffuse) bitmaptex do
			(
				with animate on (map.clipu = 0.5)
				map.clipu.controller.keys.inTangentType = map.clipu.controller.keys.outTangentType = #step
			)
		)
	)
 
	on btn07 pressed do if selection.count != 0 do
	(
		if (mtl = selection[1].mat) != undefined do
		(
			mtl = getClassInstances VRayMtl target:mtl
			if mtl.count != 0 do if isKindOf (map = mtl[1].TEXMAP_diffuse) bitmaptex do
			(
				with animate on (map.clipu = 0.6)
				map.clipu.controller.keys.inTangentType = map.clipu.controller.keys.outTangentType = #step
			)
		)
	)	
 
	on btn08 pressed do if selection.count != 0 do
	(
		if (mtl = selection[1].mat) != undefined do
		(
			mtl = getClassInstances VRayMtl target:mtl
			if mtl.count != 0 do if isKindOf (map = mtl[1].TEXMAP_diffuse) bitmaptex do
			(
				with animate on (map.clipu = 0.7)
				map.clipu.controller.keys.inTangentType = map.clipu.controller.keys.outTangentType = #step
			)
		)
	)	
 
	on btn09 pressed do if selection.count != 0 do
	(
		if (mtl = selection[1].mat) != undefined do
		(
			mtl = getClassInstances VRayMtl target:mtl
			if mtl.count != 0 do if isKindOf (map = mtl[1].TEXMAP_diffuse) bitmaptex do
			(
				with animate on (map.clipu = 0.8)
				map.clipu.controller.keys.inTangentType = map.clipu.controller.keys.outTangentType = #step
			)
		)
	)	
 
	on btn10 pressed do if selection.count != 0 do
	(
		if (mtl = selection[1].mat) != undefined do
		(
			mtl = getClassInstances VRayMtl target:mtl
			if mtl.count != 0 do if isKindOf (map = mtl[1].TEXMAP_diffuse) bitmaptex do
			(
				with animate on (map.clipu = 0.9)
				map.clipu.controller.keys.inTangentType = map.clipu.controller.keys.outTangentType = #step
			)
		)
	)
 
	)
)
AttachmentSize
face_switcher_dev.zip 86.43 KB
ste's picture

EUREKA!

It works!
...with my stupid $ and vraymaterial, but it works!!

...
btn01.images = #(Fdir.text + "\lowres.jpg", undefined, 5, 1, 1, 1, 1)
btn02.images = #(Fdir.text + "\lowres.jpg", undefined, 5, 2, 1, 1, 1)
btn03.images = #(Fdir.text + "\lowres.jpg", undefined, 5, 3, 1, 1, 1)
btn04.images = #(Fdir.text + "\lowres.jpg", undefined, 5, 4, 1, 1, 1)
btn05.images = #(Fdir.text + "\lowres.jpg", undefined, 5, 5, 1, 1, 1)
...
on btn01 pressed do 
( $.material.texmap_diffuse.clipu = 0 )
on btn02 pressed do 
( $.material.texmap_diffuse.clipu = 0.2 )
on btn03 pressed do 
( $.material.texmap_diffuse.clipu = 0.4 )
on btn04 pressed do 
( $.material.texmap_diffuse.clipu = 0.6 )
on btn05 pressed do 
( $.material.texmap_diffuse.clipu = 0.8 )
...

Now I have to make it works with multimaterial and create keyframe automatically :)

barigazy's picture

...

Just try

with animate on (selection[1].material.texmap_diffuse.clipu = 0.4)

bga

ste's picture

.

Perfect on vraymtl, but does not work on multisub material :(

barigazy's picture

...

I already post example how to manage nested VRayMtl inside any material (Multimaterial, Blend, VRayBlend, VRayLight etc.)

bga

ste's picture

.

Yup,
and your advice have produced good results :)

on btn03 pressed do if selection.count != 0 do
 (
  if (mtl = selection[1].mat) != undefined do
   (
    mtl = getClassInstances VRayMtl target:mtl
     if mtl.count != 0 do 
      if isKindOf  (map = mtl[1].texmap_diffuse) bitmaptex do with animate on (map.clipu = 0.4)
   )
  )

Did I worked well?
:)

barigazy's picture

...

Yep. I will explain line by line

-- on button is pressed check first if selection exists
on btn03 pressed do if selection.count != 0 do 
(
	-- check if first item in selection have assigned material
	if (mtl = selection[1].mat) != undefined do
	(
		-- collect all VRayMtl instance in targeted object material
		-- this material can be multimaterial or any other that have VRayMtl nested inside
		-- in this case I overide "mtl" variable 
		-- at first this variable is selection material and then array of VRayMtl's
		mtl = getClassInstances VRayMtl target:mtl
		-- check if array is not empty
		if mtl.count != 0 do
			-- check if first material in array have texturemap in diffuse slot
			if isKindOf (map = mtl[1].texmap_diffuse) bitmaptex do with animate on (map.clipu = 0.4)
		   -- the rest you get it already ;)
	)
)

bga

ste's picture

.

Wow, something I understood, something I missed :)

Last step is to set created keyframes to step with some

.inTangentType = #step
.outTangentType = #step

but all my attempts are failed.

Comment viewing options

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