Make input and run loop to create custom number of objects

删除回忆录丶 提交于 2019-12-25 00:58:26

问题


I have been trying to learn how to code for Maya for a while. And here I am making a rock generator using reduce face, quad-mesh, smooth in a loop. Now I would like to make an input so user can specify how many rock they want to make. Like if you input 5, it would make 5 rocks.

However I am currently stuck with the naming, like if I created the polyPlatonicSolid with rock#, the loop got confused and said something like rock# is invalid, so I am just looking for a simple solution for that.

import maya.cmds as MC
import random as RN

def rockGen():
    #GenerateBase
    rockCreation = MC.polyPlatonicSolid(name="rock", r=5)
    MC.displaySmoothness( polygonObject= 0)
    obj=MC.ls(sl=True)
    MC.polySmooth(rockCreation, divisions = 2)
    MC.polySoftEdge('rock', a=0, ch=1)

    #Evaluate face counts
    face_count = MC.polyEvaluate('rock', v=True)

    #Procedural rock creation
    for i in range(10):
        random_face = RN.randint(0, face_count)
        print random_face
        # Select faces
        targetFace = MC.select('rock.f[0:' + str(random_face)+ ']')
        # Reduce faces
        MC.polyReduce(p=20, kb=True, t=False)
        MC.polyQuad('rock', a=20)

    #Quad the rock
    MC.polySmooth('rock', ch=1, ost=0, khe=0, ps=0.1, kmb=1, bnr=1, mth=0, suv=1,
                  peh=0, ksb=1, ro=1, sdt=2, ofc=0, kt=1, ovb=1, dv=1, ofb=3, kb=1,
                  c=1, ocr=0, dpe=1, sl=1)

    #Select and rename
    MC.select('rock')

    MC.rename('inst#')

I tried this:

numberRock=input()
for u in range (numberRock)
   rockCreation = MC.polyPlatonicSolid(name="rock#", r=5)

But after that all my rock# command just deliver an invalid object feedback

One other challenge that was given by the teacher is that doing this script without the select command and I don't know the other way to do that yet.


回答1:


I have change the way you are naming your rock in order to be unique. Even if maya is based string, try to always use variable instead of 'rock'. Also instead of cmds.select, you can in most of the cases use cmds.ls to select your object. It is just a command that will return the name string if it exists, then you can use this to feed most of the commands in maya.

example :

my_rocks = cmds.ls('rock*')
cmds.rename(my_rocks[0], 'anythin_you_want')

here is your modified code that handle unique naming for your piece of rocks. Last advice, take the habit to write with .format() and also put parenthesis with print()

import maya.cmds as MC
import random as RN

def isUnique(name):
    if MC.ls(name):
        return False
    return True

def rockGen(name='rock'):
    #GenerateBase
    _iter = 1
    new_name = '{}_{:03d}'.format(name, _iter)

    while not isUnique(new_name):
        _iter += 1
        new_name = '{}_{:03d}'.format(name, _iter)

    rockCreation = MC.polyPlatonicSolid(name=new_name, r=5)
    MC.displaySmoothness( polygonObject= 0)
    obj=MC.ls(sl=True)
    MC.polySmooth(rockCreation, divisions = 2)
    MC.polySoftEdge(new_name, a=0, ch=1)

    #Evaluate face counts
    face_count = MC.polyEvaluate(new_name, v=True)

    #Procedural rock creation
    for i in range(10):
        random_face = RN.randint(0, face_count)
        # print(random_face)
        # Select faces
        targetFace = MC.select('{}.f[0:{}]'.format(new_name, random_face))
        # Reduce faces
        MC.polyReduce(new_name, p=20, kb=True, t=False)
        MC.polyQuad(new_name, a=20)

    #Quad the rock
    MC.polySmooth(new_name, ch=1, ost=0, khe=0, ps=0.1, kmb=1, bnr=1, mth=0, suv=1,
                  peh=0, ksb=1, ro=1, sdt=2, ofc=0, kt=1, ovb=1, dv=1, ofb=3, kb=1,
                  c=1, ocr=0, dpe=1, sl=1)

    MC.delete(new_name, ch=True)
    return new_name

so with this you can loop :

my_rocks = []
my_number = 5
for x in range(my_number):
    rock = rockGen()
    my_rocks.append(rock)
print(my_rocks)


来源:https://stackoverflow.com/questions/57793145/make-input-and-run-loop-to-create-custom-number-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!