#!/usr/bin/env python
# -*- coding: UTF-8 -*-

'''
Created on Oct 25, 2009

@author: schi
'''

# 描述:
#     创建一个螺旋状的NURBS曲线
#
# 参数:
#     p=    #螺旋曲线的间距,默认是0.5
#     r=    #螺旋曲线的半径,默认是4.0
#
# 使用例子:
#     python:
#         import maya.cmds as cmds
#         cmds.spHelix(p=0.3,r=7)
#
#     Mel:
#         spHelix -p 0.3 -r 7;

import maya.OpenMaya as om
import maya.OpenMayaMPx as ompx
import sys, math

# 定义命令的名称
kPluginCmdName = 'spHelix'
# 定义参数的名称
kPitchFlag = '-p'
kPitchLongFlag = '-pitch'
kRadiusFlag = '-r'
kRadiusLongFlag = '-radius'

class SpHelixCmd( ompx.MPxCommand ):

    def __init__( self ):
        super( SpHelixCmd, self ).__init__()

    def doIt( self, args ):
        # 定义所需要的数值,如:cv点的数量,曲线的间距,半径等
        deg = 3
        ncvs = 20
        spans = ncvs - deg
        nknots = spans + 2 * deg - 1
        radius = 4.0
        pitch = 0.5
        
        # 剖析参数
        argData = om.MArgDatabase( self.syntax(), args )
        # 查看在使用命令时是否设置-p和-r参数
        # isFlagSet()是MArgParser的方法
        # MArgDatabase继承MArgParser
        # flagArgumentDouble()也一样
        if argData.isFlagSet( kPitchFlag ):
            pitch = argData.flagArgumentDouble( kPitchFlag, 0 )
        if argData.isFlagSet( kRadiusFlag ):
            radius = argData.flagArgumentDouble( kRadiusFlag, 0 )
            
        controlVertices = om.MPointArray()
        knotSequences = om.MDoubleArray()
        
        # 给helix建立cv点
        for i in range( 0, ncvs ):
            controlVertices.append( om.MPoint( radius * math.cos( i ),
                                             pitch * i,
                                             radius * math.sin( i ) ) )
            
        for i in range( 0, nknots ):
            knotSequences.append( i )
            
        # 创建曲线
        curveFn = om.MFnNurbsCurve()
        nullObj = om.MObject()
        
        try:
            # 这个插件是通过剖析cv点来创建曲线的。
            # 通过ep点来创建曲线的方法也已经添加,
            # 只要设置为False就行
            if True:
                curveFn.create( controlVertices,
                               knotSequences,
                               deg,
                               om.MFnNurbsCurve.kOpen,
                               0, 0,
                               nullObj )
            else:
                curveFn.create( controlVertices,
                               3,
                               om.MFnNurbsCurve.kOpen,
                               False, False, False )
        except:
            sys.stderr.write( '创建曲线时发生错误\n' )
            raise 
        
# Creator
def cmdCreator():
    # Create the command
    return ompx.asMPxPtr( SpHelixCmd() )

# Syntax creator
# 命令样式创建
def syntaxCreator():
    syntax = OpenMaya.MSyntax()
    syntax.addFlag( kPitchFlag, kPitchLongFlag, om.MSyntax.kDouble )
    syntax.addFlag( kRadiusFlag, kRadiusLongFlag, om.MSyntax.kDouble )
    return syntax

# 注册插件
def initializePlugin( mobject ):
    mplugin = ompx.MFnPlugin( mobject )
    try:
        mplugin.registerCommand( kPluginCmdName, cmdCreator )
    except:
        sys.stderr.write( '插件 %s 注册失败\n' % kPluginCmdName )
        raise

# 注销插件
def uninitializePlugin( mobject ):
    mplugin = ompx.MFnPlugin( mobject )
    try:
        mplugin.deregisterCommand( kPluginCmdName )
    except:
        sys.stderr.write( '插件 %s 注销失败\n' % kPluginCmdName )
        raise


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐