A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [tutorial] 3dsmax and Flash No.1

  1. #1
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756

    [tutorial] 3dsmax and Flash No.1

    3dsmax and Flash No.1

    what?:




    a maxscript interface that lets you convert selected splines into Flash code wich displays the same spline in flash via the drawing API

    features:
    - copies code to clipboard automaticly (only Max 9+, copy by hand if you have a older version)
    - supports multiple shapes per spline (e.g text)
    - detects if shapes of a spline are meant to be closed (will add first coordinates at the end to get the closing vector)

    limitations:
    - no bezier curves just the knots of the splines
    - one spline object per click
    - only splines

    the maxscipt
    Code:
    function splineToArray =(
    	try(	
    		
    		out = "var spline = new Array();\nspline = [";
    		for s = 1 to (numSplines $) do
    		(
    			out+="["
    			for k = 1 to (numKnots $ s) do
    			(
    			
    				knot = getKnotPoint $ s k;--object, spline element, knot ID
    				
    				x    = (knot.x as Integer) as String;
    				y    = ((knot.y *-1) as Integer) as String;
    		
    				out+= ("[" + x + "," + y + "]") as String;
    				
    				if  k < (numKnots $ s) then (
    					out+=",";
    				)else (
    				
    					--check if closed spline...
    					closed = isClosed $ s;
    					print ("closed"+(closed as String));
    					if (closed == true) then(
    						knot = getKnotPoint $ s 1;--object, spline element, knot ID
    						x    = (knot.x as Integer) as String;
    						y    = ((knot.y *-1) as Integer) as String;
    						out+= ",[" + x + "," + y + "]";
    					)
    				
    					--end of shape array element
    					if  s < (numSplines $) then (
    						out+="],\n";
    					)else(
    						--end of total array
    						out+="]];\n";
    					)
    				)
    			)
    		
    		)
    		
    		out+="\n\nvar mc = createEmptyMovieClip(\"spline_mc\",1);\n";
    		out+="mc._x = Stage.width/2;\nmc._y = Stage.height/2;\n";
    		out+="mc.lineStyle(0,0xff0000,100);\n\n";
    		out+="for (var i=0;i<spline.length;i++){\n";
    		out+="\tfor (var j=0;j<spline[i].length;j++){\n";
    
    		
    		--var x = spline[i][j][0];
    		--var y = spline[i][j][1];
    		
    		
    		out+="\t\tvar x =spline[i][j][0];\n\t\tvar y =spline[i][j][1];\n";
    		out+="\t\tif (j == 0){\n\t\t\tmc.moveTo(x,y);\n\t\t}else{\n\t\t\tmc.lineTo(x,y);\n\t\t}\n\t}\n}";
    
    		return out;
    	)catch(
    		return "error!?!";
    	)
    )
    
    rollout set1 "flashkit maxscript 1" width:190 height:178
    (
    	button btn1 "Spline to Flash Array" pos:[7,6] width:109 height:25
    	edittext txt1 "" pos:[2,34] width:183 height:68
    	
    	label lbl1 "2008 renderhjs" pos:[5,160] width:80 height:14
    	label lbl3 "- draw a spline in the top viewport" pos:[7,105] width:177 height:19
    	label lbl4 "- select it and hit the button" pos:[7,119] width:177 height:15
    	label lbl5 "- paste into Flash" pos:[7,131] width:177 height:16
    	
    	on btn1 pressed do(
    		txt = splineToArray();
    		set1.txt1.text = txt ;--set text to textfield
    		
    		try(
    			clipboardClass = dotNetClass "System.Windows.Forms.Clipboard"
    			clipboardClass.SetText (txt as string);
    		)catch(
    			print "you dont have max9, cant copy to clipboard";
    		)
    	)
    )
    --
    try (-- close existing rolloutFloater
    	closeRolloutFloater panel
    ) catch ()
    panel = newRolloutFloater "Flashkit" 208 360
    addrollout set1 panel;

    1.) how to use Maxscript
    hit the following shortcuts in order: F11 then Ctrl+N to open the MaxScript Listener and create a new Scipt. Paste my code in there and hit [b]Ctrl+e[b], alternativly follow this explaination:


    2.) basic workflow after that:

    and then hit the "Spline to Flash Array" from my script window, you should see a flash script in the text window wich is already copies to clipboard if you have 3dsmax 9+ otherwise copy it manually.
    Paste that actionscript into a Flash AS1 or 2 project and hit ctrl + enter (test movie)
    something like this should appear


    3.) little advanced: converting text to splines
    because a text ´object is rather a sub object from the spline object category in Max we need to convert it to a spline so that my scipt can read it propper, do the following for that:

    should give you something like this:

    but wait why is my font so edgy?

    because the script can read out for now only the knot points not interpolate the bezier knots, compare:




    4.) maxscript in detail
    first off I wont explain not yet in this thread the grammar of maxscript because I think most of you will understand just by looking at the script like ( instead of { and other stuff- in general maxscript is quite similar to actionscript.

    A.) the last part (interface rollout)
    Code:
    rollout set1 "flashkit maxscript 1" width:190 height:178
    (
    	button btn1 "Spline to Flash Array" pos:[7,6] width:109 height:25
    	edittext txt1 "" pos:[2,34] width:183 height:68
    	
    	label lbl1 "2008 renderhjs" pos:[5,160] width:80 height:14
    	label lbl3 "- draw a spline in the top viewport" pos:[7,105] width:177 height:19
    	label lbl4 "- select it and hit the button" pos:[7,119] width:177 height:15
    	label lbl5 "- paste into Flash" pos:[7,131] width:177 height:16
    a rollout is a maxscript interface panel that can be collapsed and expanded while holding different interface elements such as buttons, textfields, sliders, dropdown menus and lots of other stuff.
    At the top the new rollout is beeing defined with a title a width and height, within the rollout I then define a button, the edit text and 4 labels.

    Code:
    	on btn1 pressed do(
    		txt = splineToArray();
    		set1.txt1.text = txt ;--set text to textfield
    		
    		try(
    			clipboardClass = dotNetClass "System.Windows.Forms.Clipboard"
    			clipboardClass.SetText (txt as string);
    		)catch(
    			print "you dont have max9, cant copy to clipboard";
    		)
    	)
    )
    --
    try (-- close existing rolloutFloater
    	closeRolloutFloater panel
    ) catch ()
    panel = newRolloutFloater "Flashkit" 208 360
    addrollout set1 panel;
    After that the event for the button is declared wich is:
    get a variable txt from my splineToArray() function (returns a value, will be explained later). Within a try()catch() event I try to prevent the script crashing in case something doesn´t work. What he tries is to copy the output to the clipboard of windows (3dsmax 9 required for that), if this fails because the user has a older max version show the error message in the Maxscript listener panel.


    B.) the body part, the function splineToArray():
    Code:
    function splineToArray =(
    	try(	
    		
    		out = "var spline = new Array();\nspline = [";
    		for s = 1 to (numSplines $) do
    		(
    			out+="["
    			for k = 1 to (numKnots $ s) do
    			(
    			
    				knot = getKnotPoint $ s k;--object, spline element, knot ID
    				
    				x    = (knot.x as Integer) as String;
    				y    = ((knot.y *-1) as Integer) as String;
    define a output variable "out" to dump the whole string into. Next are 2 loops one for the amount of spline shapes within the spline, the following one for the knot points within that spline shape.
    Inside the inner loop get the knot object wich holds the information about the knot point such as the x,y positions the tangent dimensions (if so) ect. We read out just the x and y coordinates but need to change them a little bit.
    x needs to be a Integer and then converted to string, Integer as 150.0 would be printed as 150.0 and not 150 and to add those variables later to the string convert them to a string after that.
    y is almost the same except that I flipped the direction since 3dsmax uses the mathmatical coordinate system (y going up not as in flash going down).

    Code:
    out+= ("[" + x + "," + y + "]") as String;
    				
    				if  k < (numKnots $ s) then (
    					out+=",";
    				)else (
    				
    					--check if closed spline...
    					closed = isClosed $ s;
    					print ("closed"+(closed as String));
    					if (closed == true) then(
    						knot = getKnotPoint $ s 1;--object, spline element, knot ID
    						x    = (knot.x as Integer) as String;
    						y    = ((knot.y *-1) as Integer) as String;
    						out+= ",[" + x + "," + y + "]";
    					)
    				
    					--end of shape array element
    					if  s < (numSplines $) then (
    						out+="],\n";
    					)else(
    						--end of total array
    						out+="]];\n";
    					)
    				)
    			)
    		
    		)
    the rest you see here is the part that creates the string for the out variable (the one wich will be returned/ output at the end). It adds the "," after array elements. At the ends it creates the finsihing array signs like "]," or at the very end "]];". As you may noticed maxscript uses as well the "\n" "\\" "\r" "\t",... special characters to set returns, tabs, new lines ect.

    Code:
    out+="\n\nvar mc = createEmptyMovieClip(\"spline_mc\",1);\n";
    		out+="mc._x = Stage.width/2;\nmc._y = Stage.height/2;\n";
    		out+="mc.lineStyle(0,0xff0000,100);\n\n";
    		out+="for (var i=0;i<spline.length;i++){\n";
    		out+="\tfor (var j=0;j<spline[i].length;j++){\n";
    
    		
    		--var x = spline[i][j][0];
    		--var y = spline[i][j][1];
    		
    		
    		out+="\t\tvar x =spline[i][j][0];\n\t\tvar y =spline[i][j][1];\n";
    		out+="\t\tif (j == 0){\n\t\t\tmc.moveTo(x,y);\n\t\t}else{\n\t\t\tmc.lineTo(x,y);\n\t\t}\n\t}\n}";
    
    		return out;
    	)catch(
    		return "error!?!";
    	)
    )
    the last parts adds the flash code wich is always the same to execute the drawing API to run through the array. At the later end the variable out is then returned (the variable holding the output string).
    The catcher at the end only happens if the above code fails wich happens for example if multiple objects are selected or not a spline was selected.


    this was a little intro into 3dsmax maxscript used for flash, there is alot possible with this script some examples:
    - import illustrator file, convert to splines and hit script
    - build levels in 3dsmax
    - measure 3dsmax models
    ...
    that´s it for now more to follow

  2. #2
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    did some tests with the illustrator import thingy, and ran it trhough my script, here some results:


    viza´s new logo

    as you can see the curves are not ported but it´s already pretty usefull imo.

  3. #3
    When you know are. Son of Bryce's Avatar
    Join Date
    Aug 2002
    Location
    Los Angeles
    Posts
    838
    Dude, I just glanced at this and I can already tell this'll be friggin useful! Thanks so much!

  4. #4
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Quote Originally Posted by Son of Bryce
    Dude, I just glanced at this and I can already tell this'll be friggin useful! Thanks so much!
    Ditto! Thanks a lot render!

  5. #5
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    thought I´d share this link, Gmax (free and lite version of 3dsmax) download at:
    http://www.turbosquid.com/gmax
    see the lower button "download"

    officially canceled and not anymore updated
    http://usa.autodesk.com/adsk/servlet...&siteID=123112
    but still refering to turbosquid
    http://www.turbosquid.com/Forum/Inde...ntGroupID/1004
    and turbosquid again promoting and offering at:
    http://www.turbosquid.com/gmax

    anyway the bottomline is: with Gmax you can model, skin and animate to a limited extend similar to the full package 3dsmax. From documentation reading I know that maxscript is also not as complete as 3dsmax but (e.g no render functionality script support,...) it should do quite a lot.

  6. #6
    Flash Genie letschillout's Avatar
    Join Date
    Feb 2007
    Location
    31.52949, 74.347272
    Posts
    146
    Kool Stuff @
    Charag - 3D, Flash Games, Animations,
    Website Development & More...


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center