Geometric Spline Generator

Hey guys. I made this class to generate geometry for a spline from simple meshes. I haven’t added a GUI yet but I figured I would share what I have so far.

To run the script, create a spline called “curve1” and then run the code.
You can adjust the shape using the various spline geometry generators below.


import pymel.core as pm;
import pymel.core.datatypes as dt;
import maya.mel as cmds;
from math import *;

class SplineGeometryGenerator:

    def __init__(self, curveName, splineDivisions):
        self.curveName = curveName;
        self.splineDivisions = splineDivisions;

    def getPointOnCurve( self, curveTransformName, t ):
        curve = pm.listRelatives( curveTransformName )[0];
        spans = pm.getAttr( "{0}.spans".format( curve ) );
        cpCount = spans;
  
        lt = t * cpCount;
        point = pm.pointOnCurve( curve, pr=lt );
        return point;

    def generate(self, cookieMesh):
        curveStartPoint = self.getPointOnCurve(self.curveName, 0.0 );
        curveNextPoint = self.getPointOnCurve(self.curveName, 0.0001 );

        dx = curveStartPoint[0] - curveNextPoint[0];
        dy = curveStartPoint[1] - curveNextPoint[1];
        dz = curveStartPoint[2] - curveNextPoint[2];

        look = dt.Vector( dx, dy, dz ).normal();
        up = dt.Vector( 0.0, 1.0, 0.0 )

        if abs( look.dot(up) ) >= 0.999:
            up = dt.Vector( 0.0, 0.0, 1.0 );

        right = look.cross( up ).normal();
        up = look.cross( right ).normal();

        mat = dt.TransformationMatrix();
        mat.a00 = right.x; mat.a01 = right.y; mat.a02 = right.z; mat.a03 = 0.00;
        mat.a10 = up.x; mat.a11 = up.y; mat.a12 = up.z; mat.a13 = 0.00;
        mat.a20 = look.x; mat.a21 = look.y; mat.a22 = look.z; mat.a23 = 0.00;
        mat.a30 = curveStartPoint[0]; mat.a31 = curveStartPoint[1]; mat.a32 = curveStartPoint[2]; mat.a33 = 1.00;
        pm.xform( cookieMesh, matrix = mat );
        pm.polyExtrudeFacet( "{0}".format(cookieMesh), inc=self.curveName, divisions=self.splineDivisions);

class CubeSplineGeometryGenerator(SplineGeometryGenerator):

    def __init__(self, curveName, splineDivisions, scaleVec):
        self.scaleVec = scaleVec;
        SplineGeometryGenerator.__init__(self, curveName, splineDivisions);

    def createMeshCookie( self, scaleVec ):    
        verts = [
        ( -scaleVec[0], -scaleVec[1], 0.0 ),
        ( -scaleVec[0], scaleVec[1], 0.0 ),
        (  scaleVec[0], scaleVec[1], 0.0 ),
        (  scaleVec[0], -scaleVec[1], 0.0 )
        ];
        mesh = pm.polyCreateFacet(point=verts)[0];
        return mesh;
      
    def generate( self ):
        mesh = self.createMeshCookie( self.scaleVec );
        SplineGeometryGenerator.generate( self, mesh );     

class TriangleSplineGeometryGenerator(SplineGeometryGenerator):

    def __init__(self, curveName, splineDivisions, scaleVec):
        self.scaleVec = scaleVec;
        SplineGeometryGenerator.__init__(self, curveName, splineDivisions);

    def createMeshCookie( self, scaleVec ):
        verts = [
        (  scaleVec[0], 0.0, 0.0 ),
        ( 0.0, -scaleVec[1], 0.0 ),
        ( -scaleVec[0], 0.0, 0.0 )
        ];
        mesh = pm.polyCreateFacet(point=verts)[0];
      
        return mesh;
      
    def generate( self ):
        mesh = self.createMeshCookie( self.scaleVec );
        SplineGeometryGenerator.generate( self, mesh );
      
class CylinderSplineGeometryGenerator(SplineGeometryGenerator):

    def __init__(self, curveName, splineDivisions, scaleVec, cylinderDivisions):
        self.scaleVec = scaleVec;
        self.cylinderDivisions = cylinderDivisions;
        SplineGeometryGenerator.__init__(self, curveName, splineDivisions);

    def createMeshCookie( self, scaleVec, cylinderDivisions ):
        twoPi = 3.14159 * 2.0;
        angleInc = twoPi / cylinderDivisions;
        
        verts = [];
        t = 0.00;
        radians = 0.00;
        while t <= 1.0:
            x = cos(radians) * scaleVec[0];
            y = sin( radians ) * scaleVec[1];
            verts.append( [x, y, 0.0] );
            radians -= angleInc;
            t = -radians / twoPi;
     
        mesh = pm.polyCreateFacet(point=verts)[0];
      
        return mesh
      
    def generate( self ):
        mesh = self.createMeshCookie( self.scaleVec, self.cylinderDivisions );
        SplineGeometryGenerator.generate( self, mesh );

#spline = CubeSplineGeometryGenerator("curve1", 30.0, [1.0, 1.0]);
#spline = TriangleSplineGeometryGenerator("curve1", 30.0, [1.0, 1.0]);
spline = CylinderSplineGeometryGenerator("curve1", 30.0, [1.0, 1.0], 20);
spline.generate();

Awesome! thanks