회원 로그인
정보기억 정보기억에 체크할 경우 다음접속시 아이디와 패스워드를 입력하지 않으셔도 됩니다.
그러나, 개인PC가 아닐 경우 타인이 로그인할 수 있습니다.
PC를 여러사람이 사용하는 공공장소에서는 체크하지 마세요.
소셜네트워크 서비스를 통해서 로그인하시면 별도의 로그인 절차없이 회원서비스를 이용하실 수 있습니다.


최근 게시물

1.노션에서 작성 중

1.노션에서 작성 중

개편하기 전까지 노션에서 작성 중

2024.04.04//read more

2.ChatGPT

2.ChatGPT

OpenAI로 대규모 언어 모델대화형...

2023.03.16//read more

3.노코딩 게임 엔진 - 빌..

3.노코딩 게임 엔진 - 빌..

빌드 지원안드로이드iOS윈도우즈특이사...

2023.03.14//read more

4.(완료) 미접속 회원 정..

4.(완료) 미접속 회원 정..

[완료] 36명의 회원을 정리하였습니...

2023.02.16//read more

5.매뉴얼 플러스 - 전자제..



안정적인 DNS 서비스 DNSEver
DNS Powered by DNSEver.com


Python for Maya - 14. Expression에서 python 사용

artsOne | 2009.05.11 17:57 | 조회 5395

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 사용 하기

466개(17/24페이지)
마야
번호 제목 글쓴이 조회 날짜
공지 마야 뷰포트 네비게이션 팁 푸딩뱃살 43905 2020.04.06 17:22
공지 Maya 버전 별 Python 버전 푸딩뱃살 65076 2014.01.08 17:59
144 [참고] Maya Online Help artsOne 3397 2009.08.11 01:02
143 [참고] Maya 2009 Service Pack 1a artsOne 3611 2009.07.20 12:22
>> [Script] Python for Maya - 14. Expression에서 python 사용 사진 첨부파일 artsOne 5396 2009.05.11 17:57
141 [Script] Python for Maya - 13. realtime lattice point random move 사진 첨부파일 artsOne 5094 2009.05.01 00:58
140 [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 5260 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 3233 2009.03.30 01:26
135 [Script] 자료형 - list 이해 첨부파일 artsOne 2739 2009.03.30 01:12
134 [Script] Shape 변경하기 첨부파일 artsOne 2088 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 2863 2008.06.18 00:31
127 [Expression] MEL, Expression - pow artsone 2748 2008.06.16 16:09
126 [Script] MEL 기초 #02 - 함수, 라이브러리, 클래스 artsOne 3804 2008.04.18 02:27
125 [FX] Lesson 1: Creating nCloth collisions Step 3 사진 첨부파일 artsOne 2143 2008.04.13 17:56