ScriptSpot

Forum topic · General Scripting

call multi-sub materials

By drvox · 2008-10-21

Description

Hi all,

I'm new here and also scripting.

I was working on a script to apply random materials from a material library to the selected objects.
I made a button that activates the option 'show in viewport' to the diffuse map.

My problem is that when I have multi-sub materials the show map in viewport doesnt work.
In max help it says I need to create an index to call every material in the multi-sub material.

Can anybody help me how I can I do that?

This is the code I have:

on show pressed do
(
selobjs = $

for i in 1 to selobjs.count do
(
showTextureMap selobjs[i].material selobjs[i].material.diffusemap on

)
)

Thanks a lot.

King Regards

Comments (9)

drvox · 2008-10-30

Hey,

thanks a lot.

working fine. I also made it recognize vray materials.

I'll try to find some other script now to keep practising.

Awsome help there.

cheers

Christopher Grant · 2008-10-30

You're right - the code I wrote would in effect only toggle a standard map *within* a multi-sub material... Here's a possible fix (take note that now my IF statement is getting complicated with an if/else) there are a lot better ways to do this but oh well, this works too...


-- function to toggle the showtexturemap function. accepts a map parameter and a on/off parameter
fn toggleShowTextureMap m mapstate = try (showTextureMap m m.diffuseMap mapstate) catch()
 
--get selection of objects as array
objs = selection as array
 
for o in objs where o.material != undefined do-- loop through object array only when objects have a material
	if classof o.material == Multimaterial then -- check if material assigned is multimaterial
		for m in o.material do --loop through sub materials
			if classof m == Standard do 	--check if a material is a standard material
				toggleShowTextureMap m on --toggle the map using our function above
	else if classof o.material == Standard do -- else check if mat is standard mat
		toggleShowTextureMap o.material on --toggle the map using our function above

drvox · 2008-10-29

Still have the problem that it only works for multisubmaterials. As far as I can get it is only checking standard materials inside the multisubmaterial, am I right?

I found some nice resources about scripting, but about using functions didnt find anything yet. Soulburnscripts are awesome, but they are written in a very different way from what I've been learning..its all about functions :)
For instance I made a randomizer script with the same functionalities as the one from soulburnscripts, but its written in a completly different way.

Thanks a lot for all the help:)

Christopher Grant · 2008-10-24

Actually you'll find the code I posted already checks for standard materials - it just does it in a different spot from the multi-material check.

Here's some pseudocode (programmer lingo for summarized english translation of code)

- create a list of selected objects in memory
- loop through those selected objects, check if there is a material applied
- check if material is a multi-material
-loop through the multi-mat sub materials
- check if submat is a standard material
- toggle the texture map display of submaterial diffusemap

...

As far as how to learn - the MAXScript help is a great place to start - so is ripping apart existing, working scripts. Seeing how other people do things is one of the most useful ways to learn (IMHO).

There are also several huge MAXScript forums
- CGTalk MAXScript & SDK - http://forums.cgsociety.org/forumdisplay.php?f=98
- The Area MAXScript - http://area.autodesk.com/index.php/forums/viewforum/59/

And of course there's ScriptSpot and I'm glad you found your way over here. :-) While there are some other script repositories, this is the biggest (and oldest). That means its also a great way to learn because there are so many different tools you can just rip apart and figure out what they're doing.

Here are some links to training resources:
- Free Video Tutorials - http://www.scriptspot.com/3ds-max/tutorials/free-video-tutorials
- Commercial training options - http://www.scriptspot.com/3ds-max/tutorials/commercial-training-options

Christopher Grant · 2008-10-24

Alright, here's a quick version ... Minimal error checking but I've tried to include comments for educational purposes. :-)

To run the code below, copy/paste to the maxscript listener and hit Ctrl+E (evaluate) to run it.



-- function to toggle the showtexturemap function. accepts a map parameter and a on/off parameter
fn toggleShowTextureMap m mapstate = try (showTextureMap m m.diffuseMap mapstate) catch()

--get selection of objects as array
objs = selection as array

for o in objs where o.material != undefined do-- loop through object array only when objects have a material
	if classof o.material == Multimaterial do -- check if material assigned is multimaterial
		for m in o.material do --loop through sub materials
			if classof m == Standard do 	--check if a material is a standard material
				toggleShowTextureMap m on --toggle the map using our function above

drvox · 2008-10-24

Thanks a lot, got it working fine for the multimaterials.
I was just trying to make it work also for standard materials that are not in a multimaterial.

I guess I should add something to this:
'if classof o.material == Multimaterial do ... '

Tryed to do:
'if classof o.material == Multimaterial do or classof o.material == StandardMaterial ... '

I didnt get to the functions yet, dont know exactly how they work.
Do you recommend any book/tutorials that talk about this stuff for beginners?

All tutorials I've done dont use functions.

I have soulburnscrits installed, but the scripts seems to be written in a different way I've been learning, its all about functions...really hard to understand without any programming theory background.

Thanks a lot for the help. I'm really enjoying to start scripting but feel quite lost, so complex and just a few resources to help... hopefully this forum has been helping a lot:)

Christopher Grant · 2008-10-24

You'd need to download / install Neil's script pack since the function I'm calling "sLibGetAllMaterialsOnSelectedObjects()" is something he wrote...

I suppose I shouldn't have been so lazy but his code is great and allows for all kinds of exceptions. Why reinvent the wheel ya know?

Christopher Grant · 2008-10-22

This isn't exactly what you want to know but I depend on Neil Blevins' script pack in my daily workflow and he's got a ton of function pre-written as part of his file slib.ms ... So once you install his scripts, you'll have all kinds of functions available that you can call.

Link to download Neil's scripts - http://www.neilblevins.com/soulburnscripts/soulburnscripts.htm

In this case, he has a function called sLibGetAllMaterialsOnSelectedObjects which gets all the materials on the selected objects... If your object has a multi-mat with 10 sub mats his function will return 11 materials (the root mat and the 10 subs).

The code would go something like this:


selmats = sLibGetAllMaterialsOnSelectedObjects()
for m in selmats do try(showTextureMap m m.diffuseMap on) catch()

The try/catch statement is lazy mans way of avoiding errors. The proper way to code it would be to check the material class with classof but in this case I don't have any idea what kinds of materials you use in your workflow so its just easier for me to write it this way for you...

drvox · 2008-10-24

Hey Christopher,

Thanks a lot for the reply:)

I tried the code you wrote, but got error 'Call needs function or class'. Anyway I'm a beginer in scripting, so maybe its something basic that is missing and I cant see it. I'll have to do some research.