#!BPY
"""
Name: 'Make Particle Objects Real'
Blender: 247svn
Group: 'Object'
Tooltip: 'Convert particle objects to mesh objects'
"""
__author__= "Sanne"
__url__= ["blenderartists.org"]
__version__= "0.1"

__bpydoc__= """
"""

# --------------------------------------------------------------------------
# Make Particle Objects Real v0.1 by Sanne
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------

import Blender

def makereal(ob, parts, scn, makelinks):
    """ Make particle objects real """
    
    for part in parts:
        if part.type != Blender.Particle.TYPE.EMITTER:
            continue
        if part.drawAs == Blender.Particle.DRAWAS.OBJECT:
            dupob = part.duplicateObject
            dupdata = dupob.getData(mesh =True)
            
            # get location, rotation and sizes of all particle objects
            plocs = part.getLoc()
            prots = part.getRot()
            psizes = part.getSize()
            
            grp = Blender.Group.New()
            
            numparts = len(plocs) # using part.amount sometimes gives list index out of range error
            for i in range(numparts):
                if makelinks:
                    newob = scn.objects.new(dupdata)
                else:
                    newob = scn.objects.new(dupdata.copy())
                newob.loc = [x + y for x, y in zip(plocs[i], dupob.loc)]
                newob.size = (psizes[i], psizes[i], psizes[i])
                newob.rot = Blender.Mathutils.Quaternion(prots[i]).toEuler()
                newob.modifiers = dupob.modifiers
                grp.objects.link(newob)
                
        elif part.drawAs == Blender.Particle.DRAWAS.GROUP:
            # no access to the group name yet
            pass
    
    ob.sel = False

def main():
    """GUI function"""
    
    scn = Blender.Scene.GetCurrent()
    ob = scn.objects.active
    if not ob:
        Blender.Draw.PupMenu("No or wrong selection %t | Please select an object.")
        print "No or wrong selection\nPlease select an object."
        return
    
    parts = ob.getParticleSystems()
    if len(parts) == 0:
        Blender.Draw.PupMenu("No or wrong selection %t | Active object has no particle system.")
        print "No or wrong selection\nActive object has no particle system."
        return
    
    # Can't delete/unlink particle system with Python (yet?)
    #PREF_KEEPPARTSYS = Blender.Draw.Create(1)
    PREF_MAKELINKS = Blender.Draw.Create(1)
    
    # list for popup content    
    block = []
    
    # inputs: title, button, min/max values, tooltip
    #block.append(("Keep Particles ", PREF_KEEPPARTSYS, "Keep particle system"))
    block.append(("Use Linked Data ", PREF_MAKELINKS, "Use linked data for newly generated objects"))
    
    if not Blender.Draw.PupBlock("Make Particle Objects Real", block):
        return
    
    makereal(ob, parts, scn, PREF_MAKELINKS.val)
    Blender.Window.RedrawAll()

# ------------------------
if __name__ == "__main__":
    main()
