Due to a miscommunication between Elsevier Press and the Python Software Foundation, the first printing of this title used an unauthorized modification of the trademarked Python logo. We apologize to the PSF for this, and they have been understanding of our in-press status; in the 2nd and subsequent printing of this title, we shall use a cover design that has been approved as non-dilutive by the Python Software Foundation.

Help make the world a better place and make a secure donation to the Python Software Foundation today!

 

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


14 Responses to “Chapter 2”

  1. 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

  2. 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.

    • 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

      • 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.

  3. 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?

    • 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 #

  4. Teresa the point in “”sphere+’.ty'”” too

  5. 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.

  6. 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_’)

  7. 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

  8. 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 ?

  9. Never mind, I forgot to add the s to contents

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.