Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- from bpy.props import BoolProperty
- class SeqModalTimerOperator(bpy.types.Operator):
- """Operator which runs its self from a timer"""
- bl_idname = "wm.modal_timer_operator"
- bl_label = "Default Material Timer Operator"
- _timer = None
- mat_count = len(bpy.data.materials)
- def modal(self, context, event):
- scene = context.scene
- #if event.type in {'ESC'} or not scene.default_material_switch:
- if not scene.default_material_switch:
- self.cancel(context)
- return {'CANCELLED'}
- if event.type == 'TIMER':
- if len(bpy.data.materials) > self.mat_count:
- self.mat_count = len(bpy.data.materials)
- self.switch_nodes()
- return {'PASS_THROUGH'}
- def switch_nodes(self):
- #get active object's active material
- node_tree = bpy.context.active_object.active_material.node_tree
- #save the location of the diffuse node
- location = node_tree.nodes['Diffuse BSDF'].location.copy()
- #delete that node
- node_tree.nodes.remove(node_tree.nodes['Diffuse BSDF'])
- #add new Principled node
- principled = node_tree.nodes.new('ShaderNodeBsdfPrincipled')
- # move it to where the diffuse node used to be
- principled.location = location
- #make the connection
- node_tree.links.new(principled.outputs['BSDF'], node_tree.nodes['Material Output'].inputs['Surface'])
- def execute(self, context):
- wm = context.window_manager
- self._timer = wm.event_timer_add(0.1, context.window)
- wm.modal_handler_add(self)
- return {'RUNNING_MODAL'}
- def cancel(self, context):
- scene = context.scene
- wm = context.window_manager
- wm.event_timer_remove(self._timer)
- scene.default_material_switch = False
- print("modal ended", scene.default_material_switch)
- def trigger_timmer(self, context):
- if self.default_material_switch:
- bpy.ops.wm.modal_timer_operator()
- class DMPanel(bpy.types.Panel):
- """Creates a Panel in the Properties window"""
- bl_label = "Default Material Switcher"
- bl_idname = "DM_control"
- bl_space_type = 'PROPERTIES'
- bl_region_type = 'WINDOW'
- bl_context = "material"
- def draw(self, context):
- scene = context.scene
- if scene.default_material_switch:
- text = "Stop Default Material"
- icon = "PAUSE"
- else:
- text = "Start Default Material"
- icon = "PLAY"
- scene = context.scene
- layout = self.layout
- row = layout.row()
- row.prop(scene, "default_material_switch", text=text, icon=icon, toggle=True)
- def register():
- bpy.utils.register_class(DMPanel)
- bpy.utils.register_class(SeqModalTimerOperator)
- bpy.types.Scene.default_material_switch = BoolProperty(default=False, name="Default Material Boolean", description="Turn on/off default material switcher", update=trigger_timmer)
- def unregister():
- bpy.utils.unregister_class(SeqModalTimerOperator)
- bpy.utils.unregister_class(DMPanel)
- if __name__ == "__main__":
- register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement