No More Retake

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

メニュー

「2D dot」 To 「3D dot」 Scripts

ここのところノードの話ばかりだったので、今日はScriptの話にしてみました。

次のようなスクリプトを作ってみました。
以前、Twitterでちょこっとツイートしたのですが、
下の絵のように、2Dドット絵から3Dのドット風絵に変換するスクリプトです。
(あくまで、ドットをCubeに置き換えるだけで、凸凹させるのはできません。あしからず)

試しにレンダリングすると・・・
もっとつくってみた。
調子に乗って、さらに作ってみた。ピクセルってたら彼かな、と。
えーと、スクリプトね。
画像を制御するモジュールとしてPIL(Python Imageing Library)というモジュールを使用しました。

これは別途ダウンロードする必要があったりします。
maya2012の場合は”for Python 2.6″でよかったと記憶しています。
PILモジュールに関して、Degital FronttierさんのDF TALKにすこし載ってますので参考に。

以下が作成したスクリプトです。
必要な機能のみ搭載した状態で、実際に使うならエラー処理やら関数化やらすべきでしょうね。
つまり、あまり良いコードではないと。
でも、こんなのでも出来たよということで。
スクリプトの中身はこんな事をやってます。

①画像データを取得(tga)
②各ピクセルから使用している色をリスト化
③各色のマテリアルを作成(mia_material)
④polyCubeを作成、ピクセル位置に配置
⑤対応ピクセルの色情報からpolyCubeにマテリアルをアサイン

import PIL.Image
import maya.cmds as cmds
import pymel.core as pm

######################
#画像取得
######################
getPath = cmds.fileDialog(dm = "*.tga")
im = PIL.Image.open(getPath)

#画像のサイズを取得
getSize = im.size

######################
#使用カラー割り出し、リスト化
######################
a=[]
for i in range(getSize[0]*getSize[1]):
    clr = im.getdata()[i]
    a.append(clr)

#リストの重複を削除
matList = list(set(a))

######################
#マテリアル作成
######################
count=0
dic = {}

#角丸め用にmia_roundcornersノード作成
roundMat = pm.shadingNode('mia_roundcorners', asShader=True,n="pixel_roundcorners") 

for i in matList:
    matName = "mat_%04d" % count
    mat = pm.shadingNode('mia_material_x_passes', asShader=True, n=matName)
    mat.diffuse.set(i[0]/255.0, i[1]/255.0, i[2]/255.0)
    count = count +1
    dic[i] = mat

    roundMat.outValue >> mat.overall_bump

#オブジェクト格納用の空グループ作成
gp = pm.group( em=True, name='pixel_GP' )

######################
#polyCube作成&配置
######################
#print getSize
for h in range(getSize[1]):
    for w in range(getSize[0]):

        id = getSize[0]*h+w  

        #アルファが0で無ければpolyCubeを作成
        c = im.getdata()[id]
        if len(c) == 4:
            if c[3] != 0:

                b = pm.polyCube()
                b[0].tx.set(w)
                b[0].ty.set(-h)

                mat = dic

                pm.select(b)
                pm.hyperShade(assign=mat)
                pm.parent( b, gp )
        else:
            b = pm.polyCube()
            b[0].tx.set(w)
            b[0].ty.set(-h)

            mat = dic

            pm.select(b)
            pm.hyperShade(assign=mat)
            pm.parent( b, gp ) 

ちなみにこのスクリプトの実行は自己責任ってことで、ヨロシク!
pixelが多いとCubeの生成に時間がかかります。(300×300とか多い部類。90000個のCubeだしね)
あと、このスクリプトで出来るのは真っ平らなpolyCubeの並びまでで、凸凹には対応していません。サンプルは手でいちいち移動させました。
最後は力業かよって感じですが、よい方法が思いつきませんでした。
さらにいえば、これをドット風に動かしたかったのですが、それも良い方法が思いつかなくて断念。
思いついたらサラッと、更新してるかもしれませんw
それにしてもpythonとpymelは便利ですね。もうMELには戻れませんねw

おまけ

関連記事

  1. 2014.05.05

    toxik:[Switcher]
  1. お願いします 2021.12.24 5:57am

    是非maya2022版を作って欲しいです

    • 0
    • 0

このサイトについて

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%2F11%2F19%2F2ddotto3ddotscripts%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%2F11%2F19%2F2ddotto3ddotscripts%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