Create Curve from Selected Objects [Python]

Hey Everyone, I am converting some scripts from MEL to Python. The script working on right now creates a curve based off the selected Objects

I am having trouble converting the last the last bit of code(put notes in the scripts).

Anyone know the proper way to write it in python?

Here is the working MEL script


global proc CurveFromObjs () {
	string $jnt[] = `ls -sl`; 
	vector $storePositions[];
	int $x = 0;
	for ($i in $jnt)
		{
		$storePositions[$x] = `xform -query -worldSpace -translation $i`;
		$x++;
		}
	int $degree = 3; //<-- incase u want to change the degree type 

//area that needs to be converted to python correctly
	string $buildCurve = ("curve -d " + $degree); 
		for ($each in $storePositions)
		{
		$buildCurve += (" -p " + $each);
		}
	eval $buildCurve;
}

Here is the Python Code I have so far

import maya.cmds as cmds

selObj = cmds.ls(selection = True)
selObjSize = len(selObj)
storePositions= []
crvDegree = 3

##get positions
for i in selObj:
	sp = cmds.xform (i ,query= True, worldSpace=True,translation=True  )
	print storePositions 	

#area I am having trouble with
for each in storePositions:	
	 buildCurve = cmds.curve(d = crvDegree,p=[( each, each, each )])


import maya.cmds as cmds

sel = cmds.ls(sl=True)
curve_degree = 3 # cubic curve

positions = [cmds.xform(obj, q=True, ws=True, translation=True) for obj in sel]

my_curve = cmds.curve(d=curve_degree, p=positions)

It’s pretty simple to use Python’s list comprehension to get a list of world space positions for the selected objects.
That list will be a nested list of [x, y, z] positions – which is essentially the same thing that the cmds.curve command expects for the ‘point’ argument.

Hopefully that helps!

1 Like

Thanks! Exactly what I was looking for!