[tutorial] 3dsmax and Flash No.1
3dsmax and Flash No.1
what?:
http://www.renderhjs.net/bbs/flashki...mple_panel.gifhttp://board.flashkit.com/board/imag.../2008/01/1.gif
http://board.flashkit.com/board/imag.../2008/01/1.gif
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:
http://www.renderhjs.net/bbs/flashki...nmaxscript.gif
2.) basic workflow after that:
http://board.flashkit.com/board/imag.../2008/01/1.gif
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
http://board.flashkit.com/board/imag.../2008/01/1.gif
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:
http://www.renderhjs.net/bbs/flashki..._to_spline.gif
should give you something like this:
http://www.renderhjs.net/bbs/flashki...esult_text.gif
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:
http://www.renderhjs.net/bbs/flashki...zier_flash.gif
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