<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>csa3d.com</title>
	<atom:link href="http://www.csa3d.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.csa3d.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 26 Oct 2009 03:00:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Import and drag friendly modules in MotionBuilder</title>
		<link>http://www.csa3d.com/blog/2009/10/import-and-drag-friendly-modules-in-motionbuilder/</link>
		<comments>http://www.csa3d.com/blog/2009/10/import-and-drag-friendly-modules-in-motionbuilder/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 02:52:12 +0000</pubDate>
		<dc:creator>csa3d</dc:creator>
				<category><![CDATA[MotionBuilder]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://csa3d.com/blog/?p=7</guid>
		<description><![CDATA[Did you know that through MotionBuilder&#8217;s Asset Browser, you can map any directory of Python scripts by right clicking in the window and choosing add a &#8220;favorite path&#8221;?  This allows you to now drag any script into the Viewer Window and have it execute line by line, top to bottom, calling any code it finds [...]]]></description>
			<content:encoded><![CDATA[<p>Did you know that through MotionBuilder&#8217;s Asset Browser, you can map any directory of Python scripts by right clicking in the window and choosing add a &#8220;favorite path&#8221;?  This allows you to now drag any script into the Viewer Window and have it execute line by line, top to bottom, calling any code it finds in the order it finds it.</p>
<p>To set this up, simply browse to your python scripts directory and choose the OK button.  The directory should populate with any scripts you have currently in that directory.  If you add or remove scripts in the middle of a session, right click on your python folder created above and click &#8220;Refresh Directory&#8221;.</p>
<p>Now that you&#8217;ve got that set up, let&#8217;s create a sample script to illustrate the drag to execute functionality.  In that custom directory, create the following script and name it &#8220;Greet.py&#8221;:</p>
<pre>
<pre class="brush: php">
#Greet.py

name = &quot;Chris&quot;
print &quot;Hello, %s.&quot; % name
</pre>
</pre>
<p>If you left-mouse-drag that script into the Viewer window and choose Execute, you will see the following output print to the python editor window:</p>
<pre style="padding-left: 30px;"><span style="color: #0000ff;"><span style="color: #000000;">&gt;&gt;</span>
Hello, Chris.
<span style="color: #000000;">&gt;&gt;
</span> </span></pre>
<p>Python executed that script top to bottom doing exactly as you told it to do.  While this greeting example is rather basic, imagine you&#8217;ve created a useful function such as deleting all animation layers, or renaming all takes to all use a common suffix.  You can see how handy dragging a script into the scene and executing it could be.  Triggering this code required no fancy interface to launch it, and grabbing it from the Asset Browser was quick and easy, so that&#8217;s a plus.</p>
<p>Now consider that you&#8217;ve decided your utility is super handy, and would also be great if other modules could use this code.  I&#8217;ve decided all my scripts should greet me, but the code I&#8217;ve written isn&#8217;t import friendly.  With the module written above, as soon as I import my Greet module python is going to execute the code top to bottom, and thus will immediately import the <em>name</em> object into my current namespace (possibly overwriting something important) and execute the greeting.</p>
<p>Example importing our code from above:</p>
<pre style="padding-left: 30px;">&gt;&gt;&gt; <span style="color: #ff6600;">import</span> Greet
<span style="color: #0000ff;">Hello, Chris.</span>
&gt;&gt;&gt;</pre>
<p>It is obvious that if I plan to import this module into other modules, than I need to wrap this code in a function def.  That way  I can call it whenever I want to, and not have it run  directly upon initial import.  Let&#8217;s re-write our code as follows:</p>
<pre>
<pre class="brush: php">
# Greet.py

def hello( name ):
    print &quot;Hello, %s&quot; % name
</pre>
</pre>
<p>Now from the python interactive terminal I import  the new module and test it out:</p>
<pre style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #000000;">&gt;&gt;&gt; <span style="color: #ff6600;">import</span> Greet</span><span style="color: #000000;">
&gt;&gt;&gt; Greet.hello("Chris")</span></pre>
<pre style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #0000ff;">Hello, Chris</span></pre>
<pre style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #000000;">&gt;&gt;&gt; </span></pre>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #000000;"><br />
</span></p>
<p style="margin: 0px; text-indent: 0px;"><span style="color: #000000;">Great!  Now any other module can use my greeting code.  One problem, I can no longer be greeted by dragging in my script object from the Asset Browser.  If I wanted to, I could create another module and duplicate my code above, omitting the def() wrapper, just like we had when we first started.  Now I&#8217;d have one script to drag, and one script to import.  This is really unnecessary, sloppy, and will eventually become a burden <span style="text-decoration: line-through;">if</span> &#8230; <em>when</em>&#8230; I decide to change code in one of those files, because I&#8217;ll have to go and update both scripts with the new changes.  There are better options!<br />
</span></p>
<p style="margin: 0px; text-indent: 0px;"><span style="color: #000000;"><br />
</span></p>
<p style="margin: 0px; text-indent: 0px;"><span style="color: #000000;">Let&#8217;s look at what happens when we drag a script into the Viewer Window. </span><span style="color: #000000;">Every module by default, has a <em>__name__</em> attribute, including the top level module running python.  When you drag a script into the Viewer, the module drug into the scene inherits the value stored in the top level <em>__name__</em> attribute.  If the module is imported using the import keyword, the <em>__name__</em> attribute is filled with the name of the .py file imported.<br />
</span></p>
<p style="margin: 0px; text-indent: 0px;">
<p style="margin: 0px; text-indent: 0px;">
<p style="margin: 0px; text-indent: 0px;">
<p style="margin: 0px; text-indent: 0px;">
<p style="margin: 0px; text-indent: 0px;">
<p style="margin: 0px; text-indent: 0px;"><span style="color: #000000;"><br />
To illustrate this, take this example:</span></p>
<p style="margin: 0px; text-indent: 0px;">
<pre style="margin: 0px; text-indent: 0px;">
<pre class="brush: php">
# WhatName.py

def callMe():
    print &quot;My __name__ is %s&quot; % __name__
    print &quot;You called?&quot;

if __name__ == &quot;__builtin__&quot;:
    print &quot;You drug me into the viewer..&quot;
    callMe()
</pre>
</pre>
<p style="margin: 0px; text-indent: 0px;">
<p style="margin: 0px; text-indent: 0px;">Dragging this into the viewer produces:</p>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;">
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #000000;">&gt;&gt;&gt;</span><span style="color: #0000ff;"><br />
You drug me into the viewer..</span></p>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #0000ff;">My __name__ is __builtin__</span></p>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #0000ff;">You called?</span></p>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #000000;">&gt;&gt;&gt; </span></p>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #000000;"><br />
</span></p>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;">
<p style="margin: 0px; text-indent: 0px;"><span style="color: #000000;">while manually executing the following produces different results:</span></p>
<p style="margin: 0px; text-indent: 0px;"><span style="color: #000000;"><br />
</span></p>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #000000;">&gt;&gt;&gt;<span style="color: #ff6600;"> import</span> WhatName<br />
&gt;&gt;&gt; WhatName.callMe()</span></p>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #0000ff;">My __name__ is WhatName</span></p>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #0000ff;">You called?</span></p>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #000000;">&gt;&gt;&gt; </span></p>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;"><span style="color: #000000;"><br />
</span></p>
<p style="margin: 0px; text-indent: 0px; padding-left: 30px;">
<p style="margin: 0px; text-indent: 0px;"><span style="color: #000000;">Notice  that when we drag WhatName.py into the viewer that  <em>__name__</em> ==<span style="color: #008000;"> </span><span style="color: #339966;"><span style="color: #008000;">&#8216;__builtin__&#8217;</span> </span>, but when we import it into any other module using import then <em>__name__</em> == <span style="color: #008000;">&#8216;WhatName&#8217;</span>.</span></p>
<p style="margin: 0px; text-indent: 0px;">
<p style="margin: 0px; text-indent: 0px;"><span style="color: #000000;"><br />
Taking this info back to our original Greet.py example, we now know how to edit the script so that we can import it into another module and call it by name *or* drag it into the Viewer Window and have it self execute.</span></p>
<p style="margin: 0px; text-indent: 0px;">
<p style="margin: 0px; text-indent: 0px;"><span style="color: #000000;"><br />
</span></p>
<p style="margin: 0px; text-indent: 0px;">
<p style="margin: 0px; text-indent: 0px;">
<p style="margin: 0px; text-indent: 0px;">
<p style="margin: 0px; text-indent: 0px;"><span style="color: #000000;">Here is the final code for Greet.py:</span></p>
<p style="margin: 0px; text-indent: 0px;">
<pre style="margin: 0px; text-indent: 0px;">
<pre class="brush: php">
# Greet.py

def hello( name ):
    print &quot;Hello, %s.&quot; % name

if __name__ == &quot;__builtin__&quot;:
    myName = &quot;Chris&quot;
    hello( myName )
</pre>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.csa3d.com/blog/2009/10/import-and-drag-friendly-modules-in-motionbuilder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

