diff options
author | Eugenio Pignataro <info@oscurart.com.ar> | 2019-03-06 16:08:27 +0300 |
---|---|---|
committer | Eugenio Pignataro <info@oscurart.com.ar> | 2019-03-06 16:08:27 +0300 |
commit | d388555314a51e77474253058ac6fc736061eb98 (patch) | |
tree | e7497795e72f6c3c84b009266dbf69f8b12d1f15 | |
parent | c75787ff5fee88a2fc3c591da23b9977abf964a9 (diff) |
add material overrides
-rw-r--r-- | oscurart_tools/__init__.py | 14 | ||||
-rw-r--r-- | oscurart_tools/render/material_overrides.py | 168 |
2 files changed, 180 insertions, 2 deletions
diff --git a/oscurart_tools/__init__.py b/oscurart_tools/__init__.py index 383d5eaa..f3a162e8 100644 --- a/oscurart_tools/__init__.py +++ b/oscurart_tools/__init__.py @@ -49,6 +49,7 @@ from oscurart_tools.object import search_and_select from oscurart_tools.mesh import apply_linked_meshes from oscurart_tools.render import render_tokens from oscurart_tools.render import batch_maker +from oscurart_tools.render import material_overrides from bpy.types import ( @@ -149,7 +150,14 @@ classes = ( apply_linked_meshes.ApplyLRT, batch_maker.oscBatchMaker, remove_modifiers.RemoveModifiers, - vertex_color_id.createVCMask + vertex_color_id.createVCMask, + material_overrides.OscOverridesGUI, + material_overrides.OscTransferOverrides, + material_overrides.OscAddOverridesSlot, + material_overrides.OscRemoveOverridesSlot, + material_overrides.OscOverridesUp, + material_overrides.OscOverridesDown, + material_overrides.OscOverridesKill ) def register(): @@ -161,8 +169,10 @@ def register(): bpy.app.handlers.render_pre.append(render_tokens.replaceTokens) bpy.app.handlers.render_cancel.append(render_tokens.restoreTokens) bpy.app.handlers.render_post.append(render_tokens.restoreTokens) + bpy.app.handlers.render_pre.append(material_overrides.ApplyOverrides) + bpy.app.handlers.render_cancel.append(material_overrides.RestoreOverrides) + bpy.app.handlers.render_post.append(material_overrides.RestoreOverrides) - from bpy.utils import register_class for cls in classes: register_class(cls) diff --git a/oscurart_tools/render/material_overrides.py b/oscurart_tools/render/material_overrides.py new file mode 100644 index 00000000..f62e92d6 --- /dev/null +++ b/oscurart_tools/render/material_overrides.py @@ -0,0 +1,168 @@ +# ------------------------------------------------ + + +obDict = [] +import bpy +from bpy.app.handlers import persistent + + +@persistent +def ApplyOverrides(dummy): + global obDict + for override in bpy.context.scene.ovlist: + for ob in bpy.data.collections[override.grooverride].objects: + obMss = {} + for i,ms in enumerate(ob.material_slots): + obMss[i] = ms.material + ms.material = bpy.data.materials[override.matoverride] + obDict.append([ob,obMss]) + +@persistent +def RestoreOverrides(dummy): + global obDict + for ob in obDict: + for ms,material in ob[1].items(): + ob[0].material_slots[ms].material = material + + +# --------------------------------------------------- + + + +class OscOverridesProp(bpy.types.PropertyGroup): + matoverride = bpy.props.StringProperty() + grooverride = bpy.props.StringProperty() + +bpy.utils.register_class(OscOverridesProp) +bpy.types.Scene.ovlist = bpy.props.CollectionProperty(type=OscOverridesProp) + + +class OscOverridesGUI(bpy.types.Panel): + bl_label = "Oscurart Material Overrides" + bl_idname = "Oscurart Overrides List" + bl_space_type = "PROPERTIES" + bl_region_type = "WINDOW" + bl_context = "render" + bl_options = {'DEFAULT_CLOSED'} + + def draw(self, context): + + layout = self.layout + col = layout.column(align=1) + colrow = col.row(align=1) + colrow.operator("render.overrides_add_slot") + colrow.operator("render.overrides_remove_slot") + col.operator("render.overrides_transfer") + for i, m in enumerate(bpy.context.scene.ovlist): + colrow = col.row(align=1) + colrow.prop_search(m, "grooverride", bpy.data, "collections", text="") + colrow.prop_search( + m, + "matoverride", + bpy.data, + "materials", + text="") + if i != len(bpy.context.scene.ovlist) - 1: + pa = colrow.operator( + "ovlist.move_down", + text="", + icon="TRIA_DOWN") + pa.index = i + if i > 0: + p = colrow.operator("ovlist.move_up", text="", icon="TRIA_UP") + p.index = i + pb = colrow.operator("ovlist.kill", text="", icon="X") + pb.index = i + + +class OscTransferOverrides(bpy.types.Operator): + """Applies the previously configured slots (Groups < Material) to the Scene. """ \ + """This should be transfer once the override groups are set""" + bl_idname = "render.overrides_transfer" + bl_label = "Transfer Overrides" + + def execute(self, context): + # CREO LISTA + OSCOV = [[OVERRIDE.grooverride, OVERRIDE.matoverride] + for OVERRIDE in bpy.context.scene.ovlist[:] + if OVERRIDE.matoverride != "" and OVERRIDE.grooverride != ""] + + bpy.context.scene.oscurart.overrides = str(OSCOV) + return {'FINISHED'} + + +class OscAddOverridesSlot(bpy.types.Operator): + """Add override slot""" + bl_idname = "render.overrides_add_slot" + bl_label = "Add Override Slot" + + def execute(self, context): + prop = bpy.context.scene.ovlist.add() + prop.matoverride = "" + prop.grooverride = "" + return {'FINISHED'} + + +class OscRemoveOverridesSlot(bpy.types.Operator): + """Remove override slot""" + bl_idname = "render.overrides_remove_slot" + bl_label = "Remove Override Slot" + + def execute(self, context): + context.scene.ovlist.remove(len(bpy.context.scene.ovlist) - 1) + return {'FINISHED'} + +class OscOverridesUp(bpy.types.Operator): + """Move override slot up""" + bl_idname = 'ovlist.move_up' + bl_label = 'Move Override up' + bl_options = {'INTERNAL'} + + index = bpy.props.IntProperty(min=0) + + @classmethod + def poll(self, context): + return len(context.scene.ovlist) + + def execute(self, context): + ovlist = context.scene.ovlist + ovlist.move(self.index, self.index - 1) + + return {'FINISHED'} + + +class OscOverridesDown(bpy.types.Operator): + """Move override slot down""" + bl_idname = 'ovlist.move_down' + bl_label = 'Move Override down' + bl_options = {'INTERNAL'} + + index = bpy.props.IntProperty(min=0) + + @classmethod + def poll(self, context): + return len(context.scene.ovlist) + + def execute(self, context): + ovlist = context.scene.ovlist + ovlist.move(self.index, self.index + 1) + return {'FINISHED'} + + +class OscOverridesKill(bpy.types.Operator): + """Remove override slot""" + bl_idname = 'ovlist.kill' + bl_label = 'Kill Override' + bl_options = {'INTERNAL'} + + index = bpy.props.IntProperty(min=0) + + @classmethod + def poll(self, context): + return len(context.scene.ovlist) + + def execute(self, context): + ovlist = context.scene.ovlist + ovlist.remove(self.index) + return {'FINISHED'} + |