<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Squarespace Site Server v5.11.81 (http://www.squarespace.com/) on Wed, 30 May 2012 15:04:51 GMT--><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><title>Home</title><link>http://www.3dqanda.com/imported-20110111202530/</link><description></description><lastBuildDate>Fri, 24 Feb 2012 04:15:14 +0000</lastBuildDate><copyright></copyright><language>en-US</language><generator>Squarespace Site Server v5.11.81 (http://www.squarespace.com/)</generator><item><title>PyQt and Drag and Drop functionality</title><category>Programming</category><category>Python</category><dc:creator>[Your Name Here]</dc:creator><pubDate>Fri, 24 Feb 2012 03:03:29 +0000</pubDate><link>http://www.3dqanda.com/imported-20110111202530/2012/2/23/pyqt-and-drag-and-drop-functionality.html</link><guid isPermaLink="false">775630:9094624:15164796</guid><description><![CDATA[<p>I recently started working with PyQt, and I needed to allow the user to drag and drop files paths into a listview widget. &nbsp;With a little bit of research I was able to get it to work. Below is some sample code of how to setup a class for a List widget that will allow for dragging and dropping. &nbsp;</p>
<pre><FONT color="blue">class</FONT> <FONT color="cyan">TestListBox</FONT>(ui.QtGui.QListWidget):
      <FONT color="blue">def</FONT> <FONT color="cyan">__init__</FONT>(<FONT color="blue">self</FONT>, type, parent=<FONT color="blue">None</FONT>):
            <FONT color="blue">super</FONT>(TestListBox, <FONT color="blue">self</FONT>).<FONT color="cyan">__init__</FONT>(parent)
            <FONT color="blue">self</FONT>.setAcceptDrops(True)

      <FONT color="blue">def</FONT> <FONT color="cyan">dragEnterEvent</FONT>(<FONT color="blue">self</FONT>, event):
            <FONT color="blue">if</FONT> event.mimeData().hasUrls:
                  event.accept()
            <FONT color="blue">else</FONT>:
                  event.ignore()

      <FONT color="blue">def</FONT> <FONT color="cyan">dragMoveEvent</FONT>(<FONT color="blue">self</FONT>, event):
            <FONT color="blue">if</FONT> event.mimeData().hasUrls:
                  event.setDropAction(QtCore.Qt.CopyAction)
                  event.accept()
            <FONT color="blue">else</FONT>:
                  event.ignore()

      <FONT color="blue">def</FONT> <FONT color="cyan">dropEvent</FONT>(<FONT color="blue">self</FONT>, event):
            
            <FONT color="blue">if</FONT> event.mimeData().hasUrls:
                  event.setDropAction(QtCore.Qt.CopyAction)
                  event.accept()
                  
                  links = []

                  <FONT color="blue">for</FONT> url in event.mimeData().urls():
                        links.append(str(url.toLocalFile()))
                  
                  self.emit(ui.QtCore.SIGNAL(<FONT color="yellow">"dropped"</FONT>), links)
            <FONT color="blue">else</FONT>:
                  event.ignore()
</pre>
<div></div>
<pre>From there, in your window code you can initialize the list such as self.view = TestListbox(self) . &nbsp;You must also make sure&nbsp;</pre>
<pre>that the listView widget is set as the central widget for the window. &nbsp;If you do not set it to be the central widget, the drag and</pre>
<pre>drop functionality does not appear to work. &nbsp;The last couple of steps is to then connect the drop signal to a function where you</pre>
<pre>add the list items into the list. &nbsp;The code above gets the data, but it does not actually add the data as items into the widget. &nbsp;</pre>
<pre>To do this add code such as this:</pre>
<pre></pre>
<pre><FONT color="blue">self</FONT>.connect(<FONT color="blue">self</FONT>.view, QtCore.SIGNAL(<FONT color="yellow">"dropped"</FONT>), <FONT color="blue">self</FONT>.testDropMethod )</pre>
<pre></pre>
<pre>In the function called testDropMethod just add the items to the list. The method should be defined in this way:</pre>
<pre>

<FONT color="blue">def</FONT> testDropMethod(<FONT color="blue">self</FONT>, l): (l is the actual list of files that you can iterate through and add as items to the list)
</pre>
<p>&nbsp;</p>
<p>I hope this is helpful for anyone who is also trying to do this.  There is also a very good post on this <a href="http://stackoverflow.com/questions/4151637/pyqt4-drag-and-drop-files-into-qlistwidget?answertab=active#tab-top">here</a></p>
<p><a href="http://stackoverflow.com/questions/4151637/pyqt4-drag-and-drop-files-into-qlistwidget?answertab=active#tab-top">.</a></p>]]></description><wfw:commentRss>http://www.3dqanda.com/imported-20110111202530/rss-comments-entry-15164796.xml</wfw:commentRss></item><item><title>Dynamically adding methods to a class in Python</title><category>Programming</category><category>Python</category><dc:creator>[Your Name Here]</dc:creator><pubDate>Mon, 12 Dec 2011 22:29:42 +0000</pubDate><link>http://www.3dqanda.com/imported-20110111202530/2011/12/12/dynamically-adding-methods-to-a-class-in-python.html</link><guid isPermaLink="false">775630:9094624:14080218</guid><description><![CDATA[<p>I recently found a way to dynamically create method and add it to a class. &nbsp;I wanted to figure this out because I had a decent list of properties in the class that I wanted to create a "get" function for. &nbsp;That way I can use a function called get(property name) for accessing that property instead of calling it directly. &nbsp;However, since the list way fairly long, I didn't want to have to type out all of the functions if I could find a way to dynamically create them. &nbsp;</p>
<p>The class had a list of properties such as:</p>
<pre>properties = ['name', 'number', 'location']</pre>
<p>Then, in the class, I created a fairly generic function called getPropertyValue(self, *args, **kwargs), which took an argument of the name of the property that we wanted to get the value for. &nbsp;So it ended up being something like:</p>
<pre>def getProperty(self, *args, **kwargs):
      if kwargs:
            # check to make sure we have a property argument passed in
            propertyName = kwargs.get('property', None)

            # check to make sure the property name is in the list of properties
            if propertyName in self.properties:
                  # get the value of the property and return it
                  return getattr(self, propertyName)
</pre>
<p>From there I can then create a function based off of that generic function called something like getName() dynamically. &nbsp;To do this I used a for loop in the __init__ function for the class. &nbsp;It ended up being:</p>
<pre>for prop in self.properties:
      self.__dict__['get%s' % prop] = types.MethodType(pm.windows.Callback(self.getProperty,
                                                       {}, property = prop), self)
</pre>
<p>The tricky part was calling a function with an argument using MethodType.  I started by trying to use a lambda function, but this does not work very well in for loops due to scope issues.  Luckily I was doing this for a script in Maya, so I was able to use PyMEL which has a Callback object that solves this for me.  That is what the pm.windows.Callback is, and it works well in this case since I am running this from Maya.  It returns an instance of the method back to me with the proper argument set for it.</p>]]></description><wfw:commentRss>http://www.3dqanda.com/imported-20110111202530/rss-comments-entry-14080218.xml</wfw:commentRss></item><item><title>Python @classmethod</title><category>Programming</category><category>classes object oriented programming</category><dc:creator>[Your Name Here]</dc:creator><pubDate>Tue, 02 Aug 2011 15:51:52 +0000</pubDate><link>http://www.3dqanda.com/imported-20110111202530/2011/8/2/python-classmethod.html</link><guid isPermaLink="false">775630:9094624:12368995</guid><description><![CDATA[<p>Python's class methods can come in handy, and they work fairly similarly to something such as static methods in C++, Java, etc. &nbsp;They allow you to use a function in a class without having to actually create an instance of a class. &nbsp;In this way you can call something like this:</p>
<p>myClass.doSomething()</p>
<p>as opposed to</p>
<p>myClass().doSomething(), which will run the class initialization function.</p>
<p>To define a class method much like the one above you can use the syntax:</p>
<pre>class myClass():
      def __init__(self):
            self.someVariable = "test"
      @classmethod
      def doSomething(cls):
            print "My first class method"
</pre>
<p>This is good because it allows you to skip code that you have put in your initialization function if you have overridden it.  This is also good for utility functions where you just want to run the function without initializing the class.  With Python's class methods you still also have access to the class variables.</p>
<p>This is also a good link on the subject: <a href="http://stackoverflow.com/questions/38238/what-are-class-methods-in-python-for">http://stackoverflow.com/questions/38238/what-are-class-methods-in-python-for</a></p>]]></description><wfw:commentRss>http://www.3dqanda.com/imported-20110111202530/rss-comments-entry-12368995.xml</wfw:commentRss></item><item><title>Python .net: Bringing C# into Python and Maya</title><category>Programming</category><dc:creator>[Your Name Here]</dc:creator><pubDate>Sun, 22 May 2011 23:43:16 +0000</pubDate><link>http://www.3dqanda.com/imported-20110111202530/2011/5/22/python-net-bringing-c-into-python-and-maya.html</link><guid isPermaLink="false">775630:9094624:11544184</guid><description><![CDATA[<p>Recently I have been working with Python .net in order to use some functionality from .Net and other libraries in Python scripts for Maya. &nbsp;This can be handy for building user interfaces with .Net as well as using something such as a library that you coded with C#. &nbsp;</p>
<p>As an example, I can use the System library from .Net to set the envrionment variables using this process from a Python script. &nbsp;The System library has a easy to use function that works well for setting user environment variables as well as system envrionment variables on Windows. &nbsp;The script below is an example of how this can work. &nbsp;&nbsp;</p>
<p><br /><span class="thumbnail-image-block ssNonEditable"><span><a href="javascript:showFullImage('/display/ShowImage?imageUrl=%2Fstorage%2FpythonNetScript.jpg%3F__SQUARESPACE_CACHEVERSION%3D1306109213299',201,939);"><img src="http://www.3dqanda.com/storage/thumbnails/9084612-12340456-thumbnail.jpg?__SQUARESPACE_CACHEVERSION=1306109213300" alt="" /></a></span></span><br /><span class="thumbnail-image-block ssNonEditable">&nbsp;</span></p>
<p>There are some interesting possibilities that this opens up. &nbsp;If you have an API or anything that has been built into a library, you can add that library as a reference using Python .net. &nbsp;Since they can be used with Python, they can also then be used in Maya as well.</p>
<p>Python .Net is fairly easy to setup, and you can find directions and info at <a href="http://tech-artists.org/wiki/PythonNetInMaya">http://tech-artists.org/wiki/PythonNetInMaya</a>&nbsp;. &nbsp;You can also download Python .Net source code and libraries from&nbsp;<a href="http://sourceforge.net/projects/pythonnet/files/pythonnet-2.0-alpha2-clr2.0_py25/">http://sourceforge.net/projects/pythonnet/files/pythonnet-2.0-alpha2-clr2.0_py25/</a>&nbsp;. &nbsp;</p>]]></description><wfw:commentRss>http://www.3dqanda.com/imported-20110111202530/rss-comments-entry-11544184.xml</wfw:commentRss></item><item><title>Namespaces</title><category>Programming</category><dc:creator>[Your Name Here]</dc:creator><pubDate>Fri, 04 Mar 2011 05:04:48 +0000</pubDate><link>http://www.3dqanda.com/imported-20110111202530/2011/3/4/namespaces.html</link><guid isPermaLink="false">775630:9094624:10670622</guid><description><![CDATA[<p>In Python as well as other programming languages there are namespaces. &nbsp;These are important to help differentiate between functions and variables of the same name from different modules. &nbsp;For instance if you had a module named module1.py with a function named bestever(v = True) and module2.py with bestever(v = True) and wanted to import both modules you would get a name clash without telling Python which one to use.</p>
<p>In Python you can import everything from a module using from mymodule import * . &nbsp;This will import everything from the module into the default namespace. &nbsp;This can be nice since you can just call the function as normal by putting in bestever(); however, this can create name clashes.</p>
<p>In the case of our example modules above:</p>
<p>module1.py</p>
<p>def bestever(v = True)</p>
<p>print "This is module1!"</p>
<p>&nbsp;</p>
<p>module2.py</p>
<p>&nbsp;</p>
<p>def bestever(v = True):</p>
<p>print "I am module 2!"</p>
<p>&nbsp;</p>
<p>if we ran this code:</p>
<p>&nbsp;</p>
<p>from module1 import *</p>
<p>from module2 import *</p>
<p>&nbsp;</p>
<p>bestever()</p>
<p>&nbsp;</p>
<p>we would get the output:</p>
<p>&nbsp;</p>
<p>I am module 2!</p>
<p>&nbsp;</p>
<p>Python takes the last known instance of the function bestever, which would be module 2 since we imported it last. &nbsp;If we wanted to tell python which one to use we would have to import the module another way.</p>
<p>&nbsp;</p>
<p>import module1 as m1 <span style="white-space: pre;"> </span>#"as m1" is what creates the namespace</p>
<p>import module2 as m2 <span style="white-space: pre;"> </span>#"as m2" is what creates the namespace</p>
<p>&nbsp;</p>
<p>m1.bestever()<span style="white-space: pre;"> </span># call the bestever from module 1</p>
<p>m2.bestever()<span style="white-space: pre;"> </span># call the bestever from module 2</p>
<p>&nbsp;</p>
<p>Running this code we can now call out to the version of bestever we want using the namespace. &nbsp;For small programs where we are certain that there will be no name clashes we can use the first way of importing a module if we want, but to avoid any issues it is always a good idea to use namespaces. &nbsp;You can keep the names of the namespaces minimal while avoiding issues with your program not working properly. &nbsp;&nbsp;</p>]]></description><wfw:commentRss>http://www.3dqanda.com/imported-20110111202530/rss-comments-entry-10670622.xml</wfw:commentRss></item><item><title>Intro Pymel Video</title><category>Programming</category><dc:creator>[Your Name Here]</dc:creator><pubDate>Sat, 29 Jan 2011 04:15:48 +0000</pubDate><link>http://www.3dqanda.com/imported-20110111202530/2011/1/28/intro-pymel-video.html</link><guid isPermaLink="false">775630:9094624:10276543</guid><description><![CDATA[<p>If you are interested in a simple beginner Maya python video, you can find one &nbsp;here.</p>
<p><a href="http://www.cgbootcamp.com/tutorials/category/maya-pymel">http://www.cgbootcamp.com/tutorials/category/maya-pymel</a></p>
<p>&nbsp;</p>]]></description><wfw:commentRss>http://www.3dqanda.com/imported-20110111202530/rss-comments-entry-10276543.xml</wfw:commentRss></item><item><title>Inserting an object into a python list in the correct order</title><category>Programming</category><dc:creator>[Your Name Here]</dc:creator><pubDate>Sat, 08 Jan 2011 21:33:20 +0000</pubDate><link>http://www.3dqanda.com/imported-20110111202530/2011/1/8/inserting-an-object-into-a-python-list-in-the-correct-order.html</link><guid isPermaLink="false">775630:9094624:10007754</guid><description><![CDATA[<p></p>]]></description><wfw:commentRss>http://www.3dqanda.com/imported-20110111202530/rss-comments-entry-10007754.xml</wfw:commentRss></item><item><title>Eye material painting in photoshop</title><category>Art</category><dc:creator>[Your Name Here]</dc:creator><pubDate>Thu, 16 Dec 2010 11:16:02 +0000</pubDate><link>http://www.3dqanda.com/imported-20110111202530/2010/12/16/eye-material-painting-in-photoshop.html</link><guid isPermaLink="false">775630:9094624:10007755</guid><description><![CDATA[<p></p>]]></description><wfw:commentRss>http://www.3dqanda.com/imported-20110111202530/rss-comments-entry-10007755.xml</wfw:commentRss></item><item><title>Python lambda functions</title><category>Programming</category><dc:creator>[Your Name Here]</dc:creator><pubDate>Mon, 13 Dec 2010 23:03:18 +0000</pubDate><link>http://www.3dqanda.com/imported-20110111202530/2010/12/13/python-lambda-functions.html</link><guid isPermaLink="false">775630:9094624:10007758</guid><description><![CDATA[<p></p>]]></description><wfw:commentRss>http://www.3dqanda.com/imported-20110111202530/rss-comments-entry-10007758.xml</wfw:commentRss></item><item><title>Looping through a list very quickly in Python</title><category>Programming</category><dc:creator>[Your Name Here]</dc:creator><pubDate>Thu, 02 Dec 2010 21:41:13 +0000</pubDate><link>http://www.3dqanda.com/imported-20110111202530/2010/12/2/looping-through-a-list-very-quickly-in-python.html</link><guid isPermaLink="false">775630:9094624:10007759</guid><description><![CDATA[<p></p>]]></description><wfw:commentRss>http://www.3dqanda.com/imported-20110111202530/rss-comments-entry-10007759.xml</wfw:commentRss></item></channel></rss>
