Skip to content

Commit

Permalink
Merge pull request #6 from neverhood311/Batch-shading
Browse files Browse the repository at this point in the history
Batch shading
  • Loading branch information
neverhood311 authored Sep 15, 2016
2 parents c1188b4 + 739ee71 commit 38a66f6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ PayPal: https://www.paypal.me/justinj
- 'Extend' will freeze the first and last frames before and after the mesh sequence, respectively
- 'Repeat' will repeat the animation
- 'Bounce' will play the animation in reverse once the sequence has finished
- Once your sequence is loaded, you can change the shading (smooth or flat) of the entire sequence:
- The shading buttons are found in the Mesh Sequence Settings for the object.
- Note: The normal shading buttons (in the 3D View "Tools" panel) will only affect the current frame, not the entire sequence.
64 changes: 62 additions & 2 deletions mesh_sequence_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def deselectAll():
ob.select = False

#set the frame number for all mesh sequence objects
#COMMENT THIS persistent OUT WHEN RUNNING FROM THE TEXT EDITOR
@persistent
def updateFrame(dummy):
scn = bpy.context.scene
Expand Down Expand Up @@ -288,6 +289,30 @@ def setFrameObj(self, _obj, _frameNum):
_obj.data.materials.clear()
_obj.data.materials.append(prev_material)

#iterate over the meshes in the sequence and set their shading to smooth or flat
def shadeSequence(self, _obj, _smooth):
scn = bpy.context.scene
#deselect everything in the scene
deselectAll()
#select the sequence object
scn.objects.active = _obj
_obj.select = True
#grab the current mesh so we can put it back later
origMesh = _obj.data
#for each mesh in the sequence
for idx in range(1, _obj.mesh_sequence_settings.numMeshes):
#set the object's mesh to this mesh
_obj.data = self.getMesh(_obj, idx)
if(_smooth):
#call Blender's shade_smooth operator
bpy.ops.object.shade_smooth()
else:
#call Blender's shade_flat operator
bpy.ops.object.shade_flat()
#reset the sequence's mesh to the right one based on the current frame
_obj.data = origMesh


#create a separate object for each mesh in the array, each visible for only one frame
def bakeSequence(self, _obj):
scn = bpy.context.scene
Expand Down Expand Up @@ -408,6 +433,7 @@ def freeUnusedMeshes(self):
#the remaining meshes with no real or fake users will be garbage collected when Blender is closed
print(numFreed, " meshes freed")

#COMMENT THIS persistent OUT WHEN RUNNING FROM THE TEXT EDITOR
@persistent
def initSequenceController(dummy): #apparently we need a dummy variable?
#print("initSequenceController was just called")
Expand Down Expand Up @@ -460,6 +486,30 @@ def execute(self, context):
#print("MSO object name: " + MSO.seqObject.name)
return {'FINISHED'}

class BatchShadeSmooth(bpy.types.Operator):
"""Smooth Shade Sequence"""
bl_idname = "ms.batch_shade_smooth"
bl_label = "Smooth"
bl_options = {'UNDO'}

def execute(self, context):
global MSC
obj = context.object
MSC.shadeSequence(obj, True) #True for smooth
return {'FINISHED'}

class BatchShadeFlat(bpy.types.Operator):
"""Flat Shade Sequence"""
bl_idname = "ms.batch_shade_flat"
bl_label = "Flat"
bl_options = {'UNDO'}

def execute(self, context):
global MSC
obj = context.object
MSC.shadeSequence(obj, False) #False for flat
return {'FINISHED'}

class BakeMeshSequence(bpy.types.Operator):
"""Bake Mesh Sequence"""
bl_idname = "ms.bake_sequence"
Expand Down Expand Up @@ -508,8 +558,6 @@ def draw(self, context):
row = layout.row()
row.operator("ms.load_mesh_sequence")



#start frame
row = layout.row()
row.prop(objSettings, "startFrame")
Expand All @@ -522,6 +570,13 @@ def draw(self, context):
row = layout.row()
row.prop(objSettings, "speed")

#Show the shading buttons only if a sequence has been loaded
if(objSettings.loaded == True):
layout.row().separator()
row = layout.row(align=True)
row.label("Shading:")
row.operator("ms.batch_shade_smooth")
row.operator("ms.batch_shade_flat")
#Show the Bake Sequence button only if a sequence has been loaded
if(objSettings.loaded == True):
layout.row().separator()
Expand All @@ -541,10 +596,13 @@ def register():
bpy.app.handlers.frame_change_pre.append(updateFrame)
bpy.utils.register_class(AddMeshSequence)
bpy.utils.register_class(LoadMeshSequence)
bpy.utils.register_class(BatchShadeSmooth)
bpy.utils.register_class(BatchShadeFlat)
bpy.utils.register_class(BakeMeshSequence)
bpy.utils.register_class(MeshSequencePanel)
bpy.types.INFO_MT_mesh_add.append(menu_func)
#for running the script, instead of installing the add-on
#UNCOMMENT THIS FUNCTION CALL WHEN RUNNING FROM THE TEXT EDITOR
#initSequenceController(0)

def unregister():
Expand All @@ -553,6 +611,8 @@ def unregister():
bpy.app.handlers.frame_change_pre.remove(updateFrame)
bpy.utils.unregister_class(AddMeshSequence)
bpy.utils.unregister_class(LoadMeshSequence)
bpy.utils.unregister_class(BatchShadeSmooth)
bpy.utils.unregister_class(BatchShadeFlat)
bpy.utils.unregister_class(BakeMeshSequence)
bpy.utils.unregister_class(MeshSequencePanel)
bpy.types.INFO_MT_mesh_add.remove(menu_func)
Expand Down

0 comments on commit 38a66f6

Please sign in to comment.