This is the code I wrote a little while ago for getting component
islands/shells...it's not the most efficient but it works. Oh well...
Aside from that you could just cheat and create a cluster based on those
verts and use that. or replace it with a locator after.
def getShells(objs):
'''Gets the shells for a selected list objects and returns a dict
Args: objs (list of pm.PyNode): all objects to be checked Returns
(dict): dictionary of mesh shells Usage:
detectSeparateMeshes(pm.ls(sl=True)) '''
shells_group={}
for obj in objs:
verts = [pm.PyNode(obj.getShape().name() + '.vtx[%s]'%vert )for
vert in range(0,obj.getShape().numVertices())]
vertsLeft = verts[:]
shells=[]
for vert in verts:
if vert in vertsLeft:
shell = getShell(vert)
vertsLeft = [vertex for vertex in vertsLeft if
vertex not in shell]
shells.append(shell)
shells_group[obj.name()]=shells
return shells_group
def getShell(vertex, shell=[]):
'''Returns the vert list of the shell connected to given vertex Args:
vertex (pm.nt.MeshVertex): vertex to get shell from shell
(list): internal array, not for external use Returns:
(list): pm.nt.MeshVertex list of shell verts '''
if shell == []:
shell=[]
obj = vertex.node()
shell.append(vertex)
#get connected verts to current vert
connected = pm.ls(vertex.connectedVertices(), fl=True)
for connect in connected:
#if the connected vert is not in the shell add it then run on it
if connect not in shell:
shell.append(connect)
getShell(connect, shell=shell)
return shell
On Friday, September 19, 2014 2:39:42 PM UTC-4, [email protected] wrote:
>
> okay, so I've got something kinda working. But my math is wrong somewhere
> in figuring out the average normals. I'm having a hard time visualizing it.
> Here's my code in case anybody has experience averaging normals. Maybe I'm
> doing this all wrong - I dunno. I'd really appreciate any help. Thx!
>
> #========================================
> import pymel.core as pm
> from itertools import chain
> import time
>
>
> def convertToVerts( components ):
> return pm.ls( pm.polyListComponentConversion( components,
> toVertex=True ),
> flatten=True)
>
> def getAvgVertTfm( components ):
>
> verts = convertToVerts(components)
>
> positions = []
> normals = []
> for vert in verts:
> positions.append( vert.getPosition() )
> normals.append( vert.getNormals() )
>
> avgPos = [ sum(pos)/len(pos) for pos in zip(*positions) ]
> avgNormal = [ sum(normal)/len(normal) for normal in zip(*normals) ]
>
> avgNormalRotation = [ sum(rotation)/len(rotation) for rotation
> in zip(*pm.datatypes.degrees(avgNormal)) ]
>
> avgTfm = pm.datatypes.TransformationMatrix()
> avgTfm.setTranslation(avgPos, space="world")
> avgTfm.setRotation(avgNormalRotation)
>
> null = pm.spaceLocator()
> null.setTransformation( avgTfm )
>
> '''
> still need to add object matrix multiplications
> '''
>
>
> startTime = time.time()
> getAvgVertTfm( pm.ls(sl=True) )
> print time.time()-startTime
> #=======================================
>
> <http://www.pinterest.com/pin/create/extension/>
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/3332efd9-bdab-4273-81ec-516c5657fb7e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.