online store
top of page

Why abstractions are important in scripting

I’ve been teaching scripting and especially Python for years now and one of the biggest challenges I see for scripting newcomers is writing code that is fairly abstract while solving more than just the problem in front of us.


It’s like this math joke:

I understood math until they invited letters to it!

And to be honest: If you’re not used to it it is confusing. Recently I made a video explaining this concept in Python masterclasses but I’d also like to share it here with you.

Here is why abstractions and patterns are important in scripting:



Scripting is about finding patterns and writing code that can react to different situations. For example if you want to rename 10 files you cannot rely on their actual name but you need to break them down into their components, find similarities, modify the elements that need change and put them back together. Which means to write a successful script the elements you’re processing need a specific rule or pattern they’re following else your script would need to react to each of them individually which basically is the same as if you would modify them by hand.



Usually when creating a script we collect a sample that best represents the input that we get. Typical asset or file names, paths, node structures and so on. This is for example what we do in each assignment. All the data that you will work with is just a sample of a bigger picture but the idea is if your script can flexibly work with this sample it should also be able to handle similar situations and inputs. To achieve that we must look for patterns like positions in a filename or list and avoid replacing concrete information if not specifically asked for.


Depending on your background this week can be more or less challenging. When we look at the next example we have to realize that this is in most cases a sample of assets and we don't just want to solve this sample but all the other cases that are similar. So our code cannot be too concrete to only solve this problem but should be flexible enough to solve other problems with similar patterns.



Elon Musk once described the concept of reusability the following:

If we only could use a plane once for each flight it would cost one million dollars to fly in one direction and basically no one would fly.

Same spiel here: If your script only works for this situation, shot or asset and you have to update or rewrite it for any other shot or asset no one would script anymore but do it rather by hand.


Another important element of scripting is that after generally solving the task there is not only one solution that works but multiple. Some are better and some are worse. If we look at the task ahead. How would you solve that one?



We get to the best solution by thinking about the easiest way that we can solve the task and then starting to code. If we solved the problem we look at our solution and ask ourselves: “Does this look like the simplest solution?” and if the answer is not yes we think about other ways to solve it. A great way here is to look into the lecture notes for inspiration.


I hope this gave you a feel of how to work with abstraction and patterns. Don’t worry if you don’t get it right in the assignment the first try. That’s normal. The most important thing for now is to try and to get a feel of how to write code that solves multiple but similar situations since that’s what we want from Python:


To automate a collection of similar processes.


Like to learn more? Check out the rest of the masterclasses:


Thanks for reading,

Alex


PS. Subscribe to my Newsletter for more industry insights and scripting topics.



bottom of page