Hi everyone,
An assignment of mine is requiring me to make a Python script with a GUI that places object (A) on every single vertex of a selected object (B). And be able to scale ObjA on Obj B’s vertices through the UI window. As a complete beginner in Python, I’ve been researching and watching scripting videos for the past 16 hours;however, I still cannot seem to understand how to write a script for this.
The only thing that I put down in the editor is the basic:
Import maya.cmds as cmds
//Store object names into variables(let’s say I have a cone and a sphere in the Maya scene)
Obj1=‘pCone1’
Obj2=‘pSphere1’
That’s it. And I managed to create the Ui window with 2 textfield buttons, a scale slider and a ‘Compute’ button.
Some people are mentioning commands such as poly.Evaluate, .time or xform…have read about them, looked at examples but I am STILL confused on what to do for this particular situation:(:
Sorry for the noob question but this has really been frying my mind.
start with the commands “xform” and “pointPosition” to move objects and get vertex locations respectively.
I wrote some quick code to get you started.
getAttr to get the verts,
xform to get position of the verts
Question on it?
import maya.cmds as cmds
def getVtxPos( shapeNode ) :
# Will contain all positions of the verts
vtxWorldPosition = []
# Get the amount of verts
vtxIndexList = cmds.getAttr( shapeNode+".vrts", multiIndices=True )
for i in vtxIndexList :
# Current position of the verts
curPointPosition = cmds.xform( str(shapeNode)+".pnts["+str(i)+"]", query=True, translation=True, worldSpace=True )
# Append it to the list
vtxWorldPosition.append( curPointPosition )
return vtxWorldPosition
def placeObjects():
# will contain all new objects (use this list later when scaling)
listOfObject = []
# Your initial object
objectA = 'pCubeShape1'
# Create Object B, width, heigh, depth are set to 0.1
objectB = cmds.polyCube(h = 0.1, w=0.1, d=0.1)[0]
# Call the getVtxPos to get all positions
listOfVertxPos = getVtxPos( shapeNode )
# Itterate the position list
for eachPos in listOfVertxPos:
# Duplicate object B
duplicatedObject = cmds.duplicate(objectB)[0]
# Append the new object to the list
listOfObject.append(duplicatedObject)
# Move the newly duplicated item to the position
cmds.xform(duplicatedObject, t=eachPos)
return listOfObject
print placeObjects()
if you are just starting out learning to script, it would be useful to create a skeleton of your code using comments.
def placeObjAtEachVertex(sourceObj, destinationObj):
# get a list of all the vertices on the destinationObj
# loop through each vertex
# get the position of the vertex
# make a duplicate of the sourceObj
# reposition the duplicated sourceObj
# done!
pass