Build tree 2 (xform+cmd.window)

前言

建立一個window,點擊button後在被選取的所有點上建立object。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import maya.cmds as cmd
if cmd.window("xform2",exists=True):
cmd.delete("xform2")
RandomGWin=cmd.window("xform2",t="buid trees",w=300,h=200)
cmd.columnLayout(adj=True)
cmd.separator(h=10)
cmd.text("Select random vertices")
cmd.separator(h=10)
cmd.button(l="Generate Random Trees",h=30,c="ranGenerator()")
cmd.showWindow(RandomGWin)
def ranGenerator():
myVerList = cmd.ls(selection=True, fl=True)
for ver in myVerList:
verPos = cmd.xform(ver, q=True, ws=True, t=True)
Obj = create_Obj()
cmd.move(verPos[0], verPos[1], verPos[2], Obj)
def create_Obj():
cube_height = 6
cube = cmd.polyCube(sx=1, sy=1, sz=1, w=0.5, d=0.5, h=cube_height)
cmd.move(0, cube_height / 2, 0, cube)
cone_height = 2
cone = cmd.polyCone(r=1, h=cone_height)
cmd.move(0, cone_height / 2 + cube_height, 0, cone)
cmd.select(cube,cone)
cmd.group(n='TreeGrp#')

Demo

參考

https://www.youtube.com/watch?v=sJP47Wt9YaI&index=7&list=PLdQnt7xu73OXBjKNlsA-lX9tWATVUjtA9