forked from skoshiwosh/democode
/
makecity.py
174 lines (138 loc) · 5.6 KB
/
makecity.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
import sys
import random
import logging
from pprint import pprint
import maya.cmds as cmds
#########################################################
# globals
#########################################################
VERSION = "V02"
logging.basicConfig(level=logging.INFO)
logging.info( "makecity.py Version %s" % VERSION)
#########################################################
# methods
#########################################################
def mkcube(grp_name, cube_name):
""" Make cube under group node at origin with dimension 1,1,1 and pivot at origin.
Args:
grp_name: object name of parent group to contain cube
cube_name: object name of cube node
Example:
makecity.mkcube("building_grp", "cube0")
"""
new_cube = cmds.polyCube(ax=[0, 1, 0], cuv=4, ch=True, name=cube_name)
cmds.group(new_cube, name=grp_name)
nodeattr = "%s.translateY" % new_cube[0]
cmds.setAttr(nodeattr, 0.5)
# another way to change pivot on geo
#spattr = "%s.scalePivot" % new_cube[0]
#rpattr = "%s.rotatePivot" % new_cube[0]
#cmds.move(0, 0, 0, spattr, rpattr, absolute=True)
# ToDo: fix bug so that pivot xform applies to cube and not group node
cmds.xform(worldSpace=True, pivots=[0,0,0])
cmds.makeIdentity(apply=True, t=1, r=1, s=1, n=0, pn=1 )
return new_cube[0]
####################################################################
#
# mkbldgs ToDo: complete function
#
# create simple geometry and group under group named by grp_name argument
#
####################################################################
def mkbldgs(grp_name):
"""
!!! Not implemented yet !!!
Args:
bldg_grp: group node name to contain building geos
"""
logging.info("mkbldgs: !!! Not implemented yet !!!")
return
def copy2grid(bldg_grp, numrows=4, numcols=4, stepx=3, stepz=3, hide=True):
""" Duplicate geo over grid.
Total number of copies = numrows * numcols.
Args:
bldg_grp: group node containing geo to be copied
numrows: number of rows in grid, default = 4
numcols: number of columns in grid, default = 4
stepx: x translate step for each geo copy, default = 3
stepz: z translate step for each geo copy, default = 3
hide: Booleon to hide original geo after copies completed, default = True
Example:
newcity = makecity.copy2grid("building_grp")
"""
# create list of xyz points of grid
points = []
for i in range(numcols):
for j in range(numrows):
x = stepx * i
z = stepz * j
points.append((x,0,z))
#pprint(points) # debug check of list of point tuples
# get building geo under bldg_grp and copy to each point of grid
# ToDo: if more than 1 geo under group, use random.choice(bldg_list)
# to get random choice building geo to be copied.
bldg_list = cmds.listRelatives(bldg_grp, type="transform")
this_bldg = bldg_list[0]
allgeo = []
for xyz in points:
newgeo = cmds.duplicate(this_bldg)
cmds.move(xyz[0], xyz[1], xyz[2], newgeo[0], absolute=True)
allgeo.append(newgeo[0])
# group all copies under city_grp
#pprint(allgeo) # debug check of allgeo list
city_grp = cmds.group(allgeo, name="city_grp", w=True)
# turn off original geo's visibility if hide is True
if hide:
cmds.setAttr("%s.visibility" % bldg_grp, 0)
return city_grp
####################################################################
#
# copy2vtx ToDo: complete function
#
# test whether geo is nurbs or poly mesh then use one of these code snippets
# to create list of points for duplication like code in copy2grid
#
# vtx_list = cmds.xform('cube0.vtx[*]', q=True, ws=True, t=True)
# vtx_points = zip(*[iter(vtx_list)]*3)
# pprint(vtx_points)
#
# cv_list = cmds.xform('nurbsSphere1.cv[*]', q=True, ws=True, t=True)
# cv_points = zip(*[iter(cv_list)]*3)
# pprint(cv_points)
#
####################################################################
def copy2vtx(bldg_grp, obj):
""" Copy geometry objects under bldg_grp to all surface points on obj.
!!! Not implemented yet !!!
Args:
bldg_grp: group node containing geo to be copied
obj: poly or nurbs surface containing position points for copy
Example:
new_city = makecity.copy2vtx("building_grp", "nurbsPlane1")
"""
logging.info("copy2vtx: !!! Not implemented yet !!!")
return
###############################################################################
#
# randgeo ToDo: add optional arguments and functionality
# - add optional arguments for range values to randomize scale
# - use hard-coded args in random.uniform as default values to randomize scale
# - add optional arguments to randomize rotation or do not randomize rotation at all
#
#
###############################################################################
def randgeo(city_grp = None):
""" Randomize scale on all geo under group.
Args:
city_grp: group node containing geo to randomize. If None then get geo from selection.
"""
if not city_grp:
allgeo = cmds.ls(sl=True)
else:
allgeo = cmds.listRelatives(city_grp, type="transform")
for each in allgeo:
this_scale = (round(random.uniform(0.5, 1.5), 3),
round(random.uniform(0.5, 2.0), 3),
round(random.uniform(0.5, 1.5), 3))
cmds.scale(this_scale[0], this_scale[1], this_scale[2], each, scaleXYZ=True, absolute=True)