Skip to content Skip to sidebar Skip to footer

Make Input And Run Loop To Create Custom Number Of Objects

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 inpu

Solution 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

defisUnique(name):
    if MC.ls(name):
        returnFalsereturnTruedefrockGen(name='rock'):
    #GenerateBase
    _iter = 1
    new_name = '{}_{:03d}'.format(name, _iter)

    whilenot 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 creationfor i inrange(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 = 5for x in range(my_number):
    rock = rockGen()
    my_rocks.append(rock)
print(my_rocks)

Post a Comment for "Make Input And Run Loop To Create Custom Number Of Objects"