Chapter 2
Basics of Python Data
Author
Ryan Trowbridge & Adam Mechtley
Project
Using variables on conjunction with Maya commands
Example Files
None
Synopsis
This chapter provides a brief overview of variables and Python’s data model. We compare variable typing in Python to MEL and provide information on Python’s memory management system. We then examine some details of working with Python’s core object types.
Resources
Python String Methods
Unicode Table
Regular Expressions
Other Notes
None
Errata for the First Edition
None
Hey Guys,
Good Book so far! I’m learning a lot, and it’s nice to learn python in terms of Maya versus generic computer programming. In any case I’m up to page 42 of your book where you give the example controlling a sphere’s translation with a cube’s rotation. I got to the multiply and divide note part…typed in the script and got this error message: # Error: RuntimeError: The destination attribute ‘multiplyDivide2.input1x’ cannot be found.
Here’s the code i typed into the script editor:
sphere = maya.cmds.polySphere()[0];
cube = maya.cmds.polyCube()[0];
mult = maya.cmds.createNode(‘multiplyDivide’)
maya.cmds.connectAttr(cube+’.ry’,mult+’.input1x’);
maya.cmds.setAttr(mult+’.input2x’,1.0/90.0);
maya.cmds.connectAttr(mult+’.output1x’,sphere+’,ty’);
maya.cmds.select(cube);
Can you guys tell me what’s wrong?
Also since I’m here I wanted to ask, when you use the createNode command does that first string determine what type of node you’re creating…and then if you want to name just n?
I’m a huge newb to scripting and programming–so I apologize if this is a silly question.
Thanks,
Teresa
Teresa said this on November 8, 2011 at 5:24 am |
Hi Teresa! Glad you’re enjoying the book so far. The problem in your code sample is that the capitalization of the attributes is wrong. It needs to be input1X, not input1x. As for your second question, you are correct. You may find this page helpful.
Adam said this on November 9, 2011 at 9:53 am |
Hello Adam,
I’m learning maya scripting now and I find your book very helpful.
Can you please explain the input1X, input2X, outputX thing? I tried to find them in documents but I didn’t find anything.
Thanks a lot,
Sty
Sty said this on April 9, 2014 at 11:55 pm |
Hey Sty,
You can find information on attributes of built-in nodes in the node reference in the help (Technical Documentation section at the very bottom of the help browser). For example, here is the documentation for the multiplyDivide node.
Adam said this on April 11, 2014 at 9:29 am |
Hello,
I’m having trouble with something mentioned on page 32. The book mentions that python provides a built in function, type(), which allows you to determine the type of a variable at any point.
Whenever I try to execute that function with a variable inside the parenthesis, I get an error in maya. An example would be:
contents = ‘toys’
type(contents)
# Error: TypeError: file line 2: ‘module’ object is not callable #
Any idea why I am getting this error?
Bryan Silva said this on September 27, 2015 at 7:52 pm |
Im actually further along in the book on page 65 where Im trying to following along but running into the following:
def process_all_textures():
print(‘process all textures’)
type(process_all_textures)
# Error: TypeError: file line 4: ‘module’ object is not callable #
Bryan Silva said this on September 27, 2015 at 8:11 pm |
by the way, I have the correct indenting, Im copying and pasting what is printed in the script editor after I run the code.
Bryan Silva said this on September 27, 2015 at 8:12 pm |
c =’a’
print(type(c))
Alexander Martinez said this on December 18, 2017 at 1:57 pm |
Teresa the point in “”sphere+’.ty'”” too
Alexander said this on December 13, 2017 at 4:10 pm |
There are four settings for the operator; Do nothing, Multiply, Divide, and Power. They are explained below.
outputX = input1X * input2X
outputY = input1Y * input2Y
outputZ = input1Z * input2Z
check http://download.autodesk.com/us/maya/2008help/Nodes/multiplyDivide.html.
Alexander Martinez said this on December 13, 2017 at 4:13 pm |
pag 138
no arguments
process_all_textures();
# single positional argument
process_all_textures(texture); is (‘texture’)
# multiple positional arguments
process_all_textures(texture, ’grass_’); is (‘texture’, ’grass_’)
Alexander Martinez said this on December 14, 2017 at 9:58 am |
Hi there, great book so far,
I have a question regarding the id() of the odds and evens list talk about on page 55-56
when I execute the script I get and id odds and evens,(as expected) when I execute the exact same script a second time I get a different id for odds and evens and when I execute the exact same script a third time the id for odds and evens goes back to being the same as the first time I ran the script. Not what I expected… why is this?
here is my code:
odds=[1,3,5,7]
evens=[2,4,6,8]
numbers= (odds,evens)
print(‘odds’,id(odds))
print(‘evens’,id(evens))
print(‘numbers’,id(numbers))
and running the same code three times gives me this result in the history pane:
odds=[1,3,5,7]
evens=[2,4,6,8]
numbers= (odds,evens)
print(‘odds’,id(odds))
print(‘evens’,id(evens))
print(‘numbers’,id(numbers))
(‘odds’, 3066774756616L)
(‘evens’, 3066774756552L)
(‘numbers’, 3066771084424L)
odds=[1,3,5,7]
evens=[2,4,6,8]
numbers= (odds,evens)
print(‘odds’,id(odds))
print(‘evens’,id(evens))
print(‘numbers’,id(numbers))
(‘odds’, 3066774756808L)
(‘evens’, 3066774756872L)
(‘numbers’, 3066771157256L)
odds=[1,3,5,7]
evens=[2,4,6,8]
numbers= (odds,evens)
print(‘odds’,id(odds))
print(‘evens’,id(evens))
print(‘numbers’,id(numbers))
(‘odds’, 3066774756616L)
(‘evens’, 3066774756552L)
(‘numbers’, 3066771084360L)
I had expected to get the same id every time for all three odds,evens and numbers
Matt said this on November 28, 2018 at 2:41 pm |
Hi, I am following the variable inputs on page 32 on number 9.
It asks you to convert the variable to a string:
Execute the Following lines to convert the variable to a string.
contents=str(contents);
print(type(contents));
But I am getting instead of ?
Leon Cruz said this on June 28, 2019 at 3:49 pm |
Never mind, I forgot to add the s to contents
Leon Cruz said this on June 28, 2019 at 3:52 pm |