Anim8or Community

Please login or register.

Login with username, password and session length
Advanced search  

News:

An update to Anim8or, v1.00b, is available with a few bug fixes. Get your copy HERE. See the "ReadMe" file for details.

Author Topic: setting materials in ASL  (Read 3832 times)

freesailor

  • Newbie
  • *
  • Posts: 23
    • View Profile
setting materials in ASL
« on: September 21, 2010, 10:36:07 pm »

hello all....
I'm a noob to anim8or and ASL... been playing with anim8or for a couple of months and decided i could use some plug-ins for my models of houses, specifically the windows. I have tried ever possible permutation of SetMaterial to try and set the window frame to one material and the glass to another, none of which works... sooooo... is this possible with a script??? the docs say I should be able to do this, but as I said, I'm sure I've tried every combination of instructions with no luck.... the resulting model .an8 file shows only face 4 4 0 -1(trim) (no face 4 4 1 -1(glass)).. could sure use some help with this, as most of my plug-ins will need to have at least two materials.
    I've read most of the posts and find no subjects addressing this problem,(maybe i'm just too stoopid to realize my mistake...lol), but at the same time I saw a lot of requests for a script hat would make an ASL plug-in from a model, so I wrote this little gem which will do just that for most simple! models...its works fine on my windows(some have over a thousand faces!!!). I'm not sure if it is allowed to post  a perl script on this board, but I have no website to post it to.... so here it is:
                                                       Model2script.pl (m2s.pl)

#!/perl/bin/perl
##### Windows perl = http://www.activestate.com/activeperl/downloads ActivePerl-5.8.8.822-MSWin32-x86-280952.msi... free!!!!
##### a little script that extracts the points and faces from an anim8or model.an8 file
##### then uses them to write them to write an .a8s script...
##### command line = c:\>m2s.pl model-name.an8... this program expects the model to be in C:/anim8or/projects
##### output =  c:/anim8or/scripts/model-name.a8s
##### notes: 1. output is formatted for Notepad... arial/regular 12 pt...
#####        2. this will only extract the first mesh of a model... so <join solids> and <convert to mesh> the model first....
#####        3. you will have to apply texcoords to the generated model yourself... it was becoming too much of a chore as it was...
#####        4. the plug-in will show as the default electric plug... you'll have figure out the bit map button yerself... lol
#####        5. the newly generated parametric is NOT editable, even if you convert it to a mesh...if you need to edit the new model,
#####           save it first, reopen he saved model and then you can do any editing.....
#####           I haven't really figured out how the meshes are drawn orignally, so the script creates double points for some faces...
#####           but saving it will remove the extra points and then all is well.... rick... FIXED!!!...sorta... lol
use strict;
use warnings;
our ($s, $ps, $x, $fp, $file, $i, $f, @pts, $p, $l, $fs, $ts, $ii, $pc, $sx);
our $spaces = " "x50;
$file = shift;     
open IN, "<C:/anim8or/projects/$file"; print "Working with $file\n";
while (<IN>)
{
   if ($_ eq "    points {\n")
   {
   while ( <IN> )
      {
      if ( $_ eq "    }\n" ){goto faces;} # can't modify $_ ... and a little lesson in regex ... lol
      $s = $_;                                  # $s = "            (x.xxxxx x.xxxxx x.xxxxx) (x.xxxxx x.xxxxx x.xxxxx) "   ...etc.
      $s =~ s/^\s+//;                         #       ^^^^^^                                                     remove these leading spaces...
      $s =~ s/\s{1}/\,/g;                   #                     ^       ^        ^        ^       ^        ^ substitute "," for " "
      $s =~ s/\),/\)\; /g;                   #                                                                ^ substitute "); " for "),"
      $s =~ s/\s\,$/ /;                      #                                                                ^ substitute " " for trailing " ,"
      $ps .= $s;                               # append the line to a new string      
      }
   }
faces:
if ($_ eq "    faces {\n")
   {
   $i = 0; $ts=""; $ii = 0;
   while ($s = <IN>)
      {
      if ( $s eq "    }\n" ){goto done;} 
      $s =~ s/^\s+//;
      if ( $s !~ /\)\s\)$|\)\s\)\s$/ )
         {
         $sx = <IN>; $sx=~ s/^\s+//;
         $s .= $sx;
         $s =~ s/\)\(/) (/;
         }
      $s =~ s/(\d+)\s(\d+)//;
      $fs .= "\$faces\[$ii\] = ($1, $2); ". substr ($spaces, 0, 20-(17+(length($1)+length($2)-length($ii))));
      $ii++; if ( $ii%6 == 0 ){$fs .= "\n";}
          $s =~ s/(\s\d*\s)(\-\d*\s\(\s)//;
      while ( length($s) > 3 )
         {
         $s =~ s/\((\d*)\s\d*\)\s//;
         $s =~ s/\((\d*)\)\s//;
         $ts .= "\$q[$i] = $1; ". substr ($spaces, 0, 20-(14+length($1)+length($i)));
         $i++; if ( $i%10 == 0 ){$ts .= "\n";}
         }
      }
   }
}
done: close IN;                                     # if you want more then one plug-in generated by this script, you'll need to change         
                                                    # \$basic to something else(\$basic2 comes to mind...lol)all thru this script...
$fp = "/*$spaces m2s.pl generated script for: $file. By: Freesailor Raceware */\n
/* Define Directives */
\#plugin(\"object\", \"mesh\", \"Basic\")\;
\#return(\$basic)\;

/* Declare Variables */
shape \$basic;
int \$i, \$x, \$pc, \$q[$i];\n";                   
@pts = split (" ", $ps);                            # split string into an array...
$x = scalar(@pts)-1; $f = $x+1; $l = 0;             # $x = numer of array elements...
$fp.= "point3 \$p[$f];
point2 \$faces[$ii];\n
/* Make Assignments */\n";
for $i ( 0..$x )                                    # get largest array element length...
   {
   if ( length( $pts[$i] ) > $l )
      {
      $l = length( $pts[$i] )
      }
    }
$l = $l+11; $f = 0;                                 # this works up to array element 999... if array is larger change 11 to 12...
for $i ( 0..$x )
   {
   $p = "\$p\[$i\] = $pts[$i] ";               # here we format the output to print the correct asl assignment instruction...               
                                                    # if you modify for all meshes..
   $p .= substr ($spaces, 0, $l-length($p));   # make it look pretty...
   $fp.= $p;                                   # append to final print line...
   $f++; if ($f == 4) { $fp .= "\n"; $f = 0; } # $f = number of point3 arrays per line...
   }
$fp .= "\n$fs\n\n$ts\n";                                # add our face and face vertex index arrays to the print line...then the code to execute...
$fp .= "
/* Draw Parametric */
\$pc = 0;
\$basic.Open();
for \$i = 0 to \$p.size-1 do
   {
   \$basic.AddPoint(\$p[\$i]);
   }
   for \$i = 0 to \$faces.size-1 do
      {
      \$basic.OpenFace(0,4);
      for \$x = \$pc to \$pc+(\$faces[\$i].x-1) do
          {
         \$basic.VertexN(\$q[\$x]);
         }
      \$basic.CloseFace();
      \$pc = \$pc+\$faces[\$i].x;
      }
\$basic.Close();
\n";
$fp .= "/*$spaces script for $file$spaces */";
print "\n$fp\n";
$file =~ s/.an8/.a8s/;
open OUT, ">C:/anim8or/scripts/$file";
print OUT "$fp\n";
close OUT;
exit;
hmmmm.. comments didn't copy so well, but you get the idea...lol
as the script comments, it does have some flaws, but will create a .a8s script that duplicates your model(simple ones please!!!)
aloha
freesailor


Logged

BOB_I_Ts

  • Full Member
  • ***
  • Posts: 155
    • View Profile
    • Anim8rOrg
Re: setting materials in ASL
« Reply #1 on: September 22, 2010, 07:26:43 pm »

i havent done much with materials but examples i have couple old asl material scripts

will only load cubemap~material once if material is detected this is done by using
texture $tex;
if ($tex == NULL) {} so even if material renamed the texture existence tell script not to reload them

other scripts with material i remember doing
New material.a8scommand script : only duplicate basic material setting ignoring textures

export_materialv1.a8s export basic material again no texture settings from object pallet into a .txt load script
Logged

freesailor

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: setting materials in ASL
« Reply #2 on: September 22, 2010, 08:49:16 pm »

tnx for the quick reply... i guess i wasn't very clear about my problem... i can define my materials OK( they both show on the materials button ), but  only one material is applied to the parametric... let me give an example:

$window.Open();
$window.SetMaterial(0, $mat0);
$window.OpenFace(0,4);
/* draw window frame */
...
...
$window.CloseFace();

$window.SetMaterial(1, $mat1);
$window.OpenFace(1,4);
/* draw glass pane */
...
...
$window.CloseFace();
$window.Close();

for reasons I can't solve, this doesn't work!?! the resulting parametric has only material zero applied, even to the glass...and the saved model shows no face with material index 1...unless I manually apply it... any ideas...anyone??
by the way, I downloaded your f8ce script, VERY nice piece of work BOB_I_Ts, then I ran my m2s script on a model... ended up with over 31,000 points, over 31,000 faces, and over 125,000 vertices, and script that was over 25,000 lines of code... took about 10 to 15 seconds to execute, but drew a exact match of the original, so I guess my model2script WILL work on complex models...lol
aloha and thanks again...
Logged

BOB_I_Ts

  • Full Member
  • ***
  • Posts: 155
    • View Profile
    • Anim8rOrg
Re: setting materials in ASL
« Reply #3 on: September 23, 2010, 09:35:36 pm »

thats great

im unsure about this setmaterial it had no effect for me either

i checked ASL document and it said
Note: Most mesh member functions cannot be applied to non-mesh shapes because the geometry of parameteric shapes in cannot be directly edited.  However SetMaterial() can be applied to material index 0, the default material.

another part in document say

AddPoint() and AddTexCoord() add new vertices and texture coordinates to a mesh.  The return value is an integer index that is used to refer to the new point or texture coordinate when adding new faces.  AddMaterial() adds a  new material to the Mesh and returns itís index.
« Last Edit: September 23, 2010, 09:38:38 pm by BOB_I_Ts »
Logged

freesailor

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: setting materials in ASL
« Reply #4 on: September 24, 2010, 08:47:09 pm »

hmmmm... tnx for the post....
after studying the problem some more and trying a couple other approaches, I came to the basically the same conclusion... parametrics won't accept two materials....Grrrr...
AddMaterial only produced an out of bounds memory error when I tried that...
Oh well, guess I'll have to apply the other materials after converting to mesh, not really that much of a problem for me, but if other people use my scripts, it may cause some unpredictable behavior with the face normals...
by the way, is there any other way to post scripts if you don't have a website? You might like to try my roof script, it is quite handy, it can draw a roof with full hips, full gables, or dutch gables at any position between the wall and roof peak...
I call it dial-a-gable...lol
aloha

oops, never mind, i just found the insert code  button...lol
« Last Edit: September 24, 2010, 08:55:25 pm by freesailor »
Logged

BOB_I_Ts

  • Full Member
  • ***
  • Posts: 155
    • View Profile
    • Anim8rOrg
Re: setting materials in ASL
« Reply #5 on: September 24, 2010, 10:18:50 pm »

sound like cool script

you can upload/attach files but only forum members can access them
click the reply button or when create new topic then click the "Additional Options..." link

forum allows
gif, jpg, png, txt, zip, an8, a8s, php, asp, html, htm
Maximum attachment size allowed: 9999 KB, per post: 4
Logged

freesailor

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: setting materials in ASL
« Reply #6 on: September 25, 2010, 03:12:34 am »

hmmm... guess that perl script wasn't an allowed type...oops!!!
here's the script... it isn't quite finished, I got hung up on that materials thing and kinda burned out on writing scripts... but it just needs barge rafters and texcoords... 'cept seeing as you can't set two materials, I'm thinking of just not including the coords, cause you'll just add them when you set the roof texture anyway.......
What I'm trying to do is create plug-ins that enable you to draw a structurally correct house from a blueprint...
and then use it in a 3d program so you can wander through the house....
so here's the roof script....
Logged