<?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>The Perilous Adventures of a Software Archaeologist &#187; blender</title>
	<atom:link href="http://chris.iluo.net/blog/tag/blender/feed/" rel="self" type="application/rss+xml" />
	<link>http://chris.iluo.net/blog</link>
	<description>As endured by Chris Pilkington</description>
	<lastBuildDate>Wed, 25 Aug 2010 22:40:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Blender Export OBJ Python Script</title>
		<link>http://chris.iluo.net/blog/2009/09/12/blender-export-obj-python-script/</link>
		<comments>http://chris.iluo.net/blog/2009/09/12/blender-export-obj-python-script/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 04:50:42 +0000</pubDate>
		<dc:creator>pilkch</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gamedev]]></category>
		<category><![CDATA[blender]]></category>
		<category><![CDATA[obj]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://chris.iluo.net/blog/?p=208</guid>
		<description><![CDATA[First of all I&#8217;d like to state that I&#8217;m definitely a Blender n00b and I have never touched Python in my life. In my game engine I basically have this sort of layout: sphere/ sphere.obj sphere.mat sphere.png The .obj points &#8230; <a href="http://chris.iluo.net/blog/2009/09/12/blender-export-obj-python-script/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>First of all I&#8217;d like to state that I&#8217;m definitely a <a href="http://www.blender.org/">Blender</a> n00b and I have never touched <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a> in my life.  </p>
<div id="attachment_212" class="wp-caption alignleft" style="width: 310px"><a href="http://chris.iluo.net/blog/wp-content/uploads/2009/09/sphere.png"><img src="http://chris.iluo.net/blog/wp-content/uploads/2009/09/sphere-300x225.png" alt="Baby steps, textured sphere loaded from an obj file" title="Sphere" width="300" height="225" class="size-medium wp-image-212" /></a><p class="wp-caption-text">Baby steps, textured sphere loaded from an obj file</p></div>
<p>In my game engine I basically have this sort of layout:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">sphere<span style="color: #000000; font-weight: bold;">/</span>
sphere.obj
sphere.mat
sphere.png</pre></div></div>

<p>The .obj points to the .mat file which points to the .png file.  So in Blender I would set the material name to &#8220;sphere&#8221; and ideally in the .obj file it would have a line like this, &#8220;usemtl sphere&#8221;, I would then get the sphere part and append &#8220;.mat&#8221; and load the material.  I&#8217;m not sure if this is standard practice for Blender export scripts, but when exporting to the <a href="http://en.wikipedia.org/wiki/Obj">obj</a> file <a href="http://www.royriggs.com/obj.html">format</a>, Blender adds the texture name to the material like so, &#8220;usemtl sphere_sphere.png&#8221;, not cool.  Anyway I thought, hey, the export script is written in Python, I wonder if I can fix this?</p>
<p>1) Locate the script.  I read somewhere on the Blender site that scripts are in &#8220;/usr/share/blender/scripts/&#8221;.  &#8220;export_obj.py&#8221; is there, along side &#8220;export_obj.pyc&#8221; and &#8220;export_obj.pyo&#8221;.<br />
2) Edit the script.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gedit <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>blender<span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>export_obj.py</pre></div></div>

<p>3) <a href="https://svn.blender.org/svnroot/bf-blender/trunk/blender/release/scripts/export_obj.py">The script</a> is pretty well documented.  I knew that I should search for &#8220;usemtl&#8221; as that is the only constant part of the string (Apart from &#8220;_&#8221; which is much harder to search for).  There are two in the file and we want the second one:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">mat_data= MTL_DICT.<span style="color: black;">get</span><span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> mat_data:
		<span style="color: #808080; font-style: italic;"># First add to global dict so we can export to mtl</span>
		<span style="color: #808080; font-style: italic;"># Then write mtl</span>
&nbsp;
		<span style="color: #808080; font-style: italic;"># Make a new names from the mat and image name,</span>
		<span style="color: #808080; font-style: italic;"># converting any spaces to underscores with fixName.</span>
&nbsp;
		<span style="color: #808080; font-style: italic;"># If none image dont bother adding it to the name</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> key<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> == <span style="color: #008000;">None</span>:
			mat_data = MTL_DICT<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span> = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s'</span><span style="color: #66cc66;">%</span>fixName<span style="color: black;">&#40;</span>key<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>, materialItems<span style="color: black;">&#91;</span>f_mat<span style="color: black;">&#93;</span>, f_image
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			mat_data = MTL_DICT<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span> = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s_%s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>fixName<span style="color: black;">&#40;</span>key<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, fixName<span style="color: black;">&#40;</span>key<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>, materialItems<span style="color: black;">&#91;</span>f_mat<span style="color: black;">&#93;</span>, f_image
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">if</span> EXPORT_GROUP_BY_MAT:
		<span style="color: #008000;">file</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'g %s_%s_%s<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>fixName<span style="color: black;">&#40;</span>ob.<span style="color: black;">name</span><span style="color: black;">&#41;</span>, fixName<span style="color: black;">&#40;</span>ob.<span style="color: black;">getData</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>, mat_data<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># can be mat_image or (null)</span>
&nbsp;
	<span style="color: #008000;">file</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'usemtl %s<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #66cc66;">%</span> mat_data<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># can be mat_image or (null)</span></pre></div></div>

<p>mat_data is a dictionary that is filled out and then written the file with &#8220;usemtl&#8221;.  I replaced the offending line:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">mat_data = MTL_DICT<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span> = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s_%s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>fixName<span style="color: black;">&#40;</span>key<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, fixName<span style="color: black;">&#40;</span>key<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>, materialItems<span style="color: black;">&#91;</span>f_mat<span style="color: black;">&#93;</span>, f_image</pre></div></div>

<p>with this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">mat_data = MTL_DICT<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span> = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s'</span><span style="color: #66cc66;">%</span>fixName<span style="color: black;">&#40;</span>key<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>, materialItems<span style="color: black;">&#91;</span>f_mat<span style="color: black;">&#93;</span>, f_image</pre></div></div>

<p>And then I realised that it was exactly the same as the line from the &#8220;if key[1] == None:&#8221; so you could probably remove the branch etc. if you want, I didn&#8217;t bother.</p>
<p>If you are exporting groups by material you may also need to edit the &#8220;if EXPORT_GROUP_BY_MAT:&#8221; branch also, again I didn&#8217;t bother.  Anyway, save and you&#8217;re good to go.</p>
<p>4) I was worried that I would have to compile into bytecode or something for Blender to be able to use this, nope, either restart Blender or <a href="http://wiki.blender.org/index.php/Extensions:Py/Scripts/Manual#Refreshing_the_List_of_Available_Scripts">hit &#8220;Update Menu&#8221;</a>.  Actually as it is an export script, I&#8217;m not sure you even need to do that, it probably gets reloaded when you <a href="http://wiki.blender.org/index.php/Extensions:Py/Scripts/Manual/Export/wavefront_obj">hit &#8220;File > Export > Wavefront (.obj)&#8221;</a>?</p>
<p>This is the entirety of my Python knowledge, oh, I also know that whitespace is important or something, woo.</p>
<p>PS. I&#8217;m loving <a href="http://git-scm.com/">git</a>, I can&#8217;t believe I didn&#8217;t switch earlier!</p>
]]></content:encoded>
			<wfw:commentRss>http://chris.iluo.net/blog/2009/09/12/blender-export-obj-python-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
