<?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>Smart-Page.net &#187; Python</title>
	<atom:link href="http://www.smart-page.net/blog/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.smart-page.net/blog</link>
	<description>C++ / C# / Py / Actionscript ... &#039;My God. It&#039;s full of stars!&#039;</description>
	<lastBuildDate>Thu, 23 Jun 2011 00:14:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Debugging Cinema 4D Python Plugins with PyDev</title>
		<link>http://www.smart-page.net/blog/2011/05/29/debugging-cinema-4d-python-plugins-with-pydev/</link>
		<comments>http://www.smart-page.net/blog/2011/05/29/debugging-cinema-4d-python-plugins-with-pydev/#comments</comments>
		<pubDate>Sun, 29 May 2011 10:53:32 +0000</pubDate>
		<dc:creator>flashgordon</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[cinema 4D]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[IDE]]></category>
		<category><![CDATA[Pydev]]></category>

		<guid isPermaLink="false">http://www.smart-page.net/blog/?p=1261</guid>
		<description><![CDATA[<a href="http://www.smart-page.net/blog/2011/05/29/debugging-cinema-4d-python-plugins-with-pydev/" title="Debugging Cinema 4D Python Plugins with PyDev"><img src="http://www.smart-page.net/blog/wp-content/uploads/yapb_cache/debug.ah1weo0164ekcgo0g8c48ck0s.h9fw4mcunmtegc04wgoo4wck.th.jpeg" width="200" height="150" alt="Debugging Cinema 4D Python Plugins with PyDev" style="float:left;padding:0 10px 10px 0;" ></a>When hunting down glitches, the debugger is the coder&#8217;s weapon of choice&#8230; In the last post about coding advanced Python plugins, this vital part was skipped as Cinema 4D&#8217;s Python interpreter doesn&#8217;t support it. But fear not &#8211; you don&#8217;t have to write countless &#8216;prints&#8217; or fiddle with python&#8217;s inspect module just to fix a [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.smart-page.net/blog/2011/05/29/debugging-cinema-4d-python-plugins-with-pydev/" title="Debugging Cinema 4D Python Plugins with PyDev"><img src="http://www.smart-page.net/blog/wp-content/uploads/yapb_cache/debug.ah1weo0164ekcgo0g8c48ck0s.h9fw4mcunmtegc04wgoo4wck.th.jpeg" width="200" height="150" alt="Debugging Cinema 4D Python Plugins with PyDev" style="float:left;padding:0 10px 10px 0;" ></a><p>When hunting down glitches, the debugger is the coder&#8217;s weapon of choice&#8230;</p>
<p>In the last post about <a href="http://www.smart-page.net/blog/2011/05/09/advanced-python-plugin-coding-for-cinema-4d/" target="_blank">coding advanced Python plugins</a>, this vital part was skipped as Cinema 4D&#8217;s Python interpreter doesn&#8217;t support it.</p>
<p>But fear not &#8211; you don&#8217;t have to write countless &#8216;prints&#8217; or fiddle with python&#8217;s inspect module just to fix a simple fence post error.</p>
<p>Stepping through breakpoints and browsing your plugins scope&#8230; &#8211; all this is possible with the PyDev remote debugger.</p>
<p><span id="more-1261"></span></p>
<h1>How does it work?</h1>
<p><img class="alignnone size-full wp-image-1262" title="debug2" src="http://www.smart-page.net/blog/wp-content/uploads/2011/05/debug2.jpg" alt="" width="683" height="88" /></p>
<p>The PyDev remote debugger consists of a server running in Eclipse, listening to a given port &#8211; and a client (your plugin), that inspects the scope at runtime and sends it to the server.<br />
This concept has the advantage of working with virtually any host-application one can think of &#8211; including Cinema4D.</p>
<p>The only disadvantages are that you have to add the client module to your source and to insert a small line of code for each breakpoint.</p>
<h1>Setup</h1>
<p><img class="alignnone size-full wp-image-1265" title="debug4" src="http://www.smart-page.net/blog/wp-content/uploads/2011/05/debug4.jpg" alt="" width="683" height="79" /></p>
<p>As the remote debugger comes with PyDev, it&#8217;s already installed within your IDE &#8211; we just have to find out where exactly as the installation path varies with the PyDev version:</p>
<ul>
<li>Browse to your Eclipse folder and do a search for a file &#8220;pydevd.py&#8221;</li>
</ul>
<p>You should end up with a Path somewhat similar to this:<br />
<em>&#8216;C:\Program Files\eclipse\plugins\org.python.pydev.debug_2.0.0.2011040403\pysrc&#8217;</em></p>
<p>If you find more than one &#8216;pydevd.py&#8217;, make sure you selected the PyDev folder with the latest version number within &#8216;plugins&#8217;.</p>
<ul>
<li> Now open the source file you wish to debug and insert:
<pre>import sys
sys.path.append('C:\Program Files\eclipse\plugins\org.python.pydev.debug_2.0.0.2011040403\pysrc')
import pydevd</pre>
</li>
</ul>
<p>And that&#8217;s it!</p>
<h1>Debugging</h1>
<p><img class="alignnone size-full wp-image-1263" title="debug3" src="http://www.smart-page.net/blog/wp-content/uploads/2011/05/debug3.jpg" alt="" width="683" height="79" /></p>
<p>Let&#8217;s do a quick test run and place a breakpoint.</p>
<ul>
<li>Add the line pydevd.settrace() somewhere within your source</li>
<li>Switch Eclipse to &#8220;Debug View&#8221; (upper right) or click &#8216;Window&#8217; -&gt; &#8216;Open Perspective&#8217; -&gt; &#8216;Other&#8217; -&gt; &#8216;Debug&#8217;</li>
<li>Start the PyDev remote debug server by clicking the Icon on the top left (the small green bug with the blue &#8216;P&#8217;)</li>
<li>Fire up Cinema 4D and switch back to Eclipse</li>
</ul>
<p>You can now continue or step to other breakpoints by using the small icon bar on the top left or the F5-8 shortcuts.</p>
<p>The variable tab on the right allows you to inspect your plugins scope in detail.</p>
<p>In case this doesn&#8217;t work, check your firewall settings first before repeating these steps or taking a look at the <a href="http://pydev.org/manual_adv_remote_debugger.html" target="_blank">PyDev documentation</a>.<br />
<br/><br />
<strong><span style="color: #ff6600;">Tip: </span></strong><br />
Route the standard- and error output-channels to the PyDev console with&#8230;</p>
<pre>pydevd.settrace(True, True)
</pre>
<p><br/><br />
With the debugging feature added, our Python plugin coding environment should now be complete&#8230;</p>
<p>Time to spring into action! <img src='http://www.smart-page.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-page.net/blog/2011/05/29/debugging-cinema-4d-python-plugins-with-pydev/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Advanced Python plugin coding for Cinema 4D</title>
		<link>http://www.smart-page.net/blog/2011/05/09/advanced-python-plugin-coding-for-cinema-4d/</link>
		<comments>http://www.smart-page.net/blog/2011/05/09/advanced-python-plugin-coding-for-cinema-4d/#comments</comments>
		<pubDate>Sun, 08 May 2011 22:31:06 +0000</pubDate>
		<dc:creator>flashgordon</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[cinema 4D]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[IDE]]></category>
		<category><![CDATA[Plugin development]]></category>
		<category><![CDATA[Pydev]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.smart-page.net/blog/?p=1215</guid>
		<description><![CDATA[<a href="http://www.smart-page.net/blog/2011/05/09/advanced-python-plugin-coding-for-cinema-4d/" title="Advanced Python plugin coding for Cinema 4D"><img src="http://www.smart-page.net/blog/wp-content/uploads/yapb_cache/title2.32lgm6px71s0w08c8gkk8cwsw.h9fw4mcunmtegc04wgoo4wck.th.jpeg" width="200" height="150" alt="Advanced Python plugin coding for Cinema 4D" style="float:left;padding:0 10px 10px 0;" ></a>It&#8217;s been a long time&#8230; how have you been? This is going to be another Python/Cinema 4D related post. While python is great for quickly scripting workflow optimizing tools, prototype stuff or write generators and tags, is it suited for big, complex plugin development? It is! You get &#8216;nearly&#8217; full access to the C++ API [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.smart-page.net/blog/2011/05/09/advanced-python-plugin-coding-for-cinema-4d/" title="Advanced Python plugin coding for Cinema 4D"><img src="http://www.smart-page.net/blog/wp-content/uploads/yapb_cache/title2.32lgm6px71s0w08c8gkk8cwsw.h9fw4mcunmtegc04wgoo4wck.th.jpeg" width="200" height="150" alt="Advanced Python plugin coding for Cinema 4D" style="float:left;padding:0 10px 10px 0;" ></a><p>It&#8217;s been a long time&#8230; how have you been?</p>
<p>This is going to be another Python/Cinema 4D related post.</p>
<p>While python is great for quickly scripting workflow optimizing tools, prototype stuff or write generators and tags, is it suited for big, complex plugin development?</p>
<p><span id="more-1215"></span>It is!</p>
<p>You get &#8216;nearly&#8217; full access to the C++ API of Cinema 4D, don&#8217;t have to compile or worry much about platform dependencies, can easily find a ton of neat recipes and libraries out there&#8230;</p>
<p>So it’s not only possible to code full-fledged python plugins but it even has its advantages over classical C++ development.</p>
<p>Imagine you wrote a nifty script, decided to make it a simple plugin, and now want to take it a step further &#8211; add more features, a complex GUI with some dialogs etc&#8230;</p>
<p>If you are still working with the script editor by now, things will most likely start to get out of hand as you introduce more modules and classes, deal with more code and more functionality.</p>
<p>This is the point where you might want to look out for a little help.</p>
<p>We are going to set up an IDE (Integrated Development Environment), talk a bit about modules/packages and finally I will try to give some general hints and tips.<br />
<br/></p>
<h1>Coding Environment/IDE:</h1>
<h2>Prepare Cinema 4D:</h2>
<p>You may skip this part, but I highly recommend it:</p>
<ul>
<li>Make a copy of your Cinema 4D installation &#8211; name it <em>&#8220;Cinema 4D R12 Dev&#8221;</em> for example.</li>
<li>Get rid of all Cinema 4D plugins you don&#8217;t need for your development</li>
<li>Place an empty text file named <em>&#8220;c4d_debug.txt&#8221;</em> into the root folder of your Cinema 4D copy.</li>
</ul>
<p>This will slow Cinema 4D down a bit, but enables you to monitor memory leaks.</p>
<p>We will add some useful things on Cinema 4D-side later &#8211; for now let&#8217;s continue with the Eclipse setup&#8230;<br />
<br/></p>
<h2>Eclipse:</h2>
<p><img class="alignnone size-full wp-image-1217" title="eclipse" src="http://www.smart-page.net/blog/wp-content/uploads/2011/05/eclipse.jpg" alt="" width="683" height="101" /></p>
<p>If you never used one, think of an IDE as your workshop, it’s a collection of tools as well as the place where you do and organize your coding work.</p>
<p>Eclipse is one of the most popular &#8211; its Java based, open source, free to use and most importantly has excellent support for python.</p>
<p>So let&#8217;s start by downloading and installing Eclipse Classic from <a href="http://www.eclipse.org/downloads/" target="_blank">http://www.eclipse.org/downloads/</a><br />
If you don&#8217;t have the <a href="http://www.java.com/de/download/"  target="_blank">Java Runtime Environment</a> installed jet, you need to do this first.<br />
<br/></p>
<h2>PyDev:</h2>
<p>As said before, Eclipse has excellent python support &#8211; but not out of the box, so we are going to install <a href="http://www.pydev.org" target="_blank">PyDev</a>.</p>
<p>PyDev will help you coding by adding syntax highlighting, code completion and much more.<br />
Should you encounter any trouble with the following steps, please refer to the PyDev <a href="http://pydev.org/manual_101_install.html" target="_blank">installation manual</a>.</p>
<ul>
<li>After installation, startup Eclipse, click <em>&#8220;Help&#8221;, </em>and chose <em>&#8220;Install New Software&#8230;&#8221;.</em></li>
<li>Next click the <em>&#8220;Add&#8230;&#8221;</em> button, type in <em>&#8220;PyDev&#8221;</em> as name and <em>&#8220;http://pydev.org/updates&#8221; </em>as location &#8211; then confirm.</li>
<li>Now enter <em>&#8220;PyDev&#8221;</em> in the <em>&#8220;Work with:&#8221;</em> field, you should see an entry<em> &#8220;PyDev for Eclipse&#8221; </em>- check it and click <em>&#8220;Next&#8221;.</em></li>
</ul>
<p>After confirming again and accepting the terms, it will download and install.</p>
<ul>
<li>Click <em>&#8220;Windows&#8221;</em> and open the <em>&#8220;Preferences&#8221;</em> dialog.</li>
<li>Select PyDev and the child <em>&#8220;Interpreter &#8211; Python&#8221;</em></li>
<li>Click <em>&#8220;New&#8230;&#8221;</em>, input <em>&#8220;Python Cinema 4D&#8221;</em> as name and browse to your Cinema 4D installation.</li>
<li>In <em>&#8220;/resource/modules/python/res/Python.win64.framework/&#8221;</em> select the <em>python.exe</em>.</li>
<li>Confirm your selection and the paths PyDev wants to add to your syspath.</li>
</ul>
<p>So much for the PyDev installation itself, now we have to associate the Cinema 4D Python plugin extension *.pyp with PyDev to enable syntax highlighting and code completion for those files as well:</p>
<ul>
<li>Open <em>&#8216;Window&#8217;</em> within the file-menu and click <em>&#8216;Preferences&#8217;</em></li>
<li>Select <em>&#8216;General&#8217; -> &#8216;Content Types&#8217;</em> from the list on the left.</li>
<li>In the <em>&#8216;Content types:&#8217;</em> view on the right, unfold <em>&#8216;Text&#8217;</em> and click <em>&#8216;Python File&#8217;</em></li>
<li>Hit the <em>&#8216;Add&#8230;&#8217;</em> button of the <em>&#8216;File associations:&#8217;</em> list on the lower right, type <em>&#8220;*.pyp&#8221;</em> into the popup and confirm.</li>
</ul>
<p>Its helpful to add a file containing all symbols of the Cinema 4D Python API’s “c4d” package to the ‘site-packages folder to spice up the auto-completion – <a href="http://www.smart-page.net/py4d/c4d.zip">grab it here</a>.</p>
<p>Place the file within <em>&#8220;/resource/modules/python/res/Python.win64.framework/Lib/site-packages</em><em>&#8220;</em><br />
<br/></p>
<h4>Note:</h4>
<p>You will have to update this file if you want to keep up to date with future versions of Cinema4D, it can be easily printed to the console by executing:</p>
<pre>for itm in dir(c4d):
    s = getattr(c4d, itm)
    print "%s = %s" % (itm, s)</pre>
<p><br/></p>
<h2>Project setup</h2>
<p><img class="alignnone size-full wp-image-1218" title="construction" src="http://www.smart-page.net/blog/wp-content/uploads/2011/05/construction.jpg" alt="" width="683" height="134" /></p>
<p>We take your IDE for a spin and create the first project:</p>
<ul>
<li>Click <em>&#8220;File&#8221; &#8211; &#8220;New&#8221;</em> and chose <em>&#8220;PyDev Project&#8221;</em></li>
<li>Input a Name for your Project and uncheck the <em>&#8220;Use default&#8221; </em>box below.</li>
<li>Browse to your Cinema 4D installations plugin folder, create a new directory with your plugins name and select it.</li>
<li>Select<em> &#8220;2.6&#8243; </em>from the <em>&#8220;Grammar Version&#8221;- </em>dropdown and leave the Interpreter set to default.</li>
<li>Last but not least uncheck the <em>&#8220;Create default &#8216;src&#8217; folder&#8230;&#8221; checkbox and hit &#8220;Finish&#8221;.</em></li>
</ul>
<p>To setup the needed folder structure, header- resource- and string files for your plugin, please read the Cinema 4D Python documentation <em>“Plugin structure”</em> &#8211;  especially <em>“Directory structure”</em> &#8211; and take a look at the Python plugin examples within the documentation &#8211; or the experimental plugin from <a href="http://www.smart-page.net/blog/2010/09/10/a-raytracer-in-python-for-cinema-4d" target="_blank">this post</a>:</p>
<p>Just remember this part is vital, so do not rush through it &#8211; at least read the section mentioned above and take a good look at the SDK examples.</p>
<p>Only if you have built C++ plugins before and are already familiar with the structure, you may&#8230;<br />
<br/></p>
<h4>Hint:</h4>
<p>You can also add your existing project files by just placing them within the plugin-directory we just created, or dragging them into the project-folder within Eclipse.</p>
<p>When organizing the file structure within your project, you are working with your file system &#8211; changes you do here are reflected on your harddisk.<br />
This also works the other way round, move something on the file system and Eclipse will update those changes to your project &#8211; while not always instantly.</p>
<p>You can press F5 within Eclipse to update a folder and its subfolders or the contents of a file.</p>
<p>Don&#8217;t worry, if the project-view is out of sync with the file system &#8211; Eclipse will tell you.<br />
<br/></p>
<h2>A coders Cinema 4D:</h2>
<p><img class="alignnone size-full wp-image-1219" title="c4d" src="http://www.smart-page.net/blog/wp-content/uploads/2011/05/c4d.jpg" alt="" width="683" height="112" /></p>
<p>We are all set with the IDE part &#8211; let&#8217;s tune the Cinema 4D installation a bit more to improve the workflow and finalize your coding environment.</p>
<p>If you didn&#8217;t add/import your existing code to the project we just created, write a simple hello world and fire up Cinema.</p>
<p>It&#8217;s a good idea to setup a developer friendly layout, I like adding the console window to the tabs on the right, next to the object manager &#8211; or even better to a side monitor.<br />
Should you be writing a CommandData Plugin, consider opening the Command Manager and dragging its icon into the layout, so you can quickly re-open it.</p>
<p>You will be launching and closing Cinema 4D a lot during development &#8211; it&#8217;s helpful to have a strategy to quickly start and close it.<br />
Place a link to Cinema 4D on your taskbar/dock, close it using <em>ALT+F4/Command+Q</em> or create a little python script containing <em>&#8220;exit()&#8221; </em>and add it to the layout/create a shortcut.</p>
<p>Finally, open your Plugin as well as everything you need to test its functionality and save the layout as startup layout.</p>
<p>If you have a test document that you need to check your plugin, press <em>CTRL+E</em>, click the &#8220;Open Preferences Folder&#8230;&#8221; button and place it as &#8220;default.c4d&#8221; within that folder so it will load on startup.</p>
<p>You long got the idea &#8211; try to prepare everything so you don&#8217;t have to spend time doing recurring tasks on every unit-test you are going to make during development.<br />
<br/></p>
<h2>Some additional tools:</h2>
<p>It&#8217;s often helpful to keep an eye on memory consumption and or active threads so I always have the task manager/activity monitor in reach when coding.</p>
<p>And while not really necessary, it&#8217;s convenient to have a good, fast text editor with Python syntax highlighting so you are able to read a file or snippet without having to launch the IDE first.<br />
I can recommend <a href="http://notepad-plus-plus.org/" target="_blank">Notepad++</a> and <a href="http://macromates.com/" target="_blank">TextMate </a>or <a href="http://www.gnu.org/software/emacs/" target="_blank">Emacs </a>for this job.<br />
<br/></p>
<h1>Working with eclipse:</h1>
<p><img class="alignnone size-full wp-image-1220" title="prototype" src="http://www.smart-page.net/blog/wp-content/uploads/2011/05/prototype.jpg" alt="" width="683" height="134" /></p>
<p>Eclipse in combination with PyDev is a mighty! tool &#8211; Aside from the obvious killer features &#8211; syntax highlighting and code completion, there is a ton of others.</p>
<p>Often you will discover some that will make you feel bad for not having used them right from the start, so I&#8217;ll try to point out the most useful, while I encourage you to read up on Eclipse and PyDev.<br />
<br/></p>
<h3>The Find/Replace Dialog</h3>
<p>Press CTRL+F for all your search and replace needs – just listing it to make you aware of the direction-, selection- and regex support.<br />
If you haven&#8217;t used regular expressions jet, you should directly head over to <a href="http://en.wikipedia.org/wiki/Regular_expression" target="_blank">Wikipedia </a>- you won&#8217;t regret it.</p>
<p>Even more powerful is the&#8230;<br />
<br/></p>
<h3>Search Dialog</h3>
<p>CTRL+H opens the global or project wide search – here you can also replace over all files.</p>
<p>Just keep in mind that the &#8220;PyDev Search&#8221; tab does only look within *.py files while the &#8220;File Search&#8221; tab by default searches over a *.* pattern.<br />
<br/></p>
<h3>Compare feature and history</h3>
<p>Just select two files, right click and chose <em>&#8220;Compare with&#8221; -&gt; &#8220;Each other&#8221;.</em></p>
<p>You can also compare a file with an earlier state of itself by choosing <em>&#8220;Local history&#8221;.</em></p>
<p>This is another huge plus of using an Eclipse &#8211; don&#8217;t worry anymore about changing huge sections of code or commenting it &#8211; just go ahead and delete it &#8211; you can always return to an earlier state using the &#8220;History&#8221;- tab.</p>
<p>But use this with care, as it’s possible to lose the history by moving or re-creating a project and it’s not infinite.<br />
<br/></p>
<h3>Tabbed views</h3>
<p>You can drag a tab to one side of the code view until a small arrow appears &#8211; the tab will then snap to that side, splitting your view so you can work with multiple source files at the same time.</p>
<p>This is very useful when working with the header-, resource- or string files of your project as you can code on the left and copy &#8216;n paste ID&#8217;s from the right for example.<br />
<br/></p>
<h1>Coding python for Cinema 4D:</h1>
<p><img class="alignnone size-full wp-image-1221" title="python" src="http://www.smart-page.net/blog/wp-content/uploads/2011/05/python.jpg" alt="" width="683" height="126" /></p>
<p>We have setup the coding environment &#8211; how about doing some programming now?</p>
<p>This will be a loose collection of tips that are all more or less Cinema 4D related, but mostly  good practice in general.<br />
<br/></p>
<h3>Modules and namespace:</h3>
<p>First &#8211; this is <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" target="_blank">OOP </a>(Object Oriented Programming) and your project&#8217;s structure should reflect that.</p>
<p>In general, classes and modules are a good thing, it&#8217;s a far more common fault to have less abstraction than too much.</p>
<p>Packages/Modules are also the key to keep your code organized, manageable &#8211; AND reusable.</p>
<p>So if you are going to write something that you might want to use again later, consider spending the extra time to make it a bit more user/coder-friendly and build a class or module.</p>
<p>When working with python and modules you have to keep a close eye on naming and namespace, especially in context with the Cinema 4D SDK.</p>
<p>Be creative and use unique module names &#8211; this will keep things easier when you are using other modules &#8211; and you will &#8211; at least the ones from the Cinema 4D API.<br />
Also be careful when importing from modules &#8211; it&#8217;s rarely a good idea to just import everything (<em>from foo import *</em>) &#8211; instead carefully select the members you really need and give everyone its own import line rather than stacking them with commas in one.</p>
<p>It&#8217;s often better to just import the module into your namespace and reference the members via <em>module.foo</em> – you don’t pollute your namespace and it’s more readable.</p>
<p>In any case you should avoid the API&#8217;s module AND member names like the plaque.</p>
<p>For example if you extend the GeDialog Class and have a method <em>&#8220;def close(self):&#8221;</em> &#8211; but make the slight mistake to call it as <em>&#8220;Close()&#8221;</em>, you might end up with a similar behavior but wonder for minutes while your dialog just closes without saving it&#8217;s settings first (or whatever “<em>close()“</em> should have done).</p>
<p>I know for some this is basic stuff &#8211; many have their own patterns for this and that’s ok!</p>
<p>Whatever you do &#8211; be consistent and aware of your namespace.<br />
<br/></p>
<h3>Symbols:</h3>
<p><img class="alignnone size-full wp-image-1223" title="symbols" src="http://www.smart-page.net/blog/wp-content/uploads/2011/05/symbols1.jpg" alt="" width="683" height="112" /></p>
<p>As you need to keep track of the enums within the c4d_symbols.h, it is best to define every integer by yourself and not auto-enumerate them.</p>
<p>Then mirror those symbols to a *.py file called <em>&#8220;ids.py&#8221;</em> and be careful to update both should you change them or introduce new ones.</p>
<p>Instead of hardcoding ID&#8217;s, you should always use the symbol within the c4d-package.<br />
Should you ever need to identify a symbol by it&#8217;s integer value (for example if you want to check for a certain flag that is returned) &#8211; this little script will help you out:</p>
<pre>import c4d;
from c4d import gui; 

def main():
    wanted = gui.RenameDialog("ID to search for...")
    outp = ""

    for itm in dir(c4d):
        s = getattr(c4d, itm)
        if s == int(wanted):
            outp += "%s = %s\n" % (itm, s)

    gui.MessageDialog("Sorry - none found..." if outp == "" else outp)

if __name__=='__main__':
    main()</pre>
<p><br/></p>
<h3>API calls / performance:</h3>
<p>Don&#8217;t get me wrong &#8211; the Python API is blazing fast &#8211; so fast you often can&#8217;t tell the difference to C++ &#8211; but still, API calls take time.<br />
When working with thousands of objects, this quickly adds up no matter how fast each individual call is.</p>
<p>Try to limit those calls in sections of your code, where you really need performance &#8211; look for ways to keep states within your own plugin logic rather than querying them form Cinema 4D whenever possible.</p>
<p>If you are looking for bottlenecks, be sure to check out <a href="http://docs.python.org/library/profile.html" target="_blank">cProfile </a>– it’s great.<br />
<br/></p>
<h3>Prototyping:</h3>
<p>Using an IDE doesn&#8217;t forbid you to use Cinema 4D&#8217;s script editor and console to quickly whip up and test something &#8211; you can even write and directly execute a simple test script within eclipse if you are working on parts of your plugin that don&#8217;t need the Cinema SDK (some kind of parser for example) and take advantage of the PyDev debugger.<br />
<br/></p>
<h3>Debugging:</h3>
<p>The lack of a real debugger can be compensated by using the <a href='http://www.smart-page.net/blog/2011/05/29/debugging-cinema-4d-python-plugins-with-pydev/' target='_blank'>PyDev remote debugger</a>, pythons inspect module and the logging package.</p>
<p>Remember that there is a limit to the amount of characters the Cinema 4D console can output for a single print command.</p>
<p>Also take a look at <a href="http://docs.python.org/library/sys.html" target="_blank"><em>sys.exc_info()</em></a><br />
<br/></p>
<h3>Garbage Collection:</h3>
<p>When introducing <a href="http://en.wikipedia.org/wiki/Circular_reference" target="_blank">circular references</a> you should use <a href="http://docs.python.org/library/weakref.html" target="_blank">weak references</a> to give the garbage collector a hand.<br />
<br/></p>
<h3>Compilation:</h3>
<p><a href="http://www.smart-page.net/blog/wp-content/uploads/2011/05/stuff.jpg" rel="lightbox[1215]"><img class="alignnone size-full wp-image-1226" title="stuff" src="http://www.smart-page.net/blog/wp-content/uploads/2011/05/stuff.jpg" alt="" width="683" height="112" /></a></p>
<p>How about shipping your plugin as precompiled byte code?</p>
<p>This will make Cinema 4D start up faster &#8211; as the compilation of your plugin can be skipped, obfuse your source and still be platform interoperable.</p>
<p>Just copy your plugin, open the console/terminal and execute <em>&#8220;python.exe -<a href="http://docs.python.org/library/compileall.html" target="_blank">mcompileall </a>C:\PathToMyPlugin&#8221;</em> and delete all *.py files afterwards.</p>
<p>Note that this might not work in some cases, so be sure to test your plugins functionality again.<br />
<br/></p>
<h3>Platform dependency:</h3>
<p>While Python is platform-independent, there are still some things to keep in mind.</p>
<p>Don&#8217;t use backslashes in paths but slashes &#8211; they are understood by windows AND mac OS.</p>
<p>You can also use <a href="http://docs.python.org/library/platform.html" target="_blank"><em>platform.system()</em></a> to determine on what OS your plugin runs on and import modules after a conditional check &#8211; for example:</p>
<pre>if platform.system().lower() == "windows":</pre>
<pre>    import winsound
    winsound.PlaySound(os.path.join(os.path.dirname(__file__), "../res/snd", "DingSound.wav"),
winsound.SND_FILENAME)</pre>
<p><br/></p>
<h3>GUI &#8211; Threading:</h3>
<p>It&#8217;s the old story, if the GUI thread has to wait for your plugin, you lock up Cinema 4D &#8211; unacceptable for the user.</p>
<p>Doing time intensive stuff within your own threads is the simple solution to this, but keep in mind that you can&#8217;t have them call the GUI thread &#8211; instead use messages and the GeDialog&#8217;s <em>SetTimer().</em><br />
<br/></p>
<h3>Resource reference:</h3>
<p>Classes that use the &#8220;plugins&#8221; module need a reference to the plugins resource pointer.</p>
<p>This can be handled by importing those classes within the plugins *.pyp file and setting the class variable <em>&#8220;__res__&#8221;</em> or using a <a href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank">singleton </a>(see below).<br />
<br/></p>
<h3>Singletons and invading Borgs:</h3>
<p><img class="alignnone size-full wp-image-1224" title="borg" src="http://www.smart-page.net/blog/wp-content/uploads/2011/05/borg.jpg" alt="" width="683" height="107" /></p>
<p>This is something for the advanced developer &#8211; and kind of a hot topic too.</p>
<p>The use of globally accessible objects or ‘globals’ is often considered bad practice but also unavoidable in some cases (logger) and python even somewhat encourages it.</p>
<p>So if you know what you are doing and think about using a singleton, you might want to take a look at the &#8220;borg&#8221;-(anti)pattern &#8211; a monostate proxy with a twist.</p>
<pre>class Borg:</pre>
<pre>__we_are_one = {}</pre>
<pre>    def __init__(self):       
    self.__dict__ = self.__we_are_one</pre>
<p><br/></p>
<h1>Still alive</h1>
<p>This was a lot of compressed stuff to digest, I know, but please do me the favor and don’t let any of this scare you off, it’s easier and more fun than you would expect.</p>
<p>I am sorry some topics where not covered in the depth they deserve -  it wasn’t easy to keep this a readable post rather than a book…</p>
<p>I hope this still was helpful to someone, if you have any questions, thoughts or would like me to write a more detailed post on a certain topic &#8211; let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-page.net/blog/2011/05/09/advanced-python-plugin-coding-for-cinema-4d/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>A raytracer in Python for Cinema 4D</title>
		<link>http://www.smart-page.net/blog/2010/09/10/a-raytracer-in-python-for-cinema-4d/</link>
		<comments>http://www.smart-page.net/blog/2010/09/10/a-raytracer-in-python-for-cinema-4d/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 00:58:27 +0000</pubDate>
		<dc:creator>flashgordon</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[CommandData]]></category>
		<category><![CDATA[Dialog]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Raytracer]]></category>
		<category><![CDATA[UserArea]]></category>

		<guid isPermaLink="false">http://www.smart-page.net/blog/?p=1168</guid>
		<description><![CDATA[<a href="http://www.smart-page.net/blog/2010/09/10/a-raytracer-in-python-for-cinema-4d/" title="A raytracer in Python for Cinema 4D"><img src="http://www.smart-page.net/blog/wp-content/uploads/yapb_cache/smartpray4d1.69shqbzmvsn4mcgs4w4cw8wc4.h9fw4mcunmtegc04wgoo4wck.th.jpeg" width="200" height="150" alt="A raytracer in Python for Cinema 4D" style="float:left;padding:0 10px 10px 0;" ></a>Maxon has just released the R12 version of Cinema 4D, which includes Python &#8211; so let&#8217;s dig into Python plugin development! We are going to build a ray tracer&#8230; with Python! Watch a rendered video. Update: All Python scripts in this blog have been updated to work with the R12. Ok, I&#8217;m sorry &#8211; I [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.smart-page.net/blog/2010/09/10/a-raytracer-in-python-for-cinema-4d/" title="A raytracer in Python for Cinema 4D"><img src="http://www.smart-page.net/blog/wp-content/uploads/yapb_cache/smartpray4d1.69shqbzmvsn4mcgs4w4cw8wc4.h9fw4mcunmtegc04wgoo4wck.th.jpeg" width="200" height="150" alt="A raytracer in Python for Cinema 4D" style="float:left;padding:0 10px 10px 0;" ></a><p>Maxon has just released the R12 version of Cinema 4D, which includes Python &#8211; so let&#8217;s dig into Python plugin development!</p>
<p>We are going to build a ray tracer&#8230; with Python! <strong><a href="http://www.vconverter.de/?file=SmartPray4D_output" target="_blank">Watch a rendered video</a></strong>.</p>
<p><strong><span style="color: #ff9900;">Update:</span></strong> <em>All Python scripts in this blog have been updated to work with the R12.</em></p>
<p><span id="more-1168"></span><br />
<br/><br />
Ok, I&#8217;m sorry &#8211; I should have come up with something more useful, but hey &#8211; it&#8217;s fun and it serves the purpose.<br />
Also this is not really about raytracing basics, but coding Python plugins for Cinema 4D &#8211; the cake is a lie!<br />
And while I am apologizing&#8230; I don&#8217;t do tutorial style posts that often, so please bear with me.</p>
<p>This is meant for coders coming from a C++ or Coffee background, so if you are already familiar with Python you might want to skip a few lines.</p>
<p>Please note that this is supposed to be a template for a rather complex Python plugin, so don&#8217;t feel discouraged if you had something simpler in mind &#8211; no problem &#8211; you can just write a full-fledged plugin using only one *.pyp file.<br />
<br/></p>
<h2>Preparations</h2>
<p>It&#8217;s a good idea to start by deciding which plugin type is best suited &#8211; in this case I want to write a <code>CommandData </code>plugin that will be callable from the Python plugin menu and open a dialog to display the rendered output.</p>
<p>So first I create a folder <code>'SmartPray4D'</code> within the <code>'plugins'</code> directory of my Cinema 4D installation.<br />
(SmartPray4D because &#8220;Smart Python based Ray tracer for Cinema 4D&#8221; was exactly one letter too long for my taste <img src='http://www.smart-page.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).</p>
<p>Within that folder I then make two other directory’s: <em>&#8216;res&#8217;</em> &#8211; which will hold all resource files the plugin will load, such as <em>*.res-, *.str</em> files and the plugin&#8217;s icon.<br />
The other one is called <em>&#8216;smartpray4dpackage&#8217;</em> and as the name suggest I am going to create a package here.</p>
<p>A package is really just a collection of Python modules &#8211; or better &#8216;sub modules&#8217;.<br />
It&#8217;s mandatory to place an empty file named <em>&#8216;__init__.py&#8217;</em> within a package folder. This tells Python to look for sub modules within that directory.<br />
<br/><br />
<img src="http://www.smart-page.net/blog/wp-content/uploads/2010/09/sp4ds.jpg" alt="" title="sp4ds" width="683" height="134" class="alignnone size-full wp-image-1194" /><br />
<br/></p>
<h2>Go!</h2>
<p>Now let’s get started &#8211; within the plugin directory I make a <em>*.pyp</em> file &#8211; the entry point which will register the plugin with Cinema 4D.</p>
<p>First I want to append my plugins directory to Python&#8217;s search path, so it may find the <code>'__init__.py'</code> within the <em>&#8216;smartpray4dpackage&#8217;</em> folder and treat it as package.</p>
<p><code>folder = os.path.dirname(__file__)<br />
if folder not in sys.path:<br />
&nbsp;&nbsp;&nbsp;&nbsp;sys.path.insert(0, folder)</code></p>
<p>As you can see from that statement, Python doesn&#8217;t use curly braces but indentation (4 spaces) to define code blocks and logical operators are spelled out. </p>
<p>Now I have access to my package and import a config file that will hold some constants and the <code>MyCommandData</code> class that I will register in the next step.</p>
<p><code>from smartpray4dpackage.config import PLUGIN_ID<br />
from smartpray4dpackage.mycommanddata import MyCommandData</code></p>
<p>The registration itself is done within the <code>__main__</code> block and is pretty straight forward.<br />
Note how I load an TIF icon from the <em>&#8216;res&#8217;</em> folder and the <code>PLUGIN_ID</code> which was imported from the config class &#8211; you need to apply for a unique plugin ID from <a href="http://www.maxon.net/de/support/plugin-cafe.html" target="_blank">Maxon</a> so your plugin won’t interfere with others.</p>
<p><code>if __name__ == "__main__":<br />
&nbsp;&nbsp;&nbsp;&nbsp;icon = c4d.bitmaps.BaseBitmap()<br />
&nbsp;&nbsp;&nbsp;&nbsp;icon.InitWith(os.path.join(os.path.dirname(__file__), "res", "smartpray4d.tif"))<br />
&nbsp;&nbsp;&nbsp;&nbsp;c4d.plugins.RegisterCommandPlugin(PLUGIN_ID, "Smart Pray4D", 0, icon, "A crazy Python benchmark", MyCommandData())<br />
</code><br />
<img src="http://www.smart-page.net/blog/wp-content/uploads/2010/09/sp4ds2.jpg" alt="" title="sp4ds2" width="683" height="107" class="alignnone size-full wp-image-1195" /><br />
<br/></p>
<h2>The meat</h2>
<p>Let’s take a look at the previously instantiated <code>MyCommandData</code> class.<br />
There is a private object <code>__dialog </code>which will hold the dialog that is created once the <code>CommandData</code> plugin gets executed by the user.<br />
It&#8217;s created within the overridden method <code>Execute</code> which Cinema 4D calls once the plugin gets selected. <code>ReloadLayout</code> also has to be defined so Cinema knows what to do when the dialog gets restored.<br />
In this case it&#8217;s a simple matter of checking if there already is a dialog or if it needs to be created.</p>
<p>Now this is where the fun begins &#8211; lets head over to the <em>&#8216;mydialog.py&#8217;</em>.</p>
<p>This module has the familiar <code>__init__</code> constructor in which three objects <code>__userarea, __scene</code> and <code>__renderer</code> get instantiated. Note the self-parameter &#8211; it is similar to the C++ pointer or the <code>this </code>reference in ECMA/Java and has to be the first parameter in every class method.</p>
<p>The mydialog module then overwrites the <code>CreateLayout </code>which&#8230; does exactly that, and creates a timer that will call the renderer, providing him with the scene to render and the userarea as target for its output.<br />
<br/><br/></p>
<h2>#END:</h2>
<p>I hope this gave you a first glimpse at Python plugin development for Cinema 4D as this short introduction will end here.</p>
<p>The renderer itself is a nice piece of work <a href="http://www.yomogi.sakura.ne.jp/~si/" target="_blank">keim_at_Si</a> originally wrote for Actionscript 3 and JavaScript.<br />
I did some small changes, but really just ported it to Python and tried my best to abstract it a little.</p>
<p>Now where to go from here? It&#8217;s really up to you <img src='http://www.smart-page.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Checkout the sources and have a look at the Python SDK documentation &#8211; endless possibilities&#8230;</p>
<p><strong><span style="color: rgb(255, 102, 0);">Hint:</span></strong><br />
<em>You can render a sequence of images to the picture viewer and save it as video using <code>ShowBitmap()</code>.</em><br />
<br/><br />
Last but not least <strong><a href="http://www.smart-page.net/python/SmartPray4D.zip">you can download the source here</a></strong> &#8211; have fun coding!<br />
<br/><br />
<br/></p>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-page.net/blog/2010/09/10/a-raytracer-in-python-for-cinema-4d/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Binary Kite &#8211; even more cubes with Py4D</title>
		<link>http://www.smart-page.net/blog/2010/06/25/the-binary-kite-even-more-cubes-with-py4d/</link>
		<comments>http://www.smart-page.net/blog/2010/06/25/the-binary-kite-even-more-cubes-with-py4d/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 12:47:17 +0000</pubDate>
		<dc:creator>flashgordon</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Binary kite]]></category>
		<category><![CDATA[C4D]]></category>
		<category><![CDATA[Cinema4D]]></category>
		<category><![CDATA[Generative geometry]]></category>
		<category><![CDATA[py4d]]></category>
		<category><![CDATA[Structure Synth]]></category>

		<guid isPermaLink="false">http://www.smart-page.net/blog/?p=1096</guid>
		<description><![CDATA[<a href="http://www.smart-page.net/blog/2010/06/25/the-binary-kite-even-more-cubes-with-py4d/" title="The Binary Kite &#8211; even more cubes with Py4D"><img src="http://www.smart-page.net/blog/wp-content/uploads/yapb_cache/binary_kite1.7nj4lk5ebzdm68sw00cg8soso.h9fw4mcunmtegc04wgoo4wck.th.jpeg" width="200" height="150" alt="The Binary Kite &#8211; even more cubes with Py4D" style="float:left;padding:0 10px 10px 0;" ></a>There are those popular structures we all know and love because they simply look fascinating from every angle &#8211; like the Sierpinski triangle, the Pythagoras tree or the Menger sponge. Generative geometry is one more proof that math is beautiful no matter how basic. Recently I stumbled upon a new pattern that is also phenomenally [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.smart-page.net/blog/2010/06/25/the-binary-kite-even-more-cubes-with-py4d/" title="The Binary Kite &#8211; even more cubes with Py4D"><img src="http://www.smart-page.net/blog/wp-content/uploads/yapb_cache/binary_kite1.7nj4lk5ebzdm68sw00cg8soso.h9fw4mcunmtegc04wgoo4wck.th.jpeg" width="200" height="150" alt="The Binary Kite &#8211; even more cubes with Py4D" style="float:left;padding:0 10px 10px 0;" ></a><p>There are those popular structures we all know and love because they simply look fascinating from every angle &#8211; like the <a href="http://en.wikipedia.org/wiki/Sierpinski_triangle" target="_blank">Sierpinski triangle</a>, the <a href="http://en.wikipedia.org/wiki/Pythagoras_tree" target="_blank">Pythagoras tree</a> or the <a href="http://en.wikipedia.org/wiki/Menger_sponge" target="_blank">Menger sponge</a>.<br />
Generative geometry is one more proof that math is beautiful no matter how basic.</p>
<p>Recently I stumbled upon a new pattern that is also phenomenally simple but produces rather complex shapes &#8211; the &#8220;Binary Kite&#8221;.</p>
<p><span id="more-1096"></span></p>
<p>I first saw it in Mikael Hvidtfeldt Christensen&#8217;s &#8216;<a href="http://structuresynth.sourceforge.net/" target="_blank">Structure Synth</a>&#8216; &#8211; an ingenious tool for exploration of generative 3D structures.</p>
<p>Basically it&#8217;s a 3D array of randomly black/white colored cubes that slightly scale(linear) and rotate(noise) per iteration on one axis.<br />
What makes it look so interesting is that each items transformation matrix is subordinated to its predecessor on that axis.</p>
<p>Initially, I played around with the kite in Unity 3D but quickly discovered that it can´t be animated at high iterations.<br />
<br/><br />
So I fired up C4D and wrote a simple Py4D generator.<br />
<br/></p>
<h2>The results</h2>
<p>I did end up with this gorgeous picture of a 30*30*500 cubes kite (5400000 polygons) (<a href="http://www.smart-page.net/py4d/sp_kite/binary_kite.jpg" target="_blank" rel="lightbox[1096]">fullsize</a>):</p>
<p><a href="http://www.smart-page.net/py4d/sp_kite/binary_kite_small.jpg" target="_blank" rel="lightbox[1096]"><img class="alignnone size-full wp-image-1097" title="binary_kite_01" src="http://www.smart-page.net/blog/wp-content/uploads/2010/06/binary_kite_01.jpg" alt="" width="683" height="169" rel="" /></a></p>
<p>Here is a closeup from the same viewpoint (<a href="http://www.smart-page.net/py4d/sp_kite/binary_kite_close.jpg" target="_blank" rel="lightbox[1096]">fullsize</a>):</p>
<p><a href="http://www.smart-page.net/py4d/sp_kite/binary_kite_close_small.jpg" target="_blank" rel="lightbox[1096]"><img class="alignnone size-full wp-image-1098" title="binary_kite_02" src="http://www.smart-page.net/blog/wp-content/uploads/2010/06/binary_kite_02.jpg" alt="" width="683" height="169" /></a></p>
<p>Last but not least I rendered this short animation of a <a href="http://www.vconverter.de/?file=binary_kite" target="_blank">5*5*150 kite growing</a>:</p>
<p><a href="http://www.vconverter.de/?file=binary_kite" target="_blank"><img src="http://www.smart-page.net/blog/wp-content/uploads/2010/06/binary_kite_03.jpg" alt="" title="binary_kite_03" width="683" height="169" class="alignnone size-full wp-image-1118" /></a></p>
<p>Want to render your own kite with even more cubes? Grab the <a href="http://www.smart-page.net/python/BinaryKite4Py.c4d">scene file here</a>.</p>
<p><strong><span style="color: #ff9900;">Update:</span></strong> <em>The file has been updated to work with the R12 version of Cinema 4D.</em></p>
<p><strong><span style="color: rgb(255, 102, 0);">Hint:</span></strong><br />
<em>Remember to uncheck &#8220;Optimize cache&#8221; in the Python Generator´s properties if you want to animate the parameters.</em><br />
<br/></p>
<h2>Conclusion</h2>
<p>While Structure Synth is awesome, Cinema 4D and Py4D are a great way to visualize generated geometry too as Cinema&#8217;s render instances enable you to work with millions of objects.<br />
<br/><br />
Cheers<br />
<br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-page.net/blog/2010/06/25/the-binary-kite-even-more-cubes-with-py4d/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Py4D Examples &#8211; particle party</title>
		<link>http://www.smart-page.net/blog/2010/04/10/py4d-examples-particle-party/</link>
		<comments>http://www.smart-page.net/blog/2010/04/10/py4d-examples-particle-party/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 15:33:42 +0000</pubDate>
		<dc:creator>flashgordon</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[boids]]></category>
		<category><![CDATA[Particles]]></category>
		<category><![CDATA[Physics]]></category>
		<category><![CDATA[py4d]]></category>
		<category><![CDATA[Stars]]></category>
		<category><![CDATA[Strange Attractor]]></category>

		<guid isPermaLink="false">http://www.smart-page.net/blog/?p=964</guid>
		<description><![CDATA[<a href="http://www.smart-page.net/blog/2010/04/10/py4d-examples-particle-party/" title="Py4D Examples &#8211; particle party"><img src="http://www.smart-page.net/blog/wp-content/uploads/yapb_cache/py4d_title1.3t0udbr1iklkrowok0g4owsgc.h9fw4mcunmtegc04wgoo4wck.th.jpeg" width="200" height="150" alt="Py4D Examples &#8211; particle party" style="float:left;padding:0 10px 10px 0;" ></a>I haven´t written about Py4D for quite a while now&#8230; In the meantime, Maxon has first announced a cooperation with &#8211; and now the acquisition of Sebastian Rath´s praised python implementation for Cinema4D. So finally &#8211; Cinema4D meets Python! Let´s celebrate with some cool experiments! If you are new to this blog or Py4D &#8211; [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.smart-page.net/blog/2010/04/10/py4d-examples-particle-party/" title="Py4D Examples &#8211; particle party"><img src="http://www.smart-page.net/blog/wp-content/uploads/yapb_cache/py4d_title1.3t0udbr1iklkrowok0g4owsgc.h9fw4mcunmtegc04wgoo4wck.th.jpeg" width="200" height="150" alt="Py4D Examples &#8211; particle party" style="float:left;padding:0 10px 10px 0;" ></a><p>I haven´t written about <a href="http://www.py4d.com/" target="_blank">Py4D</a> for quite a while now&#8230;</p>
<p>In the meantime, <a href="http://www.maxon.net" target="_blank">Maxon</a> has first announced a cooperation with &#8211; and now the acquisition of Sebastian Rath´s praised python implementation for Cinema4D.</p>
<p>So finally &#8211; Cinema4D meets Python!</p>
<p><strong>Let´s celebrate with some cool experiments!</strong></p>
<p><strong><span id="more-964"></span><br />
</strong></p>
<p>If you are new to this blog or Py4D &#8211; a short introduction:</p>
<p>Py4D enhances C4D with a standard Python interpreter that provides access to the C4D SDK and allows to easily code platform indepentent scripts and plugins that control or extend C4D.</p>
<p>Basicly the same as COFFEE, C4D´s own scripting language, but Py4D has two major advantages:</p>
<ul>
<li> It follows the C++ SDK more closely and implements it further than COFFEE does.</li>
<li>It is a easy to pickup, widely known language with a huge community and tons of existing code and librarys.</li>
</ul>
<p><br/><br />
While C4D is a mighty tool in itself, this powerful scripting interface enables the user to work with external data, automate processes or build his own tools.</p>
<p>Being more the C++ type when it comes to plugin development and C4D, I enjoy being able to solve small problems just by hacking a few lines of code into the script manager.<br />
I also prototype most of my plugins in Py4D before I start developing in C++ &#8211; it´s just more convenient.<br />
The step of compiling for every test takes much more time in the long run.</p>
<p><br/></p>
<h3>So here´s the meat:</h3>
<p>Four simple examples how to have a good time with Py4D and some particles&#8230;</p>
<p><del datetime="2010-09-09T18:26:28+00:00">Everyone requires C4D R11.5, the Thinking Particles module and the actual version of Py4D.</del></p>
<p><strong><span style="color: #ff9900;">Update:</span></strong> <em>All files have been updated to work with the R12 version of Cinema 4D.</em></p>
<p>Py4D has been through some changes since the open beta started, so I took the time to update the scripts I published in the <a href="http://www.smart-page.net/blog/2009/08/07/py4d-boids/" target="_blank">boids</a>, and <a href="http://www.smart-page.net/blog/2009/09/15/strange-attractor-c4dpy4d-particle-bench/" target="_blank">strange attractor</a> posts and included them here.</p>
<p><em>Click the images for a preview.</em><br />
<br/></p>
<h3>1. Stars4Py4D</h3>
<p><a href="http://www.smart-page.net/as3d/sp_stars" target="_blank"><img class="size-full wp-image-965 alignnone" title="py4" src="http://www.smart-page.net/blog/wp-content/uploads/2010/04/py4.jpg" alt="" width="680" height="100" /></a></p>
<p>This is a Py4D port of the <a href="http://www.smart-page.net/blog/2010/04/03/cruising-the-cosmos-sagan-style-flash-in-outer-space/" target="_blank">Sagan experiment</a> I did in flash <- check out this link, it´s fun <img src='http://www.smart-page.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>A good and simple first step with particles - position vectors get parsed from a CVS <a href="http://www.astronexus.com/node/34" target="_blank">database</a> and from a galaxy of suns&#8230; “My God. It’s full of stars!”</p>
<p>Hint: <em>this example might be the best to start with, as it includes comments &#8211; the others don´t, because they use pretty much the same pattern. </em> </p>
<p>Just <a href="http://www.smart-page.net/python/Stars4Py.zip" target="_blank">download the package</a>, load the scene and change the path to the CSV file within the &#8220;file&#8221; user data field of the Py4D Tag.</p>
<p><br/></p>
<h3>2. Phy4D</h3>
<p><a href="http://www.vconverter.de/?file=py4d_simple_phy" target="_blank"><img class="alignnonesize-full wp-image-968" title="py2" src="http://www.smart-page.net/blog/wp-content/uploads/2010/04/py2.jpg" alt="" width="680" height="100" /></a></p>
<p>A very simple adaptation of a 2D &#8220;physic engine&#8221; &#8211; works only with spheres.<br />
The direction of gravitation is animated to make this a little more interesting.</p>
<p>Have a look at the <a href="http://www.vconverter.de/?file=py4d_simple_phy" target="_blank">preview video</a> and <a href="http://www.smart-page.net/python/Phy4Py.c4d">scene file</a>.</p>
<p><br/></p>
<h3>3. Boids</h3>
<p><a href="http://www.vconverter.de/?file=sp_boids" target="_blank"><img class="alignnonesize-full wp-image-970" title="py1" src="http://www.smart-page.net/blog/wp-content/uploads/2010/04/py1.jpg" alt="" width="680" height="100" /></a></p>
<p>This is simply a revival of the <a href="http://www.smart-page.net/blog/2009/08/07/py4d-boids/" target="_blank">Py4D Boids post</a> &#8211; same beast, but updated to the Py4D release version.<br />
The script creates a flocking boids swarm, that behaves according to the <a href="http://en.wikipedia.org/wiki/Boids" target="_blank">rules</a> outlined by Craig Reynolds.</p>
<p>Checkout <a href="http://www.vconverter.de/?file=sp_boids" target="_blank">this video</a> from Sebastian and get the <a href="http://www.smart-page.net/python/Boids4Py.c4d">scenefile</a>.</p>
<p><br/></p>
<h3>4. Strange Attractor</h3>
<p><a href="http://www.vconverter.de/?file=strangeattractor" target="_blank"><img class="alignnonesize-full wp-image-969" title="py3" src="http://www.smart-page.net/blog/wp-content/uploads/2010/04/py3.jpg" alt="" width="680" height="100" /></a></p>
<p>Again just the script from the original strange attractor post, but ported to the final SDK.<br />
View the <a href="http://www.vconverter.de/?file=strangeattractor" target="_blank">final rendering</a> and the <a href="http://www.smart-page.net/blog/2009/09/15/strange-attractor-c4dpy4d-particle-bench/" target="_blank">original post</a> &#8211; you can use the <a href="http://www.smart-page.net/as3/sp_safinder/" target="_blank">Strange Attractor Finder</a> to locate your very own, chaotic attractor!</p>
<p>Here´s the <a href="http://www.smart-page.net/py4d/SA4Py4D.c4d">scene file</a>.</p>
<p><br/></p>
<h3>Conclusion</h3>
<p>Python and Cinema4D are a great team and Py4D is THE alternative to COFFEE.</p>
<p>I hope this gives everybody who is interested in Py4D and controlling particles, a small head start, some inspiration or at least something fun to play with <img src='http://www.smart-page.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<br/><br />
Thanks to Sebastian for suggestions, support &#8211; and of course Py4D.</p>
<p><br/><br />
Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-page.net/blog/2010/04/10/py4d-examples-particle-party/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Strange Attractor Finder and C4D/PY4D Particle Bench</title>
		<link>http://www.smart-page.net/blog/2009/09/15/strange-attractor-c4dpy4d-particle-bench/</link>
		<comments>http://www.smart-page.net/blog/2009/09/15/strange-attractor-c4dpy4d-particle-bench/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 14:52:51 +0000</pubDate>
		<dc:creator>flashgordon</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Bench]]></category>
		<category><![CDATA[C4D]]></category>
		<category><![CDATA[cinema 4D]]></category>
		<category><![CDATA[Generator]]></category>
		<category><![CDATA[Lyapunov]]></category>
		<category><![CDATA[Particles]]></category>
		<category><![CDATA[py4d]]></category>
		<category><![CDATA[Strange Attractor]]></category>
		<category><![CDATA[Strange Attractor Finder]]></category>

		<guid isPermaLink="false">http://www.smart-page.net/blog/?p=399</guid>
		<description><![CDATA[<a href="http://www.smart-page.net/blog/2009/09/15/strange-attractor-c4dpy4d-particle-bench/" title="Strange Attractor Finder and C4D/PY4D Particle Bench"><img src="http://www.smart-page.net/blog/wp-content/plugins/yet-another-photoblog/YapbThumbnailer.php?post_id=399&amp;w=200&amp;h=150&amp;zc=1" width="200" height="150" alt="Strange Attractor Finder and C4D/PY4D Particle Bench" style="float:left;padding:0 10px 10px 0;" ></a>Strange attractors are fractals that emerge at certain, sensitive parameters within the three-or more dimensional phase space. They are are researched since the early 70s and strongly related to the chaos theory as they allow to watch the transition from chaos to order/geometry. Good things first: see the result and try the chaotic attractor finder! [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.smart-page.net/blog/2009/09/15/strange-attractor-c4dpy4d-particle-bench/" title="Strange Attractor Finder and C4D/PY4D Particle Bench"><img src="http://www.smart-page.net/blog/wp-content/plugins/yet-another-photoblog/YapbThumbnailer.php?post_id=399&amp;w=200&amp;h=150&amp;zc=1" width="200" height="150" alt="Strange Attractor Finder and C4D/PY4D Particle Bench" style="float:left;padding:0 10px 10px 0;" ></a><p>Strange attractors are fractals that emerge at certain, sensitive parameters within the three-or more dimensional phase space.</p>
<p>They are are researched since the early 70s and strongly related to the chaos theory as they allow to watch the transition from chaos to order/geometry.</p>
<p><b>Good things first: <a href="http://www.vconverter.de/?file=strangeattractor" target="_blank">see the result</a> and try the <a href="http://www.smart-page.net/as3/sp_safinder/" target="_blank">chaotic attractor finder!</a></b></p>
<p><span id="more-399"></span></p>
<p><a href="http://www.smart-page.net/as3/sp_safinder/" target="_blank"><img class="alignnone size-full wp-image-417" title="sas" src="http://www.smart-page.net/blog/wp-content/uploads/2009/09/sas.jpg" alt="sas" width="596" height="157" /></a></p>
<p>The 3D visualization of a strange attractor is quite resource intensive as points race around the attractors on chaotic trajectories &#8211; so it needs many iterations or particles to see a shape emerge from that chaos.</p>
<p>There are already some <a href="http://www.unitzeroone.com/blog/2009/03/18/flash-10-massive-amounts-of-3d-particles-with-alchemy-source-included/" target="_blank">great flash experiments</a> so I turned to Cinema 4D and <a href="http://www.py4d.com/" target="_blank">PY4D</a> as an advanced renderer would easily allow to raytrace and shadow the results &#8211; and to calculate even more particles of course.</p>
<p><a href="http://www.smart-page.net/py4d/sp_strange/sp_strange_attractor_py4d.txt" target="_blank">This small PY4D script</a> creates a strange attractor that Dr. Clifford Pickover had published 1990.</p>
<p>I managed to render a maximum of 50.000 particles with the R11 on my machine.</p>
<p>But with the new R11.5 release Maxon introduced render instances to C4D allowing now to calculate up to millions of particles &#8211; depending on the system.</p>
<p>I went with a number of 300.000 points, seeded the attractor parameters and rendered four transparent animations that could be combined to a 1.2 million particle video.</p>
<p>The preparation time for each frame was ~1min while PY4D took less than a second of that duration to perform five sinus/cosinus operations for each point.<br />
Rendering itself took about five seconds per frame (CPU:Q6600).</p>
<p>If you are interested you can <a href="http://www.smart-page.net/py4d/SA4Py4D.c4d">download the szenefile</a> here.<br />
(Remember to activate render instances in the PShape node of the XPresso tag).</p>
<p><span style="color: #ff9900;"><strong>Update:</strong></span><br />
The scene file has been updated to the R12.</p>
<p>When I learned that you can actually identify strange attractors using the  <a href="http://en.wikipedia.org/wiki/Lyapunov_exponent" target="_blank">Lyapunov exponent</a> and found <a href="http://technocosm.org/chaos/attr-part2.html" target="_blank">John Holder´s</a> programm &#8216;lyapdemo.c&#8217;, I took a few minutes to <a href="http://www.smart-page.net/as3/sp_safinder/" target="_blank">port it to AS3</a>.</p>
<p>You can use it to generate parameters for the PY4D script.<a href="http://technocosm.org/chaos/attr-part2.html" target="_blank"><br />
</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-page.net/blog/2009/09/15/strange-attractor-c4dpy4d-particle-bench/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>PY4D Boids</title>
		<link>http://www.smart-page.net/blog/2009/08/07/py4d-boids/</link>
		<comments>http://www.smart-page.net/blog/2009/08/07/py4d-boids/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 17:17:02 +0000</pubDate>
		<dc:creator>flashgordon</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[boids]]></category>
		<category><![CDATA[cinema 4D]]></category>
		<category><![CDATA[flocking]]></category>
		<category><![CDATA[py4d]]></category>

		<guid isPermaLink="false">http://www.smart-page.net/blog/?p=309</guid>
		<description><![CDATA[<a href="http://www.smart-page.net/blog/2009/08/07/py4d-boids/" title="PY4D Boids"><img src="http://www.smart-page.net/blog/wp-content/plugins/yet-another-photoblog/YapbThumbnailer.php?post_id=309&amp;w=200&amp;h=150&amp;zc=1" width="200" height="150" alt="PY4D Boids" style="float:left;padding:0 10px 10px 0;" ></a>Sebastian Rath´s PY4D is a new, alternative way to script with or to code plugins for Cinema4D &#8211; without COFFE or C++ &#8211; but in Python. The current beta (v0.9.0001) already makes a fine impression and proves itself easy to get started with. This video shows a little script that was adapted in a twinkling [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.smart-page.net/blog/2009/08/07/py4d-boids/" title="PY4D Boids"><img src="http://www.smart-page.net/blog/wp-content/plugins/yet-another-photoblog/YapbThumbnailer.php?post_id=309&amp;w=200&amp;h=150&amp;zc=1" width="200" height="150" alt="PY4D Boids" style="float:left;padding:0 10px 10px 0;" ></a><p>Sebastian Rath´s <a href="http://py4d.xpresso24.com" target="_blank">PY4D</a> is a new, alternative way to script with or to code plugins for Cinema4D &#8211; without COFFE or C++ &#8211; but in Python.</p>
<p>The current beta (v0.9.0001) already makes a fine impression and proves itself easy to get started with.</p>
<p><b><a href="http://www.vconverter.de/?file=fishboids" target="_blank">This video</a></b> shows a little script that was adapted in a twinkling and controls the particles of a TP-Storm to behave after the three flocking boids rules.<a href="http://www.py4d.com/2009/08/there-comming/" target="_blank"><br />
Don</a> has rendered a <b><a href="http://www.vconverter.de/?file=tunaboids" target="_blank">much prettyer version</a></b> of the dummy above.</p>
<p>You can download the C4D-scene <a href="http://www.smart-page.net/python/Boids4Py.c4d">here</a>.</p>
<p><strong><span style="color: #ff9900;">Update:</span></strong> <em>The file has been updated to work with the R12 version of Cinema 4D.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-page.net/blog/2009/08/07/py4d-boids/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

