No More Retake

3DCG屋さん向けTips&Referenceサイト

メニュー

if ~in文

最初はよく使うMayaノードのTipsでもと思っていたのですが、気分でこっちにしました。
理路整然には程遠い!
唐突にもほどがある!
が、そのままいきます。

さて「if in文」ですが、pythonの基本構文のひとつです。
実は私これ最近知りました。(-∀-`; )
最近、pymelでスクリプトを書く機会が多くて、その際よく使用した構文のひとつですね。
MELでもできるのかどうかは、試していないので不明です。JavaScriptでは使えたはず。

この「if in 文」で何ができるかというと、文字列の検索が簡単にできます。

スクリプトを書くとき、名前で検索してオブジェクトを取得ってよく使う方法だと思います。それをこの「if in 文」で行うことができるのです。使い方は以下に↓。ちなみにpymelです。

if 「検索する文字列」 in 「検索される文字列」:
    「検索ヒット時に実行される処理」

実際に動くスクリプトを書くとこんな感じ。
下のスクリプトは、選択したノードの名前に”nurbs”があれば、そのノード名を表示するというもの。

import pymel.core as pm
#選択からリストを作成
list = pm.selected()
for i in list:
   #選択したオブジェクト名に"nurbs"が入っていればその名前を表示
   if  "nurbs" in i.name():
    print i

↑だけだとたいして役に立ちそうにないですが、これをチョイと書き換えてあげると、名前検索のリネームとか、特定の名前のノードと別の名前のノードをコネクトしたりとか、多様な場面で活躍します。
他のソフトと比べても、何かと名前縛りの多いMayaではよく使う構文のひとつだと思います。
例ではさらりと「for in文」とか使ってますが、その辺はさらりとスルーして今回はひとまずこれで。

それにしても、ブログはなかなか難しいです。
思ったとおりのレイアウトにならない。
特にプログラムのコード部分とか。
他の人のブログでは、そこだけテキストフィールドみたいなのになってたりとかするんだけど、あれってどうやるんだろう?

 

関連記事

  1. 2014.06.08

    toxik:[Link Image]

コメントをお待ちしております

このサイトについて

3DCG Tipsサイト。Houdiniの記事が多めです。

RSS

follow us in feedly

Archives

  • 2024 (1)
  • 2023 (2)
  • 2022 (9)
  • 2021 (14)
  • 2020 (46)
  • 2019 (17)
  • 2018 (33)
  • 2017 (29)
  • 2016 (36)
  • 2015 (64)
  • 2014 (54)
  • 2013 (60)
  • 2012 (55)

スポンサードリンク

言語切り替え

  • English (United States)
  • 日本語

スポンサードリンク

言語切り替え

  • English (United States)
  • 日本語

Houdiniを使えるようになる本


Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /usr/home/nomorere/nomoreretake.net/wordpress/wp-content/themes/n1-custom/functions/social.php on line 132

Warning: file_get_contents(): Failed to enable crypto in /usr/home/nomorere/nomoreretake.net/wordpress/wp-content/themes/n1-custom/functions/social.php on line 132

Warning: file_get_contents(http://api.facebook.com/restserver.php?method=links.getStats&urls=http%3A%2F%2Fnomoreretake.net%2F2012%2F05%2F06%2Fifin%2F): failed to open stream: operation failed in /usr/home/nomorere/nomoreretake.net/wordpress/wp-content/themes/n1-custom/functions/social.php on line 132

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /usr/home/nomorere/nomoreretake.net/wordpress/wp-content/themes/n1-custom/functions/social.php on line 132

Warning: file_get_contents(): Failed to enable crypto in /usr/home/nomorere/nomoreretake.net/wordpress/wp-content/themes/n1-custom/functions/social.php on line 132

Warning: file_get_contents(http://api.b.st-hatena.com/entry.count?url=http%3A%2F%2Fnomoreretake.net%2F2012%2F05%2F06%2Fifin%2F): failed to open stream: operation failed in /usr/home/nomorere/nomoreretake.net/wordpress/wp-content/themes/n1-custom/functions/social.php on line 132