Page 1 of 1

FlashLight script

Posted: Mon May 02, 2005 9:14 am
by gutaker
Hi there!

...when i was playing with those flash lights mentioned by others on these forum i noticed that in maya flashlight should have a deflector on the back to make it more realistic.

So... i made a script for all You guys that does:
1. check if maxwell plug is loaded and if not loads it
2. creates a new maxwellcamera
3. creates a flashlight with emitter mat on flashlamp and diffuse mat on deflector
4.groups it and parents it to the maxwellcamera
5.finally switches to this newly created camera
6.in camerashape there are controls for turning flash on/off and for setiing flash intensity

just copy code to notepad and save as "flashCam.mel" in your maya scripts folder (c:\documents and settings\USERNAME\My Documents\maya\scripts), reload maya and in cmdline of maya write "flashCam;"

in case you need to specify another version of maya just edit the 2nd and 3rd line of the code exchanging mayall_maya65 into whatever version u like(mayall_maya60) but...

it was tesed on maya65 :)
Code: Select all
global proc flashCam() {
	if (`pluginInfo -q -l mayall_maya65`!=1) {
		loadPlugin mayall_maya65;
	};

	//Create Maxwell camera.
	string $MXcam=`maxwellCamera 1`;
	string $MXcam_s=startString($MXcam, 6)+"Shape"+endString($MXcam, 1);
	setAttr($MXcam+".scale", 10,10,10);

	//Create Flash lamp and parent to camera
	polySphere -n Lamp -r 0.015 -sx 10 -sy 6 -ax 0 1 0 -tx 1 -ch 1;
	setAttr "Lamp.tz" .015;
	sphere -n Deflector_proxy -p 0 0 0 -ax 0 1 0 -ssw 0 -esw 180 -r 0.05 -d 3 -ut 0 -tol 0.01 -s 4 -nsp 4 -ch 1;
	nurbsToPoly -n Deflector -mnd 1  -ch 1 -f 2 -pt 1 -pc 200 -chr 0.1 -ft 0.01 -mel 0.001 -d 0.1 -ut 3 -un 2 -vt 3 -vn 2 -uch 0 -ucr 0 -cht 0.2 -es 0 -ntr 0 -uss 1 "Deflector_proxy";
	delete -ch Deflector;
	delete Deflector_proxy;
	group -n Flash Lamp Deflector;
	setAttr "Flash.ty" .1;
	parent Flash $MXcam;

	//Create 650W emitter
	string $MXem=`shadingNode -asShader MXEmitter`;
	sets -renderable true -noSurfaceShader true -empty -name ($MXem+"SG");
	connectAttr -f ($MXem+".outColor") ($MXem+"SG.surfaceShader");
	setAttr ($MXem+".intensity", 650);

	//Assign emitter to flash lamp
	select -r ($MXcam+"_group|"+$MXcam+"|Flash|Lamp");
	sets -e -forceElement ($MXem+"SG");

	//Create white deflector material
	string $MXdef=`shadingNode -asShader MXDiffuse`;
	sets -renderable true -noSurfaceShader true -empty -name ($MXdef+"SG");
	connectAttr -f ($MXdef+".outColor") ($MXdef+"SG.surfaceShader");
	setAttr ($MXdef+".outColor", 1,1,1);

	//Assign deflector material to flash deflector
	select -r ($MXcam+"_group|"+$MXcam+"|Flash|Deflector");
	sets -e -forceElement ($MXdef+"SG");

	//Create Flash control in camerashapeX for turning on/off flash (located in camerShapeX/Extra Attributes)
	addAttr -ln FlashIntensity -at double  -min 0 -dv 650 ($MXcam+"_group|"+$MXcam+"|"+$MXcam_s);
	setAttr -e -keyable true ($MXcam+"_group|"+$MXcam+"|"+$MXcam_s+".FlashIntensity");	

	addAttr -ln Flash -at bool  ($MXcam+"_group|"+$MXcam+"|"+$MXcam_s);
	setAttr -e -keyable true ($MXcam+"_group|"+$MXcam+"|"+$MXcam_s+".Flash");
	

	expression -s ("if ("+$MXcam_s+".Flash==1) {"+$MXem+".intensity="+$MXcam_s+".FlashIntensity;} else {"+$MXem+".intensity=0;};") -o $MXem -ae 1 -uc all;
	
	//Look thru created camera
	lookThroughModelPanel $MXcam_s modelPanel4;

	select $MXcam;

	warning "Camera with flashlight successfully created!";
};
hope You'll like and and find it usefull :)

Posted: Mon May 02, 2005 11:33 am
by iker
I haven't test it yet but it sounds amazing! :shock:

Great idea, Thanks a lot! :wink:

Posted: Mon May 02, 2005 10:30 pm
by gutaker
Update:
bugs fixed:
- script was unable to create more than one camera properly

to do:
- graphical ui

note:
- remember that when flashCam is created, MXCamera is equal flashCam+1

code:
Code: Select all
global proc flashCam() {
	if (`pluginInfo -q -l mayall_maya65`!=1) {
		loadPlugin mayall_maya65;
	};

	//Create Maxwell camera.
	string $MXcam=`maxwellCamera 1`;
	string $MXcam_s=startString($MXcam, 6)+"Shape"+endString($MXcam, 1);
	string $Lamp=("Lamp"+endString($MXcam, 1));
	string $Deflector=("Deflector"+endString($MXcam, 1));
	string $Flash=("Flash"+endString($MXcam, 1));
	setAttr($MXcam+".scale", 10,10,10);

	//Create Flash lamp and parent to camera
	polySphere -n $Lamp -r 0.015 -sx 10 -sy 6 -ax 0 1 0 -tx 1 -ch 1;
	setAttr ($Lamp+".tz", .015);
	sphere -n Deflector_proxy -p 0 0 0 -ax 0 1 0 -ssw 0 -esw 180 -r 0.05 -d 3 -ut 0 -tol 0.01 -s 4 -nsp 4 -ch 1;
	nurbsToPoly -n $Deflector -mnd 1  -ch 1 -f 2 -pt 1 -pc 200 -chr 0.1 -ft 0.01 -mel 0.001 -d 0.1 -ut 3 -un 2 -vt 3 -vn 2 -uch 0 -ucr 0 -cht 0.2 -es 0 -ntr 0 -uss 1 "Deflector_proxy";
	delete -ch $Deflector;
	delete Deflector_proxy;
	group -n $Flash $Lamp $Deflector;
	setAttr ($Flash+".ty", .1);
	parent $Flash $MXcam;

	//Create 650W emitter
	string $MXem=`shadingNode -asShader MXEmitter`;
	sets -renderable true -noSurfaceShader true -empty -name ($MXem+"SG");
	connectAttr -f ($MXem+".outColor") ($MXem+"SG.surfaceShader");
	setAttr ($MXem+".intensity", 650);

	//Assign emitter to flash lamp
	select -r ($MXcam+"_group|"+$MXcam+"|"+$Flash+"|"+$Lamp);
	sets -e -forceElement ($MXem+"SG");

	//Create white deflector material
	string $MXdef=`shadingNode -asShader MXDiffuse`;
	sets -renderable true -noSurfaceShader true -empty -name ($MXdef+"SG");
	connectAttr -f ($MXdef+".outColor") ($MXdef+"SG.surfaceShader");
	setAttr ($MXdef+".outColor", 1,1,1);

	//Assign deflector material to flash deflector
	select -r ($MXcam+"_group|"+$MXcam+"|"+$Flash+"|"+$Deflector);
	sets -e -forceElement ($MXdef+"SG");

	//Create Flash control in camerashapeX for turning on/off flash (located in camerShapeX/Extra Attributes)
	addAttr -ln FlashIntensity -at double  -min 0 -dv 650 ($MXcam+"_group|"+$MXcam+"|"+$MXcam_s);
	setAttr -e -keyable true ($MXcam+"_group|"+$MXcam+"|"+$MXcam_s+".FlashIntensity");	

	addAttr -ln Flash -at bool  ($MXcam+"_group|"+$MXcam+"|"+$MXcam_s);
	setAttr -e -keyable true ($MXcam+"_group|"+$MXcam+"|"+$MXcam_s+".Flash");
	
	expression -s ("if ("+$MXcam_s+".Flash==1) {"+$MXem+".intensity="+$MXcam_s+".FlashIntensity;} else {"+$MXem+".intensity=0;};") -o $MXem -ae 1 -uc all;
	
	//Look thru created camera
	lookThroughModelPanel $MXcam_s modelPanel4;

	select $MXcam;

	warning "Camera with flashlight successfully created!";
};

Posted: Mon May 02, 2005 10:34 pm
by blueplanetdesign
Have you any intention of porting the script to Maxscript for Max users?

I'd love to give it a go!

Posted: Tue May 03, 2005 2:34 am
by iker
In Maya 6.0.1 renders all black...the geometry of the flashlight is visible? A camera and 2 shaders (emitter & diffusse, I guess one for the light and one for the deflector) are created but I can't see no object as the flashlight :cry:

edited> Via Outliner I see the objects, small ones aren't they? but I still render black

Posted: Tue May 03, 2005 4:05 am
by abgrafx3d
gutaker - very cool mel script - thanks! :D

iker - make sure the flash is on - the default value is off.

I'm using Maya 6.01 and it's working - the only thing I changed was the camera scale to 1.

from: setAttr($MXcam+".scale", 10,10,10);

to: setAttr($MXcam+".scale", 1,1,1);

gutaker - any reason why you set the scale of the cam to 10? Are you using the cam's scale to scale the size of the flash?

Posted: Tue May 03, 2005 4:45 am
by iker
Thanks abgrafx3d! I didn't notice there was an ON/OFF value (I forgot point 6) :oops:

...working perfectly here now :D

one question :
Do you think is better a sphere than a plane for the emitter to simulate a flashlight?

Posted: Tue May 03, 2005 5:12 am
by abgrafx3d
iker - I'm going to try the sphere even tho the other flashlight images posted have used a plane.

Posted: Tue May 03, 2005 9:27 pm
by gutaker
hi guys!

i used scaling to 10,10,10 because of my work units - i use meters not centimeters, so whan camera was created it was almost impossible to select it via viewport :)

about ur abgrafx question: maya has special materials assignement, i.e. when u create a plane and assign an emitter to it, it spreads light with both faces :(, so i thought its neccessary to create a sphere as a bulb and a hemisphere as deflector.

BTW: its some kind of "going back to the roots" - do u remember those old movies with journalists posing - they had cameras with flashlights such as this one :D

if u find another way give me a sign and ill rewrite a script :)

Posted: Wed May 04, 2005 2:21 am
by iker
gutaker wrote:....when u create a plane and assign an emitter to it, it spreads light with both faces :(, so i thought its neccessary to create a sphere as a bulb and a hemisphere as deflector.
I think it spread light only in surface normal's direction, isn't it?

...if u find another way give me a sign and ill rewrite a script :)
I think it could be helpful to have a value to choose between some surfaces for the emitter(a plane, a sphere, a cube.....), and to get some size deflectors.... (sorry if I'm asking too much :oops: )


btw/ ....great script , great! :shock:

Posted: Wed May 04, 2005 9:14 am
by gutaker
hi iker, thx for ur feedback and ill do my best to make ur wishes happen.
as u can see in previous posts there is a to do: - gui, so ill include ur suggestions :)

Posted: Wed May 04, 2005 9:26 am
by gutaker
i forgot about stevens question :oops: , sorry

so, steve, there is a way to port it but i dont know what kind of scripting language max is using.

answer for that and ill try to rewrite it for u. :)

Posted: Wed May 04, 2005 6:38 pm
by blueplanetdesign
3dsMax uses it's own proprietary scripting language called Maxscript.