<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" version="2.0">

<channel>
	<title>ShaderOp.com</title>
	
	<link>http://shaderop.com</link>
	<description>Professional code monkey, hobbyist pixel pusher.</description>
	<lastBuildDate>Tue, 29 Nov 2011 16:32:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Shaderop" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="shaderop" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license><item>
		<title>Cubic Bezier Curve Node for Softimage ICE</title>
		<link>http://shaderop.com/2011/07/cubic-bezier-curve-node-for-softimage-ice/</link>
		<comments>http://shaderop.com/2011/07/cubic-bezier-curve-node-for-softimage-ice/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 10:48:06 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Cpp]]></category>
		<category><![CDATA[ICE]]></category>
		<category><![CDATA[plug-ins]]></category>
		<category><![CDATA[softimage]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=449</guid>
		<description><![CDATA[Bezier curves are handy little things, especially the cubic variety. A while back I was playing around with ICE rigging in Autodesk Softimage, and I found myself in need of a cubic Bezier curve compound. I initially built one using the native ICE nodes, but although the tree was rather simple and consisted of nothing more [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve">Bezier curves</a> are handy little things, especially the <a href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B.C3.A9zier_curves">cubic variety</a>. A while back I was playing around with <a href="http://download.autodesk.com/global/docs/softimage2012/en_us/userguide/files/ikine_kinematics2_ICERigs.htm">ICE rigging</a> in <a href="http://www.autodesk.com/softimage">Autodesk Softimage</a>, and I found myself in need of a cubic Bezier curve compound. I initially built one using the native ICE nodes, but although the tree was rather simple and consisted of nothing more than a few math nodes, the compound was simply too slow to be usable. So I set out to build a custom C++ ICE node to do the job.</p>
<p>Here it is in action:</p>
<div id="attachment_451" class="wp-caption aligncenter" style="width: 610px"><img class="size-full wp-image-451" title="bezier-curve-demo" src="http://shaderop.com/wp-content/uploads/2011/07/bezier-curve-demo.gif" alt="SO_CubicBezierCurve in action" width="600" height="336" /><p class="wp-caption-text">SO_CubicBezierCurve in action</p></div>
<p>&nbsp;</p>
<p>Here&#8217;s what it looks like in the ICE tree:</p>
<div id="attachment_452" class="wp-caption aligncenter" style="width: 522px"><img class="size-full wp-image-452" title="bezier-curve-ice-tree" src="http://shaderop.com/wp-content/uploads/2011/07/bezier-curve-ice-tree.png" alt="SO_CubicBezierCurve node in an ICE tree" width="512" height="256" /><p class="wp-caption-text">SO_CubicBezierCurve node in an ICE tree</p></div>
<p>&nbsp;</p>
<p>Here&#8217;s the formal description from the source code:</p>
<blockquote><p> <strong>SO Cubic Bezier Curve</strong> is an ICE node for<strong> Autodesk Softimage 2011 SAP</strong> and above that computes a position on a Bezier curve, where the curve is defined by four input control points (P0, P1, P2, P3), and the distance along the curve is controlled using a parametric input parameter &#8220;S.&#8221;</p>
<p>It&#8217;s possible to replicate this functionality using nothing more than the basic ICE math nodes, but my tests showed that the performance in that case is unacceptably slow; typical results were <strong>500 milliseconds</strong> per evaluation for the native math nodes compared to <strong>20 milliseconds</strong> for the C++ implementation.</p></blockquote>
<p>And finally, the binaries:</p>
<ul>
<li><a href="http://shaderop.com/wp-content/uploads/2011/07/SO_CubicBezierCurve-v1.0-32-bit.zip">SO_CubicBezierCurve-v1.0-32-bit.zip</a></li>
<li><a href="http://shaderop.com/wp-content/uploads/2011/07/SO_CubicBezierCurve-v1.0-64-bit.zip">SO_CubicBezierCurve-v1.0-64-bit.zip</a></li>
</ul>
<p>And a simple test scene:</p>
<ul>
<li><a href="http://shaderop.com/wp-content/uploads/2011/07/BezierCurvePlugin-DemoScene.scn_.zip">BezierCurvePlugin-DemoScene.scn.zip</a></li>
</ul>
<p>And the source code:</p>
<ul>
<li><a href="https://bitbucket.org/shaderop/so_cubicbeziercurve">https://bitbucket.org/shaderop/so_cubicbeziercurve</a></li>
</ul>
<p>I&#8217;m unable to provide a Linux build since I don&#8217;t have access to Softimage on that platform, but building from the source code provided above should be simple.</p>
<p>Sorry for being rather terse and short on the details, but I imagine this plug-in to be of interest to technical directors for the most part, and I&#8217;m assuming that the information provided here is good enough for that audience. But do <a title="Contact" href="http://shaderop.com/contact/">contact</a> me if you need any help.</p>
<h4>A final thought</h4>
<p>It&#8217;s expected for a custom C++ ICE node to perform better than an equivalent ICE compound, but the one order of magnitude performance advantage is a bit puzzling. The only thing I can conclude is that there&#8217;s a certain fixed overhead incurred during the evaluation of every ICE node. And this overhead adds up quickly when evaluating many simple, interconnected ICE nodes.</p>
<p>tl;dr: A single, monolithic ICE node &gt;&gt; a complex ICE compound.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2011/07/cubic-bezier-curve-node-for-softimage-ice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Glossy Reflection and Refraction Shaders Updated for 3Delight for Softimage 3.0.9</title>
		<link>http://shaderop.com/2011/07/glossy-reflection-and-refraction-shaders-updated-for-3delight-for-softimage-3-0-9/</link>
		<comments>http://shaderop.com/2011/07/glossy-reflection-and-refraction-shaders-updated-for-3delight-for-softimage-3-0-9/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 19:40:38 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[3delight]]></category>
		<category><![CDATA[plug-ins]]></category>
		<category><![CDATA[renderman]]></category>
		<category><![CDATA[rsl]]></category>
		<category><![CDATA[softimage]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=439</guid>
		<description><![CDATA[A while back I wrote a couple of custom shaders for 3Delight for Softimage, in which I had to rely on a few hacks to get 3Delight to see the RenderMan shader implementation files that I was using. And it worked just fine for a while. Version 3.0.9 of 3Delight for Softimage was released a [...]]]></description>
			<content:encoded><![CDATA[<p>A while back I wrote a <a title="Glossy Reflection and Refraction Shaders for 3Delight for Softimage" href="http://shaderop.com/2011/03/glossy-reflection-and-refraction-shaders-for-3delight-for-softimage/">couple of custom shaders</a> for <a href="http://www.3delight.com/en/index.php?page=3DFS_overview">3Delight for Softimage</a>,  in which I had to rely on a few hacks to get 3Delight to see the  RenderMan shader implementation files that I was using. And it worked  just fine for a while.</p>
<p>Version 3.0.9 of 3Delight for Softimage was  released a little while ago, and it introduced a better, more robust  way to distribute customs shaders. But that also meant that my hacks no  longer worked.</p>
<p>Fortunately it turned out that the fix was easy to  implement. So without further ado, here&#8217;s the improved, 3.0.9-compatible  glossy reflection and refraction shaders for 3Delight for Softimage:</p>
<p><a href="http://shaderop.com/wp-content/uploads/2011/07/SO_3DelightShaders_v0.3.xsiaddon.zip">SO_3DelightShaders_v0.3.xsiaddon.zip</a></p>
<p>Someone also asked me if I could provide a demo scene that shows these shaders in action. And I&#8217;m glad to oblige:</p>
<p><a href="http://shaderop.com/wp-content/uploads/2011/07/CornellBox-3Delight-Glossy-Refraction.scn_.zip">CornellBox-3Delight-Glossy-Refraction.scn.zip</a></p>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2011/07/glossy-reflection-and-refraction-shaders-updated-for-3delight-for-softimage-3-0-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Softimage Icons For Your Windows 7 Taskbar</title>
		<link>http://shaderop.com/2011/04/softimage-icons-for-your-windows-7-taskbar/</link>
		<comments>http://shaderop.com/2011/04/softimage-icons-for-your-windows-7-taskbar/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 13:40:50 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Miscellanea]]></category>
		<category><![CDATA[icons]]></category>
		<category><![CDATA[softimage]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=423</guid>
		<description><![CDATA[I usually keep two versions of Softimage installed on my computer: the current version and whatever version that preceded it. I also keep the shortcuts to both versions pinned to my Windows 7 taskbar for easy access. But since both of them use the same icon, it usually takes me a fraction of a second [...]]]></description>
			<content:encoded><![CDATA[<p>I usually keep two versions of <a href="http://www.autodesk.com/softimage">Softimage</a> installed on my computer: the current version and whatever version that preceded it. I also keep the shortcuts to both versions pinned to my Windows 7 taskbar for easy access. But since both of them use the same icon, it usually takes me a fraction of a second to decide which version I want to launch.</p>
<p>So to make things easier, I made a few icons to help me tell them apart. Here&#8217;s what my taskbar looks like at the moment:</p>
<p><a href="http://shaderop.com/wp-content/uploads/2011/04/Softimage-Windows7-Taskbar.png"><img class="aligncenter size-full wp-image-424" title="Softimage-Windows7-Taskbar" src="http://shaderop.com/wp-content/uploads/2011/04/Softimage-Windows7-Taskbar.png" alt="Softimage Windows 7 Taskbar icons" width="512" height="40" /></a></p>
<p>I&#8217;m not sure how many microseconds I&#8217;ll be saving every day because of those shortcuts, but at least it looks better than two identical icons next to each other.</p>
<p>Here are the icons for Softimage 2010, 2011.5, and 2012 in case someone else wants to use them:</p>
<p><a href="http://shaderop.com/wp-content/uploads/2011/04/SI2010.ico">SI2010.ico</a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2011/04/SI2011SAP.ico">SI2011SAP.ico</a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2011/04/SI2012.ico">SI2012.ico</a></p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2011/04/softimage-icons-for-your-windows-7-taskbar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using wxPython with Autodesk Softimage</title>
		<link>http://shaderop.com/2011/04/using-wxpython-with-autodesk-softimage/</link>
		<comments>http://shaderop.com/2011/04/using-wxpython-with-autodesk-softimage/#comments</comments>
		<pubDate>Sat, 09 Apr 2011 18:03:49 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[softimage]]></category>
		<category><![CDATA[wxPython]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=413</guid>
		<description><![CDATA[Every now and then someone on the Softimage mailing list would ask a question about using wxPython or PyQT with Autodesk Softimage, and such questions usually go unaddressed or receive a vague answer at best. So I&#8217;ll try to answer that question once and for all in this post. Softimage already has a robust set [...]]]></description>
			<content:encoded><![CDATA[<p>Every now and then someone on the <a href="http://groups.google.com/group/xsi_list">Softimage mailing list</a> would ask a question about using <a href="http://wxpython.org/">wxPython</a> or <a href="http://www.riverbankcomputing.co.uk/software/pyqt/intro">PyQT</a> with <a href="http://usa.autodesk.com/adsk/servlet/pc/index?id=13571168&amp;siteID=123112">Autodesk Softimage</a>, and such questions usually go unaddressed or receive a vague answer at best. So I&#8217;ll try to answer that question once and for all in this post.</p>
<p>Softimage already has a robust set of built-in user interface controls that should be adequate for most scenarios, but sometimes it would make sense to build a richer UI, like in the case of integrating external pipeline tools. And since Python is something of a <em>de facto</em> standard in the VFX industry, <strong>wxPython</strong> and <strong>PyQT</strong> are often the libraries of choice for such tasks.</p>
<p>The only problem is&#8230; Well, you better see for yourself. This is what happened when I tried to import the <strong>wxPython</strong> module in the script editor inside Softimage:</p>
<p><a href="http://shaderop.com/wp-content/uploads/2011/04/SI-wxPython-Crash.gif"><img class="aligncenter size-full wp-image-414" title="Softimage crashing when importing wxPython" src="http://shaderop.com/wp-content/uploads/2011/04/SI-wxPython-Crash.gif" alt="" width="640" height="480" /></a></p>
<p>An instant, albeit orderly crash to desktop.</p>
<p>Here&#8217;s the issue as far as I understand it: On Windows operating systems, every process that has a window (i.e. any none-console application) also has a <a href="http://msdn.microsoft.com/en-us/library/aa383682%28VS.85%29.aspx">message loop</a>, and each process can only have <em>one</em> message loop. The problem with PyQT and wxPython is that they will try to create their own message loops inside the Softimage process, which somehow leads to the whole process shutting down. I don&#8217;t know Linux that well, but the same issue should arise there.</p>
<p>It turns out that the solution to this is to run wxPython or PyQT on their own separate threads, so that both message loops will run in isolation from each other.</p>
<p>Apparently Houdini 9.5 had the same thing happening with PyQT, and its documentation contains a <a href="http://www.sidefx.com/docs/houdini9.5/hom/cookbook/pyqt/">code sample that addresses this issue</a>. All I had to do was repackage it inside a Softimage plug-in, although I used wxPython instead of PyQT because I&#8217;m more familiar with it. But it should be trivial to change the code to use PyQT instead.</p>
<p>So, without further ado, here&#8217;s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #5f5a60;"># By Mohammad &quot;ShaderOp&quot; Abdulfatah</span>
<span style="color: #5f5a60;">#</span>
<span style="color: #5f5a60;"># This little sample demonstrates how to use wxPython to display</span>
<span style="color: #5f5a60;"># a dialog box inside Autodesk Softimage. It attached an item</span>
<span style="color: #5f5a60;"># to the &quot;Window&quot; menu named &quot;WxSceneInfo&quot;, which executes a</span>
<span style="color: #5f5a60;"># a command that shows a wxPython dialog box with a labed and</span>
<span style="color: #5f5a60;"># two buttons. One button adpats the label with the number</span>
<span style="color: #5f5a60;"># of top-level objects in the current scne, and the other creates</span>
<span style="color: #5f5a60;"># a polygonal cube.</span>
<span style="color: #5f5a60;">#</span>
<span style="color: #5f5a60;"># Modifying this code to work wiht PyQT should be trivial.</span>
<span style="color: #5f5a60;">#</span>
<span style="color: #5f5a60;"># For questions, comments, and feedback, you can reach me</span>
<span style="color: #5f5a60;"># through my website at http://shaderop.com/contact/</span>
&nbsp;
<span style="color: #cda869;">import</span> win32com.<span style="color: white;">client</span>
<span style="color: #cda869;">from</span> win32com.<span style="color: white;">client</span> <span style="color: #cda869;">import</span> constants
<span style="color: #cda869;">import</span> <span style="color: white;">threading</span>
&nbsp;
<span style="color: #5f5a60;"># This will be used to ensure that only one copy of the dialog</span>
<span style="color: #5f5a60;"># is running.</span>
is_dialog_running_event = <span style="color: white;">threading</span>.<span style="color: white;">Event</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
&nbsp;
<span style="color: #cda869;">def</span> XSILoadPlugin<span style="color: white;">&#40;</span>in_reg<span style="color: white;">&#41;</span>:
  in_reg.<span style="color: white;">Author</span> = <span style="color: #8f9d6a;">&quot;Mohammad Abdulfatah&quot;</span>
  in_reg.<span style="color: white;">Name</span> = <span style="color: #8f9d6a;">&quot;WxSceneInfoPlugin&quot;</span>
  in_reg.<span style="color: white;">Major</span> = <span style="color: #cf6a4c;">1</span>
  in_reg.<span style="color: white;">Minor</span> = <span style="color: #cf6a4c;">0</span>
&nbsp;
  in_reg.<span style="color: white;">RegisterCommand</span><span style="color: white;">&#40;</span><span style="color: #8f9d6a;">&quot;WxSceneInfo&quot;</span>,<span style="color: #8f9d6a;">&quot;WxSceneInfo&quot;</span><span style="color: white;">&#41;</span>
  in_reg.<span style="color: white;">RegisterMenu</span><span style="color: white;">&#40;</span>constants.<span style="color: white;">siMenuMainWindowID</span>,
    <span style="color: #8f9d6a;">&quot;WxSceneInfo_Menu&quot;</span>,
    <span style="color: #cf6a4c;">False</span>,
    <span style="color: #cf6a4c;">False</span><span style="color: white;">&#41;</span>
&nbsp;
  <span style="color: #5f5a60;">#RegistrationInsertionPoint - do not remove this line</span>
&nbsp;
  <span style="color: #cda869;">return</span> <span style="color: #cf6a4c;">True</span>
&nbsp;
<span style="color: #cda869;">def</span> XSIUnloadPlugin<span style="color: white;">&#40;</span>in_reg<span style="color: white;">&#41;</span>:
  strPluginName = in_reg.<span style="color: white;">Name</span>
  <span style="color: #cda869;">return</span> <span style="color: #cf6a4c;">True</span>
&nbsp;
<span style="color: #cda869;">def</span> WxSceneInfo_Init<span style="color: white;">&#40;</span>in_ctxt<span style="color: white;">&#41;</span>:
  oCmd = in_ctxt.<span style="color: white;">Source</span>
  oCmd.<span style="color: white;">Description</span> = <span style="color: #8f9d6a;">&quot;Wx Scene Info&quot;</span>
  oCmd.<span style="color: white;">ReturnValue</span> = <span style="color: #cf6a4c;">True</span>
&nbsp;
  <span style="color: #cda869;">return</span> <span style="color: #cf6a4c;">True</span>
&nbsp;
<span style="color: #cda869;">def</span> WxSceneInfo_Execute<span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>:
  <span style="color: #5f5a60;"># only launch the dialog if it's not already running.</span>
  <span style="color: #cda869;">if</span> is_dialog_running_event.<span style="color: white;">is_set</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span> == <span style="color: #cf6a4c;">False</span>:
    __queueCommand<span style="color: white;">&#40;</span>runDialog<span style="color: white;">&#41;</span>
  <span style="color: #cda869;">else</span>:
    <span style="color: #5f5a60;"># It's probably a good idea to somehow notify the user that</span>
    <span style="color: #5f5a60;"># the dialog is already running.</span>
    <span style="color: #cda869;">pass</span>
&nbsp;
  <span style="color: #cda869;">return</span> <span style="color: #cf6a4c;">True</span>
&nbsp;
<span style="color: #cda869;">def</span> WxSceneInfo_Menu_Init<span style="color: white;">&#40;</span> in_ctxt <span style="color: white;">&#41;</span>:
  oMenu = in_ctxt.<span style="color: white;">Source</span>
  oMenu.<span style="color: white;">AddCommandItem</span><span style="color: white;">&#40;</span><span style="color: #8f9d6a;">&quot;WxSceneInfo&quot;</span>,<span style="color: #8f9d6a;">&quot;WxSceneInfo&quot;</span><span style="color: white;">&#41;</span>
  <span style="color: #cda869;">return</span> <span style="color: #cf6a4c;">True</span>
&nbsp;
<span style="color: #cda869;">def</span> runDialog<span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>:
  <span style="color: #5f5a60;"># Any attempt to import the wx module on the main application thread</span>
  <span style="color: #5f5a60;"># will cause Softiamge to crash and burn, but since this method runs</span>
  <span style="color: #5f5a60;"># on a separate thread and has its own message loop, importing wx</span>
  <span style="color: #5f5a60;"># will work here.</span>
  <span style="color: #cda869;">import</span> wx	
&nbsp;
  <span style="color: #5f5a60;"># Now go to town</span>
  <span style="color: #cda869;">class</span> MyFrame<span style="color: white;">&#40;</span>wx.<span style="color: white;">Frame</span><span style="color: white;">&#41;</span>:
    <span style="color: #cda869;">def</span> <span style="color: white;">__init__</span><span style="color: white;">&#40;</span><span style="color: #cf6a4c;">self</span>, title<span style="color: white;">&#41;</span>:
      wx.<span style="color: white;">Frame</span>.<span style="color: white;">__init__</span><span style="color: white;">&#40;</span><span style="color: #cf6a4c;">self</span>,
        <span style="color: #cf6a4c;">None</span>,
        size=<span style="color: white;">&#40;</span><span style="color: #cf6a4c;">400</span>,<span style="color: #cf6a4c;">300</span><span style="color: white;">&#41;</span>,
        style=wx.<span style="color: white;">DEFAULT_FRAME_STYLE</span>,
        title=title<span style="color: white;">&#41;</span>
&nbsp;
      <span style="color: #5f5a60;"># set background color to SI's signature desert gray.</span>
      <span style="color: #cf6a4c;">self</span>.<span style="color: white;">SetBackgroundColour</span><span style="color: white;">&#40;</span>wx.<span style="color: white;">Color</span><span style="color: white;">&#40;</span><span style="color: #cf6a4c;">171</span>, <span style="color: #cf6a4c;">168</span>, <span style="color: #cf6a4c;">166</span><span style="color: white;">&#41;</span><span style="color: white;">&#41;</span>
      <span style="color: #cf6a4c;">self</span>.<span style="color: white;">Bind</span><span style="color: white;">&#40;</span>wx.<span style="color: white;">EVT_CLOSE</span>, <span style="color: #cf6a4c;">self</span>.<span style="color: white;">OnClose</span><span style="color: white;">&#41;</span>
&nbsp;
      <span style="color: #cf6a4c;">self</span>.<span style="color: white;">objectCountLabel</span> = wx.<span style="color: white;">StaticText</span><span style="color: white;">&#40;</span><span style="color: #cf6a4c;">self</span>,
        label=<span style="color: #8f9d6a;">&quot;Number of objects in scene:&quot;</span>,
        pos = <span style="color: white;">&#40;</span><span style="color: #cf6a4c;">10</span>, <span style="color: #cf6a4c;">10</span><span style="color: white;">&#41;</span>,
        size = <span style="color: white;">&#40;</span><span style="color: #cf6a4c;">300</span>, -<span style="color: #cf6a4c;">1</span><span style="color: white;">&#41;</span><span style="color: white;">&#41;</span>
      <span style="color: #cf6a4c;">self</span>.<span style="color: white;">refreshButton</span> = wx.<span style="color: white;">Button</span><span style="color: white;">&#40;</span><span style="color: #cf6a4c;">self</span>, label = <span style="color: #8f9d6a;">&quot;Refresh&quot;</span>,
                                     pos = <span style="color: white;">&#40;</span><span style="color: #cf6a4c;">310</span>, <span style="color: #cf6a4c;">10</span><span style="color: white;">&#41;</span><span style="color: white;">&#41;</span>
      <span style="color: #cf6a4c;">self</span>.<span style="color: white;">Bind</span><span style="color: white;">&#40;</span>wx.<span style="color: white;">EVT_BUTTON</span>, <span style="color: #cf6a4c;">self</span>.<span style="color: white;">OnRefresh</span>, <span style="color: #cf6a4c;">self</span>.<span style="color: white;">refreshButton</span><span style="color: white;">&#41;</span>
&nbsp;
      <span style="color: #cf6a4c;">self</span>.<span style="color: white;">createCubeButton</span> = wx.<span style="color: white;">Button</span><span style="color: white;">&#40;</span><span style="color: #cf6a4c;">self</span>,
                                      label=<span style="color: #8f9d6a;">&quot;Create cube&quot;</span>,
                                      pos=<span style="color: white;">&#40;</span><span style="color: #cf6a4c;">310</span>, <span style="color: #cf6a4c;">50</span><span style="color: white;">&#41;</span><span style="color: white;">&#41;</span>
      <span style="color: #cf6a4c;">self</span>.<span style="color: white;">Bind</span><span style="color: white;">&#40;</span>wx.<span style="color: white;">EVT_BUTTON</span>, <span style="color: #cf6a4c;">self</span>.<span style="color: white;">OnCreateCube</span>, <span style="color: #cf6a4c;">self</span>.<span style="color: white;">createCubeButton</span><span style="color: white;">&#41;</span>
&nbsp;
    <span style="color: #cda869;">def</span> OnRefresh<span style="color: white;">&#40;</span><span style="color: #cf6a4c;">self</span>, event<span style="color: white;">&#41;</span>:
      count = Application.<span style="color: white;">ActiveSceneRoot</span>.<span style="color: white;">Children</span>.<span style="color: white;">Count</span>
      label = <span style="color: #8f9d6a;">&quot;Number of objects in scene: {0}&quot;</span>.<span style="color: white;">format</span><span style="color: white;">&#40;</span>count<span style="color: white;">&#41;</span>
      <span style="color: #cf6a4c;">self</span>.<span style="color: white;">objectCountLabel</span>.<span style="color: white;">SetLabel</span><span style="color: white;">&#40;</span>label<span style="color: white;">&#41;</span>
&nbsp;
    <span style="color: #cda869;">def</span> OnCreateCube<span style="color: white;">&#40;</span><span style="color: #cf6a4c;">self</span>, event<span style="color: white;">&#41;</span>:
      Application.<span style="color: white;">ActiveSceneRoot</span>.<span style="color: white;">AddGeometry</span><span style="color: white;">&#40;</span><span style="color: #8f9d6a;">'Cube'</span>, <span style="color: #8f9d6a;">'MeshSurface'</span><span style="color: white;">&#41;</span>
&nbsp;
    <span style="color: #cda869;">def</span> OnClose<span style="color: white;">&#40;</span><span style="color: #cf6a4c;">self</span>, event<span style="color: white;">&#41;</span>:
      <span style="color: #cf6a4c;">self</span>.<span style="color: white;">Destroy</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
      <span style="color: #5f5a60;"># IMPORTANT: Must release the event once the windows is closed,</span>
      <span style="color: #5f5a60;"># else the dialog won't launch again until after SI is restarted</span>
      <span style="color: #cda869;">global</span> is_dialog_running_event
      is_dialog_running_event.<span style="color: white;">clear</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
&nbsp;
  app = __getApplication<span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
  frame = MyFrame<span style="color: white;">&#40;</span><span style="color: #8f9d6a;">&quot;Hello World&quot;</span><span style="color: white;">&#41;</span>
  frame.<span style="color: white;">Show</span><span style="color: white;">&#40;</span><span style="color: #cf6a4c;">True</span><span style="color: white;">&#41;</span>
  is_dialog_running_event.<span style="color: #cf6a4c;">set</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
  app.<span style="color: white;">MainLoop</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
&nbsp;
<span style="color: #5f5a60;"># Following code is copied almost verbatim from Houdini 9.5 sample</span>
<span style="color: #5f5a60;"># code at http://www.sidefx.com/docs/houdini9.5/hom/cookbook/pyqt/</span>
&nbsp;
__command_queue = <span style="color: white;">&#91;</span><span style="color: white;">&#93;</span>
__command_queue_lock = <span style="color: white;">threading</span>.<span style="color: white;">RLock</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
__command_queue_event = <span style="color: white;">threading</span>.<span style="color: white;">Event</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
&nbsp;
__wx_thread = <span style="color: #cf6a4c;">None</span>
&nbsp;
<span style="color: #cda869;">def</span> __queueCommand<span style="color: white;">&#40;</span><span style="color: #cf6a4c;">callable</span>, arguments=<span style="color: white;">&#40;</span><span style="color: white;">&#41;</span><span style="color: white;">&#41;</span>:
  <span style="color: #cda869;">global</span> __wx_thread
&nbsp;
  <span style="color: #cda869;">if</span> __wx_thread <span style="color: #cda869;">is</span> <span style="color: #cf6a4c;">None</span>:
    __wx_thread = <span style="color: white;">threading</span>.<span style="color: white;">Thread</span><span style="color: white;">&#40;</span>target=__wxThreadMain,
                                   name=<span style="color: #8f9d6a;">&quot;wxThread&quot;</span><span style="color: white;">&#41;</span>
    __wx_thread.<span style="color: white;">start</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
&nbsp;
  __command_queue_lock.<span style="color: white;">acquire</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
  __command_queue.<span style="color: white;">append</span><span style="color: white;">&#40;</span><span style="color: white;">&#40;</span><span style="color: #cf6a4c;">callable</span>, arguments<span style="color: white;">&#41;</span><span style="color: white;">&#41;</span>
  __command_queue_lock.<span style="color: white;">release</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
&nbsp;
  __command_queue_event.<span style="color: #cf6a4c;">set</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
&nbsp;
<span style="color: #cda869;">def</span> __wxThreadMain<span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>:
  <span style="color: #cda869;">while</span> <span style="color: #cf6a4c;">True</span>:
    __command_queue_event.<span style="color: white;">wait</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
    __command_queue_lock.<span style="color: white;">acquire</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
    command = __command_queue.<span style="color: white;">pop</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
    __command_queue_event.<span style="color: white;">clear</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
    __command_queue_lock.<span style="color: white;">release</span><span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>
&nbsp;
    command<span style="color: white;">&#91;</span><span style="color: #cf6a4c;">0</span><span style="color: white;">&#93;</span>.<span style="color: white;">__call__</span><span style="color: white;">&#40;</span><span style="color: #66cc66;">*</span>command<span style="color: white;">&#91;</span><span style="color: #cf6a4c;">1</span><span style="color: white;">&#93;</span><span style="color: white;">&#41;</span>
&nbsp;
__wx_app = <span style="color: #cf6a4c;">None</span>
&nbsp;
<span style="color: #cda869;">def</span> __getApplication<span style="color: white;">&#40;</span><span style="color: white;">&#41;</span>:
  <span style="color: #cda869;">import</span> wx
  <span style="color: #cda869;">global</span> __wx_app
  <span style="color: #cda869;">if</span> __wx_app <span style="color: #cda869;">is</span> <span style="color: #cf6a4c;">None</span>:
    __wx_app = wx.<span style="color: white;">App</span><span style="color: white;">&#40;</span><span style="color: #cf6a4c;">False</span><span style="color: white;">&#41;</span>
  <span style="color: #cda869;">return</span> __wx_app</pre></div></div>

<p>Here&#8217;s what it looks like inside Softimage:</p>
<p><a href="http://shaderop.com/wp-content/uploads/2011/04/SI-wxPython-Dialog.gif"><img class="aligncenter size-full wp-image-416" title="wxPython dialog in Softimage" src="http://shaderop.com/wp-content/uploads/2011/04/SI-wxPython-Dialog.gif" alt="" width="604" height="398" /></a></p>
<p>And here&#8217;s the link to the Github gist:</p>
<p><a href="https://gist.github.com/911265">https://gist.github.com/911265</a></p>
<p>Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2011/04/using-wxpython-with-autodesk-softimage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>One Awesome Spam Comment</title>
		<link>http://shaderop.com/2011/03/one-awesome-spam-comment/</link>
		<comments>http://shaderop.com/2011/03/one-awesome-spam-comment/#comments</comments>
		<pubDate>Sat, 05 Mar 2011 11:28:04 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Miscellanea]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=400</guid>
		<description><![CDATA[This one really pushes the envelope: Attractive section of content. I just stumbled upon your website and in accession capital to assert that I get in fact enjoyed account your blog posts. Anyway I’ll be subscribing to your augment and even I achievement you access consistently rapidly. I hope they&#8217;re not selling dictionaries.]]></description>
			<content:encoded><![CDATA[<p>This one really pushes the envelope:</p>
<blockquote><p>Attractive section of content. I just stumbled upon your website and in accession capital to assert that I get in fact enjoyed account your blog posts. Anyway I’ll be subscribing to your augment and even I achievement you access consistently rapidly.</p></blockquote>
<p>I hope they&#8217;re not selling dictionaries.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2011/03/one-awesome-spam-comment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Glossy Reflection and Refraction Shaders for 3Delight for Softimage</title>
		<link>http://shaderop.com/2011/03/glossy-reflection-and-refraction-shaders-for-3delight-for-softimage/</link>
		<comments>http://shaderop.com/2011/03/glossy-reflection-and-refraction-shaders-for-3delight-for-softimage/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 17:54:54 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[3delight]]></category>
		<category><![CDATA[renderman]]></category>
		<category><![CDATA[rsl]]></category>
		<category><![CDATA[shaders]]></category>
		<category><![CDATA[softimage]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=391</guid>
		<description><![CDATA[Watch: Download: SO_3DelightShaders_v0.2.xsiaddon.zip Enjoy! Update (July 5th 2011): This version of the plug-in does not work with 3Delight v3.0.9 and later. If you are using that or a later version of 3Delight then you need to Grab the updated plug-in from here.]]></description>
			<content:encoded><![CDATA[<p>Watch:</p>
<iframe src="http://player.vimeo.com/video/20608026" width="666" height="374" frameborder="0"></iframe>
<p>Download:<br />
<a href="http://shaderop.com/wp-content/uploads/2011/03/SO_3DelightShaders_v0.2.xsiaddon.zip">SO_3DelightShaders_v0.2.xsiaddon.zip</a></p>
<p>Enjoy!</p>
<p><strong>Update (July 5th 2011):<br />
</strong>This version of the plug-in does <em>not</em> work with 3Delight v3.0.9 and later. If you are using that or a later version of 3Delight then you need to Grab the updated plug-in from <a title="Glossy Reflection and Refraction Shaders Updated for 3Delight for Softimage 3.0.9" href="http://shaderop.com/2011/07/glossy-reflection-and-refraction-shaders-updated-for-3delight-for-softimage-3-0-9/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2011/03/glossy-reflection-and-refraction-shaders-for-3delight-for-softimage/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>The Story of Cube and Sphere</title>
		<link>http://shaderop.com/2010/12/the-story-of-cube-and-sphere/</link>
		<comments>http://shaderop.com/2010/12/the-story-of-cube-and-sphere/#comments</comments>
		<pubDate>Thu, 23 Dec 2010 21:05:37 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://shaderop.com/2010/12/the-story-of-cube-and-sphere/</guid>
		<description><![CDATA[I’ve been trying to up my 3D-fu recently, but I keep finding that my commitment to any personal project falters when I don’t have a deadline looming and a client breathing down my neck. So I came up with an idea for a little animated ultra-short movie about accessibility. Since my current employer cares a [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been trying to up my 3D-fu recently, but I keep finding that my commitment to any personal project falters when I don’t have a deadline looming and a client breathing down my neck.</p>
<p>So I came up with an idea for a little animated ultra-short movie about <a href="http://en.wikipedia.org/wiki/Accessibility">accessibility</a>. Since my <a href="http://www.pscdr.org.sa/">current employer</a> cares a lot about this subject and is known to engage in advocacy for the rights of persons with special needs from time to time, I was hoping that they would be interested in sponsoring this little project of mine. I would get to practice my favorite hobby and learn something along the way, and they would get a little animated clip they they could use on the web or in TV ads. Win-win all around.</p>
<p>To help sell the idea better I <em>tried</em> to mimic the pros as best as I can by making a storyboard.</p>
<p>I pitched it around to different stakeholders, but there were no takers. So, I thought I would post the storyboard here instead of letting it collect e-dust on my hard desk.</p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-01.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-01" border="0" alt="Plate-01" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-01_thumb.jpg" width="644" height="381" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-02.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-02" border="0" alt="Plate-02" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-02_thumb.jpg" width="644" height="381" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-03.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-03" border="0" alt="Plate-03" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-03_thumb.jpg" width="644" height="381" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-04.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-04" border="0" alt="Plate-04" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-04_thumb.jpg" width="644" height="381" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-05.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-05" border="0" alt="Plate-05" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-05_thumb.jpg" width="644" height="337" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-06.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-06" border="0" alt="Plate-06" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-06_thumb.jpg" width="644" height="337" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-07.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-07" border="0" alt="Plate-07" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-07_thumb.jpg" width="644" height="337" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-08.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-08" border="0" alt="Plate-08" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-08_thumb.jpg" width="644" height="337" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-09.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-09" border="0" alt="Plate-09" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-09_thumb.jpg" width="644" height="337" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-10.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-10" border="0" alt="Plate-10" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-10_thumb.jpg" width="644" height="337" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-11.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-11" border="0" alt="Plate-11" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-11_thumb.jpg" width="644" height="337" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-12.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-12" border="0" alt="Plate-12" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-12_thumb.jpg" width="644" height="337" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-13.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-13" border="0" alt="Plate-13" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-13_thumb.jpg" width="644" height="337" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-14.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-14" border="0" alt="Plate-14" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-14_thumb.jpg" width="644" height="337" /></a></p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/12/Plate-15.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Plate-15" border="0" alt="Plate-15" src="http://shaderop.com/wp-content/uploads/2010/12/Plate-15_thumb.jpg" width="644" height="337" /></a></p>
<p>And scene.</p>
<p>Maybe I’ll get them next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2010/12/the-story-of-cube-and-sphere/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Buy This DVD: Creative Character Design Techniques</title>
		<link>http://shaderop.com/2010/09/buy-this-dvd-creative-character-design-techniques/</link>
		<comments>http://shaderop.com/2010/09/buy-this-dvd-creative-character-design-techniques/#comments</comments>
		<pubDate>Thu, 30 Sep 2010 02:23:55 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[drawing]]></category>
		<category><![CDATA[human]]></category>
		<category><![CDATA[male]]></category>
		<category><![CDATA[portraits]]></category>

		<guid isPermaLink="false">http://shaderop.com/2010/09/buy-this-dvd-creative-character-design-techniques/</guid>
		<description><![CDATA[More accurately titled “Creative Character Design Techniques with Neville Page: Fostering Happy Accidents,” which is a short set of training videos showing one artist’s approach to generating character design ideas quickly and almost effortlessly. For me it was an eye-opening experience. I don’t have any formal artistic training to speak of, and yet after watching [...]]]></description>
			<content:encoded><![CDATA[<p>More accurately titled “<a href="http://www.thegnomonworkshop.com/store/product/862/Creative-Character-Design-Techniques-with-Neville-Page">Creative Character Design Techniques with Neville Page: Fostering Happy Accidents</a>,” which is a short set of training videos showing one artist’s approach to generating character design ideas quickly and almost effortlessly.</p>
<p>For me it was an eye-opening experience. I don’t have any formal artistic training to speak of, and yet after watching the training videos and spending a few hours with a <a href="http://www.wacom.com/intuos/">Wacom Intuos</a> tablet and <a href="http://usa.autodesk.com/adsk/servlet/pc/index?siteID=123112&amp;id=6848332">Sketchbook Pro</a>, this came out:</p>
<p><a href="http://shaderop.com/wp-content/uploads/2010/09/Portraitofaman.jpg"><img style="display: block; float: none; margin-left: auto; margin-right: auto; border: 0px;" title="Portrait-of-a-man" src="http://shaderop.com/wp-content/uploads/2010/09/Portraitofaman_thumb.jpg" border="0" alt="Portrait-of-a-man" width="515" height="484" /></a></p>
<p>For me this a personal best, and it’s miles ahead of anything I have ever attempted to paint in my whole life. I’m specially proud of the fact that I didn’t use any reference images.</p>
<p>Thank you <a href="http://www.nevillepage.com/">Mr. Page</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2010/09/buy-this-dvd-creative-character-design-techniques/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Simple, Intra-Process Event Bus in C++, Part I</title>
		<link>http://shaderop.com/2010/09/a-simple-intra-process-event-bus-in-c-part-i/</link>
		<comments>http://shaderop.com/2010/09/a-simple-intra-process-event-bus-in-c-part-i/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 20:48:11 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Cpp]]></category>
		<category><![CDATA[event-bus]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=348</guid>
		<description><![CDATA[A previous post introduced a way of assigning a unique ID number to arbitrary C++ types. As already mentioned in that post, my motivation for doing this was to lay the foundation for a simple, intra-process messaging system suitable for use in games (particularly the examples presented in Programming Game AI by Example). This post [...]]]></description>
			<content:encoded><![CDATA[<p>A <a href="http://shaderop.com/2010/09/uniquely-identifying-types-in-c-without-using-rtti/">previous post</a> introduced a way of assigning a unique ID number to arbitrary C++ types. As already mentioned in that post, my motivation for doing this was to lay the foundation for a simple, intra-process messaging system suitable for use in games (particularly the examples presented in <a href="http://www.amazon.com/Programming-Game-Example-Mat-Buckland/dp/1556220782">Programming Game AI by Example</a>).</p>
<p>This post presents a possible implementation for such a messaging system. To avoid confusion with inter-process or network messaging, I’ll refer to “messages” as “events” from now on, which is a less loaded and more accurate term.</p>
<p>But first, let’s introduce the hero.</p>
<h4>Saving the world from nine to five</h4>
<p>We’ll be building a console-based, non-interactive game that depicts a normal day in the life of a superhero. And please don’t take the phrase “<em>normal</em> day in the life of a <em>superhero</em>” to be some sort of cynical commentary about the state of contemporary story telling or the human condition on my part.</p>
<p>Our hero’s day consists of six main events: waking up, driving to work, checking in, checking out, driving back, arriving home, and going to bed. Here’s one possible way to present his entire miserable existence in code:</p>
<p><span style="font-size: small;"> </span></p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">class</span> Hero
{
<span style="color: #cc7832;">private</span>:
    <span style="color: #cc7832;">int</span> m_clockTicks;

<span style="color: #cc7832;">public</span>:
    Hero(): m_clockTicks(<span style="color: #6897bb;">0</span>) {}

    <span style="color: #cc7832;">void</span> Update()
    {
        <span style="color: #cc7832;">switch</span> (m_clockTicks++ % <span style="color: #6897bb;">6</span>)
        {
        <span style="color: #cc7832;">case</span> <span style="color: #6897bb;">0</span>:
            std::cout &lt;&lt; <span style="color: #a5c25c;">"Hero: Yaaaaawn!"</span> &lt;&lt; std::endl;
            <span style="color: #cc7832;">break</span>;
        <span style="color: #cc7832;">case</span> <span style="color: #6897bb;">1</span>:
            std::cout &lt;&lt; <span style="color: #a5c25c;">"Hero: Getting into the Hero Mobile(tm)."
</span>                      &lt;&lt; std::endl;
            <span style="color: #cc7832;">break</span>;
        <span style="color: #cc7832;">case</span> <span style="color: #6897bb;">2</span>:
            std::cout &lt;&lt; <span style="color: #a5c25c;">"Hero: Punching into the Hero Cave(tm)."
</span>                      &lt;&lt; std::endl;
            <span style="color: #cc7832;">break</span>;
        <span style="color: #cc7832;">case</span> <span style="color: #6897bb;">3</span>:
            std::cout &lt;&lt; <span style="color: #a5c25c;">"Hero: Phew! Finally going home."
</span>                      &lt;&lt; std::endl;
            <span style="color: #cc7832;">break</span>;
        <span style="color: #cc7832;">case</span> <span style="color: #6897bb;">4</span>:
            std::cout &lt;&lt; <span style="color: #a5c25c;">"Hero: Honey! I'm home!"
</span>                      &lt;&lt; std::endl;
            <span style="color: #cc7832;">break</span>;
        <span style="color: #cc7832;">case</span> <span style="color: #6897bb;">5</span>:
            std::cout &lt;&lt; <span style="color: #a5c25c;">"Hero: Zzzzzzz..."</span> &lt;&lt; std::endl;
            <span style="color: #cc7832;">break</span>;
        <span style="color: #cc7832;">default</span>:
            <span style="color: #cc7832;">break</span>;
        }
    }
};

<span style="color: #cc7832;">int</span> main(<span style="color: #cc7832;">int</span> argc, <span style="color: #cc7832;">char</span>** argv)
{
    Hero theHero;

    <span style="color: #cc7832;">for</span> (<span style="color: #cc7832;">int</span> i = <span style="color: #6897bb;">0</span>; i &lt; <span style="color: #6897bb;">12</span>; ++i)
    {
        theHero.Update();
    }
    <span style="color: #cc7832;">return</span> <span style="color: #6897bb;">0</span>;
}</pre>
<p>The program iterates over just two days of the hero’s life (hence the <strong>12</strong> in the <strong>for</strong> loop), and produces the following output:</p>
<pre style="font-family: consolas;">Hero: Yaaaaawn!
Hero: Getting into the Hero Mobile(tm).
Hero: Punching into the Hero Cave(tm).
Hero: Phew! Finally going home.
Hero: Honey! I'm home!
Hero: Zzzzzzz...
Hero: Yaaaaawn!
Hero: Getting into the Hero Mobile(tm).
Hero: Punching into the Hero Cave(tm).
Hero: Phew! Finally going home.
Hero: Honey! I'm home!
Hero: Zzzzzzz...</pre>
<p>Certain species of plankton probably lead more exciting lives, but that’s beside the point.</p>
<p>This game, as awesome as it is already, is missing something. Something that would turn it into a classic. Something that would make it stand shoulder to shoulder next to <a href="http://fallout.bethsoft.com/index.html">Fallout 3</a>, <a href="http://lionhead.com/Fable/FableII/">Fable II</a>, and <a href="http://www.torchlightgame.com/">Torchlight</a>. It needs a <em>pet</em>.</p>
<p>Like any good, faithful companion, the pet will greet our hero every time he comes back from work. So, we’ll need a way to communicate that event to the pet and any other interested parties (like, say, the Mad Scientist’s advanced abduction and extraction team).</p>
<p>There are many ways to go about implementing this, but for the purpose of maximum decoupling between components it would probably be best to use some sort of <em>event bus</em>, where any object can send an event to the bus, which in turn takes care of relaying the events to all objects interested in receiving them.</p>
<p>After some false starts and dead ends, I ended up with a solution composed of the following components:</p>
<ul>
<li><strong>TypeIdentifer&lt;T&gt;</strong>: discussed at length in a <a href="http://shaderop.com/2010/09/uniquely-identifying-types-in-c-without-using-rtti/">previous post</a>.</li>
<li><strong>Event</strong>: The base case for all events sent and received in the system.</li>
<li><strong>IReceiver</strong>: An interface to be implemented by event receivers.</li>
<li><strong>IEventBus</strong>: The interface for the event bus.</li>
<li><strong>EventBus</strong>: A concrete implementation of <strong>IEventBus</strong>.</li>
</ul>
<p>What follows is a closer look at some of these components.</p>
<h4>The Event class</h4>
<p>This one is ridiculously simple:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">namespace</span> Events
{
    <span style="color: #cc7832;">class</span> Event
    {
    <span style="color: #cc7832;">public</span>:
        <span style="color: #cc7832;">virtual</span> ~Event() {}
    };
}</pre>
<p><span style="font-size: small;">It’s just a <a href="http://c2.com/cgi-bin/wiki?MarkerInterface">marker interface</a> that does nothing on its own.</span></p>
<p>Our game will have just one event, <strong>HeroMovedEvent</strong>, defined as follows:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">class</span> HeroMovedEvent : <span style="color: #cc7832;">public</span> Events::Event
{
<span style="color: #cc7832;">public</span>:
    <span style="color: #cc7832;">enum</span> Locations { HOME, WORK, ROAD };
<span style="color: #cc7832;">private</span>:
    Locations m_location;
<span style="color: #cc7832;">public</span>:
    <span style="color: #cc7832;">explicit</span> HeroMovedEvent(Locations location)
        : m_location(location)
    {
    }

    Locations Location() <span style="color: #cc7832;">const
</span>    {
        <span style="color: #cc7832;">return</span> m_location;
    }
};</pre>
<p>We’ll see how to use it a bit later.</p>
<h4>The IReceiver interface</h4>
<p>Which declares a single virtual method:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">namespace</span> Events
{
    <span style="color: #cc7832;">class</span> IReceiver
    {
    <span style="color: #cc7832;">public</span>:
        <span style="color: #cc7832;">virtual</span> <span style="color: #cc7832;">void</span> Receive(<span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> eventId, Event* e) = <span style="color: #6897bb;">0</span>;
        <span style="color: #cc7832;">virtual</span> ~IReceiver() {};
    };
}</pre>
<p>A partial implementation of the Hero’s pet would look like this:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">class</span> HerosPet: <span style="color: #cc7832;">public</span> Events::IReceiver
{
    <span style="color: gray;">/* Some detailed omitted */
</span><span style="color: #cc7832;">public</span>:

    <span style="color: #cc7832;">void</span> Receive(<span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> eventId, Events::Event* e)
    {
        <span style="color: #cc7832;">if</span> (eventId == Events::TypeIdentifier&lt;HeroMovedEvent&gt;::id())
        {
            HeroMovedEvent* e_ = <span style="color: #cc7832;">static_cast</span>&lt;HeroMovedEvent*&gt;(e);
            OnHeroMoved(e_);
        }
    }

    <span style="color: #cc7832;">void</span> OnHeroMoved(HeroMovedEvent* e)
    {
        <span style="color: #cc7832;">if</span> (e-&gt;Location() == HeroMovedEvent::HOME)
        {
            std::cout &lt;&lt; <span style="color: #a5c25c;">"Pet:  Woof! Woof!"</span> &lt;&lt; std::endl;
        }
    }
};</pre>
<p>We’ll add the missing details in just a bit.</p>
<p><span style="font-size: small;"> </span></p>
<p><strong>The IEventBus interface</strong></p>
<p>Defined thusly:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">namespace</span> Events
{
    <span style="color: #cc7832;">class</span> IEventBus
    {
    <span style="color: #cc7832;">public</span>:
        <span style="color: #cc7832;">virtual</span> <span style="color: #cc7832;">void</span> Register(<span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> eventId, IReceiver* receiver) = <span style="color: #6897bb;">0</span>;
        <span style="color: #cc7832;">virtual</span> <span style="color: #cc7832;">void</span> Unregister(<span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> eventId, IReceiver* receiver) = <span style="color: #6897bb;">0</span>;
        <span style="color: #cc7832;">virtual</span> <span style="color: #cc7832;">void</span> Unregister(IReceiver* receiver) = <span style="color: #6897bb;">0</span>;
        <span style="color: #cc7832;">virtual</span> <span style="color: #cc7832;">void</span> Broadcast(<span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> eventId, Event* <span style="color: #cc7832;">event</span>) = <span style="color: #6897bb;">0</span>;
        <span style="color: #cc7832;">virtual</span> ~IEventBus() {}
    };
}</pre>
<p>Any class that wants to be notified of events registers itself with the bus by passing the event id (obtained through <strong>TypeIdentifier&lt;T&gt;::id</strong> method) and itself to the <strong>Register</strong> method. The reverse operation is done by passing the same arguments to the <strong>Unregister</strong> method. The second version of <strong>Unregister</strong> is just a convenience method that unsubscribes a receiver from all events.</p>
<p>I provided an implementation for this interface in a class named <strong>EventBus</strong>, the details of which aren’t very interesting, so I won’t go over them here.</p>
<h4>The Revised Game</h4>
<p>Using the components above, the Hero class changes to this:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">class</span> Hero
{<span style="color: #cc7832;">
private</span>:
    <span style="color: #cc7832;">int</span> m_clockTicks;
    Events::IEventBus* m_eventBus;
<span style="color: #cc7832;">public</span>:
    <span style="color: #cc7832;">explicit</span> Hero(Events::IEventBus* eventBus)
        : m_clockTicks(<span style="color: #6897bb;">0</span>), m_eventBus(eventBus) {}

    <span style="color: #cc7832;">void</span> Update()
    {
        <span style="color: #cc7832;">switch</span> (m_clockTicks++ % <span style="color: #6897bb;">6</span>)
        {
        <span style="color: #cc7832;">case</span> <span style="color: #6897bb;">0</span>:
            {
                std::cout &lt;&lt; <span style="color: #a5c25c;">"Hero: Yaaaaawn!"</span> &lt;&lt; std::endl;
                <span style="color: #cc7832;">break</span>;
            }
        <span style="color: #cc7832;">case</span> <span style="color: #6897bb;">1</span>:
            {
                std::cout &lt;&lt; <span style="color: #a5c25c;">"Hero: Getting into the Hero Mobile(tm)."
</span>                          &lt;&lt; std::endl;
                HeroMovedEvent e(HeroMovedEvent::ROAD);
                m_eventBus-&gt;Broadcast
                    ( Events::TypeIdentifier&lt;HeroMovedEvent&gt;::id()
                    , &amp;e
                    );
                <span style="color: #cc7832;">break</span>;
            }
        <span style="color: #cc7832;">case</span> <span style="color: #6897bb;">2</span>:
            {
                std::cout &lt;&lt; <span style="color: #a5c25c;">"Hero: Punching into the Hero Cave(tm)."
</span>                          &lt;&lt; std::endl;
                HeroMovedEvent e(HeroMovedEvent::WORK);
                m_eventBus-&gt;Broadcast
                    ( Events::TypeIdentifier&lt;HeroMovedEvent&gt;::id()
                    , &amp;e
                    );
                <span style="color: #cc7832;">break</span>;
            }
        <span style="color: #cc7832;">case</span> <span style="color: #6897bb;">3</span>:
            {
                std::cout &lt;&lt; <span style="color: #a5c25c;">"Hero: Phew! Finally going home."
</span>                          &lt;&lt; std::endl;
                HeroMovedEvent e(HeroMovedEvent::ROAD);
                m_eventBus-&gt;Broadcast
                    ( Events::TypeIdentifier&lt;HeroMovedEvent&gt;::id()
                    , &amp;e
                    );
                <span style="color: #cc7832;">break</span>;
            }
        <span style="color: #cc7832;">case</span> <span style="color: #6897bb;">4</span>:
            {
                std::cout &lt;&lt; <span style="color: #a5c25c;">"Hero: Honey! I'm home!"
</span>                          &lt;&lt; std::endl;
                HeroMovedEvent e(HeroMovedEvent::HOME);
                m_eventBus-&gt;Broadcast
                    ( Events::TypeIdentifier&lt;HeroMovedEvent&gt;::id()
                    , &amp;e
                    );
                <span style="color: #cc7832;">break</span>;
            }
        <span style="color: #cc7832;">case</span> <span style="color: #6897bb;">5</span>:
            {
                std::cout &lt;&lt; <span style="color: #a5c25c;">"Hero: Zzzzzzz..."</span> &lt;&lt; std::endl;
                <span style="color: #cc7832;">break</span>;
            }
        <span style="color: #cc7832;">default</span>:
            <span style="color: #cc7832;">break</span>;
        }
    }
};</pre>
<p>Nothing fancy here. The <strong>Hero</strong> class raises a <strong>HeroMovedEvent</strong> whenever the Hero changes locations.</p>
<p>And here’s the full implementation of the <strong>HerosPet</strong> class presented earlier:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">class</span> HerosPet: <span style="color: #cc7832;">public</span> Events::IReceiver
{
<span style="color: #cc7832;">private</span>:
    Events::IEventBus* m_eventBus;
<span style="color: #cc7832;">public</span>:
    <span style="color: #cc7832;">explicit</span> HerosPet(Events::IEventBus* eventBus)
        : m_eventBus(eventBus)
    {
        m_eventBus-&gt;Register
            ( Events::TypeIdentifier&lt;HeroMovedEvent&gt;::id()
            , <span style="color: #cc7832;">this
</span>            );
    }

    ~HerosPet()
    {
        m_eventBus-&gt;Unregister(<span style="color: #cc7832;">this</span>);
    }

    <span style="color: #cc7832;">void</span> Receive(<span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> eventId, Events::Event* e)
    {
        <span style="color: #cc7832;">if</span> (eventId == Events::TypeIdentifier&lt;HeroMovedEvent&gt;::id())
        {
            HeroMovedEvent* e_ = <span style="color: #cc7832;">static_cast</span>&lt;HeroMovedEvent*&gt;(e);
            OnHeroMoved(e_);
        }
    }

    <span style="color: #cc7832;">void</span> OnHeroMoved(HeroMovedEvent* e)
    {
        <span style="color: #cc7832;">if</span> (e-&gt;Location() == HeroMovedEvent::HOME)
        {
            std::cout &lt;&lt; <span style="color: #a5c25c;">"Pet:  Woof! Woof!"</span> &lt;&lt; std::endl;
        }
    }
};</pre>
<p>And here’s the main method that wires all the components together:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">int</span> main(<span style="color: #cc7832;">int</span> argc, <span style="color: #cc7832;">char</span>** argv)
{
    Events::EventBus theBus;
    Hero theHero(&amp;theBus);
    HerosPet thePet(&amp;theBus);

    <span style="color: #cc7832;">for</span> (<span style="color: #cc7832;">int</span> i = <span style="color: #6897bb;">0</span>; i &lt; <span style="color: #6897bb;">12</span>; ++i)
    {
        theHero.Update();
    }
    <span style="color: #cc7832;">return</span> <span style="color: #6897bb;">0</span>;
}</pre>
<p>And here’s the output:</p>
<pre style="font-family: consolas;">Hero: Yaaaaawn!
Hero: Getting into the Hero Mobile(tm).
Hero: Punching into the Hero Cave(tm).
Hero: Phew! Finally going home.
Hero: Honey! I'm home!<strong>
Pet:  Woof! Woof!</strong>
Hero: Zzzzzzz...
Hero: Yaaaaawn!
Hero: Getting into the Hero Mobile(tm).
Hero: Punching into the Hero Cave(tm).
Hero: Phew! Finally going home.
Hero: Honey! I'm home!
<strong>Pet:  Woof! Woof!
</strong>Hero: Zzzzzzz...</pre>
<p>This works nicely. The sender and receiver only need to know about the <strong>IEventBus</strong> interface and the type of the events they wants to exchange. But there’s one problem, and that is the <strong>Receive</strong> method in <strong>HerosPet</strong>:</p>
<pre style="font-family: consolas;">    <span style="color: #cc7832;">void</span> Receive(<span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> eventId, Events::Event* e)
    {
        <span style="color: #cc7832;">if</span> (eventId == Events::TypeIdentifier&lt;HeroMovedEvent&gt;::id())
        {
            HeroMovedEvent* e_ = <span style="color: #cc7832;">static_cast</span>&lt;HeroMovedEvent*&gt;(e);
            OnHeroMoved(e_);
        }
    }</pre>
<p>This is fine for now, but as the number of events increases, the Receive method will grow into a series of ugly if-else statements, reminiscent of the <a href="http://en.wikipedia.org/wiki/Message_loop_in_Microsoft_Windows">Windows message loop</a>.</p>
<p>Ideally, this should be made easier by using something like <a href="http://docs.wxwidgets.org/stable/wx_eventhandlingoverview.html#eventhandlingoverview">event tables in wxWidgets</a> or <a href="http://www.codeproject.com/KB/wtl/wtl4mfc1.aspx#msgmap">message maps in WTL</a>, preferably without using any pre-processor hacks. And I’ll try do just that in the next post. So I’ll defer providing the full source code until then.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2010/09/a-simple-intra-process-event-bus-in-c-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Uniquely Identifying Types in C++ without using RTTI</title>
		<link>http://shaderop.com/2010/09/uniquely-identifying-types-in-c-without-using-rtti/</link>
		<comments>http://shaderop.com/2010/09/uniquely-identifying-types-in-c-without-using-rtti/#comments</comments>
		<pubDate>Sun, 05 Sep 2010 02:15:08 +0000</pubDate>
		<dc:creator>Mohammad</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Cpp]]></category>
		<category><![CDATA[preproccessor]]></category>
		<category><![CDATA[RTTI]]></category>

		<guid isPermaLink="false">http://shaderop.com/?p=335</guid>
		<description><![CDATA[In which the evils of the C pre-processor macros are exploited for the greater good to produce unique IDs for types in C++.]]></description>
			<content:encoded><![CDATA[<p>One of the books on my current reading list is <a href="http://www.amazon.com/Programming-Game-Example-Mat-Buckland/dp/1556220782">Programming Game AI by Example</a> by Mat Buckland. So far it has been an excellent read, except that some of the code snippets have a certain Win32 API feel to them that I find a bit less than optimal.</p>
<p>For example, Chapter 2 has a subsection about adding inter-object messaging capabilities to the sample presented in that chapter. The author proposes using <strong>enums</strong> to identify each message type that the system can send or receive. So somewhere in the program there should be one (possibly huge) header with the following:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">enum</span> message_type
{
    Msg_HeroMoved,
    Msg_WeaponFired,
    Msg_BulletHit,
    .
    .
};</pre>
<p>There’s also a corresponding message class for each <strong>message_type</strong> enum that holds information related to the message, like the new location of the hero in case of a <strong>Msg_HeroMoved</strong> message, or the type of projectile fired in the case of a <strong>Msg_WeaponFired</strong> message.</p>
<p>I happen to think that using unique IDs for each message is actually a good idea because if its simplicity. I just have a problem with how it’s implemented. Maintaining both a long list of message IDs <em>and</em> the corresponding message classes feels redundant and error-prone to me.</p>
<p>One could move the ID to the message classes, like so:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">class</span> HeroMovedMessage
    : <span style="color: #cc7832;">public</span> MessageBase
{
    <span style="color: gray;">/* Some details omitted ... */</span>

<span style="color: #cc7832;">public</span>:
    <span style="color: #cc7832;">static</span> <span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> TypeId();
    Point Location() <span style="color: #cc7832;">const</span>;
}</pre>
<p>But that leads to more potential issues. It’s not possible to force every message class to include a static <strong>TypeId</strong> method, and ensuring that the IDs don’t collide becomes a burden requiring the code author to either maintain a list of IDs externally or check all other message classes manually before adding a new ID.</p>
<p>I can think of a few ways that could make the process less tiresome, but the added complications make the original <strong>enum</strong> list solution look simple and elegant in comparison.</p>
<p>Something simpler is needed.</p>
<h4>Back to the drawing board</h4>
<p>What I would like to have is a method that looks like this:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">template</span> &lt;<span style="color: #cc7832;">typename</span> T&gt; <span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> TypeId()
{
    <span style="color: gray;">// Should return a unique value for each T...</span>
}</pre>
<p>If such a method can be implemented, then it would become trivial to identify any message class (or any class in the system for that matter) like so:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">class</span> A{};

<span style="color: #cc7832;">int</span> main(<span style="color: #cc7832;">int</span> argc, <span style="color: #cc7832;">char</span>** argv)
{
    <span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> id = TypeId&lt;A&gt;();

    <span style="color: #cc7832;">return</span> <span style="color: #6897bb;">0</span>;
}</pre>
<p>This would also work nicely in an ID-based messaging system, since both sender and receiver already know about the message classes they care about and can directly identify them using their type IDs. Message passing could look like the following code:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">class</span> HeroMovedMessage
    : <span style="color: #cc7832;">public</span> MessageBase
{
    <span style="color: gray;">/* some details omitted ... */
</span><span style="color: #cc7832;">public</span>:
    Point Location() <span style="color: #cc7832;">const</span>;
};

<span style="color: #cc7832;">class</span> Hero
{
<span style="color: #cc7832;">private</span>:
    <span style="color: #cc7832;">void</span> SendMessage(<span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> messageId, Message* msg)
    {
        <span style="color: gray;">// ...</span>
    }
<span style="color: #cc7832;">public</span>:
    <span style="color: #cc7832;">void</span> Update()
    {
        HeroMovedMessage msg = HeroMovedMessage(x_, y_);
        SendMessage(TypeId&lt;HeroMovedMessage&gt;(), &amp;msg);
    }
};

<span style="color: #cc7832;">class</span> HerosPet
{<span style="color: #cc7832;">
public</span>:
    <span style="color: #cc7832;">void</span> ReceiveMessage(<span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> messageId, Message* msg)
    {
        <span style="color: #cc7832;">if</span> (messageId == TypeId&lt;HeroMovedMessage&gt;())
        {
            Message* msg_ = <span style="color: #cc7832;">static_cast</span>&lt;HeroMovedMessage&gt;(msg);
            <span style="color: gray;">// ...
</span>        }
    }
};</pre>
<p>So, yes, a working <strong>TypeId </strong>function would actually work pretty well in this particular case. Now all that is left is an implementation for it. For that, I’ll need two ingredients:</p>
<ol>
<li>A way to obtain the name of a class as a string.</li>
<li>A hashing algorithm that will produce a unique ID from said string.</li>
</ol>
<p>Item <strong>number two</strong> is a solved problem. As discussed at length in the post titled “<a href="http://shaderop.com/2010/08/bad-assumptions-hashing-algorithms/">Hashing Algorithms</a>,” a CRC32 checksum or a SHA-1 or MD5 digest of the name of the class should give a unique ID for any reasonably-sized set of classes. I’ll be using CRC32 for this example because it’s already <a href="http://www.boost.org/doc/libs/1_44_0/libs/crc/index.html">implemented</a> in <a href="http://www.boost.org/">Boost</a>.</p>
<p>Item <strong>number one</strong> requires more effort.</p>
<h4>Getting the type name through RTTI</h4>
<p>The closest thing to <a href="http://en.wikipedia.org/wiki/Reflection_%28computer_science%29">reflection</a> in C++ is <a href="http://en.wikipedia.org/wiki/RTTI">Run-Time Type Information (RTTI)</a>. The bits I’ll need are the <a href="http://msdn.microsoft.com/en-us/library/fyf39xec%28VS.80%29.aspx">typeid</a> operator and the <a href="http://msdn.microsoft.com/en-us/library/70ky2y6k%28VS.80%29.aspx">type_info</a> class. Extracting a class name this was is a breeze:</p>
<pre style="font-family: consolas;"><span style="color: gray;">// Forward declaration
</span><span style="color: #cc7832;">int</span> GenerateCrcChecksum(<span style="color: #cc7832;">const</span> <span style="color: #cc7832;">char</span>* data);

<span style="color: #cc7832;">template</span> &lt;<span style="color: #cc7832;">typename</span> T&gt; <span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> TypeId()
{
    <span style="color: #cc7832;">return</span> GenerateCrcChecksum(<span style="color: #cc7832;">typeid</span>(T).name());
}</pre>
<p>The <strong>name</strong> method returns the fully qualified name of the class, including the namespace and template parameters (if any). And since it’s part of the standard it <em>should</em> be portable across all compilers.</p>
<p>But we’re not quite done yet. RTTI has a bit of a reputation for being slow. How slow it is is a subject of long and lengthy <a href="http://stackoverflow.com/questions/579887/how-expensive-is-rtti">debates</a>, but I would rather err on the side of <a href="http://cowboyprogramming.com/2007/01/04/mature-optimization-2/">mature optimization</a> and avoid RTTI altogether if possible.</p>
<p>Fortunately, there is a way.</p>
<h4>Getting some help from the preprocessor</h4>
<p>After some furious head scratching I ended up looking through the <a href="http://msdn.microsoft.com/en-us/library/b0084kay%28VS.80%29.aspx">Microsoft Visual C++ predefined macros</a> for help. The most promising one was the nonstandard <strong>__FUNCTION__</strong> macro, described thusly:</p>
<blockquote><p>Valid only within a function and returns the undecorated name of the enclosing function (as a string). __FUNCTION__ is not expanded if you use the /EP or /P compiler option.</p></blockquote>
<p>The initial test was disappointing. Here’s the code:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">class</span> A{};

<span style="color: #cc7832;">template</span> &lt;<span style="color: #cc7832;">typename</span> T&gt; <span style="color: #cc7832;">void</span> WhatsMyName()
{
    std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;
}

<span style="color: #cc7832;">int</span> main(<span style="color: #cc7832;">int</span> argc, <span style="color: #cc7832;">char</span>** argv)
{
    WhatsMyName&lt;A&gt;();
    <span style="color: #cc7832;">return</span> <span style="color: #6897bb;">0</span>;
}</pre>
<p>And here’s the output</p>
<pre style="font-family: consolas;">WhatsMyName</pre>
<p>Which not useful at all since there’s no mention of the template type used to invoke the function.</p>
<p>I took a shot in the dark and tried using a static member function instead of a free standing one:</p>
<pre style="font-family: consolas;"><span style="color: #cc7832;">class</span> A{};

<span style="color: #cc7832;">template</span> &lt;<span style="color: #cc7832;">typename</span> T&gt; <span style="color: #cc7832;">class</span> WhatsMyName
{<span style="color: #cc7832;">
public</span>:
    <span style="color: #cc7832;">static</span> <span style="color: #cc7832;">void</span> Name()
    {
        std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;
    }
};

<span style="color: #cc7832;">int</span> main(<span style="color: #cc7832;">int</span> argc, <span style="color: #cc7832;">char</span>** argv)
{
    WhatsMyName&lt;A&gt;::Name();
    <span style="color: #cc7832;">return</span> <span style="color: #6897bb;">0</span>;
}</pre>
<p>Here’s the output:</p>
<pre>WhatsMyName&lt;class A&gt;::Name</pre>
<p>Which is just perfect. I don’t even need to trim the resulting string to extract only the name of the class. I only need to have the fully qualified name somewhere inside the string since all the other text around it is fixed. I have also tested this method with templates and namespaces, and it still works.</p>
<p><a href="http://gcc.gnu.org/">GCC</a> has a similar macro called <strong>__PRETTY_FUNCTION__</strong> that works along the same lines. Since MSVC and GCC are the only two compilers that I care about, I can fall back to the <strong>RTTI</strong> method for other compilers.</p>
<p>So the new implementation for <strong>TypeId</strong> (or <strong>TypeIdentifier</strong> as it’s now called) becomes:</p>
<pre style="font-family: consolas;"><span style="color: #da4832;">#include</span> <span style="color: #a5c25c;">"GenerateTypeId.h"</span>

<span style="color: #cc7832;">template</span>&lt;<span style="color: #cc7832;">typename</span> T&gt; <span style="color: #cc7832;">class</span> TypeIdentifier
{
<span style="color: #cc7832;">public</span>:
    <span style="color: #cc7832;">static</span> <span style="color: #cc7832;">unsigned</span> <span style="color: #cc7832;">int</span> id()
    {
        <span style="color: #da4832;">#if</span> <span style="color: #da4832;">defined</span> (_MSC_VER)
            <span style="color: #cc7832;">return</span> GenerateTypeId(__FUNCTION__);
        <span style="color: #da4832;">#elif</span> <span style="color: #da4832;">defined</span>(__GNUC__)
            <span style="color: #cc7832;">return</span> GenerateTypeId(__PRETTY_FUNCTION__);
        <span style="color: #da4832;">#else
</span>            <span style="color: #cc7832;">return</span> GenerateTypeId(<span style="color: #cc7832;">typeid</span>(T).name());
        <span style="color: #da4832;">#endif
</span>    }<span style="color: #cc7832;">
private</span>:
    TypeIdentifier();
};</pre>
<p>I have moved the actual generation of the CRC checksum to a separate function in a different file and added a bit of caching and some checking for duplicate IDs while I was at it. The full source code is provided at the end of this post.</p>
<h4>Final thoughts</h4>
<p>One caveat here is that I would be very careful about using IDs generated this way for persistence or for sending objects across the wire. CRC checksums are deterministic, but the data used to generate them in this case is not the same across different compilers or possibly different versions of the same compiler. So <em>caveat emptor</em> there.</p>
<p>The other issue is that the IDs might collide given a large set of classes (or a particularly unlucky choice of names). But that problem is easy to detect early on in debug builds, which is what I opted to do in my code. And if that problem does occur then switching from CRC32 to SHA-1 or MD5 or simply renaming one of the two offending classes should take care of it.</p>
<p>Now I’ll leave you with the <a href="/wp-content/uploads/2010/09/TypeIdentifier.zip">code</a>. I tested it with Visual C++ 2010, but it should compile on G++ with C++0x extensions enabled.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaderop.com/2010/09/uniquely-identifying-types-in-c-without-using-rtti/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

