회원 로그인
|
Python for Maya - 12. sphere를 이용한 random move
'Maya Python 시작하기'의 기반으로 복습하는 차원에서 재정리한 것입니다.
lattice와 nurbsSphere를 선택한다.
script를 적용하면 nurbsSphere안에 들어간 lattice point가 random으로 move하게 된다.
* Code
# coding: utf-8 import maya.cmds as cmds import maya.mel as mm import random sel_ = cmds.ls(sl=1) # 선택된 것이 2개인지 확인 if len(sel_) == 2: # 첫번째 선택된 것이 lattice인지, 두번째 선택된 것이 nurbsSphere인지 확인 if cmds.nodeType(cmds.listHistory(sel_[0], lv=1)[0]) == 'lattice' and 'nurbsSphere' in (cmds.listHistory(sel_[1], lv=1)[0]): shape_ = cmds.listHistory(sel_[0], lv=1)[0] # lattice의 정보 추출 s_count = cmds.getAttr('%s.sd' % shape_) t_count = cmds.getAttr('%s.td' % shape_) u_count = cmds.getAttr('%s.ud' % shape_) nsp_ = cmds.listHistory(sel_[1], lv=1)[1] # nurbsSphere의 위치값 추출 pos_ = cmds.xform(sel_[1], q=1, ws=1, t=1) # nurbsSphere의 scale 추출 radius_ = cmds.getAttr('%s.radius' % nsp_) y_scale = radius_ * cmds.getAttr('%s.scaleY' % sel_[1]) for s_ in range(s_count): for t_ in range(t_count): for u_ in range(u_count): # lattice point의 위치값 추출 lpos_ = cmds.xform('%s.pt[%s][%s][%s]' % (sel_[0], s_, t_, u_), q=1, ws=1, t=1) # nurbsSphere의 위치값, lattice point의 위치값을 tuple로 전환하고 mag()함수로 거리값 추출 dis_ = mm.eval('mag(<<%s, %s, %s>> -<<%s, %s, %s>>)' % tuple(pos_ + lpos_)) # nurbsShpere 안에 있는 lattice point를 if radius_ > dis_: # random 범위 value_ = 1 - (dis_ / radius_) range_ = (-1 * value_, 1 * value_) # random move x_ = random.uniform(*range_) y_ = random.uniform(*range_) * y_scale z_ = random.uniform(*range_) cmds.move(x_, y_, z_, '%s.pt[%s][%s][%s]' % (sel_[0], s_, t_, u_), r=1) cmds.select('%s.pt[%s][%s][%s]' % (sel_[0], s_, t_, u_), tgl=1)* Comment
if cmds.nodeType(cmds.listHistory(sel_[0], lv=1)[0]) == 'lattice' and 'nurbsSphere' in (cmds.listHistory(sel_[1], lv=1)[0]):
흔히 사용되는 if문이다. 주의해서 볼 것은 cmds.listHistory(sel_[0], lv=1)[0] 문구인데, sel_[0]의 listHistory로 추출한 값의 첫번째 배열을 치환하게 된다.
import maya.cmds as cmds
sel_ = cmds.ls(sl=1)
selHis_ = cmds.listHistory(sel_[0], lv=1)
selHis_
# Result: [u'nurbsSphereShape1', u'makeNurbSphere1'] #
한 object의 listHistory를 하게 되면 1개 이상의 정보를 추출하게 되는데, 이것 또한 배열로 설정할 수 있다.
import maya.mel as mm
......
dis_ = mm.eval('mag(<<%s, %s, %s>> - <<%s, %s, %s>>)' % tuple(pos_ + lpos_))
python에서의 mag() 함수가 없으므로 maya의 mel을 사용할 수 있도록 eval 명령어를 사용한 것이다.
반대로 mel에서 python을 사용하려면 python(문법);으로 사용하면 된다.
<<%s, %s, %s>> - <<%s, %s, %s>>의 중간 - (마이너스)는 차집합의 의미이다.
(자세한 사항은 Python for Maya - 4. set (집합)을 참고한다.)
───────────────────────────────────────────────
source : http://cafe.naver.com/digitaldream/493
'Maya Python' 12) sphere를 이용한 random move
- mayaPython12.py (1.4KB) (0)
댓글 0개
| 엮인글 0개
466개(17/24페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
마야 뷰포트 네비게이션 팁 | 푸딩뱃살 | 43893 | 2020.04.06 17:22 | |
Maya 버전 별 Python 버전 | 푸딩뱃살 | 65064 | 2014.01.08 17:59 | |
144 | [참고] Maya Online Help | artsOne | 3396 | 2009.08.11 01:02 |
143 | [참고] Maya 2009 Service Pack 1a | artsOne | 3611 | 2009.07.20 12:22 |
142 | [Script] Python for Maya - 14. Expression에서 python 사용 | artsOne | 5395 | 2009.05.11 17:57 |
141 | [Script] Python for Maya - 13. realtime lattice point random move | artsOne | 5093 | 2009.05.01 00:58 |
>> | [Script] Python for Maya - 12. sphere를 이용한 random move | artsOne | 4694 | 2009.04.22 00:26 |
139 | [Script] Python for Maya - 11. Lattice vertex random move (re.fin | artsOne | 5259 | 2009.04.17 12:12 |
138 | [Rigging] Expression을 이용한 Flollow Through | artsOne | 2672 | 2009.04.14 20:24 |
137 | [Script] MEL - UI / menu, frameLayout | artsOne | 2955 | 2009.04.03 00:33 |
136 | [Script] MEL, Python 표기 | artsOne | 3232 | 2009.03.30 01:26 |
135 | [Script] 자료형 - list 이해 | artsOne | 2739 | 2009.03.30 01:12 |
134 | [Script] Shape 변경하기 | artsOne | 2086 | 2009.03.11 00:13 |
133 | [Rendering] Default Color | artsOne | 2283 | 2008.09.12 01:40 |
132 | [Script] MEL - clear | artsOne | 2759 | 2008.09.12 00:12 |
131 | [Script] MEL - window #2 | artsOne | 2774 | 2008.09.01 23:49 |
130 | [Rigging] Guide Line (Deform > Point On Curve) | artsOne | 2598 | 2008.08.13 02:16 |
129 | [Base] Marking Menu Custom | artsOne | 2166 | 2008.08.04 12:48 |
128 | [Rigging] Expression을 이용한 Stretch, Squash | artsone | 2862 | 2008.06.18 00:31 |
127 | [Expression] MEL, Expression - pow | artsone | 2747 | 2008.06.16 16:09 |
126 | [Script] MEL 기초 #02 - 함수, 라이브러리, 클래스 | artsOne | 3803 | 2008.04.18 02:27 |
125 | [FX] Lesson 1: Creating nCloth collisions Step 3 | artsOne | 2143 | 2008.04.13 17:56 |