Maya cruve converter

Hello,
I started working on this two days ago because it is something I have always wanted to make for myself. I never even looked but I am sure there is a tool out there like this. I am creating a tool where you will input a MEL curve command and it will spit out the python version. So far I have completed the conversion part and am now working on implementing a GUI that will store all your conversions for later, more convenient use. This is my conversion code, any ideas on simplifying it or just flat out redundant code corrections would be greatly appreciated. I will be updating this as I truck along.


#import needed modules
import maya.cmds as cmds

#declare Variables
nCurve = 'curve -d 1 -p 4 0 -1 -p 4 0 0 -p 5 0 0 -p 5 0 -1 -p 4 0 -1 -p 4 1 -1 -p 5 1 -1 -p 5 0 -1 -p 5 0 0 -p 5 1 0 -p 5 1 -1 -p 4 1 -1 -p 4 1 0 -p 5 1 0 -p 5 0 0 -p 4 0 0 -p 4 1 0 -k 0 -k 1 -k 2 -k 3 -k 4 -k 5 -k 6 -k 7 -k 8 -k 9 -k 10 -k 11 -k 12 -k 13 -k 14 -k 15 -k 16 ;'
pPoints = []
cDegree = 'd='
cPoints = 'p=('
cKnots = 'k=('

#Create Base variable lists
degree = nCurve.split('-p', 1)
points = degree[1].split('-p')
knots = points[-1].split('-k')
        
points.remove(points[-1])
points.append(knots[0])
degree.remove(degree[1])
knots.remove(knots[0])
hKnots = knots[-1].rstrip(';')
knots.remove(knots[-1])
knots.append(hKnots)

#Create final degree string
nDegree = degree[0].rstrip()
fDegree = nDegree[-1]
fDegree = cDegree + fDegree

#Create final points string
for o in points:
    n = o.strip()
    nVal = n.replace(' ', ',')
    fVal = '(' + nVal + ')'
    pPoints.append(fVal)
for o in pPoints:
    cPoints = cPoints + o + ','
fPoints = cPoints.rstrip(',')
fPoints = fPoints + ')'

#Create final knots string
for o in knots:
    i = o.strip()
    cKnots = cKnots + i + ','
fKnots = cKnots.rstrip(',')
fKnots = fKnots + ')'

#concatinate it all together
final = 'cmds.curve('+fDegree+', '+fPoints+', '+ fKnots+')'

print final


It may be easier to append points as you find them.
My pseudo-codey version:



# Get rid of knots, you dont need them if you are appending points
myString = myString.split('-k')[0]

# Get dNumber(i.e. 1 in this case)
dNumber = myString.split('-d ')[1]
dNumber = int(dNumber.split()[0])

# Just leave yourself with just the string of pure -p entries
myString = myString.split('-p', 1)[1]
myString = '-p' + myString

z = 0
for entry in myString.strip().split('-p'):
	if not entry:
		continue
        
        # Get point
	point = [float(myFloat) for myFloat in entry.split()]

        # Create curve
        if z == 0:
            maya.cmds.curve(name='myCurve', d=dNumber, ws=True, p=point)
        # Append to curve
        else:
            maya.cmds.curve( 'myCurve', a=True, ws=True, p=point )
	z += 1


Thats all untested pseudo-code, but should work in theory. Note in a real-world situation, I would error proof it a lot more, but I’ll leave that stuff up to you :stuck_out_tongue:

Also, must be a better way to do this:
myString = myString.split(’-p’, 1)[1]
myString = ‘-p’ + myString

but im on my lunch break and need food before code good

I will definitely play around with this, thank you.