Friday 21 December 2018

Saving data to json with python

Wabaluba dub dub it's free-code Friday!
Sometimes you sit down on nice chill weekend and think to yourself, oh boy I want to store some data!

But in all seriousness, let's say you want to run something this evening and return to it tomorrow. Or perhaps you would like to retrieve data from a previous Maya session that crashed, or from a different Maya session entirely - You can save this data out to a separate file (essentially a big text file) called a Json.
Json's are nice to use because they're easy to read/edit and easy to implement into your code. Here's some example code of how to use them below!

import json

# Create some data
data = {1 : 'a', 2 : 'b', 3 : 'c'}

filePath = "C:/User/Documents/test_data.json"

# Write/Export from variable to text file

with open(filePath, 'w') as f:

  json.dump(data, f, indent=4)

# Read/Load from text file to variable

with open(filePath, 'r') as fp:

    data = json.load(fp)

# Read/Load from dictionary text file to ORDERED dictionary

with open(filePath, 'r') as fp:

    data = json.load(fp, object_pairs_hook=collections.OrderedDict)

Wednesday 18 July 2018

Python dictionary examples

Over the course of my ramblings and 3d work I have collated a collection of example scripts that I refer back to every once in a while, perhaps when I forget how to use a particular piece of syntax or if it's something I use fairly irregularly.

Dictionaries are something that I use all-the-time. And if you're looking to get into python scripting then is definitely something you'll want to be using too.

Dictionaries are like a more-lovely list/array.
Here's what I have jotted down in my file:
# Create an empty dictionary, just like we would with a list.
vegetables = {}

# Append/Create our first entries; 'carrot' and 'tomato'. We assign a sub-dictionary to both of them as their values.
vegetables['carrot'] = {'colour' : 'orange','grown' :' underground'}
vegetables['tomato'] = {'colour' : 'red','grown' : 'vine'}

# An example of looping through each key and values using the in-built '.iteritems()' dictionary method:
for key,value in vegetables.iteritems():
    print key, value

# An example of running logic with a '.get()' method:
for key, value in vegetables.iteritems():
    if value.get("colour") == "orange":
        print "{} is an orange vegie".format(k.title())

# Make it ordered - (Explaination below)
import collections
vegetables = OrderedDict(sorted(vegetables.items()))

One thing about dictionaries, is that by default they store their information un-ordered. This means if we wanted to access the first element when we created the dictionary, it would not necessarily be the first element returned when we go to retrieve the first element later on. 

Think of the process like this:
creating a dictionary = drawing out a new deck of cards
immediately afterwards, python helpfully shuffles this deck of cards, keeping all the cards & data, but not the order. This is to help save memory when dealing with large amounts of data but.. this has caused me more than one headache when I assume that the dictionary would retain the order that it had when I first created it. BEWARE of this.

Happy.. dictionarying?

Tuesday 17 July 2018

Intuitive Maths

Check out this b-e-a-utiful website dedicated to helping people understand common maths concepts intuitively.

From articles about the history of mathematical language, to the incredibly intuitive explanation of the Pythagoras theorem. This website details methods that have completely changed my perspective on the way I view some key mathematical concepts.

Tuesday 29 May 2018

Incredible Photoshop plugin - Nezumi (lazy mouse)

Hot diggity dog this one is a game-changer.
Do you ever think to yourself one of these three things:

"Man, I wish I could have lazy mouse in Photoshop just like I do in Zbrush."
"If only Photoshop's brushes were just slightly.. smoother.. more voluptuous.."
"... just one more ctrl+z... need to redraw this line as its not quite perfect yet.."

Well fret no longer! Take a look at this fancy Photoshop plugin
It has a 15 day free-trial, and believe me at the end of your trial you'll be throwing your wallet at this goddamn beautiful plugin.

Nezumi essentially takes your line, and makes it lovely. Fortunately, they've included a load of fantastic gifs on their website which I will put right below here so I don't need to arduously type out the description of what this thing does:



And a beautiful array of settings you can choose from.

Not only that but as well as the smoothing function it has a load of awesome, easily editable rulers that you can use to help draw circles/ovals. Go grab this tool.

Friday 25 May 2018

List of Insanely Useful Sine Graph Equations

Hooolla hola hola hola! This is insanely useful!

Do you want to animate something with code, but you don't want it to be your everyday dull sine?
Look no further, I have found an -incredible- image!


Look at all these glorious equations beautifully laid out before your eyes!

Now you can have a bouncy-ball (pointy) sine, which could be useful for procedurally animating waves,
A linear zig-zag sine, which could be useful for animating a robot or mechanics,
A slightly rounded sine, which could be useful for animating something gently hovering

and much much more!

The possibilities are limitless! Now GO! Go use these fantastic formulas and animate me all that is siney and wavey!