Complete Maya Programming, Vol. II: An In-Depth Guide to 3D Fundamentals, Geometry, and Modeling (Morgan Kaufmann Series in Computer Graphics and Geometric ... Morgan Kaufmann Series in Computer Graphics)
معرفی کتاب «Complete Maya Programming, Vol. II: An In-Depth Guide to 3D Fundamentals, Geometry, and Modeling (Morgan Kaufmann Series in Computer Graphics and Geometric ... Morgan Kaufmann Series in Computer Graphics)» نوشتهٔ David A. D Gould، منتشرشده توسط نشر Morgan Kaufmann Publishers در سال 2005. این کتاب در فرمت pdf، زبان انگلیسی ارائه شده است.
Chapter One
Introduction
This book endeavors to build upon your existing experience in Maya programming. As such, this book assumes that you are already familiar with basic MEL and/or C++ API programming. If you have never written MEL scripts or C++ plug-ins, you are highly encouraged to read the first volume. It covers all of the basics of Maya programming, as well as how Maya works internally. This knowledge will be critical when developing more advanced scripts and plug-ins.
All too often your work in Maya will involve problem solving. Although it is often easy to formulate a solution in general abstract terms, the task of actually implementing the solution can be daunting. If you read the first volume, you have a strong understanding of how Maya works, as well as of the MEL and C++ API programming interfaces. Thus, the actual task of writing a script or plug-in shouldn't be too difficult. The next step will be to apply your knowledge of computer graphics principles and mathematics to implement the solution.
This often proves to be the greatest hurdle. The most common reason for not being able to implement a solution is due to a lack of understanding of computer graphics principles. Without a solid grasp of basic computer graphics concepts, all problem solving will become more difficult. Computer graphics is based on mathematics, and many people are quite reluctant to learn mathematics. A common reason for this is that the mathematics is presented in abstract and theoretical terms with little application to solving real-world problems. This book presents the most important fundamental mathematical concepts without diverging into esoteric mathematical areas that have little practical use. For instance, the dot product is covered in detail. This mathematical operation is used extensively in computer graphics for calculating such things as angles, rotations, sidedness, lengths, areas, and the amount of light reflected from a surface. All of this is possible from an operation that involves little more than a few multiplications and additions. Independently of Maya programming, a solid grasp of these computer graphics concepts will hold you in good stead for all of your future work. As your knowledge increases, you will become more adept at combining and using these mathematical building blocks to create more robust and efficient solutions.
The explanation of each mathematical concept is accompanied by ample source code and scripts that demonstrate how to implement the concept. The source code and scripts can be used as a starting point for your own solutions. The entire spectrum of computer graphics concepts through to geometry and modeling is covered.
The most fundamental building blocks of computer graphics are points and vectors. Many problems can be solved using the simple point and vector operations, and thus understanding them will provide a strong foundation for all further chapters. Rotations merit their own chapter in that they can often be the source of much confusion. There are many ways to represent rotations and orientations, and thus it is important to understand their advantages and disadvantages in order to best apply them to your work. Integral to computer graphics is the process of transforming (scaling, shearing, rotating, translating, and projecting) objects. Transformations are most efficiently implemented using matrices, covered in this book in detail. Transformations provide an important level of abstraction for building hierarchies of objects. Being able to retrieve and transform points at any level in a hierarchy are particularly useful skills in many computer graphics applications.
Progressing from the basic building blocks, the next topic covered is geometry. Geometry uses points and vectors to represent more complex shapes such as curves and surfaces. All of Maya's supported geometry types are covered in their own respective chapters. Each chapter covers the tasks of displaying, editing, and creating each geometry type. A detailed explanation of the components that make up each type is also given. The most basic, yet most pervasive, type of geometry — polygonal meshes — is covered first. NURBS curves and surfaces are subsequently explained in detail.
Finally, the increasingly popular geometry type, subdivision surfaces, is covered. Each different type of geometry has its strengths and weaknesses. Some are better suited for games development, whereas others are more appropriate for industrial design. The reader will gain a greater understanding of each geometry type's advantages and disadvantages, so that an informed decision can be made as to which one is best to use. The process of writing geometry importers and exporters is greatly simplified once you have a greater understanding of Maya's various geometry types. Custom modeling tools can also be created that are adapted to a given geometry type. Developing your own tools will provide you with a far greater level of control and functionality than those provided with Maya.
1.1 EXAMPLE FILES
Note that all files used in this book are available at:
www.davidgould.com
Information available at the site includes the following.
MEL scripts, C++ source code, and makefiles for all examples in the book
Additional example MEL scripts
Additional example C++ source code
Errata for this book
Continually updated glossary
Updated list of other relevant web sites and online resources
1.1.1. COMPILING EXAMPLE PLUG-INS
New versions of both Maya and C++ compilers are being constantly released. Rather than provide potentially outdated and obsolete instructions in this book, the complete set of instructions for downloading and compiling the companion files for this book are available at:
www.davidgould.com
Here you will find all C++ source code and makefiles necessary to compile the plug-ins on your own computer. There are also instructions for creating your own plug-in makefiles from scratch.
1.1.2. SOURCING EXAMPLE MEL SCRIPTS
To source any of the example MEL scripts, Maya must be able to locate them. It is possible to include the complete path to the source command, but if multiple MEL scripts are being sourced it is easier to set up the MAYA_SCRIPT_PATH environment variable correctly. This variable simply needs to be updated, as follows, to include the directory where the example MEL scripts are located.
1. Open the Script Editor.
2. Execute the following to initialize the $exampleScripts string to the path of the directory containing the example MEL scripts.
string $exampleScripts = .
For example:
string $exampleScripts = "C./DavidGould/MEL Scripts".
When specifying a path in Windows with backslashes, be sure to use two backslashes. A single backslash will be interpreted as an escape sequence. The same path with backslashes would therefore be written as follows.
string $exampleScripts = "C-\\DavidGould\\MEL Scripts";
Maya will automatically convert all directory paths with backslashes to forward slashes.
3. Execute the following:
string $newScriptPath-$exampleScripts + ";" +'getenv "MAYA_SCRIPT_PATH"'; putenv "MAYA_SCRIPT_PATH" $newScriptPath
The first line initializes the $newScriptPath variable to the example MEL scripts path and then appends the current setting for the MAYASCRIPT_PATH variable. The second line uses the putenv command to set the MAYA_SCRIPT_PATH variable to the path.
With the MAYA_SCRIPT_PATH environment variable now updated, sourcing any MEL script can be done the same way. For example, to source the foobar.mel script the following code would be executed:
source foobar.mel;
The previous steps need to be completed for each new Maya session. Thus, if Maya is restarted the previous steps should be performed.
1.2 EXECUTING MEL CODE IN THE SCRIPT EDITOR
There are a lot of snippets of MEL code throughout this book. Many readers will want to type this MEL code into the Script Editor and then execute it. This will work fine in most cases. There will often be a problem when you execute different blocks of MEL code that use the same variable name but assume a different type. This problem is demonstrated in Maya as follows.
1. Open the Script Editor.
2. Execute the following.
$myVar = 1.0
The result is displayed. // Result. 1 //
This creates the $myVar and assigns it an initial value.
3. To see what would happen if there were another piece of MEL code that used the same variable but as a different type, execute the following.
$myVar = "hi"
This produces an error.
// Warning" Converting string "hi" to a float value of O. // // Result- 0 //
4. Execute the following.
whatls "$myVar"
The variable's data type is printed out.
// Result" float variable //
The problem is that although you are executing another piece of MEL code the $myVar variable still exists. The attempt to assign a string to it failed because the variable is already defined as a float. Once the data type (string, float, int, and so on) is defined for a variable it can't be changed.
The underlying problem is that all variables defined in the Script Editor are automatically made global variables, even if you don't explicitly make them. Thus, executing the statement
$myVar = 1.0
in a script would make it a local variable. This same statement executed in the Script Editor is the equivalent of writing
global $myVar = 1.0
The variable is implicitly made global. Once a variable is global there is no way to delete it. The only way is to restart Maya and thereby remove all global variables and start afresh. Note that this behavior also extends to procedures. Any procedure defined in the Script Editor will automatically become global.
What is needed is a way of defining the variable to be local. Unfortunately there is no explicit keyword (an opposite to the gl 0bal keyword) that makes a variable local. This is, however, a way of implicitly making a variable local. By using code blocks, a variable is implicitly made local. At the end of the code block the variable is automatically deleted. This is precisely what is needed to ensure that running several sections of MEL code doesn't define the same global variable. This also prevents a "contamination" of the global name space of variables with variables you intended only for learning and testing.
5. Restart Maya by closing it and then opening it again.
6. Execute the following in the Script Editor.
{ $myVar = 1.0; print $myVar; }
The value of $myVaris printed out.
1
Because the definition of the variable was enclosed in braces, this created a separate code block. All variables defined within the block are automatically local. When the closing brace (}) is reached, all variables defined within the block are deleted. This ensures that the $myVar variable no longer exists after the code is executed and prevents it being added to the list of global variables.
7. Execute the following.
{ $myVar = "hi"; print $myVar ; }
The value of $myVar is printed out.
hi
There was no error this time when $myVarwas defined because it is local to the block and is deleted when the block is finished.
Thus, the general rule is that if you ever intend on executing MEL code in the Script Editor simply enclose it in braces to ensure that it runs as a separate block. There may be times when you want to determine if a given variable is global. The following MEL procedure is designed to return true if the supplied variable is global, and false otherwise.
global proc int isGlobal( string $var ) { string $globals = 'env'; for( $glob in $globals ) { if( $glob =-$var ) return true; } return false; }
This procedure can then be used to test if $myVar is a global variable.
isGlobal( "$myVar" )
The result is 0 (false). Note that the variable name is enclosed in quotation marks ("). This ensures that the variable name is passed to the procedure and not its value. Also note that this procedure is a global procedure and thus can be called from anywhere within Maya (script, Script Editor, and so on) once it is defined.
Chapter Two
Points
Points and vectors provide the fundamental building blocks upon which geometry is based. Before covering the specifics of Maya's point and vector types it is important to understand the mathematical basis for points and vectors.
2.1 DIMENSIONS
The dimension of a point is the number of coordinates it has. Maya doesn't provide an explicit 2D point or vector, although a 3D point or vector can be used for the same purpose. Maya provides 3D points in MEL and 4D points (homogenous points) in the C++ API.
(Continues...)
Excerpted from COMPLETE MAYA PROGRAMMING VOLUME II by David A. D. Gould Copyright © 2006 by Elsevier Inc.. Excerpted by permission of MORGAN KAUFMANN. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site. David Gould's acclaimed first book, Complete Maya Programming: An Extensive Guide to MEL and the C++ API, provides artists and programmers with a deep understanding of the way Maya works and how it can be enhanced and customized through programming. In his new book David offers a gentle, intuitive introduction to the core ideas of computer graphics.
Each concept is explained progressively and is fully implemented in both MEL and C++ so that an artist or programmer can use the source code directly in their own programs. Geometry and modeling are covered in detail with progressively more complex examples demonstrating all of Maya's possible programming features. David Gould's first volume is widely regarded as the most authoritative reference on Maya programming. Volume II continues this tradition and provides an unmatched guide for the artist and programmer tackling complex tasks.
* Covers a spectrum of topics in computer graphics including points and vectors, rotations, transformations, curves and surfaces (polygonal, NURBS, subdivision), and modeling.
* Offers insights to Maya's inner workings so that an artist or programmer can design and develop customized tools and solutions.
* Discusses problem solving with MEL (Maya's scripting language) and the more powerful and versatile C++ API, with plenty of code examples for each. David Gould's acclaimed first book, Complete Maya Programming: An Extensive Guide to MEL and the C++API, provides artists and programmers with a deep understanding of the way Maya works and how it can be enhanced and customized through programming. In his new book David offers a gentle, intuitive introduction to the core ideas of computer graphics. A solid understanding of these concepts is critical to solving a wide variety of problems. Each concept is explained progressively and is fully implemented in both MEL and C++ so that an artist or programmer can use the source code directly in their own programs. Geometry and modeling are covered in detail with progressively more complex examples demonstrating all of Maya's possible programming features.David Gould's first volume is widely regarded as the most authoritative reference on Maya programming. Volume II continues this tradition and provides an unmatched guide for the artist and programmer tackling complex tasks.