1

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

batFINGER
  • 84,216
  • 10
  • 108
  • 233
user33993
  • 75
  • 1
  • 10
  • 1
    Could get very slow with large sample size Rather than undo could copy original mesh 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 avoid bpy.ops altogether. Can you provide a sample whale? – batFINGER Oct 11 '17 at 05:17
  • @batFINGER I think that could work! I'd be happy to provide a sample whale, but how would I upload the file? I'm very new to Blender and this website so I'm not sure how I could most effectively load the mesh. – user33993 Oct 11 '17 at 14:18
  • @batFINGER I apologize for the radio silence. Unfortunately I cannot upload my .blend file, for some reason the above mentioned site won't allow it. Is there another way I can send you my model as an .stl or something? I'm not sure if messaging is permitted on this site but if there is a way to private message it to you here let me know. – user33993 Nov 27 '17 at 18:03
  • it is only 669 KB, I even tried to compress it to 104 KB and that would not upload. Again, I'm new to blender so perhaps it is a simple fix, but I'm not quite sure what the issue would be with uploading a simple file. I apologize for the confusion! – user33993 Nov 29 '17 at 14:43
  • That website worked! Although I am not sure where it is posted here is the link. http://pasteall.org/blend/index.php?id=48432 and here is the url for the code I posted http://pasteall.org/695336. Let me know if you can access it via these links. – user33993 Nov 29 '17 at 15:37

0 Answers0