如何在python中创建一行随机宽度的多维数据集

时间:2017-01-01 21:40:54

标签: python row maya

我想创建一行随机宽度的图块。我可以做2个立方体,但我不知道怎么做100个立方体。

import maya.cmds as cmds
import random

cubeList = cmds.ls( 'Tiles*' )
if len(cubeList) > 0:
    cmds.delete( cubeList )

#create row and col list
cols = 2
arr = []
for col in xrange (cols):
    width_rand_Size = random.uniform(0.8,3)

    arr.append(cmds.polyCube (ax = (0,0,1), w = width_rand_Size, h = 1, d =1 , n='Tiles#'))
    if col != 0:
        cmds.setAttr( "Tiles2.tx",(cmds.polyCube('Tiles1', q = 1, w = 1))/2 + (cmds.polyCube('Tiles2', q = 1, w = 1))/2)

1 个答案:

答案 0 :(得分:0)

每次迭代时,都必须使脚本自动找到对象和上一个对象的名称。然后计算当前图块和之前创建的所有图块之间的空间。

以下是代码:

import maya.cmds as cmds
import random

cubeList = cmds.ls( 'Tiles*' )
if len(cubeList) > 0:
    cmds.delete( cubeList )

#create row and col list
cols = 10 # number of tiles to create
x = 1 # increment variable
arr = []
allTilesSpace = [] # cumalated space between tiles
for col in xrange (cols):
    # if there is no tile to create, do nothing
    if not cols:
        break
    # get the names of the objects
    currentTile = 'Tiles%d' % x
    previousTile = "Tiles%d" % (x - 1)
    # set random width
    width_rand_Size = random.uniform(0.8,3)
    arr.append(cmds.polyCube (ax = (0,0,1), w = width_rand_Size, h = 1, d =1 , n=currentTile))

    # Move the tiles
    currentTileWidth = cmds.polyCube(currentTile, q = 1, w = 1)
    if cmds.objExists(previousTile):
        previousTileWidth = cmds.polyCube(previousTile, q = 1, w = 1)
        allTilesSpace.append(previousTileWidth)
        tilesSpace = sum(allTilesSpace) + (currentTileWidth / 2)
        cmds.setAttr(currentTile + ".tx",tilesSpace)
    else:
        cmds.setAttr(currentTile + ".tx", currentTileWidth / 2)

    x += 1 # increment