Showing posts with label 03B. Show all posts
Showing posts with label 03B. Show all posts

Sunday, July 12, 2009

03B - Deborah Kaiser - Extrudying faces of a polygon according to locator




#################################################
#####
### 03B
#####

import maya.cmds as cmds
import math
import random

#EXTRUDE FACES OF POLYGON ACCORDING TO DISTANCE TO LOCATOR

def magnitude(v):
x=v[0]
y=v[1]
z=v[2]
m=math.sqrt ((x*x)+(y*y)+(z*z))
return m

def distance (p1, p2):
x1=p1[0]
y1=p1[0]
z1=p1[1]
x1=p1[2]
x2=p1[0]
y2=p1[1]
z2=p1[2]
x= x1-x2
y= y1-y2
z= z1-z2
m= magnitude([x, y, z])
return m

def ExtrudeToLocator():
"Extrude based on a distance of a certain Locator"

#GET LOCATOR AND POLYGON
selPoly= cmds.filterExpand(sm=12)
selLoc= cmds.filterExpand(sm=22)
selPoly= selPoly[0]

#GET ALL FACES OF POLYGON
allFaces= cmds.ls(selPoly + ".f[:]", fl=1)

#LOOP THROUGH FACES
for face in allFaces:
vertex= cmds.polyListComponentConversion (face, fromFace=1, toVertex=1)
vertex= cmds.ls (vertex, fl=1)
# find the center of the face
xs= 0
ys= 0
zs= 0

# LOOP THROUGH VERTICES
for v in vertex:
pos= cmds.pointPosition(v)
x= pos[0]
y= pos[1]
z= pos[2]
xs= xs + x
ys= ys + y
zs= zs + z
centerX= xs/len(vertex)
centerY= ys/len(vertex)
centerZ= zs/len(vertex)

#PLACE LOCATOR
cmds.spaceLocator(p=(centerX, centerY, centerZ))

#DISTANCE TO LOCATOR
posLoc= cmds.pointPosition(selLoc)
d= distance([centerX, centerY, centerZ], posLoc)
print d
#extrude based on distance
cmds.polyExtrudeFacet(face, ltz=d/10)

#EXTRUDE TO LOCATOR
ExtrudeToLocator()

Ex03B_PolyPlane_Deborah&Claudia




#######################
## Ex03B_Move Cvs of a Nurbs according to Locator direction
#######################

import maya.cmds as cmds
import math

#define distance between PolFaces and locator
def magnitude (v):
#v is a list of x,y,z values
x= v[0]
y= v[1]
z= v[2]
m= math.sqrt ( (x*x) + (y*y) + (z*z) )
return m

def distance (posVert, posLoc):
#subtract both vectors
x1= posVert[0]
y1= posVert[1]
z1= posVert[2]
x2= posLoc[0]
y2= posLoc[1]
z2= posLoc[2]
x= x1 - x2
y= y1 - y2
z= z1 - z2
m= magnitude ([x,y,z])
return m

#define moveToLocator function
def moveToLocator():
# A) get all the verticesNames of a plane
allVertices= cmds.ls ("pPlane1.vtx[:]", fl=1)

#create an empty list to store the points
MyVerts=[]

#get selected Locator
selLoc= cmds.filterExpand(sm=22)

#loop through vertices
for v in allVertices:
pos= cmds.pointPosition(v)
x= pos[0]
y= pos[1]
z= pos[2]

#find distance to Locator
posVert= cmds.pointPosition(v)
posLoc= cmds.pointPosition(selLoc)
d= distance(posVert, posLoc)
print d
cmds.move (d,3,d,v,r=True)

#store the cvs in a list
MyVerts.append(allVertices)

#call the function
moveToLocator()

Ex03B_NurbsSphere_Deborah&Claudia




#######################
## Ex03B_Move Cvs of a Nurbs according to Locator direction
#######################

import maya.cmds as cmds
import math

#define distance between cvs and locator
def magnitude (v):
#v is a list of x,y,z values
x= v[0]
y= v[1]
z= v[2]
m= math.sqrt ( (x*x) + (y*y) + (z*z) )
return m

def distance (posPoint, posLoc):
#subtract both vectores
x1= posPoint[0]
y1= posPoint[1]
z1= posPoint[2]
x2= posLoc[0]
y2= posLoc[1]
z2= posLoc[2]
x= x1 - x2
y= y1 - y2
z= z1 - z2
m= magnitude ([x,y,z])
return m

#define moveToLocator function
def moveToLocator():
# A) get all the cvsNames of a NurbsPlane
NurbsCvs = cmds.ls("nurbsSphere1.cv[:]", fl=1)

#create an empty list to store the points
MyCvs=[]

#get selected Locator
selLoc= cmds.filterExpand(sm=22)

#loop through cvs
for i in NurbsCvs:
posPoint= cmds.pointPosition(i)
posLoc= cmds.pointPosition (selLoc)
d= distance(posPoint, posLoc)
print d
cmds.move (d,d,d,i,r=True)

#store the cvs in a list
MyCvs.append(NurbsCvs)

#call the function
moveToLocator()

Ex03B_Curve_Claudia&Deborah




#####################
## Ex03B_Move Cvs of a Curve according to Locator direction
#######################

import maya.cmds as cmds
import math

#define distance between cvs and locator
def magnitude (v):
#v is a list of x,y,z values
x= v[0]
y= v[1]
z= v[2]
m= math.sqrt ( (x*x) + (y*y) + (z*z) )
return m

def distance (posPoint, posLoc):
#subtract both vectores
x1= posPoint[0]
y1= posPoint[1]
z1= posPoint[2]
x2= posLoc[0]
y2= posLoc[1]
z2= posLoc[2]
x= x1 - x2
y= y1 - y2
z= z1 - z2
m= magnitude ([x,y,z])
return m

#define moveToLocator function
def moveToLocator():
# A) get all the cvsNames of a curve
allCvs = cmds.ls("curve1.cv[:]", fl=1)

#create an empty list to store the points
MyCvs=[]

#get selected Locator
selLoc= cmds.filterExpand(sm=22)

#loop through cvs
for i in allCvs:
posPoint= cmds.pointPosition(i)
posLoc= cmds.pointPosition (selLoc)
d= distance(posPoint, posLoc)
print d
cmds.move (0,0,d,i,r=True)

#store the cvs in a list
MyCvs.append(cvs)

#call the function
moveToLocator()

Clau_Ex03B_Doubts

Daniel, I am trying to move the vertices of a Curve according to the direction of a Locator. I could move the position of the Curve to the direction of a locator -see script below- however, when i try to apply this logic to move the vertices of the curve, i get lost. Don't tell you about moving the cv's of a nurbs surface according the direction of a locator. Could you help me?
This is the scripting with probls
## Ex03B_Move Curve Cvs according to Locator direction
#####

import maya.cmds as cmds
import math

#get names of selected objects on Stage
#first select the objects
selObj= cmds.ls(sl=1)
print selObj

#find position of this two objetcs
obj1= selObj[0] #this is for locator1
obj2= selObj[1] #this is for curve1
print obj1
print obj2

pos1= cmds.pointPosition(obj1)
pos2 = cmds.pointOnCurve(obj2)
print pos1
print pos2

#find distance between two objects
d= distance (pos1,pos2)
print d

#move all vertices of obj2 10 times in direction to obj1
# select all vertices of obj2
allCvs = cmds.ls("curve1.cv[:]", fl=1)

# I dont know how to continue....


Here is the script to move the curve position according to loc1 direction
## Ex03B_Move Curve according to Locator direction
#####

import maya.cmds as cmds
import math

#get names of selected objects on Stage
#first select the objects
selObj= cmds.ls(sl=1)
print selObj

#find position of this two objetcs
obj1= selObj[0] #this is for locator1
obj2= selObj[1] #this is for curve1
print obj1
print obj2

pos1= cmds.pointPosition(obj1)
pos2= cmds.pointOnCurve(obj2)
print pos1
print pos2

#find distance between two objects
d= distance (pos1,pos2)
print d

#move obj2 4 times in direction to obj1
# first get vectors between obj2 and obj1
v= vectorBetweenPoints(pos1,pos2)
# get the unit of vector v
dir= unit(v)

#move 4 units in dir obj1
newPos= move (dir,4)

#move obj2 to that position
cmds.move (newPos[0], newPos[1], newPos[2], obj2)

This is the exercise of extruding faces of a polygon according to the distance of a Locator.



### Ex03B ######
## Vector Functions
import maya.cmds as cmds
import math
import random

########### extrude the faces of a polygonal object
# according to its distance to a certain
# locator

def ExtrudeToLocator():
"Extrude based on a distance of a certain Locator"
#get selected Poly and selected Locator
selPoly= cmds.filterExpand(sm=12)
selLoc= cmds.filterExpand(sm=22)
selPoly= selPoly[0]

#get all faces of the Poly
allFaces= cmds.ls(selPoly + ".f[:]", fl=1)

#start a loop through all faces
for face in allFaces:
vertex= cmds.polyListComponentConversion (face, fromFace=1, toVertex=1)
vertex= cmds.ls (vertex, fl=1)
# find the center of the face
xs= 0
ys= 0
zs= 0

#loop through vertices
for v in vertex:
pos= cmds.pointPosition(v)
x= pos[0]
y= pos[1]
z= pos[2]
xs= xs + x
ys= ys + y
zs= zs + z
centerX= xs/len(vertex)
centerY= ys/len(vertex)
centerZ= zs/len(vertex)

#check by placing a Locator
cmds.spaceLocator(p=(centerX, centerY, centerZ))

#find distance to Locator
posLoc= cmds.pointPosition(selLoc)
d= distance([centerX, centerY, centerZ], posLoc)

#extrude based on distance
cmds.polyExtrudeFacet(face, ltz=d)

#extrude to locator
ExtrudeToLocator()