Advertisement
Guest User

default material switcher

a guest
Jan 31st, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. import bpy
  2. from bpy.props import BoolProperty
  3.  
  4. class SeqModalTimerOperator(bpy.types.Operator):
  5.     """Operator which runs its self from a timer"""
  6.     bl_idname = "wm.modal_timer_operator"
  7.     bl_label = "Default Material Timer Operator"
  8.  
  9.     _timer = None
  10.     mat_count = len(bpy.data.materials)
  11.  
  12.     def modal(self, context, event):
  13.         scene = context.scene
  14.  
  15.         #if event.type in {'ESC'} or not scene.default_material_switch:
  16.         if not scene.default_material_switch:
  17.             self.cancel(context)
  18.             return {'CANCELLED'}
  19.  
  20.         if event.type == 'TIMER':
  21.             if len(bpy.data.materials) > self.mat_count:
  22.                 self.mat_count = len(bpy.data.materials)
  23.                 self.switch_nodes()
  24.  
  25.         return {'PASS_THROUGH'}
  26.    
  27.     def switch_nodes(self):
  28.         #get active object's active material
  29.         node_tree = bpy.context.active_object.active_material.node_tree
  30.         #save the location of the diffuse node
  31.         location = node_tree.nodes['Diffuse BSDF'].location.copy()
  32.         #delete that node
  33.         node_tree.nodes.remove(node_tree.nodes['Diffuse BSDF'])
  34.         #add new Principled node
  35.         principled = node_tree.nodes.new('ShaderNodeBsdfPrincipled')
  36.         # move it to where the diffuse node used to be
  37.         principled.location = location
  38.         #make the connection
  39.         node_tree.links.new(principled.outputs['BSDF'], node_tree.nodes['Material Output'].inputs['Surface'])
  40.  
  41.     def execute(self, context):
  42.         wm = context.window_manager
  43.         self._timer = wm.event_timer_add(0.1, context.window)
  44.         wm.modal_handler_add(self)
  45.         return {'RUNNING_MODAL'}
  46.  
  47.     def cancel(self, context):
  48.         scene = context.scene
  49.         wm = context.window_manager
  50.         wm.event_timer_remove(self._timer)
  51.         scene.default_material_switch = False
  52.         print("modal ended", scene.default_material_switch)
  53.  
  54. def trigger_timmer(self, context):
  55.     if self.default_material_switch:
  56.         bpy.ops.wm.modal_timer_operator()
  57.  
  58.  
  59. class DMPanel(bpy.types.Panel):
  60.     """Creates a Panel in the Properties window"""
  61.     bl_label = "Default Material Switcher"
  62.     bl_idname = "DM_control"
  63.     bl_space_type = 'PROPERTIES'
  64.     bl_region_type = 'WINDOW'
  65.     bl_context = "material"
  66.  
  67.     def draw(self, context):
  68.         scene = context.scene
  69.         if scene.default_material_switch:
  70.             text = "Stop   Default Material"
  71.             icon = "PAUSE"
  72.         else:
  73.             text = "Start   Default Material"
  74.             icon = "PLAY"
  75.         scene = context.scene
  76.         layout = self.layout
  77.  
  78.         row = layout.row()
  79.         row.prop(scene, "default_material_switch", text=text, icon=icon, toggle=True)
  80.  
  81.  
  82. def register():
  83.     bpy.utils.register_class(DMPanel)
  84.     bpy.utils.register_class(SeqModalTimerOperator)
  85.     bpy.types.Scene.default_material_switch = BoolProperty(default=False, name="Default Material Boolean", description="Turn on/off default material switcher", update=trigger_timmer)
  86.  
  87.  
  88. def unregister():
  89.     bpy.utils.unregister_class(SeqModalTimerOperator)
  90.     bpy.utils.unregister_class(DMPanel)
  91.  
  92.  
  93. if __name__ == "__main__":
  94. register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement