Is there any way to use code to revert back to an original mesh? I am currently manipulating and scaling measurements of a mesh through various iterations (representing different individual animals). The dataset is very large so I created loops so only one function is called to run through all of my data. However, after each individual iteration the mesh needs to be reset to the original before the next measurements are manipulated. Is there any way to do this.
I've pasted my code below (I am only using 8 whales in this sample set to see if the code works before entering my large dataset). I expect the "undo" statement would fit in beneath the print statement in line 32. Thank you in advance!
import bpy
import bmesh
import numpy as np
obj = bpy.context.active_object
totallength = [1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9] #scaling factors for the length of each whale
whale = [0,1,2,3,4,5,6,7] #creates a placeholder for each individual whale for the code below
vgrp = ['Reye','Reyepecmid', 'Rpecant', 'Rpecpost', 'Rdslfinant', 'Rdslfnmid', 'Rdslfnpost', 'Rmidkeel', 'Rkeel', 'Ranus', 'Rmidkeelanus', 'Rpostanus', 'Rend'] #names of vertex groups being manipulated
sfac = np.arange(1,11.4,.1) #list of all scaling factors for vertex group and whale (the first 13 numbers correspond to the 13 vertex groups of the first whale)
whalesDims = np.zeros((8,2)) #a temporary array of all the whale surface areas and volumes
def scale(vgrp,sfac): #scale vertex group of name vgrp based on scaling factor of name sfac
bpy.ops.object.mode_set(mode='EDIT') #set program in edit mode
bpy.ops.mesh.select_all(action='DESELECT') #deselect any previously selected vertex groups
bpy.ops.object.vertex_group_set_active(group=vgrp) #set vertex group active based on name in vgrp
bpy.ops.object.vertex_group_select() #select active vertex group
bpy.ops.transform.resize(value=(sfac,sfac,sfac)) #resize vertex group by sfac
bpy.ops.object.mode_set(mode='OBJECT') #set program in object mode
def scale_whale(a,whale,vgrp,sfac):
bpy.ops.object.mode_set(mode='OBJECT') #set program in object mode
for w in whale:
bpy.ops.transform.resize(value=(1,totallength[w],1)) #resizes the length of the whale (only the y axis) based on an index in total length corresponding to the index in whale
for v in vgrp:
scale(v,sfac[vgrp.index(v)])
if sfac[vgrp.index(v)] == 12:
sfac = np.delete(sfac,[0,1,2,3,4,5,6,7,8,9,10,11,12]) #delete first 13 numbers in sfac so next iteration will move on to the next whale
bm=bmesh.new() #needed to calculate volume and area
bm.from_object(bpy.context.object,bpy.context.scene) #needed to calculate volume and area
volume = bm.calc_volume() #volume of whale
area = sum(f.calc_area() for f in bm.faces) #surface area of whale
print ('whale',w,'surface area = ',area, 'volume = ',volume) #print surface area and volume with each whale
whalesDims[w, :] = (area,volume) #add the area and volume to an array
print(whalesDims) #print out the list of whale areas and volumes
return(whalesDims) #return the list

copy = obj.data.copy()and swap it in and discard after, for each iteration. Or I think you could do this all in bmesh and just about avoidbpy.opsaltogether. Can you provide a sample whale? – batFINGER Oct 11 '17 at 05:17