<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<title>Loïc Pinsard</title>
	<subtitle>Ramblings about rigging, programming and games.</subtitle>
	<link href="https://loic-pinsard.com/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="https://loic-pinsard.com"/>
	<generator uri="https://www.getzola.org/">Zola</generator>
	<updated>2023-02-23T00:00:00+00:00</updated>
	<id>https://loic-pinsard.com/atom.xml</id>
	<entry xml:lang="en">
		<title>Effortless Serialisation with Cattrs</title>
		<published>2023-02-23T00:00:00+00:00</published>
		<updated>2023-02-23T00:00:00+00:00</updated>
		<link href="https://loic-pinsard.com/blog/effortless-serialisation-with-cattrs/" type="text/html"/>
		<id>https://loic-pinsard.com/blog/effortless-serialisation-with-cattrs/</id>
		<content type="html">&lt;p&gt;Cattrs is an amazing library that I feel isn&#x27;t known well enough.&lt;br &#x2F;&gt;
I am making this post as a quick introduction hoping more people will pick up on it.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;serialising-dataclasses&quot;&gt;Serialising Dataclasses&lt;&#x2F;h2&gt;
&lt;p&gt;One of its biggest strength is how it&#x27;s capable of serialising &lt;code&gt;dataclasses&lt;&#x2F;code&gt; (and &lt;code&gt;attrs&lt;&#x2F;code&gt; classes) simply by leveraging the type annotations of the fields of the classes.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-circle-info&quot;&gt;&lt;&#x2F;i&gt;
    Info
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;In all the examples I will be using &lt;code&gt;attrs&lt;&#x2F;code&gt; but everything would work equally as well with dataclasses&lt;&#x2F;p&gt;
&lt;p&gt;Definitely not me trying to get you to use attrs over dataclasses, I swear.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Say we have a simple Vector3 class like so:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;attr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;define
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;define
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;Vector3&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    x: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;float
&lt;&#x2F;span&gt;&lt;span&gt;    y: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;float
&lt;&#x2F;span&gt;&lt;span&gt;    z: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;float
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;we can serialise it with &lt;code&gt;cattrs.unstructure&lt;&#x2F;code&gt; which gives us this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;cattrs
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;my_vector &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;Vector3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;serialised_vector &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;unstructure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(my_vector)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;print(serialised_vector)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;gt;&amp;gt;&amp;gt; {&amp;#39;x&amp;#39;: 1.0, &amp;#39;y&amp;#39;: 2.0, &amp;#39;z&amp;#39;: 3.0}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and to deserialise it with &lt;code&gt;cattrs.structure&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;deserialised_vector &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;unstructure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(serialised_vector, Vector3)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;print(deserialised_vector)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;gt;&amp;gt;&amp;gt; Vector3(x=1.0, y=2.0, z=3.0)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote class=&quot;admonition success&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-circle-question&quot;&gt;&lt;&#x2F;i&gt;
    Question
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;That&#x27;s fine and all but how is that different from &lt;code&gt;asdict&lt;&#x2F;code&gt; and &lt;code&gt;Vector3(**serialised_vector)&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Glad you asked!&lt;br &#x2F;&gt;
In this simple case, the results would be exactly the same but let&#x27;s compare with a more complex example.&lt;&#x2F;p&gt;
&lt;p&gt;let&#x27;s first improve our &lt;code&gt;Vector3&lt;&#x2F;code&gt; class to make our lives easier&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;define
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;Vector3&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    x: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;float
&lt;&#x2F;span&gt;&lt;span&gt;    y: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;float
&lt;&#x2F;span&gt;&lt;span&gt;    z: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;float
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;classmethod
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;zeros&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;cls&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; Self:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;cls&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;classmethod
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;ones&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;cls&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; Self:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;cls&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This lets us easily create vectors with commonly used default values.&lt;&#x2F;p&gt;
&lt;p&gt;Now let&#x27;s define a &lt;code&gt;Transform&lt;&#x2F;code&gt; class that uses our &lt;code&gt;Vector3&lt;&#x2F;code&gt; class:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;define
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;Transform&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    translate: Vector3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;field&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;Vector3.zeros)
&lt;&#x2F;span&gt;&lt;span&gt;    rotate: Vector3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;field&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;Vector3.zeros)
&lt;&#x2F;span&gt;&lt;span&gt;    scale: Vector3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;field&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;Vector3.ones)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here&#x27;s how &lt;code&gt;asdict&lt;&#x2F;code&gt; fares against &lt;code&gt;cattrs.unstructure&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;transform &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;Transform&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;print(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;asdict&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(transform))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;gt;&amp;gt;&amp;gt; {&amp;#39;translate&amp;#39;: {&amp;#39;x&amp;#39;: 0, &amp;#39;y&amp;#39;: 0, &amp;#39;z&amp;#39;: 0}, &amp;#39;rotate&amp;#39;: {&amp;#39;x&amp;#39;: 0, &amp;#39;y&amp;#39;: 0, &amp;#39;z&amp;#39;: 0}, &amp;#39;scale&amp;#39;: {&amp;#39;x&amp;#39;: 1, &amp;#39;y&amp;#39;: 1, &amp;#39;z&amp;#39;: 1}}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;print(cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;unstructure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(transform))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;gt;&amp;gt;&amp;gt; {&amp;#39;translate&amp;#39;: {&amp;#39;x&amp;#39;: 0, &amp;#39;y&amp;#39;: 0, &amp;#39;z&amp;#39;: 0}, &amp;#39;rotate&amp;#39;: {&amp;#39;x&amp;#39;: 0, &amp;#39;y&amp;#39;: 0, &amp;#39;z&amp;#39;: 0}, &amp;#39;scale&amp;#39;: {&amp;#39;x&amp;#39;: 1, &amp;#39;y&amp;#39;: 1, &amp;#39;z&amp;#39;: 1}}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Alright, exactly the same.&lt;&#x2F;p&gt;
&lt;p&gt;What about &lt;code&gt;Transform(**serialised_transform)&lt;&#x2F;code&gt; vs &lt;code&gt;cattrs.structure&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;serialised_transform &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;unstructure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(transform)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;print(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;Transform&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;serialised_transform))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;gt;&amp;gt;&amp;gt; Transform(translate={&amp;#39;x&amp;#39;: 0, &amp;#39;y&amp;#39;: 0, &amp;#39;z&amp;#39;: 0}, rotate={&amp;#39;x&amp;#39;: 0, &amp;#39;y&amp;#39;: 0, &amp;#39;z&amp;#39;: 0}, scale={&amp;#39;x&amp;#39;: 1, &amp;#39;y&amp;#39;: 1, &amp;#39;z&amp;#39;: 1})
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;print(cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;structure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(serialised_transform, Transform))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;gt;&amp;gt;&amp;gt; Transform(translate=Vector3(x=0.0, y=0.0, z=0.0), rotate=Vector3(x=0.0, y=0.0, z=0.0), scale=Vector3(x=1.0, y=1.0, z=1.0))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oh! Now there&#x27;s a difference!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Transform(**serialised_transform)&lt;&#x2F;code&gt; didn&#x27;t deserialise things recursively and our &lt;code&gt;translate&lt;&#x2F;code&gt;, &lt;code&gt;rotate&lt;&#x2F;code&gt;, &lt;code&gt;scale&lt;&#x2F;code&gt; fields are plain old dictionnaries when they should be &lt;code&gt;Vector3&lt;&#x2F;code&gt; instances.&lt;&#x2F;p&gt;
&lt;p&gt;However, &lt;code&gt;cattrs.structure&lt;&#x2F;code&gt; did what we were expecting and we can use our &lt;code&gt;Transform&lt;&#x2F;code&gt; instance as intended.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-pen&quot;&gt;&lt;&#x2F;i&gt;
    Note
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;You&#x27;ll notice that the individual Vector values are actual &lt;code&gt;float&lt;&#x2F;code&gt; and not &lt;code&gt;int&lt;&#x2F;code&gt;.&lt;br &#x2F;&gt;
This makes sense since this is how we&#x27;ve annotated the fields but it&#x27;s nice to see.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;This alone makes &lt;code&gt;cattrs&lt;&#x2F;code&gt; a worthwile addition to your toolbet, but it doesn&#x27;t stop there.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;serialising-anything&quot;&gt;Serialising Anything&lt;&#x2F;h2&gt;
&lt;p&gt;So, we&#x27;ve seen this worked well with nicely type annotated classes, but this isn&#x27;t always the case and we don&#x27;t always have the possibility to add annotations to all the types we use (in legacy code, 3rd party libraries, etc).&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m guessing most of my readers are Maya Users so let&#x27;s use that as an example and modify our &lt;code&gt;Transform&lt;&#x2F;code&gt; class to use &lt;code&gt;MVector&lt;&#x2F;code&gt; instead&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;maya.api.OpenMaya &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;MVector
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;define
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;Transform&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    translate: MVector &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;field&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;MVector.kZeroVector)
&lt;&#x2F;span&gt;&lt;span&gt;    rotate: MVector &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;field&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;MVector.kZeroVector)
&lt;&#x2F;span&gt;&lt;span&gt;    scale: MVector &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;field&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;MVector.kOneVector)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s see how that serialises:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;transform &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;Transform&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;print(cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;unstructure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(transform))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;gt;&amp;gt;&amp;gt; {&amp;#39;translate&amp;#39;: maya.api.OpenMaya.MVector(0, 0, 0), &amp;#39;rotate&amp;#39;: maya.api.OpenMaya.MVector(0, 0, 0), &amp;#39;scale&amp;#39;: maya.api.OpenMaya.MVector(1, 1, 1)}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Eh. &lt;code&gt;cattrs&lt;&#x2F;code&gt; did its best, but at this point it has no way of knowing what a serialised &lt;code&gt;MVector&lt;&#x2F;code&gt; looks like.&lt;&#x2F;p&gt;
&lt;p&gt;Fortunately, we can tell it how to do exactly that:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;register_unstructure_hook&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(MVector, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;lambda &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;: {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;x&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;: v.x,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;y&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;: v.y,  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;z&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;: v.z})
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;print(cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;unstructure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(transform))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;gt;&amp;gt;&amp;gt; {&amp;#39;translate&amp;#39;: {&amp;#39;x&amp;#39;: 0.0, &amp;#39;y&amp;#39;: 0.0, &amp;#39;z&amp;#39;: 0.0}, &amp;#39;rotate&amp;#39;: {&amp;#39;x&amp;#39;: 0.0, &amp;#39;y&amp;#39;: 0.0, &amp;#39;z&amp;#39;: 0.0}, &amp;#39;scale&amp;#39;: {&amp;#39;x&amp;#39;: 1.0, &amp;#39;y&amp;#39;: 1.0, &amp;#39;z&amp;#39;: 1.0}}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that&#x27;s better!&lt;&#x2F;p&gt;
&lt;p&gt;Can it deserialise it though?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;serialised_transform &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;unstructure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(transform)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;print(cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;structure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(serialised_transform, Transform))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# Error: While structuring Transform (3 sub-exceptions)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# Traceback (most recent call last):
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#   File &amp;quot;&amp;lt;maya console&amp;gt;&amp;quot;, line 19, in &amp;lt;module&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#   File &amp;quot;C:\Users\loic\projects\website\.venv\Lib\site-packages\cattrs\converters.py&amp;quot;, line 309, in structure
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#     return self._structure_func.dispatch(cl)(obj, cl)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#   File &amp;quot;&amp;lt;cattrs generated structure __main__.Transform&amp;gt;&amp;quot;, line 22, in structure_Transform
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#     if errors: raise __c_cve(&amp;#39;While structuring &amp;#39; + &amp;#39;Transform&amp;#39;, errors, __cl)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# cattrs.errors.ClassValidationError: While structuring Transform (3 sub-exceptions) #
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Uh oh!&lt;br &#x2F;&gt;
Just like with serialisation cattrs doesn&#x27;t know what to do with our &lt;code&gt;MVector&lt;&#x2F;code&gt;, but again, we can tell it what to do:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;register_structure_hook&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(MVector, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;lambda &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;MVector&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;d.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;values&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;()))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;serialised_transform &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;unstructure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(transform)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;print(cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;structure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(serialised_transform, Transform))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;gt;&amp;gt;&amp;gt; Transform(translate=maya.api.OpenMaya.MVector(0, 0, 0), rotate=maya.api.OpenMaya.MVector(0, 0, 0), scale=maya.api.OpenMaya.MVector(1, 1, 1))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Success!&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-pen&quot;&gt;&lt;&#x2F;i&gt;
    Note
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;&lt;code&gt;MVector&lt;&#x2F;code&gt; doesn&#x27;t take keyword arguments so we have to unpack the values directly.&lt;br &#x2F;&gt;
for most classes you would be able to write this instead&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;register_structure_hook&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(C, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;lambda &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;d))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;working-with-json&quot;&gt;Working with JSON&lt;&#x2F;h2&gt;
&lt;p&gt;Now as nice as this is, you&#x27;ll want to write this to a file at some point but unfortunately, this won&#x27;t always work&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s go over a failing example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;typing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Set
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;define
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;MyClass&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    my_set: Set[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;float&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And let&#x27;s try dumping that to a json string&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;json
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;my_instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;MyClass&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;({&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;})
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;serialised_instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;unstructure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(my_instance)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;print(serialised_instance)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;gt;&amp;gt;&amp;gt; {&amp;#39;my_set&amp;#39;: {1, 2, 3, 4}}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;json_str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;json.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;dumps&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(serialised_instance)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# Error: Object of type set is not JSON serialisable
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# Traceback (most recent call last):
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#   File &amp;quot;&amp;lt;maya console&amp;gt;&amp;quot;, line 12, in &amp;lt;module&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#   File &amp;quot;C:\Program Files\Autodesk\Maya2023\Python\lib\json\__init__.py&amp;quot;, line 231, in dumps
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#     return _default_encoder.encode(obj)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#   File &amp;quot;C:\Program Files\Autodesk\Maya2023\Python\lib\json\encoder.py&amp;quot;, line 199, in encode
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#     chunks = self.iterencode(o, _one_shot=True)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#   File &amp;quot;C:\Program Files\Autodesk\Maya2023\Python\lib\json\encoder.py&amp;quot;, line 257, in iterencode
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#     return _iterencode(o, 0)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#   File &amp;quot;C:\Program Files\Autodesk\Maya2023\Python\lib\json\encoder.py&amp;quot;, line 179, in default
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;#     raise TypeError(f&amp;#39;Object of type {o.__class__.__name__} &amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# TypeError: Object of type set is not JSON serialisable #
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can see that &lt;code&gt;cattrs.unstructure&lt;&#x2F;code&gt; worked as intended, but JSON has no concept of what a &lt;code&gt;set&lt;&#x2F;code&gt; is and simply doesn&#x27;t know what to do with it.&lt;&#x2F;p&gt;
&lt;p&gt;There are a couple options to solve this, I can think of 3 right now:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Make a custom JSON encoder. Eh.&lt;&#x2F;li&gt;
&lt;li&gt;Register structure&#x2F;unstructure hooks for &lt;code&gt;set&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;Use the preconfigured Json Converter provided by &lt;code&gt;cattrs&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Let&#x27;s explore that 3rd point:
Up until now when we&#x27;ve registered hooks, we&#x27;ve registered them in cattrs&#x27; &amp;quot;Global Converter&amp;quot;
Cattrs provides us with a bunch of preconfigured Converters with some pre-defined hooks, including one for &lt;code&gt;json&lt;&#x2F;code&gt;.
On top of that, these converters implement the &lt;code&gt;loads&lt;&#x2F;code&gt; and &lt;code&gt;dumps&lt;&#x2F;code&gt; functions, making them easy to use instead of the actual&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;cattrs.preconf.json &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;make_converter
&lt;&#x2F;span&gt;&lt;span&gt;json &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;make_converter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;my_instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;MyClass&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;({&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;})
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;serialised_instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cattrs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;unstructure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(my_instance)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;json_str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;json.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;dumps&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(serialised_instance, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;indent&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;gt;&amp;gt;&amp;gt; &amp;#39;{&amp;quot;my_set&amp;quot;: [1, 2, 3, 4]}&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And we can load it back like that:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;new_instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;json.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;loads&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(json_str, MyClass)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-pen&quot;&gt;&lt;&#x2F;i&gt;
    Note
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;&lt;code&gt;cattrs&lt;&#x2F;code&gt; provides a few other converters, you can find more information about them &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;catt.rs&#x2F;en&#x2F;stable&#x2F;preconf.html&quot;&gt;here&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;Hopefully that gave you a good taste of what cattrs is capable of and how easy it is to work with.
I encourage you to go through the &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;catt.rs&#x2F;en&#x2F;stable&#x2F;index.html&quot;&gt;docs&lt;&#x2F;a&gt; to find out more and learn about some of the features I haven&#x27;t covered.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Rigging as a programming language</title>
		<published>2022-11-07T00:00:00+00:00</published>
		<updated>2022-11-07T00:00:00+00:00</updated>
		<link href="https://loic-pinsard.com/blog/rigging-as-a-programming-language/" type="text/html"/>
		<id>https://loic-pinsard.com/blog/rigging-as-a-programming-language/</id>
		<content type="html">&lt;p&gt;There are a lot of parallels that can be made between rigging and programming, especially when working with node-based frameworks&lt;&#x2F;p&gt;
&lt;h2 id=&quot;riggers-are-programmers&quot;&gt;Riggers are programmers&lt;&#x2F;h2&gt;
&lt;p&gt;I don&#x27;t mean that in the sense that riggers are writing python code to generate their rigs. This is just a byproduct of the lack of good rigging tools out there.&lt;&#x2F;p&gt;
&lt;p&gt;I mean that &lt;em&gt;rigging is programming.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The job of the rigger is to build the tool (The rig) that the animator will use to move and animate an asset.&lt;br &#x2F;&gt;
That comes with the same considerations needed when building a piece of software.&lt;br &#x2F;&gt;
To name a few:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Good User experience
Animators need rigs that are well designed and that suit their purposes.&lt;&#x2F;li&gt;
&lt;li&gt;Performance.&lt;br &#x2F;&gt;
A rig that has all the features but runs at 2 FPS is just not usable.&lt;&#x2F;li&gt;
&lt;li&gt;Scalability of deployment.&lt;br &#x2F;&gt;
Whether you&#x27;re an indie dev or working on a AAA Game or Big budget movie, Making a rig and &amp;quot;deploying&amp;quot; it to the animators should be a process that you can trust and that doesn&#x27;t get in the way.&lt;&#x2F;li&gt;
&lt;li&gt;Probably more.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;nodes-are-functions&quot;&gt;Nodes are functions&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;function-definitions&quot;&gt;Function Definitions&lt;&#x2F;h3&gt;
&lt;p&gt;Most node graphs will let you make a node out of other nodes. When you do that, you are essentially creating a function definition.
The contents of your node is its definition:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The &amp;quot;Input Node&amp;quot; holds the function arguments&lt;&#x2F;li&gt;
&lt;li&gt;The &amp;quot;Output Node&amp;quot; holds the return values&lt;&#x2F;li&gt;
&lt;li&gt;Everything in between is the body of the function.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Here&#x27;s a mock up graph of a Dot Product Function:
&lt;img src=&quot;images&#x2F;function_definition.png&quot; alt=&quot;Node Graph Function Definition&quot; &#x2F;&gt;
Which is exactly the same as this python function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;dot_product&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;: Vector3, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;: Vector3) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;float&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;a.x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span&gt;b.x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;a.y &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span&gt;b.y &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;a.z &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span&gt;b.z
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;function-calls&quot;&gt;Function Calls&lt;&#x2F;h3&gt;
&lt;p&gt;Creating a new &amp;quot;instance&amp;quot; of a node is the same as calling that function:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The node itself is the Function Call&lt;&#x2F;li&gt;
&lt;li&gt;Connecting or setting the input attributes is the same as passing values to the function arguments.&lt;&#x2F;li&gt;
&lt;li&gt;Connecting&#x2F;setting the output attributes is the same as using the return values of the function.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Again, here&#x27;s an example of a graph that calls the Dot Product function we just made:
&lt;img src=&quot;images&#x2F;function_call.png&quot; alt=&quot;Node Graph Function Call&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;And here&#x27;s the python code equivalent to this graph&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;vector_a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;Vector3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;vector_b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;Vector3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;dot_product&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(vector_a, vector_b)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;objects-are-uh-objects&quot;&gt;Objects are... uh... Objects&lt;&#x2F;h2&gt;
&lt;p&gt;Things like joints transforms, geometries, etc are not much more than Structs.&lt;br &#x2F;&gt;
Every object you create in your scene is an instance of that struct.&lt;br &#x2F;&gt;
They have attributes that can be manipulated by the Nodes&#x2F;functions&lt;&#x2F;p&gt;
&lt;h1 id=&quot;so-what-are-the-advantages-of-thinking-of-it-this-way&quot;&gt;So what are the advantages of thinking of it this way?&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;scalability&quot;&gt;Scalability&lt;&#x2F;h2&gt;
&lt;p&gt;In theory a rigging framework built this way would only need to come with low level functions (basic math operations and types)
Then from that higher level functions can be built: IK Solvers, constraints, twist extractors, etc.&lt;br &#x2F;&gt;
Then you have even higher level functions, these are your typical rig components: Arms, Legs, Wings, Spines, etc.&lt;br &#x2F;&gt;
Then finally the highest level functions are your full rigs: Bipeds, Quadrupeds, Birds, etc.&lt;&#x2F;p&gt;
&lt;p&gt;And just like that you have a very complete Rigging Library (Oh look another programming term!)&lt;&#x2F;p&gt;
&lt;h2 id=&quot;iteration-speed&quot;&gt;Iteration speed&lt;&#x2F;h2&gt;
&lt;p&gt;Most DCCs require some sort of custom tooling to make rigging viable in producion.&lt;&#x2F;p&gt;
&lt;p&gt;The workflow tends to be like this:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Prototype the rig manually&lt;&#x2F;li&gt;
&lt;li&gt;Automate the rig creation&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;When it should be this:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Create the rig.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;A system like this would most likely make build scripts or rigging systems (mGear, Rigify, etc) obselete.&lt;&#x2F;p&gt;
&lt;p&gt;In a way, the rig &lt;em&gt;is&lt;&#x2F;em&gt; the build script.&lt;br &#x2F;&gt;
It is the program that takes all the control transformations and solves the positions of the vertices.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;an-approach-that-works-for-everyone&quot;&gt;An approach that works for everyone&lt;&#x2F;h2&gt;
&lt;p&gt;The beauty of such a system is it can work for all types of users.&lt;br &#x2F;&gt;
On one hand, the power users will want to mess around with the low level stuff and have precise control over everything.&lt;br &#x2F;&gt;
Oh the other hand the less tech savy will simply want to create a rig for their characters and will just need to use the Biped function.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;versatile&quot;&gt;Versatile&lt;&#x2F;h2&gt;
&lt;blockquote class=&quot;admonition warning&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-fire-flame-curved&quot;&gt;&lt;&#x2F;i&gt;
    Warning
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;I am not a Software Engineer so some details here might not be accurate.&lt;br &#x2F;&gt;
I do think the overall Ideas I&#x27;m describing are true though.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;A Programming Language is at its core an Abstract Syntax Tree (The data structure that represents the order of all the operations of the program)&lt;&#x2F;p&gt;
&lt;p&gt;Basically, this is a Generic representation of the source code that can be used to do many different ways:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;It can be Interpreted to actually run the Program&#x2F;Rig.&lt;&#x2F;li&gt;
&lt;li&gt;It can be &amp;quot;compiled&amp;quot; back to a Source file of the same language (This is typically what Code Formatters do)
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-pen&quot;&gt;&lt;&#x2F;i&gt;
    Note
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;The AST would be the bridge between the node graph and the source code, letting users define and call functions in their preferred way.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;It can be &amp;quot;compiled&amp;quot; (or transpiled) to a source file of a different language.
This is what TypeScript does to end up generating JavaScript code.
&lt;blockquote class=&quot;admonition success&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-lightbulb&quot;&gt;&lt;&#x2F;i&gt;
    Tip
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;Imagine a rig transpiled to GLSH or HLSL shaders that runs &lt;em&gt;entirely&lt;&#x2F;em&gt; on the GPU.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;It can be &amp;quot;compiled&amp;quot; to something else.
The same AST could be used to Build a Maya rig as well as Building the equivalent  Blender Rig&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Pytest, VScode and Maya</title>
		<published>2022-06-26T00:00:00+00:00</published>
		<updated>2022-06-26T00:00:00+00:00</updated>
		<link href="https://loic-pinsard.com/blog/pytest-vscode-and-maya/" type="text/html"/>
		<id>https://loic-pinsard.com/blog/pytest-vscode-and-maya/</id>
		<content type="html">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h2&gt;
&lt;p&gt;So you’re using VScode and you’re into unit testing but unfortunately for you, you’re a maya user and you have to resort to running your tests in the command line.&lt;&#x2F;p&gt;
&lt;p&gt;Rejoice! You can actually get the VSCode test explorer to work with mayapy though the setup can be a bit hacky.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-circle-info&quot;&gt;&lt;&#x2F;i&gt;
    Info
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;This blog post assumes you’re running Windows, Python 3 and Maya 2022+
But you should be able to adapt this for any other OS, Python 2 and earlier versions of Maya (And likely other test libraries too.).&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;maya-project-structure&quot;&gt;Maya Project Structure&lt;&#x2F;h2&gt;
&lt;p&gt;You’ll need a project folder structured as a maya module looking similar to this.
The actual template project can be found here on &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Muream&#x2F;maya-pytest-template&quot;&gt;github&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;plaintext&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-plaintext &quot;&gt;&lt;code class=&quot;language-plaintext&quot; data-lang=&quot;plaintext&quot;&gt;&lt;span&gt;maya-pytest-template&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;├── .venv&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;├── .vscode&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;│   └── settings.json
&lt;&#x2F;span&gt;&lt;span&gt;├── scripts&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;│   ├── maya_pytest_template&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;│   │   └── __init__.py
&lt;&#x2F;span&gt;&lt;span&gt;│   └── userSetup.py
&lt;&#x2F;span&gt;&lt;span&gt;├── tests&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;│   ├── __init__.py
&lt;&#x2F;span&gt;&lt;span&gt;│   ├── test_maya_pytest_template.py
&lt;&#x2F;span&gt;&lt;span&gt;│   └── conftest.py
&lt;&#x2F;span&gt;&lt;span&gt;├── mayapy.bat
&lt;&#x2F;span&gt;&lt;span&gt;├── pytest.bat
&lt;&#x2F;span&gt;&lt;span&gt;└── maya_pytest_template.mod
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you&#x27;ve worked with maya modules before, this should look pretty familiar with a few exceptions we&#x27;ll cover in a minute.&lt;br &#x2F;&gt;
If you haven&#x27;t you can read up on that more here:&lt;&#x2F;p&gt;
&lt;h2 id=&quot;setting-up-pytest-to-work-with-maya&quot;&gt;Setting up Pytest to work with Maya&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;create-a-virtualenv&quot;&gt;Create a virtualenv&lt;&#x2F;h3&gt;
&lt;p&gt;Let’s first create a virtualenv and install pytest in it. In a terminal type this:&lt;&#x2F;p&gt;
&lt;div class=&quot;console no-output&quot;&gt;
  &lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;python -m venv .venv
&lt;&#x2F;span&gt;&lt;span&gt;.venv&#x2F;scripts&#x2F;activate
&lt;&#x2F;span&gt;&lt;span&gt;pip install pytest
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;

&lt;&#x2F;div&gt;&lt;h3 id=&quot;setup-mayapy-bat&quot;&gt;Setup mayapy.bat&lt;&#x2F;h3&gt;
&lt;p&gt;Now let&#x27;s create a file called &lt;code&gt;mayapy.bat&lt;&#x2F;code&gt; and set its content to this.&lt;&#x2F;p&gt;
&lt;pre data-linenos data-lang=&quot;bat&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-bat &quot;&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;rem&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt; mayapy.bat
&lt;&#x2F;span&gt;&lt;tr&gt;&lt;td&gt;2&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;tr&gt;&lt;td&gt;3&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;ECHO&lt;&#x2F;span&gt;&lt;span&gt; OFF
&lt;&#x2F;span&gt;&lt;tr&gt;&lt;td&gt;4&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;tr&gt;&lt;td&gt;5&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;set &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;MAYA_APP_DIR&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;%~dp0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;\temp\maya
&lt;&#x2F;span&gt;&lt;tr&gt;&lt;td&gt;6&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;set &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;MAYA_MODULE_PATH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;%~dp0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;%MAYA_MODULE_PATH%
&lt;&#x2F;span&gt;&lt;tr&gt;&lt;td&gt;7&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;set &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;PYTHONPATH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;%~dp0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;\.venv\Lib\site-packages;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;%PYTHONPATH%
&lt;&#x2F;span&gt;&lt;tr&gt;&lt;td&gt;8&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;tr&gt;&lt;td&gt;9&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;f &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;tokens=2*&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;%%&lt;&#x2F;span&gt;&lt;span&gt;a in (&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;reg query &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\Maya\2023\Setup\InstallPath&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;v MAYA_INSTALL_LOCATION&amp;#39;) do &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;set &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;MAYA_LOCATION&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;%%&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;~b&amp;quot;
&lt;&#x2F;span&gt;&lt;tr&gt;&lt;td&gt;10&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;%MAYA_LOCATION%&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;\bin\mayapy.exe&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;%*
&lt;&#x2F;span&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let’s break it down a bit:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;@ECHO OFF&lt;&#x2F;code&gt; makes it so the content of the script is not printed to the console.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;set MAYA_APP_DIR=%~dp0\temp\maya&lt;&#x2F;code&gt; lets us use a custom &lt;code&gt;MAYA_APP_DIR&lt;&#x2F;code&gt; instead of using the default one from &lt;code&gt;Documents&#x2F;maya&lt;&#x2F;code&gt;
This lets us start mayapy with zero bloat and will improve startup time significantly.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;set MAYA_MODULE_PATH=%~dp0;%MAYA_MODULE_PATH%&lt;&#x2F;code&gt; Adds the current folder to the &lt;code&gt;MAYA_MODULE_PATH&lt;&#x2F;code&gt; which ensures that our module gets loaded properly.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;set PYTHONPATH=%~dp0\.venv\Lib\site-packages;%PYTHONPATH%&lt;&#x2F;code&gt; adds our virtualenv to the python path, making mayapy aware of all of our dependencies.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;for &#x2F;f &amp;quot;tokens=2*&amp;quot; %%a in (&#x27;reg query &amp;quot;HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\Maya\2023\Setup\InstallPath&amp;quot; &#x2F;v MAYA_INSTALL_LOCATION&#x27;) do set &amp;quot;MAYA_LOCATION=%%~b&amp;quot;&lt;&#x2F;code&gt;
Oooh scary.
This dynamically finds the install location of Maya 2023 using Windows’ registry and sets the variable &lt;code&gt;MAYA_LOCATION&lt;&#x2F;code&gt; variable.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;&amp;quot;%MAYA_LOCATION%\bin\mayapy.exe&amp;quot; %*&lt;&#x2F;code&gt; runs mayapy from the &lt;code&gt;MAYA_LOCATION&lt;&#x2F;code&gt; and forwards all the command line arguments we passed to &lt;code&gt;mayapy.bat&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;We can now call &lt;code&gt;mayapy.bat&lt;&#x2F;code&gt; as we would any python interpreter except our environment is loaded properly:&lt;&#x2F;p&gt;
&lt;div class=&quot;console output&quot;&gt;
  &lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;.&#x2F;mayapy.bat -m site
&lt;&#x2F;span&gt;&lt;span&gt;sys.path = [
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;#39;D:\\projects\\maya\\maya-pytest-template&amp;#39;, &amp;lt;─────────────────────────────────┬── our project is properly added to the PYTHONPATH
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;#39;D:\\projects\\maya\\maya-pytest-template\\.venv\\Lib\\site-packages&amp;#39;, &amp;lt;──────┘
&lt;&#x2F;span&gt;&lt;span&gt;    ...
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;

&lt;&#x2F;div&gt;&lt;h3 id=&quot;initialize-maya-standalone&quot;&gt;Initialize maya.standalone&lt;&#x2F;h3&gt;
&lt;p&gt;While we can call &lt;code&gt;mayapy.bat -m pytest&lt;&#x2F;code&gt; to run our tests, they currently fail because &lt;code&gt;maya.standalone&lt;&#x2F;code&gt; is not initialized yet.&lt;br &#x2F;&gt;
In this case, the tests fail because our Maya Module is not yet loaded:&lt;&#x2F;p&gt;
&lt;div class=&quot;console output&quot;&gt;
  &lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;.&#x2F;mayapy.bat -m pytest tests
&lt;&#x2F;span&gt;&lt;span&gt;====================================================== test session starts ======================================================
&lt;&#x2F;span&gt;&lt;span&gt;platform win32 -- Python 3.9.7, pytest-7.1.2, pluggy-1.0.0
&lt;&#x2F;span&gt;&lt;span&gt;rootdir: D:&#x2F;projects&#x2F;maya&#x2F;maya-pytest-template
&lt;&#x2F;span&gt;&lt;span&gt;collected 0 items &#x2F; 1 error
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;============================================================ ERRORS =============================================================
&lt;&#x2F;span&gt;&lt;span&gt;______________________________________ ERROR collecting tests&#x2F;test_maya_pytest_template.py ______________________________________
&lt;&#x2F;span&gt;&lt;span&gt;ImportError while importing test module &amp;#39;D:&#x2F;projects&#x2F;maya&#x2F;maya-pytest-template&#x2F;tests&#x2F;test_maya_pytest_template.py&amp;#39;.
&lt;&#x2F;span&gt;&lt;span&gt;Hint: make sure your test modules&#x2F;packages have valid Python names.
&lt;&#x2F;span&gt;&lt;span&gt;Traceback:
&lt;&#x2F;span&gt;&lt;span&gt;C:&#x2F;Program Files&#x2F;Autodesk&#x2F;Maya2023&#x2F;Python&#x2F;lib&#x2F;importlib&#x2F;__init__.py:127: in import_module
&lt;&#x2F;span&gt;&lt;span&gt;    return _bootstrap._gcd_import(name[level:], package, level)
&lt;&#x2F;span&gt;&lt;span&gt;tests&#x2F;test_maya_pytest_template.py:5: in &amp;lt;module&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    import maya_pytest_template
&lt;&#x2F;span&gt;&lt;span&gt;E   ModuleNotFoundError: No module named &amp;#39;maya_pytest_template&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;==================================================== short test summary info ====================================================
&lt;&#x2F;span&gt;&lt;span&gt;ERROR tests&#x2F;test_maya_pytest_template.py
&lt;&#x2F;span&gt;&lt;span&gt;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
&lt;&#x2F;span&gt;&lt;span&gt;======================================================= 1 error in 0.14s ========================================================
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;

&lt;&#x2F;div&gt;
&lt;p&gt;Pytest has a couple hooks that let us run some code whenever the test sessions start and end, let&#x27;s set those up.&lt;br &#x2F;&gt;
Let&#x27;s write this in out &lt;code&gt;conftest.py&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# tests&#x2F;conftest.py
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;maya.standalone
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;pytest
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;pytest_sessionstart&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;session&lt;&#x2F;span&gt;&lt;span&gt;: pytest.Session):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;maya.standalone.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;initialize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;pytest_sessionfinish&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;session&lt;&#x2F;span&gt;&lt;span&gt;: pytest.Session, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;exitstatus&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;maya.standalone.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;uninitialize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let’s try again.&lt;&#x2F;p&gt;
&lt;div class=&quot;console output&quot;&gt;
  &lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;.\mayapy.bat -m pytest tests
&lt;&#x2F;span&gt;&lt;span&gt;====================================================== test session starts ======================================================
&lt;&#x2F;span&gt;&lt;span&gt;platform win32 -- Python 3.9.7, pytest-7.1.2, pluggy-1.0.0
&lt;&#x2F;span&gt;&lt;span&gt;rootdir: D:\projects\maya\maya-pytest-template
&lt;&#x2F;span&gt;&lt;span&gt;collected 2 items
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;tests\test_maya_pytest_template.py ..                                                                                      [100%] 
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;======================================================= 2 passed in 0.16s ======================================================= 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;

&lt;&#x2F;div&gt;
&lt;p&gt;Success! 🎉&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-pen&quot;&gt;&lt;&#x2F;i&gt;
    Note
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;There is a &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;docs.pytest.org&#x2F;en&#x2F;6.2.x&#x2F;usage.html#calling-pytest-through-python-m-pytest&quot;&gt;slight difference&lt;&#x2F;a&gt; when calling pytest as a module rather than using the &lt;code&gt;pytest&lt;&#x2F;code&gt; command directly but this should have little to no impact how things work.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;making-it-work-in-vscode&quot;&gt;Making it work in VSCode&lt;&#x2F;h2&gt;
&lt;p&gt;The first thing we need to do is to let VScode know that we want to use pytest as our testing library.&lt;br &#x2F;&gt;
Let’s add this 3 lines in our workspace settings:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&#x2F;&#x2F; .vscode&#x2F;settings.json
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;python.testing.unittestEnabled&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;python.testing.pytestEnabled&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;python.testing.pytestArgs&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;tests&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote class=&quot;admonition success&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-lightbulb&quot;&gt;&lt;&#x2F;i&gt;
    Tip
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;You can also use the &lt;code&gt;Python: Configure tests&lt;&#x2F;code&gt; task from the command palette to automatically add these entries to your workspace settings.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;h3 id=&quot;fixing-discovery-errors&quot;&gt;Fixing Discovery Errors&lt;&#x2F;h3&gt;
&lt;p&gt;If we now go to the testing tab we are greeted by a “Pytest Discovery Error”.&lt;&#x2F;p&gt;
&lt;p&gt;This is because we’re importing maya libraries in both our &lt;code&gt;conftest.py&lt;&#x2F;code&gt; and our different tests.&lt;br &#x2F;&gt;
At this point, vscode is still using the interpreter from our virtualenv and has no knowledge of those libraries.&lt;br &#x2F;&gt;
We will properly address this when we’ll want to actually run the tests but for now, let’s refactor our tests a bit so that they can be discovered by any python interpreter.&lt;&#x2F;p&gt;
&lt;p&gt;A simple way to address this problem is to add a safeguard around all our maya imports:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# tests&#x2F;conftest.py
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;pytest
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# Whether the tests are running in mayapy or not.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;HAS_MAYA&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;bool
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;try&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;maya.standalone
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;except &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;ImportError&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;HAS_MAYA &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;False
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;HAS_MAYA &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;pytest_sessionstart&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;session&lt;&#x2F;span&gt;&lt;span&gt;: pytest.Session):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# Initialize maya standalone only if it is running in mayapy.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;HAS_MAYA&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;maya.standalone.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;initialize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;pytest_sessionfinish&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;session&lt;&#x2F;span&gt;&lt;span&gt;: pytest.Session, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;exitstatus&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# Uninitialize maya standalone only if it is running in mayapy.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;HAS_MAYA&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;maya.standalone.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;uninitialize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# tests&#x2F;test_maya_pytest_template.py
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from .&lt;&#x2F;span&gt;&lt;span&gt;conftest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;HAS_MAYA
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# Safeguard maya imports to avoid test discovery errors
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;HAS_MAYA&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;maya_pytest_template
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;maya &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;cmds
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;test_version&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;maya_pytest_template.__version__ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;0.0.1&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;test_create_node&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    node &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cmds.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;createNode&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;transform&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;node &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;transform1&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And just like that, we have our tests discovered! But we can’t run them yet.&lt;&#x2F;p&gt;
&lt;p class=&quot;flex-container&quot; style=&quot;display: flex;  flex-wrap: wrap; justify-content: space-evenly;&quot;&gt;
    &lt;img src=&quot;.&#x2F;images&#x2F;tests-discovered.png&quot;&gt;
    &lt;img src=&quot;.&#x2F;images&#x2F;tests-failing.png&quot;&gt;
&lt;&#x2F;p&gt;
&lt;h3 id=&quot;running-the-tests&quot;&gt;Running the tests&lt;&#x2F;h3&gt;
&lt;p&gt;Now is the time where we need to make VScode actually use mayapy.&lt;&#x2F;p&gt;
&lt;p&gt;Unfortunately we can’t specify a different interpreter for tests than for the rest of VScode.&lt;br &#x2F;&gt;
And since our dependencies exist in our virtualenv - not in mayapy - we definitely want to keep that interpreter as the one used for most of vscode’s.&lt;&#x2F;p&gt;
&lt;p&gt;Luckily, vscode lets us use a custom pytest executable and we can hack around that to trick vscode into running our &lt;code&gt;mayapy.bat&lt;&#x2F;code&gt; script.&lt;&#x2F;p&gt;
&lt;p&gt;To do that, we’ll need to make another bat file, let’s call it &lt;code&gt;pytest.bat&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bat&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-bat &quot;&gt;&lt;code class=&quot;language-bat&quot; data-lang=&quot;bat&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;rem&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt; pytest.bat
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;ECHO&lt;&#x2F;span&gt;&lt;span&gt; OFF
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;.\mayapy.bat&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; -m pytest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;%*
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Simple enough.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;We use &lt;code&gt;@ECHO OFF&lt;&#x2F;code&gt; for the same reason we did in &lt;code&gt;mayapy.bat&lt;&#x2F;code&gt;.&lt;br &#x2F;&gt;
We don’t want stdout to be cluttered by unwanted things, this would confuse vscode.&lt;&#x2F;li&gt;
&lt;li&gt;With &lt;code&gt;&amp;quot;.\mayapy.bat&amp;quot; -m pytest %*&lt;&#x2F;code&gt; we call &lt;code&gt;mayapy.bat&lt;&#x2F;code&gt; and ask it to run pytest as a module.&lt;br &#x2F;&gt;
Again, we forward all the command line arguments with &lt;code&gt;%*&lt;&#x2F;code&gt; so that we can use &lt;code&gt;pytest.bat&lt;&#x2F;code&gt; as if we were using the actual executable directly.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;We can use &lt;code&gt;pytest.bat&lt;&#x2F;code&gt; like we would use the regular pytest executable now:&lt;&#x2F;p&gt;
&lt;div class=&quot;console output&quot;&gt;
  &lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;.\pytest.bat tests
&lt;&#x2F;span&gt;&lt;span&gt;====================================================== test session starts ======================================================
&lt;&#x2F;span&gt;&lt;span&gt;platform win32 -- Python 3.9.7, pytest-7.1.2, pluggy-1.0.0
&lt;&#x2F;span&gt;&lt;span&gt;rootdir: D:\projects\maya\maya-pytest-template
&lt;&#x2F;span&gt;&lt;span&gt;collected 2 items
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;tests\test_maya_pytest_template.py ..                                                                                      [100%]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;======================================================= 2 passed in 0.08s =======================================================
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;

&lt;&#x2F;div&gt;
&lt;p&gt;Cool!&lt;br &#x2F;&gt;
Now let’s tell vscode to use our new &lt;code&gt;pytest.bat&lt;&#x2F;code&gt; file as its pytest executable.&lt;br &#x2F;&gt;
In our workspace settings let’s add this line:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&#x2F;&#x2F; .vscode&#x2F;settings.json
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&#x2F;&#x2F; Previous config ...
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;python.testing.pytestPath&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;${workspaceFolder}&#x2F;pytest.bat&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And voilà!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;.&#x2F;images&#x2F;tests-passing.png#center&quot; alt=&quot;Tests Passing&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;debugging-the-tests&quot;&gt;Debugging the tests&lt;&#x2F;h3&gt;
&lt;p&gt;Now that our tests are running, the last step is to be able to debug them within VScode as well.&lt;&#x2F;p&gt;
&lt;p&gt;This part is going to be less hacky as VScode has support for custom debug configurations through the &lt;code&gt;launch.json&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;p&gt;To generate a new configuration:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Go in the Run and Debug tab and click &lt;code&gt;create a launch.json file&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Then select &lt;code&gt;Python&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;And finally &lt;code&gt;Python File&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Now that the &lt;code&gt;launch.json&lt;&#x2F;code&gt; file has been created, we need to make a few modifications to it.&lt;br &#x2F;&gt;
Here’s the final file we’ll use:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&#x2F;&#x2F; .vscode&#x2F;launch.json
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;version&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;0.2.0&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;configurations&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: [
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;name&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;MayaPy: Current File&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;type&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;python&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;request&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;launch&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;program&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;${file}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;console&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;internalConsole&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;justMyCode&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;python&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;${workspaceFolder}&#x2F;mayapy.bat&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;purpose&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;debug-test&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;debug-in-terminal&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  ]
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here’s the modifications we made to the one vscode generated:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Changed the name to &lt;code&gt;MayaPy: Current File&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Set &lt;code&gt;console&lt;&#x2F;code&gt; to &lt;code&gt;internalConsole&lt;&#x2F;code&gt; to use the debug console instead of the terminal (Optional)&lt;&#x2F;li&gt;
&lt;li&gt;Set &lt;code&gt;python&lt;&#x2F;code&gt; to use our &lt;code&gt;mayapy.bat&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Set the &lt;code&gt;purpose&lt;&#x2F;code&gt; to &lt;code&gt;[&amp;quot;debug-test&amp;quot;, &amp;quot;debug-in-terminal&amp;quot;]&lt;&#x2F;code&gt;
This one does two things
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;debug-test&lt;&#x2F;code&gt; makes this configuration the one that vscode will use when debugging the tests.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;debug-in-terminal&lt;&#x2F;code&gt; tells vscode to use this configuration when using the &lt;code&gt;Debug Python File&lt;&#x2F;code&gt; task.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;.&#x2F;images&#x2F;debug-python-file-button.png&quot; alt=&quot;Debug Python File&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This one is not necessary for what we’re doing but it is a pretty handy feature and there’s no reason not to include it.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;admonition warning&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-fire-flame-curved&quot;&gt;&lt;&#x2F;i&gt;
    Warning
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;&lt;code&gt;maya.standalone.initialize&lt;&#x2F;code&gt; is not called automatically with this so you will need to manually call it in the file you’re debugging.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;that-s-all-folks&quot;&gt;That’s all folks&lt;&#x2F;h2&gt;
&lt;p&gt;We can now Discover, Run and Debug Maya tests as if we were real python developers… 🙃&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Python environments and Maya</title>
		<published>2022-01-21T00:00:00+00:00</published>
		<updated>2022-01-21T00:00:00+00:00</updated>
		<link href="https://loic-pinsard.com/blog/python-environments-and-maya/" type="text/html"/>
		<id>https://loic-pinsard.com/blog/python-environments-and-maya/</id>
		<content type="html">&lt;p&gt;Working cleanly with python environments is something that I feel is quite obscure for a lot of people but is not actually hard or convoluted.&lt;&#x2F;p&gt;
&lt;p&gt;In this blog post I&#x27;m going to describe how I typically setup my python environments when developing tools and how to make maya aware of them without having bloat mayapy by installing dependencies to it directly.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-circle-info&quot;&gt;&lt;&#x2F;i&gt;
    Info
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;The workflow I&#x27;m describing here is likely more suited to developping standalone tools rather setting up a studio environment with lots of dependencies.&lt;&#x2F;p&gt;
&lt;p&gt;For that you should probably look into &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;nerdvegas&#x2F;rez&quot;&gt;Rez&lt;&#x2F;a&gt; or some kind of environment manager.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;h1 id=&quot;tldr&quot;&gt;TLDR&lt;&#x2F;h1&gt;
&lt;p&gt;Simply create a virtualenv with a python interpreter matching your mayapy version and add the venv&#x27;s &lt;code&gt;site-packages&lt;&#x2F;code&gt; folder to mayapy&#x27;s PYTHONPATH like so:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;site
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;site.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;addsitedir&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;&#x2F;path&#x2F;to&#x2F;venv&#x2F;Lib&#x2F;site-packages&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And that&#x27;s it. You can now pip install packages in your venv and you&#x27;ll be able to import them in maya.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-pen&quot;&gt;&lt;&#x2F;i&gt;
    Note
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;Make sure to use &lt;code&gt;site.addsitedir()&lt;&#x2F;code&gt; and not &lt;code&gt;sys.path.append()&lt;&#x2F;code&gt;.&lt;br &#x2F;&gt;
The first one properly adds paths to your PYTHONPATH by taking the &lt;code&gt;.pth&lt;&#x2F;code&gt; files into account.&lt;br &#x2F;&gt;
In most cases you won&#x27;t see a difference but it can avoid some confusing issues.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;h1 id=&quot;going-further&quot;&gt;Going Further&lt;&#x2F;h1&gt;
&lt;p&gt;Managing python interpreters and virtualenvs is sadly pretty barebones by default but there&#x27;s two great great tools I&#x27;ve been using lately that I want to talk about.
It&#x27;s by all means not necessary to use these tools but they have made my life much easier.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;pyenv&quot;&gt;Pyenv&lt;&#x2F;h2&gt;
&lt;p&gt;Contrary to what its name suggests &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;pyenv&#x2F;pyenv&quot;&gt;pyenv&lt;&#x2F;a&gt; is not something that manages virtualenvs but a tool used to manage multiple python versions easily.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-pen&quot;&gt;&lt;&#x2F;i&gt;
    Note
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;If you use windows, you can use &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;pyenv-win&#x2F;pyenv-win&quot;&gt;pyenv-win&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;You can install as many python version as you want like so:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt; install 3.7.9
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt; install 2.7.18
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then pick the system wide python with to easily run &lt;code&gt;python&lt;&#x2F;code&gt; from anywhere&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt; global 3.7.9
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And finally pick which interpreter is used per project by adding a &lt;code&gt;.python-version&lt;&#x2F;code&gt; file containg the version number you want to use.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;2.7.18
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-pen&quot;&gt;&lt;&#x2F;i&gt;
    Note
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;It&#x27;s a good idea to specify the python version even if it&#x27;s the same as your system wide one so that other people can use the right one.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;poetry&quot;&gt;Poetry&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;python-poetry.org&#x2F;&quot;&gt;Poetry&lt;&#x2F;a&gt; is a package manager for python, similar to NPM for NodeJS, cargo for Rust and many others.
Poetry ensures that you and everyone working on your project will have a consistent environment&lt;&#x2F;p&gt;
&lt;p&gt;Even for solo projects, poetry is my go to package manager because it makes everything much more comfortable.&lt;&#x2F;p&gt;
&lt;p&gt;Poetry&#x27;s docs are great so I won&#x27;t go about everything but here&#x27;s the basics you need to know:&lt;&#x2F;p&gt;
&lt;p&gt;Once installed, you can create a new project with:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt; new my-project-name
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It will create a default folder structure for you with a &lt;code&gt;pyproject.toml&lt;&#x2F;code&gt; file (more on that in a second).&lt;&#x2F;p&gt;
&lt;p&gt;Or you can init poetry in an existing project with:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt; init
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You will be asked to give some informations about your project (name, description, license, etc.) as well as the possibility to add dependencies. I tend to skip that last part and add my dependencies after the fact.&lt;&#x2F;p&gt;
&lt;p&gt;Before doing anything more, I like for poetry to create my virtualenvs inside of my projects (Instead of my user folder) which you can do like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt; config virtualenvs.in-project true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is absolutely not necessary but it makes it easy to add the virtualenv to mayapy&#x27;s PYTHONPATH since it is now accessible with a relative path to your project.&lt;&#x2F;p&gt;
&lt;p&gt;Now to add dependencies to your project, simply run:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt; add Qt.py
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can also install dev only dependencies (only required for people working on the project):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt; add black&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt; --dev
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you have a look in &lt;code&gt;pyproject.toml&lt;&#x2F;code&gt;, you&#x27;ll see that those dependencies have been added along with the minimum required version.&lt;&#x2F;p&gt;
&lt;p&gt;You will also notice that a &lt;code&gt;poetry.lock&lt;&#x2F;code&gt; file has been created. It contains information about every package installed in your venv and their &lt;em&gt;exact&lt;&#x2F;em&gt; version.&lt;br &#x2F;&gt;
This file is important and should be commited to your repository.&lt;br &#x2F;&gt;
When people run &lt;code&gt;poetry install&lt;&#x2F;code&gt; they will have the exact same version of all your project&#x27;s dependencies as you have.&lt;&#x2F;p&gt;
&lt;p&gt;To update your dependencies, use:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt; update
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There is more to Poetry like building and publishing packages to pypi but these aren&#x27;t features I&#x27;ve used yet.&lt;&#x2F;p&gt;
&lt;p&gt;Hopefully that gave you a good sense of how you can better manage virtualenvs and how you can use them to bring dependencies in maya without bloating the mayapy interpreter directly.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>New Python 3 features</title>
		<published>2021-04-20T00:00:00+00:00</published>
		<updated>2021-04-20T00:00:00+00:00</updated>
		<link href="https://loic-pinsard.com/blog/new-python3-features/" type="text/html"/>
		<id>https://loic-pinsard.com/blog/new-python3-features/</id>
		<content type="html">&lt;p&gt;Maya 2022 finally has support for python 3 and I&#x27;ve been using it quite a lot these past few months so I figured I would shed some light on all the features I&#x27;ve enjoyed using so far.&lt;br &#x2F;&gt;
And because Python 2 isn&#x27;t quite dead yet, I&#x27;ll link to alternatives or backports that you can use  for python 2 and let you maintain a python 2 and 3 compatible code base.&lt;&#x2F;p&gt;
&lt;p&gt;You&#x27;ll see a common trend with all of these features, they greatly help writing more readable and self documenting code.
If you&#x27;re in a situation where can use them, I highly recommend that you do.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;type-hints&quot;&gt;Type Hints&lt;&#x2F;h1&gt;
&lt;p&gt;Python 3 comes with a new syntax to assign a type to your variables.&lt;br &#x2F;&gt;
Adding type hints doesn&#x27;t have any impact on the runtime performance of your code. It&#x27;s only there for readability and to be parsed by type checkers.&lt;br &#x2F;&gt;
They are also entirely optional, Here&#x27;s how they look:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;a: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;b: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;some string&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;above_zero&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;bool&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;value &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Type hinting is amazing for a few reasons:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;It&#x27;s a great way to write self documenting code.&lt;br &#x2F;&gt;
It simply removes a lot of guess work when reading through some code and makes it much easier for multiple people to work on the same codebase.&lt;&#x2F;li&gt;
&lt;li&gt;It improves code completion&#x2F;inspection a lot.&lt;br &#x2F;&gt;
Without type hints, your editor has no way of knowing the types of a function arguments and can&#x27;t give you any relevant completions for those.
The same is true for what a function returns
Good type hinting fixes all that.&lt;&#x2F;li&gt;
&lt;li&gt;It prevents a lot of bugs before they even happen.&lt;br &#x2F;&gt;
Due to the dynamic nature of python, a lot of variables tend to have different types based on the context. If you&#x27;re not checking their type at runtime, you&#x27;ll end up calling the wrong methods on the wrong types.&lt;br &#x2F;&gt;
&lt;code&gt;AttributeError: &amp;lt;SomeType&amp;gt; has no attribute &amp;lt;whatever&amp;gt;&lt;&#x2F;code&gt; is an error you&#x27;ll won&#x27;t be seeing much when you get into static typing. Your editor will let you know about those errors before you even run your code.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;For type hints to actually be useful, you need something to enforce and validate them. This is the job of a Type Checker.&lt;br &#x2F;&gt;
I personally use &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;mypy-lang.org&#x2F;&quot;&gt;mypy&lt;&#x2F;a&gt; but &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;pyright&quot;&gt;pyright&lt;&#x2F;a&gt; looks like an interesting alternative. If you use PyCharm, it seems have its own type checker built-in.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-pen&quot;&gt;&lt;&#x2F;i&gt;
    Note
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;While the type hint syntax is new to python 3, it&#x27;s possible to use a &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mypy.readthedocs.io&#x2F;en&#x2F;stable&#x2F;python2.html&quot;&gt;comment based syntax&lt;&#x2F;a&gt; for python 2.&lt;br &#x2F;&gt;
From what I&#x27;ve seen, mypy, pyright and pycharm seem to all be able to use this alternative syntax.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Converting an existing codebase to use static typing can be overwhelming, mypy recommends to &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;mypy.readthedocs.io&#x2F;en&#x2F;stable&#x2F;existing_code.html&quot;&gt;start small&lt;&#x2F;a&gt;.&lt;br &#x2F;&gt;
If you&#x27;re starting fresh however, do yourself a favor and use a type checker before you even write your first line of code.&lt;&#x2F;p&gt;
&lt;p&gt;The quality of my code has improved significantly these past months both in terms of design and readability. Proper static typing was one of the reasons why. It strongly discourages me from writing unreliable code and encouraged me to create more small and meaningfull classes, leading nicely to the next point.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;dataclasses&quot;&gt;Dataclasses&lt;&#x2F;h1&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-circle-info&quot;&gt;&lt;&#x2F;i&gt;
    Info
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;The python2 compatible implementation of the dataclasses is &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.attrs.org&#x2F;en&#x2F;stable&#x2F;index.html&quot;&gt;attrs&lt;&#x2F;a&gt;.&lt;br &#x2F;&gt;
Python3&#x27;s dataclasses actually come from the attrs library. Their syntax differs a bit and attrs has a &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.attrs.org&#x2F;en&#x2F;stable&#x2F;why.html#data-classes&quot;&gt;few more features&lt;&#x2F;a&gt;.&lt;br &#x2F;&gt;
If you use the first party dataclasses and are going through attrs&#x27; documentation, read &lt;code&gt;@dataclass&lt;&#x2F;code&gt; when you see &lt;code&gt;@attr.s&lt;&#x2F;code&gt; and &lt;code&gt;field()&lt;&#x2F;code&gt; when you see &lt;code&gt;attr.ib()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Dataclasses makes writing classes a lot less cumbersome than it used to be.&lt;br &#x2F;&gt;
They are a way to cut boilerplate code by automatically implementing a lot of &amp;quot;dunder&amp;quot; methods for you. They make your code more readable and more useful.&lt;&#x2F;p&gt;
&lt;p&gt;You only need to define a class by its name, attributes and specific methods.&lt;br &#x2F;&gt;
The &lt;code&gt;@dataclass&lt;&#x2F;code&gt; decorator implements &lt;code&gt;__init__&lt;&#x2F;code&gt;, &lt;code&gt;__eq__&lt;&#x2F;code&gt;, &lt;code&gt;__repr__&lt;&#x2F;code&gt; automatically for you as well as all those other methods you were not implementing but should have.
This means that dataclasses can automatically be compared, sorted, etc. with absolutely zero effort on your end.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.attrs.org&#x2F;en&#x2F;stable&#x2F;why.html&quot;&gt;attrs&#x27; documentation&lt;&#x2F;a&gt; has a great overview of why you would want to use dataclasses as opposed to any other alternative.&lt;&#x2F;p&gt;
&lt;p&gt;One of the ways I&#x27;ve been using dataclasses is by making multiple constructors for my objects.&lt;br &#x2F;&gt;
This is something I&#x27;ve picked up when learning Rust and I felt that dataclasses were encouraging me to do the same in python.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Your default constructor (the &lt;code&gt;__init__&lt;&#x2F;code&gt;, automatically implemented by the dataclass) doesn&#x27;t make any assumptions about the &amp;quot;default&amp;quot; value of the arguments.&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;code&gt;new&lt;&#x2F;code&gt; constructor returns an object with the most sensible default values.&lt;&#x2F;li&gt;
&lt;li&gt;You can add as many constructors as you want and give them a clear name as to what they do.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;dataclasses &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;dataclass
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;dataclass
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;Vector3&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    x: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;int
&lt;&#x2F;span&gt;&lt;span&gt;    y: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;int
&lt;&#x2F;span&gt;&lt;span&gt;    z: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;int
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;classmethod
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;cls&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; Vector3:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;Create a new &amp;#39;zero&amp;#39; vector.&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;cls&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;classmethod
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;from_sequence&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;cls&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;sequence&lt;&#x2F;span&gt;&lt;span&gt;: Sequence) -&amp;gt; Vector3:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;Create a vector from any sequence of length 3.&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;len(sequence) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;!= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;raise &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;ValueError&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;A Vector3 can be only constructed from a sequence of length 3.&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;            )
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;cls&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;sequence)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# These are all valid ways of instantiating the Vector3 class
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;Vector3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# using the default constructor
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;Vector3.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;Vector.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;from_sequence&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;])  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# instantiating from a list
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;Vector.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;from_sequence&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;))  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# and now from a tuple
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you have access to them, you should likely use dataclasses by default, they&#x27;ll make your life easier.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;protocols&quot;&gt;Protocols&lt;&#x2F;h1&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-circle-info&quot;&gt;&lt;&#x2F;i&gt;
    Info
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;Maya currently uses python 3.7 and Protocols are a 3.8 feature. To use them you need to install the &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;typing-extensions&#x2F;&quot;&gt;typing_extensions&lt;&#x2F;a&gt; package, which also has python 2 support.&lt;&#x2F;p&gt;
&lt;p&gt;If you use python 3.8+, Protocols are available in the typing module.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;This feature might not be relevant for everyone but I really like it and it pairs really well with static typing.&lt;br &#x2F;&gt;
Protocols are a way to define a consistent interface for multiple classes without using inheritance.&lt;br &#x2F;&gt;
Some other languages call them Interfaces, Rust calls them traits.&lt;&#x2F;p&gt;
&lt;p&gt;When you define a Protocol, you just create a class with empty methods that have to be implemented in the classes that implement the protocol.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m taking the next example straight from &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;andrewbrookins.com&#x2F;technology&#x2F;building-implicit-interfaces-in-python-with-protocol-classes&#x2F;&quot;&gt;this article&lt;&#x2F;a&gt; which goes more in depth on the topic than I will.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;typing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Protocol
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;Flyer&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;Protocol&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;fly&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;A Flyer can fly&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;FlyingHero&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;This hero can fly, which is BEAST.&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;fly&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# Do some flying...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;RunningHero&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;This hero can run. Better than nothing!&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;run&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# Run for your life!
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;Board&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;An imaginary game board that doesn&amp;#39;t do anything.&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;make_fly&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;obj&lt;&#x2F;span&gt;&lt;span&gt;: Flyer) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;:   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;lt;- Here&amp;#39;s the magic
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;Make an object fly.&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;obj.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;fly&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    board &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;Board&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;board.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;make_fly&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;FlyingHero&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;board.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;make_fly&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;RunningHero&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;())  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;lt;- Fails mypy type-checking!
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;__name__ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;#39;__main__&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that we never explicitely define that Bird and Human implement the HasWings and HasLegs Protocols, it&#x27;s implicit because those classes implement all the methods from the Protocol.&lt;br &#x2F;&gt;
I find this quite awkward and you won&#x27;t get warned that a class implements a protocol incorrectly. Any slight mistake means the protocol isn&#x27;t implemented.&lt;br &#x2F;&gt;
Luckily, it&#x27;s possible to check for this at runtime with the &lt;code&gt;isinstance&lt;&#x2F;code&gt; and &lt;code&gt;issubclass&lt;&#x2F;code&gt; methods. If you use unit tests you can simply write a test like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;typing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;runtime_checkable
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;runtime_checkable &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;lt;- necessary to use issubclass and isinstance against the protocol
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;Flyer&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;Protocol&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;fly&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;Make the object fly.&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;FlyingHero&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;This hero can fly, which is BEAST.&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;fly&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# Do some flying...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;RunningHero&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;This hero can run. Better than nothing!&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;run&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# Run for your life!
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;test_FlyingHero_implements_Flyer&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;issubclass(FlyingHero, HasWings)  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;lt;- This test passes
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;test_RunningHero_implements_Flyer&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;issubclass(RunningHero, HasWings)  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;# &amp;lt;- This test fails
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;f-strings&quot;&gt;F-strings&lt;&#x2F;h1&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-circle-info&quot;&gt;&lt;&#x2F;i&gt;
    Info
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;I have not used a python 2 implementation of f-strings yet but &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;future-fstrings&#x2F;&quot;&gt;future-strings&lt;&#x2F;a&gt; looks promising.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;F-strings are a new way to format strings that is way more readable than previous methods because you are able to embed variables directly into your strings.&lt;br &#x2F;&gt;
Your eyes don&#x27;t need to constantly jump from the beginning and end of the line to make sense of the strings.&lt;&#x2F;p&gt;
&lt;p&gt;This means your ugly maya.cmds calls will go from this&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;control &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cmds.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;createNode&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;transform&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;blendshape &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;blendShape1&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cmds.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;connectAttr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ced1cf;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;.translateX&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;format&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(control),
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ced1cf;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;.some_shape&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;format&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(blendshape),
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;control &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cmds.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;createNode&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;transform&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;blendshape &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;blendShape1&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cmds.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;connectAttr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;{control}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;.translateX&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;{blendshape}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;.some_shape&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;enums&quot;&gt;Enums&lt;&#x2F;h1&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-circle-info&quot;&gt;&lt;&#x2F;i&gt;
    Info
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;The Python 2 implementation of this that I&#x27;ve used is &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;aenum&#x2F;&quot;&gt;aenum&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Enums are a way to define constants and give meaning to otherwise abstract values.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;Enum&lt;&#x2F;code&gt; class can be used to map a name to anything, for example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;enum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Enum
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;Color&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;Enum&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    red &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    green &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    blue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;IntEnum&lt;&#x2F;code&gt; is useful to map a name to an int, making it a very good way to interface with maya&#x27;s enum attribtues.
Say you want to change the interpolation type of a keyframe on a remapValue node here&#x27;s what you&#x27;d do:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;enum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;IntEnum
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;Interpolation&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;IntEnum&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    none &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;    linear &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;    smooth &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;span&gt;    spline &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;3
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;remap_value &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cmds.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;createNode&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;remapValue&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cmds.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;setAttr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;{remap_value}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;.value[0].value_Interp&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, Interpolation.smooth)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In fact, here&#x27;s a simple Keyframe class I&#x27;ve written recently that abstracts all the relevant information:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#1d1f21;color:#c5c8c6;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;enum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;IntEnum
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;dataclasses &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;dataclass
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;dataclass
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;Keyframe&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#969896;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;Keyframe class meant to work with maya&amp;#39;s remapValue and remapColor nodes.&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;Interpolation&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;IntEnum&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        none &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;        linear &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;        smooth &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;span&gt;        spline &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;3
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    x: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;float 
&lt;&#x2F;span&gt;&lt;span&gt;    y: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f0c674;&quot;&gt;float
&lt;&#x2F;span&gt;&lt;span&gt;    interpolation: Interpolation
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;remap_value &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cmds.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;createNode&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;remapValue&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;keyframe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8abeb7;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;Keyframe&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#de935f;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, Keyframe.Interpolation.smooth)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cmds.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;setAttr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;{remap_value}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;.value[0].value_Position&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, keyframe.x)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cmds.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;setAttr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;{remap_value}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;.value[0].value_FloatValue&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, keyframe.y)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;cmds.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cc6666;&quot;&gt;setAttr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b294bb;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;{remap_value}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b5bd68;&quot;&gt;.value[0].value_Interp&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#81a2be;&quot;&gt;, keyframe.interpolation)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote class=&quot;admonition note&quot;&gt;
  &lt;p class=&quot;title&quot;&gt;
    &lt;i class=&quot;fa-solid fa-pen&quot;&gt;&lt;&#x2F;i&gt;
    Note
  &lt;&#x2F;p&gt;
  &lt;div class=&quot;content&quot;&gt;&lt;p&gt;in this example, I define the &lt;code&gt;Interpolation&lt;&#x2F;code&gt; class inside of the &lt;code&gt;Keyframe&lt;&#x2F;code&gt; class as a way to namespace it.
I&#x27;m not sure if that&#x27;s good practice but I find it makes it very explicit that the &lt;code&gt;Interpolation&lt;&#x2F;code&gt; class is only meant to be used with the &lt;code&gt;Keyframe&lt;&#x2F;code&gt; class and not something else.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Matrices explained - Part 1</title>
		<published>2020-10-04T00:00:00+00:00</published>
		<updated>2020-10-04T00:00:00+00:00</updated>
		<link href="https://loic-pinsard.com/blog/matrices-explained-part-1/" type="text/html"/>
		<id>https://loic-pinsard.com/blog/matrices-explained-part-1/</id>
		<content type="html">&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h1&gt;
&lt;p&gt;This series of blog posts is going to cover how transformation matrices work and how to leverage them to take precise control over your rigs most of this is going to be through the lens of a Maya Rigger but the knowledge is applicable in pretty much any 3D app.&lt;&#x2F;p&gt;
&lt;p&gt;Matrices are at the core of any 3D application and understanding how they store transformations as well as how they interact with each other is going to give you a much deeper understanding of what actually goes on when you move or constrain stuff around.&lt;&#x2F;p&gt;
&lt;p&gt;Quick disclaimer before we jump in, All my knowledge about matrices is self taught. I have built a good intuition of how they work and how the operations you can do on them behave but I don&#x27;t actually know how the math works under the hood.&lt;&#x2F;p&gt;
&lt;p&gt;But enough rambling around, let&#x27;s jump in.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;so-what-is-a-matrix&quot;&gt;So, what is a matrix?&lt;&#x2F;h1&gt;
&lt;p&gt;In the context of a 3D application, a matrix is simply a representation of a transformation. One transformation matrix can encode the translation, orientation, scale and shearing of an object. Transformation matrices are stored as 4x4 matrices, meaning they have 4 rows and 4 columns and they look like this:&lt;&#x2F;p&gt;
&lt;script type=&quot;math&#x2F;tex;mode=display&quot;&gt;Identity=
    \begin{bmatrix}
    1 &amp; 0 &amp; 0 &amp; 0 \\
    0 &amp; 1 &amp; 0 &amp; 0 \\
    0 &amp; 0 &amp; 1 &amp; 0 \\
    0 &amp; 0 &amp; 0 &amp; 1 \\
    \end{bmatrix}&lt;&#x2F;script&gt;
&lt;p&gt;What you&#x27;re seeing here is is called the Identity matrix, it&#x27;s filled with zeros and have ones on the top left to bottom right diagonal.&lt;&#x2F;p&gt;
&lt;p&gt;Maya uses row major matrices, meaning the data it stores are encoded into its rows. You might see column major matrices if you use other applications (including Maya&#x27;s Bifrost, Blender and more).
But let&#x27;s stick with row major for now as this is what I&#x27;m mostly used to working with.&lt;&#x2F;p&gt;
&lt;p&gt;The last column of the matrix is only there for us to have a square matrix but doesn&#x27;t actually do anything, just remember that it&#x27;s 0, 0, 0, 1 and you&#x27;re good. I am not 100% sure of that last part but this assumption hasn&#x27;t caused me any trouble so far.&lt;&#x2F;p&gt;
&lt;p&gt;Edit: That last column can be used to represent the camera projection in 3D engines, definitely not something you need to worry about unless you&#x27;re writing your own engine so let&#x27;s continue with that assumption that it&#x27;s just 0, 0, 0, 1 and we&#x27;ll be just fine.&lt;br &#x2F;&gt;
Thanks &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;willemkokke&#x2F;status&#x2F;1312885609720143873?s=20&quot;&gt;Willem&lt;&#x2F;a&gt; for the info.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;translation&quot;&gt;Translation&lt;&#x2F;h1&gt;
&lt;p&gt;The easiest part of a matrix to explain is the translation. Your translate X&#x2F;Y&#x2F;Z values are simply thrown in the last row.&lt;br &#x2F;&gt;
For example the matrix that stores the translation of (3, 4, 2) looks like this:&lt;&#x2F;p&gt;
&lt;script type=&quot;math&#x2F;tex;mode=display&quot;&gt;Translation=
    \begin{bmatrix}
    1 &amp; 0 &amp; 0 &amp; 0 \\
    0 &amp; 1 &amp; 0 &amp; 0 \\
    0 &amp; 0 &amp; 1 &amp; 0 \\
    3 &amp; 4 &amp; 2 &amp; 1 \\
    \end{bmatrix}&lt;&#x2F;script&gt;
&lt;p&gt;And that&#x27;s pretty much it for translations, just remember stick a 1 in the last column and leave it at that.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;orientation-scale-and-shear&quot;&gt;Orientation, Scale and Shear&lt;&#x2F;h1&gt;
&lt;p&gt;You might be wondering why the Translation had its own section but the Orientation, Scale and Shear are grouped together. That&#x27;s because all three are represented in the same place inside the matrix.&lt;&#x2F;p&gt;
&lt;p&gt;How is that possible you ask?&lt;&#x2F;p&gt;
&lt;p&gt;Well they aren&#x27;t stored as is but are rather the interpretation of something else: 3 Vectors.
You know them already, it&#x27;s those three fellas:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;.&#x2F;images&#x2F;identity-matrix.png&quot; alt=&quot;Identity Matrix Representation&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s copy the identity matrix here again and break it down a bit&lt;&#x2F;p&gt;
&lt;script type=&quot;math&#x2F;tex;mode=display&quot;&gt;Identity=
    \begin{bmatrix}
    1 &amp; 0 &amp; 0 &amp; 0 \\
    0 &amp; 1 &amp; 0 &amp; 0 \\
    0 &amp; 0 &amp; 1 &amp; 0 \\
    0 &amp; 0 &amp; 0 &amp; 1 \\
    \end{bmatrix}&lt;&#x2F;script&gt;
&lt;p&gt;Ignoring the last column, let&#x27;s have a look at the first three rows:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;First row is the X vector of our object and looks like this: (1, 0, 0)
This means our X vector points one unit out in the world X and 0 in Y&#x2F;Z&lt;&#x2F;li&gt;
&lt;li&gt;Second row is the Y vector: (0, 1, 0)
Our Y vector points one unit in world Y and 0 in X&#x2F;Z&lt;&#x2F;li&gt;
&lt;li&gt;Third row: (0, 0, 1)
Our Z vector points one unit in world Z and 0 in X&#x2F;Y&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Since this is the identity matrix, all the vectors line up with the world&#x27;s, makes sense.&lt;&#x2F;p&gt;
&lt;p&gt;The rest of this post going to explain what happens when we change those 3 rows so make sure you have a grasp of what we just said.
If you&#x27;re struggling, keep going, the scale and orientation sections will likely help you wrap your mind around the idea more.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;scale&quot;&gt;Scale&lt;&#x2F;h2&gt;
&lt;p&gt;Scaling a matrix is simply a matter of scaling those vectors. By default they are unit vectors, meaning they have a length of one but let&#x27;s change that.&lt;&#x2F;p&gt;
&lt;p&gt;In this example we&#x27;re scaling our matrix by 2 on the X axis, meaning our X vector gets a length of 2 while the other two don&#x27;t change.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;.&#x2F;images&#x2F;x_scale.png&quot; alt=&quot;X axis scale&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;script type=&quot;math&#x2F;tex;mode=display&quot;&gt;scaleX=
    \begin{bmatrix}
    2 &amp; 0 &amp; 0 &amp; 0 \\
    0 &amp; 1 &amp; 0 &amp; 0 \\
    0 &amp; 0 &amp; 1 &amp; 0 \\
    0 &amp; 0 &amp; 0 &amp; 1 \\
    \end{bmatrix}&lt;&#x2F;script&gt;
&lt;p&gt;And here&#x27;s an example of a negative scale along the Z Axis:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;.&#x2F;images&#x2F;z_scale.png&quot; alt=&quot;Negative Scale on Z&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;script type=&quot;math&#x2F;tex;mode=display&quot;&gt;ScaleZ (negative)=
    \begin{bmatrix}
    1 &amp; 0 &amp; 0 &amp; 0 \\
    0 &amp; 1 &amp; 0 &amp; 0 \\
    0 &amp; 0 &amp; -1 &amp; 0 \\
    0 &amp; 0 &amp; 0 &amp; 1 \\
    \end{bmatrix}&lt;&#x2F;script&gt;
&lt;p&gt;Here the length of all our vectors is still 1 but the Z vectors has flipped its direction.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;orientation&quot;&gt;Orientation&lt;&#x2F;h2&gt;
&lt;p&gt;Up until now we&#x27;ve kept our vectors on the same line as the world vectors, let&#x27;s spice it up a bit and make a 90° rotation around the Z axis&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s a screenshot of the result, see if you can predict what the matrix looks like from that (you can see the world vectors in the lower left corner of the screenshot to help you):&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;.&#x2F;images&#x2F;z_rot.png&quot; alt=&quot;90° rotation on the Z axis&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s the answer:&lt;&#x2F;p&gt;
&lt;script type=&quot;math&#x2F;tex;mode=display&quot;&gt;RotateZ=
    \begin{bmatrix}
    0 &amp; 1 &amp; 0 &amp; 0 \\
    -1 &amp; 0 &amp; 0 &amp; 0 \\
    0 &amp; 0 &amp; 1 &amp; 0 \\
    0 &amp; 0 &amp; 0 &amp; 1 \\
    \end{bmatrix}&lt;&#x2F;script&gt;
&lt;p&gt;If you didn&#x27;t find that, no problem, let&#x27;s break it down a bit like we did with the identity matrix.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;First row (X): &lt;script type=&quot;math&#x2F;tex&quot;&gt;(0, 1, 0)&lt;&#x2F;script&gt;
&lt;br &#x2F;&gt;
The X vector now points towards world Y.&lt;&#x2F;li&gt;
&lt;li&gt;Second row (Y): &lt;script type=&quot;math&#x2F;tex&quot;&gt;(-1, 0, 0)&lt;&#x2F;script&gt;
&lt;br &#x2F;&gt;
The Y vector now points 1 unit towards the world&#x27;s -X.&lt;&#x2F;li&gt;
&lt;li&gt;Third row (Z): &lt;script type=&quot;math&#x2F;tex&quot;&gt;(0, 0 , 1)&lt;&#x2F;script&gt;
&lt;br &#x2F;&gt;
The Z vector hasn&#x27;t changed since the rotation happened around it.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;shearing&quot;&gt;Shearing&lt;&#x2F;h2&gt;
&lt;p&gt;Small disclaimer, this is the part of matrices I&#x27;ve used the least and therefore am the least comfortable with so take this with a grain of salt.
Generally if I get shearing it means I messed up my math somewhere ¯\_(ツ)_&#x2F;¯&lt;&#x2F;p&gt;
&lt;p&gt;Up until now, our three XYZ vectors were all orthogonal to each other. Introducing shearing breaks this.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s take a quick example of that where our Y vector points towards both Y and X&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;.&#x2F;images&#x2F;y_shear.png&quot; alt=&quot;Shearing on the Y axis&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;script type=&quot;math&#x2F;tex;mode=display&quot;&gt;Shear=
    \begin{bmatrix}
    1 &amp; 0 &amp; 0 &amp; 0 \\
    1 &amp; 1 &amp; 0 &amp; 0 \\
    0 &amp; 0 &amp; 1 &amp; 0 \\
    0 &amp; 0 &amp; 0 &amp; 1 \\
    \end{bmatrix}&lt;&#x2F;script&gt;
&lt;p&gt;By now you should be able to make sense of this matrix but I&#x27;ll go over it anyway.
Nothing has changed except our Y vector (2nd row), it now goes 1 unit in world Y and 1 unit in X, resulting in the vector not being orthogonal to the X vector anymore.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;I tried to be as exhaustive as I could be if you need to remember one thing from this is this:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The translation is stored as is in the last row&lt;&#x2F;li&gt;
&lt;li&gt;The First three rows represent the X, Y and Z vectors of your object&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;I purposefully kept the transformations simple to keep the vectors lined up with the world as this makes for easy to read matrices but you are obviously not limited to this.
You will very rarely manipulate those values directly but rather do a bit of vector math to compute your XYZ vectors and throw those in the matrix instead.&lt;&#x2F;p&gt;
&lt;p&gt;I highly recommend that you experiment with matrices yourself to build that intuition as this is going to beat any explanation in your understanding of them.
Maya 2020&#x27;s offset parent matrix is great for that because you can manipulate the matrix directly in the attribute editor:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;.&#x2F;images&#x2F;offset_parent_matrix.png#center&quot; alt=&quot;Offset Parent Matrix UI&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The &amp;quot;Composition&amp;quot; tab gives you a more traditional Translate&#x2F;Rotate&#x2F;Scale&#x2F;Shear interface over the same matrix which is great to understand the relation between those values and the underlying matrix.&lt;&#x2F;p&gt;
&lt;p&gt;If you&#x27;re looking for a cheat sheet of what we covered, Marin Petrov has a great Zine that does this and more&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;twitter-tweet&quot;&gt;
  &lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;
    Time for another Zine for TDs. This one is about Transformation Matrices
    (mostly in Maya) 😃&lt;a
      href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;hashtag&#x2F;cg?src=hash&amp;amp;ref_src=twsrc%5Etfw&quot;
      &gt;#cg&lt;&#x2F;a
    &gt;
    &lt;a
      href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;hashtag&#x2F;animation?src=hash&amp;amp;ref_src=twsrc%5Etfw&quot;
      &gt;#animation&lt;&#x2F;a
    &gt;
    &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;hashtag&#x2F;math?src=hash&amp;amp;ref_src=twsrc%5Etfw&quot;
      &gt;#math&lt;&#x2F;a
    &gt;
    &lt;a
      href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;hashtag&#x2F;LinearAlgebra?src=hash&amp;amp;ref_src=twsrc%5Etfw&quot;
      &gt;#LinearAlgebra&lt;&#x2F;a
    &gt;
    &lt;a href=&quot;https:&#x2F;&#x2F;t.co&#x2F;scEHrbBo7K&quot;&gt;pic.twitter.com&#x2F;scEHrbBo7K&lt;&#x2F;a&gt;
  &lt;&#x2F;p&gt;
  &amp;mdash; Marin Petrov 🇺🇦 (@marinwaves)
  &lt;a
    href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;marinwaves&#x2F;status&#x2F;1289127678390472704?ref_src=twsrc%5Etfw&quot;
    &gt;July 31, 2020&lt;&#x2F;a
  &gt;
&lt;&#x2F;blockquote&gt;
&lt;script
  async
  src=&quot;https:&#x2F;&#x2F;platform.twitter.com&#x2F;widgets.js&quot;
  charset=&quot;utf-8&quot;
&gt;&lt;&#x2F;script&gt;
&lt;p&gt;The next post is going to cover how to apply transformations through matrix multiplications.&lt;&#x2F;p&gt;
</content>
	</entry>
</feed>
