[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4524] trunk/py/scripts/addons/ space_view3d_materials_utils.py: Material Utils: Added feature to set fake user (on/off) for materials as requested by meta-androcto.

Sebastian Nell codemanx at gmx.de
Wed May 15 01:07:33 CEST 2013


Revision: 4524
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4524
Author:   codemanx
Date:     2013-05-14 23:07:33 +0000 (Tue, 14 May 2013)
Log Message:
-----------
Material Utils: Added feature to set fake user (on/off) for materials as requested by meta-androcto. There are different modes: ACTIVE, SELECTED, SCENE (materials of objects), USED, UNUSED, ALL (materials).

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

Modified: trunk/py/scripts/addons/space_view3d_materials_utils.py
===================================================================
--- trunk/py/scripts/addons/space_view3d_materials_utils.py	2013-05-14 14:53:55 UTC (rev 4523)
+++ trunk/py/scripts/addons/space_view3d_materials_utils.py	2013-05-14 23:07:33 UTC (rev 4524)
@@ -23,7 +23,7 @@
 bl_info = {
     "name": "Material Utils",
     "author": "michaelw",
-    "version": (1, 5),
+    "version": (1, 6),
     "blender": (2, 66, 6),
     "location": "View3D > Q key",
     "description": "Menu of material tools (assign, select..)  in the 3D View",
@@ -82,13 +82,52 @@
     option allows you to update object selection, to indicate which objects
     were affected and which not.
 
+* set fake user
+    enable/disable fake user for materials. You can chose for which materials
+    it shall be set, materials of active / selected / objects in current scene
+    or used / unused / all materials.
+
 """
 
 
 import bpy
-from bpy.props import StringProperty, BoolProperty
+from bpy.props import StringProperty, BoolProperty, EnumProperty
 
 
+def fake_user_set(fake_user='ON', materials='UNUSED'):
+    if materials == 'ALL':
+        mats = (mat for mat in bpy.data.materials if mat.library is None)
+    elif materials == 'UNUSED':
+        mats = (mat for mat in bpy.data.materials if mat.library is None and mat.users == 0)
+    else:
+        mats = []
+        if materials == 'ACTIVE':
+            objs = [bpy.context.active_object]
+        elif materials == 'SELECTED':
+            objs = bpy.context.selected_objects
+        elif materials == 'SCENE':
+            objs = bpy.context.scene.objects
+        else: # materials == 'USED'
+            objs = bpy.data.objects
+            # Maybe check for users > 0 instead?
+
+        """ more reable than the following generator:
+        for ob in objs:
+            if hasattr(ob.data, "materials"):
+                for mat in ob.data.materials:
+                    if mat.library is None: #and not in mats:
+                        mats.append(mat)
+        """
+        mats = (mat for ob in objs if hasattr(ob.data, "materials") for mat in ob.data.materials if mat.library is None)
+
+    for mat in mats:
+        mat.use_fake_user = fake_user == 'ON'
+
+    for area in bpy.context.screen.areas:
+        if area.type in ('PROPERTIES', 'NODE_EDITOR'):
+            area.tag_redraw()
+
+
 def replace_material(m1, m2, all_objects=False, update_selection=False):
     # replace material named m1 with material named m2
     # m1 is the name of original material
@@ -676,6 +715,44 @@
         return {'FINISHED'}
 
 
+class VIEW3D_OT_fake_user_set(bpy.types.Operator):
+    """Enable/disable fake user for materials"""
+    bl_idname = "view3d.fake_user_set"
+    bl_label = "Set Fake User (Material Utils)"
+    bl_options = {'REGISTER', 'UNDO'}
+
+    fake_user = EnumProperty(
+            name="Fake User",
+            description="Turn fake user on or off",
+            items=(('ON', "On", "Enable fake user"),('OFF', "Off", "Disable fake user")),
+            default='ON'
+            )
+
+    materials = EnumProperty(
+            name="Materials",
+            description="Which materials of objects to affect",
+            items=(('ACTIVE', "Active object", "Materials of active object only"),
+                   ('SELECTED', "Selected objects", "Materials of selected objects"),
+                   ('SCENE', "Scene objects", "Materials of objects in current scene"),
+                   ('USED', "Used", "All materials used by objects"),
+                   ('UNUSED', "Unused", "Currently unused materials"),
+                   ('ALL', "All", "All materials in this blend file")),
+            default='UNUSED'
+            )
+
+    def draw(self, context):
+        layout = self.layout
+        layout.prop(self, "fake_user", expand=True)
+        layout.prop(self, "materials")
+
+    def invoke(self, context, event):
+        return context.window_manager.invoke_props_dialog(self)
+
+    def execute(self, context):
+        fake_user_set(self.fake_user, self.materials)
+        return {'FINISHED'}
+
+
 # -----------------------------------------------------------------------------
 # menu classes
 
@@ -707,7 +784,11 @@
                         text='Replace Material',
                         icon='ARROW_LEFTRIGHT')
 
+        layout.operator("view3d.fake_user_set",
+                        text='Set Fake User',
+                        icon='UNPINNED')
 
+
 class VIEW3D_MT_assign_material(bpy.types.Menu):
     bl_label = "Assign Material"
 



More information about the Bf-extensions-cvs mailing list