python parent items in range

python parent items in range

Gregorybruneau
Participant Participant
1,525 Views
4 Replies
Message 1 of 5

python parent items in range

Gregorybruneau
Participant
Participant

I'm having trouble parenting each item in my range with each item in another range. Just wonder how to parent each new cube with 1 nurbs curve. Sorry I'm really new and this is my second project for school.

import maya.cmds as cmds

#Object Creation
import random

item_count = 50
myScale = random.uniform(0.1,3)

for number in range (item_count):
    cube = cmds.polyCube()[0]
    cmds.setAttr(cube + '.ty', 0.5)
    cmds.setAttr(cube + '.tx', random.uniform(-20,20))
    cmds.setAttr(cube + '.ty', random.uniform(-20,20))
    cmds.setAttr(cube + '.tz', random.uniform(-20,20))
    
    for number in range (item_count):
        ctrl = cmds.circle(nr=(0,1,0))[0]
        cmds.setAttr(ctrl + '.ty', 0.5)
        cmds.setAttr(ctrl + '.ry', random.uniform(0,360))
        
        for number in range (item_count):
            cmds.parent(cube,ctrl)

 

0 Likes
Accepted solutions (1)
1,526 Views
4 Replies
Replies (4)
Message 2 of 5

brentmc
Autodesk
Autodesk
Accepted solution

Hi,

 

I think the problem is you have 3 nested loops where only one is needed.

import maya.cmds as cmds

#Object Creation
import random

item_count = 50
myScale = random.uniform(0.1,3)

for number in range (item_count):
    cube = cmds.polyCube()[0]
    cmds.setAttr(cube + '.ty', 0.5)
    cmds.setAttr(cube + '.tx', random.uniform(-20,20))
    cmds.setAttr(cube + '.ty', random.uniform(-20,20))
    cmds.setAttr(cube + '.tz', random.uniform(-20,20))
    
    ctrl = cmds.circle(nr=(0,1,0))[0]
    cmds.setAttr(ctrl + '.ty', 0.5)
    cmds.setAttr(ctrl + '.ry', random.uniform(0,360))
        
    cmds.parent(cube,ctrl)




Brent McPherson
Principle Software Developer
Message 3 of 5

Kahylan
Advisor
Advisor

Hi!

 

as @brentmc already mentioned, you have 3 nested loops instead of 1.
You also can save some lines by using the xform command instead of the setAttr command. Unless you need to work with setAttr fo your assignment ofcourse.

import maya.cmds as cmds

#Object Creation
import random

item_count = 50
myScale = random.uniform(0.1,3)

for number in range (item_count):
    cube = cmds.polyCube()[0]
    cmds.xform(cube, t = (random.uniform(-20,20),random.uniform(-20,20),random.uniform(-20,20)), ws = True)
    
    ctrl = cmds.circle(nr=(0,1,0))[0]
    cmds.xform(ctrl, t= (0,0.5,0),ro = (0,random.uniform(0,360),0), ws = True)
        
    cmds.parent(cube,ctrl)

 

xform also allows you to query values, so you can snap your curves to your cubes if you want, then they aren't all just overlapping. That would looks like this:

import maya.cmds as cmds

#Object Creation
import random

item_count = 50
myScale = random.uniform(0.1,3)

for number in range (item_count):
    cube = cmds.polyCube()[0]
    cmds.xform(cube, t = (random.uniform(-20,20),random.uniform(-20,20),random.uniform(-20,20)), ws = True)
    translation = cmds.xform(cube, t = True, q = True)
    rotation = cmds.xform(cube, ro = True, q = True)
    
    ctrl = cmds.circle(nr=(0,1,0))[0]
    cmds.xform(ctrl, t= translation,ro = rotation, ws = True)
        
    cmds.parent(cube,ctrl)

It all depends on what exactly you want the end product to be.  🙂
I just thought I'd mention xform, because it is the most used function to place objects in space.

Have a nice day!

Message 4 of 5

Gregorybruneau
Participant
Participant
Thanks you so much! That worked perfectly! Thanks for taking the time to help me
0 Likes
Message 5 of 5

Gregorybruneau
Participant
Participant
Thanks you so much too! Ya they want us to do it a certian way for this assignment, but I'll definitely have to keep this in mind for my future assigments. Thanks again!
0 Likes