26th Jan2012

Intro to Python – A must read !

by Johan Borgström


If you are interested to learn Python for use in Maya I recommend “Maya Python for Games and Film” by Adam Mechtley and Ryan Trowbridge. It is simply awesome. I can honestly say that it is one of the best software / workflow / programming books I ever read. As I explain Python on this blog much of the info is based on their work.

26th Jan2012

Intro to Python – Accessing & distributing scripts

by Johan Borgström

To make a folder containing Python scripts visible to Maya one way is to add the path to the folder to the Pyhon Path in the Maya.env file. It is located C:\Users\[your user name]\Documents\maya\[your maya version].

Simply add the path to the file. If you wish to add multiple folders just insert a semi colon between the paths


PYTHONPATH = D:\Projects\python

To check wich paths Maya has setup run the following Python script in the script editor.


import sys

syspaths = sys.path

for p in syspaths: print p

Module

A Module is simply a standalone Python file and can consist of how many or few lines of code as required.

Packages

One convenient way to distribute Python scripts is to use packages. To make a folder into a package add a file called __init__.py to the folder. This file does not need to contain anything but a convenient thing is to import the modules inside the folder ( or package ). Importing in the __init__.py file lets us access the “sub modules” through the package namespace. Letsuse an example to demonstrate this.

We have in our maya.env file added the path to a folder containing scripts or packages we wish to access. In the folder specified by the python path we add a folder called “petfactory” for instance. This folder contains a file called __init__.py and a python module ( script file ) named mammal.py. Below is the content of the mammal.py file.


Below is the content of the mammal.py file. For an intro to creating a python class see the post Intro to Python – How to write a class


class Pet(object):

	type = 'mammal'

	def __init__(self, aSpecies):
		self.species = aSpecies
		print('a pet %s was created' %self.species)

	def speak(self, aSound):
		print(aSound * 2)

Below is the content of the __init__.py file.


import mammal

So when we wish to create a Pet in Maya we enter the following in a Python tab in the script editor


import petfactory

# create a Pet instance
myPet = petfactory.mammal.Pet("Dog")

myPet.species
# Result: Dog #
26th Jan2012

Intro to Python – How to write a class

by Johan Borgström

In this post I give a quick intro to Python. To get the most out of this example it is good to be familiar to the concept of Object Oriented Programming (OOP). My aim is to give an intermediate introduction / reference resource of how to use Python in your Maya workflow.


class Pet(object):

    # When a var is declared in the class scope it is
    # available to all instances.
    # Further more we do not need to instantiate the class to access it.
    # To me it seems equivalent to static vars of other languages.

    type = 'mammal'

    # The __init__ function is run when a the class is instantiated.
    # Altough it seems like a constructor it isn´t.
    # The __init__ function is not required,
    # but if defined it will run when an object is instantiated.

    def __init__(self, aSpecies):
        self.species = aSpecies
        print('a pet %s was created' %self.species)

    # the first arg to a method is aleays self, it is ref to the instance
    # if you leave this out you will get argument error.

    def speak(self, aSound):
        print(aSound * 2)

# Lets create an instance of our Pet class
# and give it a species of type dog

a = Pet('dog')

# then lets check out some properties of our new pet !

a.species
# Result: 'dog' #

a.type
# Result: 'mammal' #

a.speak('wooof ');
# prints wooof wooof

And some complementary trivia from Wiki:

The word “mammal” comes from the Latin mamma (“breast”). All female mammals nurse their young with milk which comes out from special glands, the mammary glands

25th Jan2012

V-Ray produced overbright or invalid color

by Johan Borgström

I just had an interesting error that I never had before. I got the following warning:

// Warning: Material "VRayMtl6@material" produced overbright or invalid color
 (rgb=1.#INF00 1.#INF00 1.#INF00). //
// Warning: Material "VRayMtl4@material" produced overbright or invalid color
 (rgb=1.#INF00 1.#INF00 1.#INF00). //
// Warning: Invalid light cache sample(s). //

When I looked at the light multiplier it said inf. When I tried to use the slider it toggled between inf and nan. I interpret that as infinite and not a number. Really strange. I set it back to a resonable number using the keyboard and the error was gone.

Weird.

23rd Jan2012

Preikestolen, Stavanger, Norway

by Johan Borgström

A little bit of topic perhaps or maybe not. I saw this picture in a swedish paper the other day and I realized that I must go there. So if you are going to Stavanger in Norway you must see this ! According to Lonley Planet It is one of the 10 most amazing views in the world among other sites like Grand Canyon Skywalk, Victoria Falls och Aucklands Sky Tower.

20120123-223800.jpg

23rd Jan2012

V-Ray sphere fade

by Johan Borgström

The Vray sphere fade volume node lets you render everything inside a spherical volume and disregard the rest. To set this up first enable the “use environment volume” in the Vray > environment tab in the render settings. Then map a volumetric > Vray sphere fade volume  to the input map. Then create create > V-Ray > VRaySpherFade volume. Control the bg color, sphere radius etc through the attributes on the VRayspherFade node.

20th Jan2012

Code hinting stop working Flash CS5

by Johan Borgström

I am still using the Flash CS5 IDE for my interaction mockup sketches. I am thinking of switching to Flashbuilder or the open source alternative FlashDevelop. One thing that bugged me for a day or two was that the code hinting suddenly stopped working. I looked on some forums and was advised to clear the codehinting cache etc. One post said that there should be an icon in the bottom right corner of the code editor that was informing the user about this. It is really hard to notice if you don’t know where to look for it. it looks like this.

As you can see I had to many files on my class path, so what I needed to make this work was to remove some of my files on my class path.

edit > Preferences > ActionScript > Language: ActionScript 3 Settings > Class Path.

And my Code hinting is back to its sweet self  ! Hurra !

17th Jan2012

Pecha Kucha GBG

by Johan Borgström

If you are in Gothenburg the 21st of February or 24th of April and you are in the mood for some potential inspiring presentations make sure that you meet up at Pecha Kucha GBG. It starts at 20.00 at Park Lane. ( I think, check www.pecha-kucha.se for details )

20120117-224352.jpg

04th Jan2012

Illustrator tip !

by Johan Borgström

I just happen to discover a nice keyboard shortcut using illustrator. To create a new layer just press ctrl + L. To popup the layer properties press Ctrl + alt + L. ( it is really a lowercase L but for disambiguation I use the uppercase one).

I like !