LoginSignup
0
0

maya で円形の位置を取りたい

Last updated at Posted at 2023-12-23

mayaでちょっと円形に物を配置したくなりまして・・・・

過去にそんなようなツールは作ったんですけども
どこに行ったかなぁ・・・・

(highend3Dとかで放流してた気がするのですが~ こんどHDDを漁ってみよう

雑に考える

円形に配置したいので
円を定義して
円周上の位置を取ればいいんでしょー?

雑すぎるのでもう少し正しく

  • 円を定義するために、中心と半径が必要
  • スタート位置となる円周上の点を定義する
  • スタート位置から配置数分-1だけ回転させて位置を取得していく

とかまぁそんなかんじか?

円と言いつつも、
どちらかというと多角形を作るイメージでやった方がよさそう。

image.png

雑に実装

ベクトルABをx度回転させる。
ベクトルを回転させるには〜

MVector.rotateBy(rot)

これでできそう
rotは クォタニオンの値を入れれば良いようなのでそれも用意する。

import maya.api.OpenMaya as om2
import math

def generateCirclePoints(centerPoint,startPoint,num,axis = om2.MVector.kYaxisVector
):
    centerPosition = cmds.xform(centerPoint, q =True,ws =True, t =True)
    startPosition = cmds.xform(startPoint, q =True,ws =True, t =True)
    
    #quat用回転軸ベクトル
    pointMatrix = om2.MMatrix(cmds.getAttr(centerPoint + ".worldMatrix[0]"))
    rotAxis = om2.MTransformationMatrix(pointMatrix).setTranslation(axis,om2.MSpace.kObject).translation(om2.MSpace.kWorld)
    
    #1回あたりの回転値
    degStep = 360.0 / num
    
    centerPosVector = om2.MVector(centerPosition)
    startPosVector = om2.MVector(startPosition)
    startVector = om2.MVector(startPosition) - om2.MVector(centerPosition)
    radStep = math.radians(degStep)
    onCirclePoints = []
        
    for i in range(0, num):    
        if i != 0:
            radStep = math.radians(degStep*i)
            nextVector = startVector.rotateBy(om2.MQuaternion(radStep, rotAxis))
            nextPosition =  nextVector + centerPosVector
    
        else:
            #スタート位置
            nextPosition = startPosition
        
        #とりあえず見やすくjointを生成
        node = cmds.createNode("joint")
        cmds.xform(node, ws = True, t = nextPosition)

centerPoint = "centerPos"
startPoint = "startPos"
axis = om2.MVector.kYaxisVector
num = 35
generateCirclePoints(centerPoint,startPoint,num,axis)

さてこんな感じで実行すると

image.png

こんなかんじですかね
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0