|
Python for Maya - 14. Expression에서 python 사용
Digital Dream ( http://cafe.naver.com/digitaldream ) 카페의 'Maya Python'의
기반으로 복습하는 차원에서 재정리한 것입니다.
* Code
# coding: utf-8 # maya python 14 - expression에서 python 사용하기 import maya.cmds as cmds import os, sys # 0 = lattice , 1 = nurbsSphere sel_ = cmds.ls(sl=1) # 2개 선택인지 확인 if len(sel_) == 2: # lattice, nurbs 인지 확인 if cmds.nodeType(cmds.listHistory(sel_[0], lv=1)[0]) == 'lattice' and 'nurbsSphere' in (cmds.listHistory(sel_[1], lv=1)[0]): head_name, sphere_ = sel_ # lattice 정보 shape_ = cmds.listHistory(head_name, lv=1)[0] s_count = cmds.getAttr('%s.sd' % shape_) t_count = cmds.getAttr('%s.td' % shape_) u_count = cmds.getAttr('%s.ud' % shape_) # nsphere 정보 nsp_ = cmds.listHistory(sphere_ , lv=1)[1] value_list = {} for s_ in range(s_count): for t_ in range(t_count): for u_ in range(u_count): # lattice point 위치 정보 value_list[(s_, t_, u_)] = cmds.xform('%s.pt[%s][%s][%s]' % (head_name, s_, t_, u_), q=1, ws=1, t=1) # string formatting set_ = {} set_['head_name'] = head_name set_['sphere_'] = sphere_ set_['s_count'] = s_count set_['t_count'] = t_count set_['u_count'] = u_count set_['nsp_'] = nsp_ set_['value_list'] = value_list
# python script set_noise = u''' # coding: utf-8 import maya.cmds as cmds import maya.mel as mm value_list = %(value_list) # 노이즈를 적용할 함수 def set_noise(time_): pos_ = cmds.xform('%(sphere_)s', q=1, ws=1, t=1) radius_ = cmds.getAttr('%(nsp_)s.radius') y_scale = cmds.getAttr('%(sphere_)s.scaleY') for s_ in range(%(s_count)d): for t_ in range(%(t_count)d): for u_ in range(%(u_count)d): # lattice point 위치 정보 lpos_ = value_list[(s_, t_, u_)] # noise의 seed 값 sort_ = "%%s%%s%%s" %% (s_, t_, u_) # expression 조건 dis_ = mm.eval('mag(〈> - 〈>)' %% tuple(pos_ + lpos_) ) width_ = (1 - (dis_ / radius_)) * (radius_ * y_scale) if dis_ < radius_ * y_scale: x_ = lpos_[0] + mm.eval('noise((%%s + %%s + 10) * 2)' %% (time_, sort_)) * width_ y_ = lpos_[1] + mm.eval('noise((%%s + %%s + 20) * 2)' %% (time_, sort_)) * width_ z_ = lpos_[2] + mm.eval('noise((%%s + %%s + 30) * 2)' %% (time_, sort_)) * width_ cmds.move(x_, y_, z_, '%(head_name)s.pt[%%s][%%s][%%s]' %% (s_, t_, u_)) else: cmds.move(lpos_[0], lpos_[1], lpos_[2], '%(head_name)s.pt[%%s][%%s][%%s]' %% ( s_, t_, u_)) ''' % set_# python script 저장
f = open('c:/temp/set_noise.py', 'w') # 한글 문제 해결 위해 코딩 set_noise = unicode(set_noise).encode('utf-8') f.write(set_noise) f.close()# mel script
set_mel = ''' python("import sys"); python("sys.path.append('c:/temp')"); python("import set_noise"); '''# mel script 저장
f = open('c:/temp/set_noise.mel', 'w') f.write(set_mel) f.close()# mel referencing
cmds.file('c:/temp/set_noise.mel', r=1)# expression 적용
cmds.expression(s='python("set_noise.set_noise(" + time + ")");', n="lattice_random_move")
* Comment
expression 적용한 python script를 저장하기 위한 referencing
* 사전형 변수 설정
head_name, sphere_ = sel_
선택된 lattice와 nurbsSphere가 sel_에서 head_name과 sphere_에 각각 치환한다.
# string formatting
set_ = {}
set_['head_name'] = head_name
set_['sphere_'] = sphere_
set_['s_count'] = s_count
set_['t_count'] = t_count
set_['u_count'] = u_count
set_['nsp_'] = nsp_
set_['value_list'] = value_list
lattice와 sphere를 선택 후
sel_ = cmds.ls(sl=1) # 선택
head_name, sphere_ = sel_ # 각각 치환
set_ = {} # 사전형 지정
set_['head_name'] = head_name # 사전형 head_name 키라는 이름에 넣기
set_['sphere_'] = sphere_ # 사전형 sphere_ 키라는 이름에 넣기
set_
# Result: {'head_name': u'ffd1Lattice', 'sphere_': u'nurbsSphere1'} #
set_['head_name']
# Result: ffd1Lattice #
set_['sphere_']
# Result: nurbsSphere1 #
s_count, t_count, u_count, nsp_, value_list 각각 사전형으로 만들게 된다.
사전형의 문자열 사용은..
print '%(head_name)s %(sphere_)s' % set_
ffd1Lattice nurbsSphere1
* python script
f = open('c:/temp/set_noise.py', 'w')
set_noise = unicode(set_noise).encode('utf-8')
f.write(set_noise)
f.close()
set_noise 변수에 string으로 넣어 파일(c:/temp/set_noise.py)로 저장한다.
저장하기 전에 한글 주석으로 된 string이 있기 때문에 maya에서는 ascii error가 나게 되므로 저장할 때 한글을 사용할 수 있도록 unicode script를 추가하게 된다.
* mel script
set_mel = ''' python("import sys");
python("sys.path.append('c:/temp')");
python("import set_noise"); '''
python을 reference를 할 수 없으므로 mel로 저장 후 reference로 사용하게 된다.
f = open('c:/temp/set_noise.mel', 'w')
f.write(set_mel)
f.close()
저장된 python 파일을 import 하는 string을 set_mel 변수에 넣고 파일로 저장한다.
* reference
cmds.file('c:/temp/set_noise.mel', r=1)
저장된 mel을 reference에 넣는다.
* expression 적용
cmds.expression(s='python("set_noise.set_noise(" + time + ")");', n="lattice_random_move")
lattice_random_move 이름의 expression을 적용한다.
set_noise.set_noise(" + time + ")
import명.함수명(인자)
───────────────────────────────────────────────
source : http://cafe.naver.com/digitaldream/505 - 'Maya Python' 14) expression 에서 python 사용 하기
- maya_python14.py (3KB) (0)
- maya_python14.mb (68.4KB) (0)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
마야 뷰포트 네비게이션 팁 | 푸딩뱃살 | 43845 | 2020.04.06 17:22 | |
Maya 버전 별 Python 버전 | 푸딩뱃살 | 65037 | 2014.01.08 17:59 | |
464 | [Dev] Autodesk Maya Devkit 다운로드 | 푸딩뱃살 | 706 | 2023.01.28 14:28 |
463 | [Base] (해결 중) modules 환경설정 중 | 푸딩뱃살 | 655 | 2022.11.09 11:47 |
462 | [Script] pymel 딕셔너리형 사용 시 KeyError | 푸딩뱃살 | 864 | 2022.11.07 12:08 |
461 | [오류] Building Numpy for Maya Python 2.7.x | 푸딩뱃살 | 600 | 2022.10.23 14:38 |
460 | [Base] 뷰포트에서 조절자가 안 보일때 | 푸딩뱃살 | 789 | 2022.10.13 15:47 |
459 | [Rigging] mirror joints 사용 시 유의 사항 | 푸딩뱃살 | 803 | 2022.10.04 10:46 |
458 | [Script] 2022에서 enum34 모듈 설치 금지 | 푸딩뱃살 | 610 | 2022.08.17 18:08 |
457 | [Script] pymel 예제 | 푸딩뱃살 | 624 | 2022.07.05 19:20 |
456 | [Script] 인코드 / 디코드 - 2.7 한글 사용 | 푸딩뱃살 | 901 | 2022.03.08 17:52 |
455 | [Dev] ui 없이 mayapy로 자동화 | 푸딩뱃살 | 705 | 2022.02.17 13:56 |
454 | [Dev] mayapy로 ui파일 py로 | 푸딩뱃살 | 531 | 2022.02.15 18:20 |
453 | [오류] Error : MayaBonusTools | 푸딩뱃살 | 922 | 2022.01.21 17:52 |
452 | [오류] Error: ModuleNotFoundError | 푸딩뱃살 | 776 | 2022.01.21 16:24 |
451 | [Dev] mayapy | 푸딩뱃살 | 654 | 2022.01.19 20:08 |
450 | [Base] function selCom at 0x7f29c5c04aa0 | 푸딩뱃살 | 617 | 2022.01.19 17:24 |
449 | [Base] wireframe on shaded 단축키 만들기 | 푸딩뱃살 | 916 | 2022.01.04 10:55 |
448 | [오류] OpenCL Error | 푸딩뱃살 | 526 | 2021.12.28 01:40 |
447 | [Script] Easily Translate MEL Commands to Python | 푸딩뱃살 | 834 | 2021.12.02 11:22 |
446 | [Base] output window 띄우지 않기 | 푸딩뱃살 | 827 | 2021.11.24 21:44 |
445 | [Rigging] shapeEditorManager 삭제 안됨 | 푸딩뱃살 | 896 | 2021.11.12 23:30 |