<?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:48 GMT--><feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><title>Home</title><subtitle>Home</subtitle><id>http://www.3dqanda.com/imported-20110111202530/</id><link rel="alternate" type="application/xhtml+xml" href="http://www.3dqanda.com/imported-20110111202530/"/><link rel="self" type="application/atom+xml" href="http://www.3dqanda.com/imported-20110111202530/atom.xml"/><updated>2012-02-24T04:15:14Z</updated><generator uri="http://www.squarespace.com/" version="Squarespace Site Server v5.11.81 (http://www.squarespace.com/)">Squarespace</generator><entry><title>PyQt and Drag and Drop functionality</title><category term="Programming"/><category term="Python"/><id>http://www.3dqanda.com/imported-20110111202530/2012/2/23/pyqt-and-drag-and-drop-functionality.html</id><link rel="alternate" type="text/html" href="http://www.3dqanda.com/imported-20110111202530/2012/2/23/pyqt-and-drag-and-drop-functionality.html"/><author><name>[Your Name Here]</name></author><published>2012-02-24T03:03:29Z</published><updated>2012-02-24T03:03:29Z</updated><content type="html" xml:lang="en-US"><![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>]]></content></entry><entry><title>Dynamically adding methods to a class in Python</title><category term="Programming"/><category term="Python"/><id>http://www.3dqanda.com/imported-20110111202530/2011/12/12/dynamically-adding-methods-to-a-class-in-python.html</id><link rel="alternate" type="text/html" href="http://www.3dqanda.com/imported-20110111202530/2011/12/12/dynamically-adding-methods-to-a-class-in-python.html"/><author><name>[Your Name Here]</name></author><published>2011-12-12T22:29:42Z</published><updated>2011-12-12T22:29:42Z</updated><content type="html" xml:lang="en-US"><![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>]]></content></entry><entry><title>Python @classmethod</title><category term="Programming"/><category term="classes object oriented programming"/><id>http://www.3dqanda.com/imported-20110111202530/2011/8/2/python-classmethod.html</id><link rel="alternate" type="text/html" href="http://www.3dqanda.com/imported-20110111202530/2011/8/2/python-classmethod.html"/><author><name>[Your Name Here]</name></author><published>2011-08-02T15:51:52Z</published><updated>2011-08-02T15:51:52Z</updated><content type="html" xml:lang="en-US"><![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>]]></content></entry><entry><title>Python .net: Bringing C# into Python and Maya</title><category term="Programming"/><id>http://www.3dqanda.com/imported-20110111202530/2011/5/22/python-net-bringing-c-into-python-and-maya.html</id><link rel="alternate" type="text/html" href="http://www.3dqanda.com/imported-20110111202530/2011/5/22/python-net-bringing-c-into-python-and-maya.html"/><author><name>[Your Name Here]</name></author><published>2011-05-22T23:43:16Z</published><updated>2011-05-22T23:43:16Z</updated><content type="html" xml:lang="en-US"><![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>]]></content></entry><entry><title>Namespaces</title><category term="Programming"/><id>http://www.3dqanda.com/imported-20110111202530/2011/3/4/namespaces.html</id><link rel="alternate" type="text/html" href="http://www.3dqanda.com/imported-20110111202530/2011/3/4/namespaces.html"/><author><name>[Your Name Here]</name></author><published>2011-03-04T05:04:48Z</published><updated>2011-03-04T05:04:48Z</updated><content type="html" xml:lang="en-US"><![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>]]></content></entry><entry><title>Intro Pymel Video</title><category term="Programming"/><id>http://www.3dqanda.com/imported-20110111202530/2011/1/28/intro-pymel-video.html</id><link rel="alternate" type="text/html" href="http://www.3dqanda.com/imported-20110111202530/2011/1/28/intro-pymel-video.html"/><author><name>[Your Name Here]</name></author><published>2011-01-29T04:15:48Z</published><updated>2011-01-29T04:15:48Z</updated><content type="html" xml:lang="en-US"><![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>]]></content></entry><entry><title>Inserting an object into a python list in the correct order</title><category term="Programming"/><id>http://www.3dqanda.com/imported-20110111202530/2011/1/8/inserting-an-object-into-a-python-list-in-the-correct-order.html</id><link rel="alternate" type="text/html" href="http://www.3dqanda.com/imported-20110111202530/2011/1/8/inserting-an-object-into-a-python-list-in-the-correct-order.html"/><author><name>[Your Name Here]</name></author><published>2011-01-08T21:33:20Z</published><updated>2011-01-08T21:33:20Z</updated><summary type="html" xml:lang="en-US"><![CDATA[<p></p>]]></summary></entry><entry><title>Eye material painting in photoshop</title><category term="Art"/><id>http://www.3dqanda.com/imported-20110111202530/2010/12/16/eye-material-painting-in-photoshop.html</id><link rel="alternate" type="text/html" href="http://www.3dqanda.com/imported-20110111202530/2010/12/16/eye-material-painting-in-photoshop.html"/><author><name>[Your Name Here]</name></author><published>2010-12-16T11:16:02Z</published><updated>2010-12-16T11:16:02Z</updated><summary type="html" xml:lang="en-US"><![CDATA[<p></p>]]></summary></entry><entry><title>Python lambda functions</title><category term="Programming"/><id>http://www.3dqanda.com/imported-20110111202530/2010/12/13/python-lambda-functions.html</id><link rel="alternate" type="text/html" href="http://www.3dqanda.com/imported-20110111202530/2010/12/13/python-lambda-functions.html"/><author><name>[Your Name Here]</name></author><published>2010-12-13T23:03:18Z</published><updated>2010-12-13T23:03:18Z</updated><summary type="html" xml:lang="en-US"><![CDATA[<p></p>]]></summary></entry><entry><title>Looping through a list very quickly in Python</title><category term="Programming"/><id>http://www.3dqanda.com/imported-20110111202530/2010/12/2/looping-through-a-list-very-quickly-in-python.html</id><link rel="alternate" type="text/html" href="http://www.3dqanda.com/imported-20110111202530/2010/12/2/looping-through-a-list-very-quickly-in-python.html"/><author><name>[Your Name Here]</name></author><published>2010-12-02T21:41:13Z</published><updated>2010-12-02T21:41:13Z</updated><summary type="html" xml:lang="en-US"><![CDATA[<p></p>]]></summary></entry></feed>
