Python OOP & Maya


#1

Hello everyone, I studied the python and wrote some rather short scripts with it in maya.
I wanted to know where I should start learning OOP related to maya scripting…
Because I’ve read some books that briefly touched the OOP out of maya environment and now I can’t figure out where should I use OOP in my scripts and how to start with that.
I know all of GUIs can be written as OOP and depend on complexity of the other stuff, they can be written as OOP or by functions… But can anyone explain what is the benefit of OOP for GUIs and as such a workflow for writing them?


#2

If you are writing scripts with pymel then you probably should.

PS. OOP is better for reusing the code for one.


#3

Thanks “whisperwing”, but let me ask the question this way:

Here is a piece of code that I wrote earlier for may rename Tool (). Now lets say I want to convert the code to python and use the class structure, How should I do that? It would be great if there be a someone who translates some of this to python using classes and show me the results, and is there any benefit from doing that?

Code:

// Main Code: Getting the inputs from textFields and 
// Defining the output and new names for selected objects
//--------------------------------------------------------

global proc Rename()
{
//==============================================
//declaring internal variables
	
	string $selObjects[] = `ls -sl`;
	string $prefix = `textField -q -tx prefixTF`;
	string $baseName = `textField -q -tx newNameTextField`;
	string $suffix = `textField -q -tx suffixTF`;
	int $kfCount = `intField -q -value keepFirstIf`;
	int $klCount = `intField -q -value keepLastIf`;
	int $objNum = 0;
	string $kfDigits = startString($selObjects[$objNum], $kfCount);
	string $klDigits = endString($selObjects[$objNum], $klCount);
	int $number = `intField -q -value baseNumIf`;
	int $step = `intField -q -value stepNumIf`;
	

//===============================================================
//Conditions for different state of checkboxes

	if(`checkBox -q -value baseNameCB` == 1)
		{
		for($each in $selObjects)
			{
			string $newName = $prefix+$kfDigits+$baseName+$klDigits+$suffix+"_"+$number;
			rename $each $newName;
			$number += $step;
			$objNum++;
			}	
		}

	else if(`checkBox -q -value baseNameCB` == 0 && `checkBox -q -value kfCB`== 0 && `checkBox -q -value klCB`== 0)	
		{
		for($each in $selObjects)
			{
			string $newName = $prefix+$selObjects[$objNum]+$suffix+"_"+$number;
			rename $each $newName;
			$number += $step;
			$objNum++;
			}
		}
	
	else if(`checkBox -q -value baseNameCB` == 0 && `checkBox -q -value kfCB`== 1 || `checkBox -q -value klCB`== 1)
		{
		for($each in $selObjects)
			{
			string $newName = $prefix+$kfDigits+$klDigits+$suffix+"_"+$number;
			
			$number += $step;
			$objNum++;	
			}
		}
	else
	{
	confirmDialog -button "OK" 
			-message "An unknown error happend!" 	
			-ma "center" -title "Fatal Error";
}
}

Thanks in advance :slight_smile:


#4

@lightSicle (and probably others who have the same question)… :smiley:
Here is a tutorial that may answer to some if not all of your questions about python classes:
http://pytut.infogami.com/node11-baseline.html

It helped me to some degree… now I can understand why is the difference between using
classes and functions somehow.
Anyway, I try to translate my “Rename Tool” to python using classes and find the answers to
my other questions along the way.
You can also find the original Rename Tool (3ds max’s rename window for maya) here:
http://www.creativecrash.com/maya/downloads/scripts-plugins/modeling/misc/c/rename-tool

After reading the books like “Python Programming for the Absolute Beginner” and “How to Think Like a Computer Scientist Learning with Python”, can someone lists beginner to advance
python books for me?
Thanks


#5

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.