I currently have a script that scales total length and selects and scales vertex groups of a "whale" mesh. I noticed that the output when scaling vertex groups by a particular scaling factor does not result in the desired size (in terms of sum of edges within the vertex groups) and realized this was because I had to set the 3D cursor to the center of the groups (since I used a mirror modifier the center of the vgroup is really in between two vertices that I will note in the below code). However, since I have to run multiple iterations of this I need to use python rather than scale it manually, however I haven't had much luck figuring out the best way to incorporate this into my code. Does anyone have any thoughts?
To provide a visual:
The output of 209.5910 makes sense because 104..79550*2 is 209.59100.
However if I take the same steps as above and instead of scaling using the keyboard command I use
If you want to take a look at the problem as a whole the code is as follows (for the sake of ease this code is only representative of one iteration and the sfac and total length values are simplified numbers):
obj = bpy.context.active_object totallength =  whale =  vgrp = ['Face','Reye','Rpostpec','Rantdslfin', 'Rpostpdslfin', 'Ranus', 'Rmidkeel','Rend'] sfac = [2,2,2,2,2,2,2,2] #list of all scaling factors for vertex group and whale vgrpindex = [2,23,77,79,145,147,167,169,189,191,211,213,233,235,255,257] #list of indices of vertices to set 3D cursor to (in pairs of 2, first two correspond to Face, second two to Reye, etc.) whalesDims = np.zeros((1,2)) 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: #I think that this is where my new code setting the cursor to a particular location should go scale(v,sfac[vgrp.index(v)]) #scale each vertex group based on the index in sfac that matches the vgrp index bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.select_all(action='SELECT') 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))) 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 scale_whale(obj,whale,vgrp,sfac)
I assume the code that needs to be inserted would look something like this:
def vgrouporigin(vgroup): vertices=[e for e in bm.verts] for vert in vertices: if vgrp =='Face': if vert.index==2 or vert.index==23: vert.select=True #Code to set 3D cursor here else: vert.select=False if vgrp =='Reye': if vert.index==77 or vert.index==79: vert.select=True #Code to set 3D cursor here else: vert.select=False if vgrp =='Rpostpec': if vert.index==145 or vert.index==147: vert.select=True #Code to set 3D cursor here else: vert.select=False if etc. etc. etc. vgrouporigin(vgrp)
However, I feel that there may be an easier way to loop through this/I'm still not sure of how to actually set the cursor to said location. I hope you guys can help me out! I apologize if the code is a bit messy, I'm very new at coding, particularly for Blender.
Here's a download of the blender file! https://blend-exchange.giantcowfilms.com/b/4457/