online store
top of page
Writer's pictureAlex

Mastering Python Code for Artists: A Beginner's Guide

Artists working in Visual Effects, Animation and Games often find themselves working in increasingly technical environments. In technologically-driven industries like films and games, the artistic and technical aspects go hand-in-hand to create the final product.


"The art challenges the technology, and the technology inspires the art." - John Lasseter

More and more artists feel the need to work with code to create faster and better work, but struggle with the steep learning curve to create automations associated with scripting and programming. In this article, we will discuss how artists with limited technical expertise can still integrate code snippets into their work and use them effectively.




reading foreign python scripts


A colleague popping by offering a few lines of helpful code or some lines of code found on the internet with promising results are often easy to come by. The hard part is to make them work. [Watch the "Scripting for Artists" series for how to use code in DCCs.] To effectively work with code snippets as an artist, there are two key questions to ask ourselves:


  1. What does the script do?

  2. How can I modify it to make it work for my goals?


By focusing on these 2 questions we can learn to work with code snippets without needing to understand every aspect of them.



Example 1:

Get Current Scene Path

Let's begin with a simple example. We have two lines of code in Maya:


import maya.cmds as cmds
cmds.file(q=True, sn=True)

As an artist, these lines might seem like gibberish at first. To understand the code, we need to answer our two key questions:

  1. What does the script do? In this case, the script gets the current scene path of the scene that is currently open in Maya.

  2. How can I modify it? To modify the script, we need to focus on the second line, which is where the scene path is extracted. We can modify this line to save the path in a variable or print it out.

By answering these questions, we can effectively use and modify the code snippet, even without extensive scripting knowledge.


import maya.cmds as cmds
file_path = cmds.file(q=True, sn=True)
print(file_path)


Example 2:

Snapping to First Selection

For a slightly more complex example, let's consider a function that snaps all selected objects in a scene to the first selected object:


import maya.cmds as cmds

selections = cmds.ls(selection=True, type='transform')

if len(selections) > 1:
    for asset in selections:
        cmds.copyAttr(selections[0], asset, values=True, 
                      attribute=['tx', 'ty', 'tz'])

else:
    cmds.warning('Select at least 2 objects.')

Again, we will answer our two key questions:

  1. What does the script do? This script snaps all selected objects in the scene to the first selected object.

  2. How can I modify it? To modify the script, for example, to snap objects to the second selection instead, we can change the index selections[0] to selections[1].

Through trial and error and by focusing on the specific functionality we want to achieve, we can modify the code snippet to suit our needs.


import maya.cmds as cmds

selections = cmds.ls(selection=True, type='transform')

if len(selections) > 1:
    for asset in selections:
        cmds.copyAttr(selections[1], asset, values=True, 
                      attribute=['tx', 'ty', 'tz'])

else:
    cmds.warning('Select at least 2 objects.')

Note: This code really works! So try it yourself in Maya. 🧑‍💻


Python for Maya
Python for Maya

"By focusing on these 2 questions, artists can learn to work with code snippets without needing to understand every aspect of the code."


Conclusion


Artists who are not experienced in Python can still work with code snippets by focusing on what the code does and how to modify it for their specific needs. By understanding these two aspects, everyone can adapt simple code snippets to their own projects and enhance their work.


If you want to learn how to make an impact in your work using Python; check out our Python for Maya, Python for Max or Python for Nuke masterclasses.


Until next time,

Alex


 

About the Author



I'm Alexander, an Award-Winning Technical Director & Coach in Visual Effects, Animation and Games. My skills are solving technical problems, simplifying workflows and mentoring career goals.


For more check out our exciting TD Bootcamp, 21 Artist Show Podcast and YouTube channel.

Comments


bottom of page