[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [658] trunk/py/scripts/addons/ space_view3d_materials_utils.py: add trunk/py/scripts/addons/ space_view3d_materials_utils.py

Brendon Murphy meta.androcto1 at gmail.com
Wed May 12 11:07:47 CEST 2010


Revision: 658
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=658
Author:   meta-androcto
Date:     2010-05-12 11:07:46 +0200 (Wed, 12 May 2010)

Log Message:
-----------
add trunk/py/scripts/addons/space_view3d_materials_utils.py
handy script for full screen quick access to materials

Added Paths:
-----------
    trunk/py/scripts/addons/space_view3d_materials_utils.py

Added: trunk/py/scripts/addons/space_view3d_materials_utils.py
===================================================================
--- trunk/py/scripts/addons/space_view3d_materials_utils.py	                        (rev 0)
+++ trunk/py/scripts/addons/space_view3d_materials_utils.py	2010-05-12 09:07:46 UTC (rev 658)
@@ -0,0 +1,665 @@
+#(c) 2010 Michael Williamson (michaelw)
+#ported from original by Michael Williamsn
+#
+#tested r28370
+#
+#
+# ##### 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+bl_addon_info = {
+    'name': '3D View: Material Utils',
+    'author': 'michaelw',
+    'version': '0.9',
+    'blender': (2, 5, 3),
+    'location': 'View3D > Q key',
+    'description': 'Menu of material tools (assign, select by etc)  in the 3D View',
+    'url':
+    'http://wiki.blender.org/index.php/Extensions:2.5/Py/' \
+        'Scripts',  # @todo Create wiki page and fix this link.
+    'category': '3D View'}
+"a menu of material tools"
+
+"""
+Name: 'Master Material Menu'
+Blender: 253
+"""
+
+__author__ = ["michaelW"]
+__version__ = '1.3'
+__url__ = [""]
+__bpydoc__ = """
+this script has several functions and operators... grouped for convenience
+* assign material:
+    offers the user a list of ALL the materials in the blend file and an additional "new" entry
+    the chosen material will be assigned to all the selected objects in object mode.
+    
+    in edit mode the selected faces get the selected material applied.
+
+    if the user chose "new" the new material can be renamed using the "last operator" section of the toolbox
+    After assigning the material "clean material slots" and "material to texface" are auto run to keep things tidy (see description bellow)
+
+
+*select by material
+    in object mode this offers the user a menu of all materials in the blend file
+    any objects using the selected material will become selected, any objects without the material will be removed from selection.
+    
+    in edit mode:  the menu offers only the materials attached to the current object. It will select the faces that use the material and deselect those that do not.
+
+*clean material slots
+    for all selected objects any empty material slots or material slots with materials that are not used by the mesh faces will be removed.
+
+*
+
+
+ Any un-used materials and slots will be removed 
+"""
+
+
+
+import bpy
+from bpy.props import*
+
+
+def replace_material(m1 , m2, all_objects = False):
+    #replace material named m1 with material named m2
+    #m1 is the name of original material
+    #m2 is the name of the material to replace it with
+    #'all' will replace throughout the blend file
+    try:
+        matorg = bpy.data.materials[m1]
+        matrep = bpy.data.materials[m2]
+        
+        
+        #store active object
+        scn = bpy.context.scene
+        ob_active = bpy.context.active_object
+        
+        if all_objects:
+            objs = bpy.data.objects
+        
+        else:
+            objs = bpy.context.selected_editable_objects
+        
+        for ob in objs:
+            if ob.type == 'MESH':
+                scn.objects.active = ob
+                print(ob.name)   
+                ms = ob.material_slots.values()
+            
+                for m in ms:
+                    if m.material == matorg:
+                        m.material = matrep
+                        #don't break the loop as the material can be 
+                        # ref'd more than once
+        
+        #restore active object
+        scn.objects.active = ob_active
+    except:
+        print('no match to replace')
+
+def select_material_by_name(find_mat):
+    #in object mode selects all objects with material find_mat
+    #in edit mode selects all faces with material find_mat
+    
+    #check for editmode
+    editmode = False
+
+    scn = bpy.context.scene
+    actob = bpy.context.active_object
+    if actob.mode == 'EDIT':
+        editmode =True
+        bpy.ops.object.mode_set()
+    
+    
+    if not editmode:
+        objs = bpy.data.objects 
+        for ob in objs:
+            if ob.type == 'MESH':
+                ms = ob.material_slots.values()
+                for m in ms:
+                    if m.material.name == find_mat:
+                        ob.selected = True
+                        #the active object may not have the mat!
+                        #set it to one that does!
+                        scn.objects.active = ob
+                        break
+                    else:
+                        ob.selected = False
+                            
+            #deselect non-meshes                
+            else:
+                ob.selected = False
+    
+    else:
+        #it's editmode, so select the faces
+        ob = actob
+        ms = ob.material_slots.values()
+    
+        #same material can be on multiple slots
+        slot_indeces =[]
+        i = 0
+        found = False
+        for m in ms:
+            if m.material.name == find_mat:
+                slot_indeces.append(i)
+                found = True
+        me = ob.data
+        for f in me.faces:
+            if f.material_index in slot_indeces:
+                f.selected = True
+            else:
+                f.selected = False
+        me.update   
+    if editmode:
+        bpy.ops.object.mode_set(mode = 'EDIT')
+
+def mat_to_texface():
+    #assigns the first image in each material to the faces in the active uvlayer
+    #for all selected objects
+    
+    #check for editmode
+    editmode = False
+    
+    actob = bpy.context.active_object
+    if actob.mode == 'EDIT':
+        editmode =True
+        bpy.ops.object.mode_set()
+    
+    for ob in bpy.context.selected_editable_objects:
+        #get the materials from slots
+        ms = ob.material_slots.values()
+        
+        #build a list of images, one per material
+        images=[]
+        #get the textures from the mats
+        for m in ms:
+            gotimage = False
+            textures = m.material.texture_slots.values()
+            if len(textures) >= 1:
+                for t in textures:
+                    if t != None:
+                        tex = t.texture
+                        if tex.type == 'IMAGE':
+                            img = tex.image
+                            images.append(img)
+                            gotimage =True
+                            break
+    
+            if not gotimage:
+                print('noimage on', m.name)
+                images.append(None)
+    
+        #now we have the images
+        #applythem to the uvlayer
+    
+        
+        me = ob.data
+        #got uvs?
+        if not me.uv_textures:
+            scn = bpy.context.scene
+            scn.objects.active = ob
+            bpy.ops.mesh.uv_texture_add()
+            scn.objects.active = actob
+        
+        #get active uvlayer
+        for t in  me.uv_textures:
+            if t.active:
+                uvtex = t.data.values()
+                for f in me.faces:
+                    #check that material had an image!
+                    if images[f.material_index] != None:
+                        uvtex[f.index].image = images[f.material_index]
+                        uvtex[f.index].tex =True
+                    else:
+                        uvtex[f.index].tex =False
+    
+        me.update
+        
+    
+    if editmode:
+        bpy.ops.object.mode_set(mode = 'EDIT')
+                    
+        
+
+def assignmatslots(ob, matlist):
+    #given an object and a list of material names
+    #removes all material slots form the object
+    #adds new ones for each material in matlist
+    #adds the materials to the slots as well.
+
+    scn = bpy.context.scene
+    ob_active = bpy.context.active_object
+    scn.objects.active = ob
+
+    for s in ob.material_slots:
+        bpy.ops.object.material_slot_remove()
+
+
+    #re-add them and assign material
+    i = 0
+    for m in matlist:
+        mat = bpy.data.materials[m]
+        bpy.ops.object.material_slot_add()
+        ob.material_slots.values()[i].material = mat
+        i += 1
+
+    #restore active object:
+    scn.objects.active = ob_active
+
+
+def cleanmatslots():
+    #check for edit mode
+    editmode = False
+    actob = bpy.context.active_object
+    if actob.mode == 'EDIT':
+        editmode =True
+        bpy.ops.object.mode_set()
+
+
+    objs = bpy.context.selected_editable_objects
+    
+    for ob in objs:
+        print(ob.name)    
+        mats = ob.material_slots.keys()
+    
+        #check the faces on the mesh to build a list of used materials
+        usedMatIndex =[]        #we'll store used materials indices here
+        faceMats =[]    
+        me = ob.data
+        for f in me.faces:
+            #get the material index for this face...
+            faceindex = f.material_index
+                    
+            #indices will be lost: Store face mat use by name
+            currentfacemat = mats[faceindex]
+            faceMats.append(currentfacemat)
+                    
+                    
+            #check if index is already listed as used or not
+            found = 0
+            for m in usedMatIndex:
+                if m == faceindex:
+                    found = 1
+                    #break
+                        
+            if found == 0:
+            #add this index to the list     
+                usedMatIndex.append(faceindex)
+    
+        #re-assign the used mats to the mesh and leave out the unused
+        ml = []
+        mnames = []
+        for u in usedMatIndex:
+            ml.append( mats[u] )
+            #we'll need a list of names to get the face indices...
+            mnames.append(mats[u])
+                    
+        assignmatslots(ob, ml)

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list