Fitting polynomial curve

Astan Chee astan.chee at gmail.com
Thu Mar 17 20:44:13 EDT 2011


On Thu, Mar 17, 2011 at 5:09 PM, Terry Reedy <tjreedy at udel.edu> wrote:

> Look at scipy.
>
> --
>

Thanks for the info. I realized I made some mistakes. Anyway, what I'm
trying to do is in maya (python), fit selected vertices on a curve. Here is
what I have so far:

import maya.cmds as cmds
import numpy

def run_main():
    verts = cmds.ls(sl=True,fl=True)
    if len(verts) >= 2:
        degree = 5
        x = []
        y = []
        for vert in verts:
            vert_x,vert_y,vert_z = cmds.pointPosition(vert,w=True)
            x.append(vert_x)
            y.append(vert_z)
        print "x ", x
        print "y ", y
        x_numpy = numpy.array(x)
        y_numpy = numpy.array(y)
        funct = numpy.polyfit(x_numpy,y_numpy,degree)
        print funct #p : ndarray, shape (M,) or (M, K)
                #Polynomial coefficients, highest power first. If y was 2-D,
the coefficients for k-th data set are in p[:,k].
        #make an outline curve
        curvs = []
        for i in range(len(verts)):
            vert_x,vert_y,vert_z = cmds.pointPosition(verts[i],w=True)
            pos_x = 0
            pos_y = 0
            for j in range(degree):
                pos_x += (funct[j] *(vert_x**(degree-j)))
                pos_y += (funct[j] *(vert_y**(degree-j)))
            centerPos = (pos_x,pos_y,vert_z)
            curvs.append(centerPos)
        if curvs:
            print "cirv ", curvs
            crv = cmds.curve(p=curvs)
            cmds.select(cl=True)
            for v in verts:
                cmds.select(v,tgl=True)

    else:
        print "please select more than 2 verts"

But this only works for 2D (x,z-axis). Is it possible to make it work at 3
by combining them or doing (x,y) and (x,z) and somehow colate (average?) the
results? Is there a formula for combining the results?
Thanks again for any help or suggestions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110318/a3000c96/attachment-0001.html>


More information about the Python-list mailing list