Mel Barebones L-System

 

Mel
Barebones L-System


return to main index

Links:
    scripts.zip
    L-System Introduction


 
Introduction

This tutorial presents a simple L-System intended to create a Koch snowflake, figure 1, and similar shapes (figure 2). Despite Mel being somewhat clumsy in regard to the handling of strings the code presented in listing 1 does demonstrate the principles of L-System string re-writing and string interpretation. A more extensive python implementation of a L-System can be found in the tutorial "L system: Code and Testing".



Figure 1 
$axiom = "L<zzL<zzL";
$ruleL = "L>zL<zzL>zL";
$angle = 60.0; 


Figure 2 
$axiom = ">zL";
$ruleL = "L<zL>zL>zL<zL";
$angle = 90.0;


The Scripts

The mel scripts in listings 1, 2 and 3 should be saved in the "scripts" directory of a Maya project directory. For example,

    maya/projects/Koch/scripts/koch.mel
    maya/projects/Koch/scripts/node_utils.mel
    maya/projects/Koch/scripts/preamble.mel

When the main script, kock.mel, is run it will generate a script named "snowflake.mel" that when sourced, 
    rehash;
    source "snowflake.mel";
will generate the shape shown in figure 1. Interpreting a string of characters as a sequence mel transformations and geometries is a little tricky. For example,
        zL 
means "apply a rotation around the z axis", then "insert a straight line curve" ie.

    rotate 0 0 60.0;
    curve -d 1 -p 0 0 0 -p 0 1 0;

In mel, however, the rotate statement is normally specified AFTER the geometry ie.

    curve -d 1 -p 0 0 0 -p 0 1 0;
    rotate 0 0 60.0;

otherwise the transformation will not effect the curve! The "node_utils.mel" (listing 2) and "preamble.mel" (listing 3) ensure that transformations and geometries are "parented" to a group node named "$tnode". The parenting enables the characters of a L strings to be interpreted in the classic "transform followed geometry" sequence without the need to rearrange the characters in either the "axiom" or the "rule". This makes it relatively easy to adapt examples of L strings taken from texts that deal with "Algorithmic Botany".


Extension to 3D

If the convertToMel() proc (koch.mel) is extended to handle rotations around the x and y axes some interesting 3D forms can be generated. 3D extension are left to the reader to explore.


 


Figure 3 
$axiom = ">zL";
$ruleL = ">yL<zL>xL>zL>zL>xL<zL";
$angle = 90.0; 


 

Listing 1 (koch.mel)


/*
A barebones example of an L-System for creating a Koch snowflake fractal.
Save this script, "node_utils.mel" and "preamble.mel" in the scripts folder
of the Maya project directory that is used for visualizing the graphics
output.
Started: Jan 9 2015
Malcolm Kesson
  
Character Interpretation:
L     draw a (stright curve) line
<     subsequent rotations are positive
>    subsequent rotations are negative
x    rotation axis is 'x'
y    rotation axis is 'y'
z     rotation axis is 'z'
*/
global float  $angle = 60.0;
global string $ruleL = "L>zL<zzL>zL";
  
string $axiom = "L<zzL<zzL";
//____________________________________________________________
// rewrite
//____________________________________________________________
// Given an input string this proc rewrites it by substituting 
// each occurance of "L" with a sequence of characters.
global proc string rewrite(string $in_str, int $generations) {
    global string $ruleL;
    int     $n, $i;
    string  $out_str = "";
    
    for($n = 0; $n < $generations; $n++) {
        for($i = 0; $i < size($in_str); $i++) {
            string $c = substring($in_str, $i+1, $i+1);
            if($c == "L")
                $out_str += $ruleL;
            else
                $out_str += $c;
            }
        $in_str = $out_str;
        if($n < $generations - 1)
             $out_str = "";
        }
    return $out_str;
    }
    
//____________________________________________________________
// convertToMel
//____________________________________________________________
// Interprets the characters of the input Lstr as a series of
// ouput mel statements. Note that not all characters have a
// mel equivalent. For example, the characters "<" and ">".
global proc string convertToMel(string $lstring, string $name) {
    global float  $angle;
    string  $lines[];
    int     $line_count = 0;
    int     $n;
    for($n = 0; $n < size($lstring); $n++) {
        string $c = substring($lstring, $n+1, $n+1);
        if($c == "<") 
            $angle = abs($angle);
        else if($c == ">")
            $angle = abs($angle) * -1;
        else if($c == "L")
            $lines[$line_count++] = "$tnode = addCurveTo($tnode);\n";
        else if($c == "z") {
            //$angle = rand($angle - 5, $angle + 5);
            $lines[$line_count++] = "rotate -r 0 0 " + $angle + " $tnode;\n";
            }
        }
        
    // In addition to generating an output "snowflake.mel" data script 
    // we make use of two other mel scripts. The "utilities.mel" script 
    // is sourced by "snowflake.mel". The contents of the "preamble.mel"
    // script is read and added to "snowflake.mel". Finally, the mel
    // statements generated from the Lstr are added to "snowflake.mel".
    string  $projPath = `workspace -q -rootDirectory`;
    string  $scriptsPath = $projPath + "scripts/";
    
    // 1. Open the output "Koch_shape.mel" for writing.
    string  $output_path = $scriptsPath + $name;
    int     $Koch_output = fopen($output_path, "w");
    
    // 2. Add the source statement for the node utilities mel script.
    string $source = "source \"" + $scriptsPath + "node_utils.mel\";\n";
    fprint($Koch_output, $source);
    
    // 3. Read "preamble.mel" and add its statements.
    int     $preamble = fopen($scriptsPath + "preamble.mel", "r");
    string  $text;
    $text = fread($preamble, $text);
    fprint($Koch_output, $text);
    fclose($preamble);
    
    // 4. Finally, write the mel commands that define the shape of
    //      the Koch snowflake.
    for($n = 0; $n < size($lines); $n++)
        fprint($Koch_output, $lines[$n]);
    fclose($Koch_output);
    
    return $output_path;
    }
  
string $Lstr = rewrite($axiom, 4);
string $outpath = convertToMel($Lstr, "snowflake.mel");
// print($Lstr + "\n");
print("Final LString has " + size($Lstr) + " characters.\n");


 

Listing 2 (node_utils.mel)


//Author: Malcolm Kesson (2007)
string $stack[];
int    $index = 0;
  
//===============================================
// addCurveTo
//===============================================
// Attaches a curve to the input group "node" then
// creates an empty group and makes it a child of
// the curve (transformation) node. So that the
// next curve will "grow" from the end of the current
// curve the local coordinate system is moved up
// one unit.
global proc string addCurveTo(string $node) {
    $shape = `curve -d 1 -p 0 0 0 -p 0 1 0`;
    parent -r $shape $node;  
    $child = `group -em`;    
    parent -r $child $shape; 
    move -os 0 1 0 $child;   
    return $child;            
    }
global proc string addConeTo(string $node) {
    $shape = `cone -ax 0 1 0 -r 1 -hr 3`;
    parent -r $shape[0] $node;  
    $child = `group -em`;    
    parent -r $child $shape[0]; 
    move -os 0 3 0 $child;   
    return $child;            
    }
global proc string addShapeTo(string $node, string $shapeCmd, 
                              float $premove, float $postmove) {
    $shape = eval($shapeCmd);
    move -os 0 $premove 0;
    parent -r $shape[0] $node;  
    $child = `group -em`;    
    parent -r $child $shape[0]; 
    move -os 0 $postmove 0 $child;   
    return $child;            
    }
  
//===============================================
// push - transformation stack
//===============================================
// To enable branching to occur we must keep a 
// reference to the "active" group node so that 
// we can return to it for parenting.
global proc string push(string $node) {
    global string $stack[];
    global int $index;
  
    $child = `group -em`;
    $test = `duplicate $node`;
    $stack[$index] = $test[0];
    $index += 1;
    return $child;
    }
  
//===============================================
// pop - transformation stack
//===============================================
// Calling this proc enables us to return to the
// base of a branch.
global proc string pop() {
    global string $stack[];
    global int $index;
  
    $index -= 1;
    if($index < 0) {
        print("Error: stack has become negative\n");
        return "";
        }
    return $stack[$index];
    }


 

Listing 3 (preamble.mel)


global string $stack[];
$nulls = `ls -tr "LSYS"`;
if(size($nulls) > 0) {
    select $nulls;
    delete;
    }
$geoList = `ls -geometry`;
select $geoList;
delete;
$nulls = `ls -tr "null*"`;
select $nulls;
delete;
    
clear($stack);
$root = `group -em -n LSYS`;
$tnode = `group -em`;
parent -r $tnode $root;







© 2002- Malcolm Kesson. All rights reserved.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生参加学科竞赛有着诸多好处,不仅有助于个人综合素质的提升,还能为未来职业发展奠定良好基础。以下是一些分析: 首先,学科竞赛是提高专业知识和技能水平的有效途径。通过参与竞赛,学生不仅能够深入学习相关专业知识,还能够接触到最新的科研成果和技术发展趋势。这有助于拓展学生的学科视野,使其对专业领域有更深刻的理解。在竞赛过程中,学生通常需要解决实际问题,这锻炼了他们独立思考和解决问题的能力。 其次,学科竞赛培养了学生的团队合作精神。许多竞赛项目需要团队协作来完成,这促使学生学会有效地与他人合作、协调分工。在团队合作中,学生们能够学到如何有效沟通、共同制定目标和分工合作,这对于日后进入职场具有重要意义。 此外,学科竞赛是提高学生综合能力的一种途径。竞赛项目通常会涉及到理论知识、实际操作和创新思维等多个方面,要求参赛者具备全面的素质。在竞赛过程中,学生不仅需要展现自己的专业知识,还需要具备创新意识和解决问题的能力。这种全面的综合能力培养对于未来从事各类职业都具有积极作用。 此外,学科竞赛可以为学生提供展示自我、树立信心的机会。通过比赛的舞台,学生有机会展现自己在专业领域的优势,得到他人的认可和赞誉。这对于培养学生的自信心和自我价值感非常重要,有助于他们更加积极主动地投入学习和未来的职业生涯。 最后,学科竞赛对于个人职业发展具有积极的助推作用。在竞赛中脱颖而出的学生通常能够引起企业、研究机构等用人单位的关注。获得竞赛奖项不仅可以作为个人履历的亮点,还可以为进入理想的工作岗位提供有力的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值