Home Technical Talk

Mel Scripting Help

2

Replies

  • gilesruscoe
    Options
    Offline / Send Message
    gilesruscoe polycounter lvl 10
    global proc randomPlace(){     int $density =  `intSliderGrp -q -value density`;
         int $scale = `intSliderGrp -q -value area`;
          string $placementHelper[] = `polyPlane -w $scale -h $scale -sx $density -sy $density -ax 0 1 0 -cuv 2 -ch 1`;
         int $vertexCount[] = `polyEvaluate -v $placementHelper`;
         
         int $randomNumber1 = `rand $vertexCount[0]`;
         float $vertexList[];
         
         
          float $treePos[] = `pointPosition pPlane1.vtx[$randomNumber1]`;   
          for( $i=0; $i<$density; $i++ )
           {
                 string $tree[] = `polyCube -name ("treeA"+$i)`;          
               int $randomNumber1 = `rand $vertexCount[0]`;
               float $treePos[] = `pointPosition pPlane1.vtx[$randomNumber1]`;  
               $vertexList[size($vertexList)] = `pointPosition pPlane1.vtx[$randomNumber1]`;   
               move -a $treePos[0] $treePos[1] $treePos[2] $tree[0];;
           }   
              //------ON DONE MOVING TREES------------//
         delete $placementHelper;
     }
      gilesTreeGen();
    
    I added the new code to stop getting double placements but getting back error : Cannot Convert data of type Float[] to type Float.

    Why is it trying to convert the array back into a normal float? ://
  • mccartm6
    Options
    Offline / Send Message
    Thanks for the help so far guys.
    Ok so i still have;nt manage to figure out the overlapping i am about as useful as a bag of hammers when it comes to Mel.
    I have a new question either way, i want to create a boundry which is adjustable using two intSLiderGrp. So say a limit on the x and z axis which the user dictates. Then within this boundry the trees will be randomly placed but none will go outside this boundry. Is this possible? or is there an easier way of doing it?

    heres my code so far
    // Create window with simple interface//
    
    string $window = `window -title "Forest Editor"
    -iconName "Short Name"
    -widthHeight 500 150
    -sizeable 0`;
    
    
    // creat lsider layout to determin th number of each tree species you wish to create//
    
    columnLayout -adjustableColumn true;
     frameLayout -label "Tree selection" -collapsable true -borderStyle "etchedIn";
    text -label  "Select the Number of each tree type you wood like to place. * for even placing see NOTE below";
    
            intSliderGrp -label "Number Of Oak Trees"
            -value 0
            -fieldMinValue 0
            -fieldMaxValue 100.0
            -field true Oak;
            intSliderGrp -label "Number Of Birch Trees"
            -value 0
            -fieldMinValue 0
            -fieldMaxValue 100
            -field true Birch;
            intSliderGrp -label "Number Of Maple Trees"
            -value 0
            -fieldMinValue 0
            -fieldMaxValue 100.0
            -field true Maple;
    
    // Create executable buttons with attached commands//
    
     frameLayout -label "Dispersal Pattern" -collapsable true -collapse false 
              -borderStyle "etchedIn";
    button -label "Create Forest With Random Dispersal" -command "CreateForest" "selectAndGroup";
    text -label " NOTE: When placing even spaced trees the number you select is represenative of rows. EX. selecting 3 oak wil give a 3x3 array";
    button -label "Create Forest with  Even" -command "CreateForestEven";
    
    button -label "Close" -command ("deleteUI -window " + $window);
    
    showWindow;
    
    // creat a global procedure for creating a randomly dispersed forest. The number of each tree type created should be tie to
    // intslidergroups in the gui. Here Polycubes represent the Oak tree, PolySpheres represent the Birch trees and PolyCones 
    // represent the maple trees.//
    
    global proc CreateForest(){
        int $count = `intSliderGrp -q -value Oak`;
        for( $i=0; $i<$count; $i++ )
    {
        polyCube
        -name ("myCube_"+$i);
        
        float $x = rand(-10, 10);
        float $y = rand(0, 0);
        float $z = rand(-10, 10);
        move $x 0.5 $z;
    }
    // To make each tree type easily selectable, when they are created they are placed into named group in the outliner//	
    string $myCubes[] = `ls -type shape "myCube_*"`; 
            group -n OakRandom $myCubes;
    
    
    {
        int $count = `intSliderGrp -q -value Birch`;
        for( $i=0; $i<$count; $i++ )
    {
        polySphere
        -name ("mySphere_"+$i);
        float $x = rand(-10, 10);
        float $y = rand(0, 0);
        float $z = rand(-10, 10);
        move $x 0.5 $z;
    }
    // To make each tree type easily selectable, when they are created they are placed into named group in the outliner//
    string $mySpheres[] = `ls -type shape "mySphere_*"`; 
            group -n BirchRandom $mySpheres;
    }
    
    
    {
        int $count = `intSliderGrp -q -value Maple`;
        for( $i=0; $i<$count; $i++ )
    {
        polyCone
        -name ("myCone_"+$i);
        float $x = rand(-10, 10);
        float $y = rand(0, 0);
        float $z = rand(-10, 10); 
        move $x 0.5 $z;
    }
    // To make each tree type easily selectable, when they are created they are placed into named group in the outliner//
    string $myCones[] = `ls -type shape "myCone_*"`; 
            group -n MapleRandom $myCones;
    
    }
    }
    
    
    // creat a global procedure for creating a evenly dispersed forest. The number of each tree type created should be tie to
    // intslidergroups in the gui. Here Polycubes represent the Oak tree, PolySpheres represent the Birch trees and PolyCones 
    // represent the maple trees.//
    
    
    
    global proc CreateForestEven(){
        int $count = `intSliderGrp -q -value Oak`;
        for($rows=0; $rows<$count; $rows++)
        {
            for ($cols=0; $cols<$count; $cols++)       
           { 
            polyCube
            -name ("myCube_"+$rows);
            move -r ($cols*-4) 0.5 ($rows*4);
            
        }
    }
    // To make each tree type easily selectable, when they are created they are placed into named group in the outliner//
    string $myCubes[] = `ls -type shape "myCube_*"`; 
            group -n Oakeven $myCubes;
    
    {
        int $count = `intSliderGrp -q -value Birch`;
       for($rows=0; $rows<$count; $rows++)
        {
            for ($cols=0; $cols<$count; $cols++)       
           { 
            polySphere
            -name ("mySphere_"+$rows);
            move -r ($cols*4) 0.5 ($rows*-4);
            
        }
    }
    // To make each tree type easily selectable, when they are created they are placed into named group in the outliner//
    string $mySpheres[] = `ls -type shape "mySphere_*"`; 
            group -n BrichEven $mySpheres;
    }
    
    
    {
        int $count = `intSliderGrp -q -value Maple`;
       for($rows=0; $rows<$count; $rows++)
        {
            for ($cols=0; $cols<$count; $cols++)       
           { 
            polyCone
            -name ("myCone_"+$rows);
            move -r ($cols*4) 0.5 ($rows*4);
            
        }
    }
    // To make each tree type easily selectable, when they are created they are placed into named group in the outliner//
    string $myCones[] = `ls -type shape "myCone_*"`; 
            group -n MaplesEven $myCones;
    }
    }
    
    

    Any help would be great!
  • haiddasalami
    Options
    Offline / Send Message
    haiddasalami polycounter lvl 14
    Okay so the reason is pointPosition is giving 3 values but vertex list will only take one value (float). So we just change the data type to vector and all is good :)
    float $vertexList[]
    
    to
    vector $vertexList[]
    
  • Denny
    Options
    Offline / Send Message
    Denny polycounter lvl 14
    Edit* haiddasalami beat me to it. :)

    gilesruscoe >> This line is wrong:
    $vertexList[size($vertexList)] = `pointPosition pPlane1.vtx[$randomNumber1]`;
    

    You're trying to store a position to a float array. In this case you would want a vector array or remember the vertices by index.


    mccartm6 >> It's absolutely doable but it changes a little bit depending on if you're just cutting away what's placed outside or if you want to squeeze in the same amount of trees but on a smaller area. I think you could run a procedure afterwards that checks if any tree is placed outside a coordinate and remove it then.
  • mccartm6
    Options
    Offline / Send Message
    ideally id like to squeeze the selected number of trees into the coordinates. That way the user gets the number of trees they have selected. What would be best of going about this. I was thinking one way would be to set the grid as the boundary that way all the user would do is set the grid the size they want and then run the script but i don't know if that's possible?
2
Sign In or Register to comment.