#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Very challenging python question.


    I'm trying to make these cubes to first rise up vertical direction uniformly and then fall off.






    here is the script that I used, I want to know if someone can modify and help me with this.

    Thank you.









    import random
    import math
    road,obs=cmds.ls(sl=True)
    insideRoad=cmds.listRelatives(road,c=True)
    bullet={}
    for r in insideRoad:
    bulletname=cmds.listRelatives(r,type='bulletRigidBodyShape')[0]
    cmds.setAttr('%s.bodyType' %bulletname,0)
    bullet[r]=bulletname



    def attrExists(obj,attr):
    l=cmds.listAttr(obj,k=True,ud=True)
    if l == None:
    return False
    else:
    if attr in l:
    return True
    else:
    return False



    def distance(a,b):
    x=a[0]-b[0]
    y=a[1]-b[1]
    z=a[2]-b[2]
    x=math.sqrt(x*x + y*y + z*z)
    return x
    for each in insideRoad:
    x=attrExists(each,'trigger')
    if x==False:
    cmds.addAttr(each,ln="trigger", at="long")
    cmds.setAttr('%s.trigger' %each,e=True,keyable=True)
    cmds.setAttr('%s.trigger' %each,-1)
    else:
    cmds.setAttr('%s.trigger' % each,-1)

    min=cmds.playbackOptions(q=True,min=True)
    max=cmds.playbackOptions(q=True,max=True)
    activeObjects=[]
    for i in range (int(min),int(max)):
    randDist=random.uniform(2,3.5)
    for each in insideRoad:
    objPos=cmds.xform(each,q=True,rp=True,ws=True)
    obstaclePos=list(cmds.getAttr('%s.t' %obs,t=i)[0])
    dist=distance(objPos,obstaclePos)
    if dist<=random.uniform(2,3.5):
    cmds.setAttr('%s.trigger' %each,i)
    activeObjects.append(each)

    for active in activeObjects:
    frame=cmds.getAttr('%s.trigger' %active)
    x=bullet[active]
    cmds.setKeyframe('%s.bodyType' %x , t=frame ,v=2 )
    cmds.setKeyframe('%s.bodyType' %x , t=frame-1 ,v=0 )
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    pictures


    http://
    imageshack.us/
    a/img811/8250/
    sztm.png


    http://
    imageshack.us
    /a/img703/3079/
    7bdy.jpg


    I cannot post the images link. sorry that I had to break it down like this.

    for the second picture, I meant to say diagonal not vertical.
    sorry about that.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    Code:
    # http://imageshack.us/a/img811/8250/sztm.png
    # http://imageshack.us/a/img703/3079/7bdy.jpg
    
    import random
    import math
    
    road,obs=cmds.ls(sl=True)
    insideRoad=cmds.listRelatives(road,c=True)
    bullet={}
    for r in insideRoad:
        bulletname=cmds.listRelatives(r,type='bulletRigidBodyShape')[0]
        cmds.setAttr('%s.bodyType' %bulletname,0)
        bullet[r]=bulletname
    
    def attrExists(obj,attr):
        l=cmds.listAttr(obj,k=True,ud=True)
        if l == None:
            return False
        return attr in l
                
    def distance(a,b):
        x=a[0]-b[0]
        y=a[1]-b[1]
        z=a[2]-b[2]
        return math.sqrt(x*x + y*y + z*z)
    
    for each in insideRoad:
        if attrExists(each,'trigger'):
            cmds.setAttr('%s.trigger' % each,-1)
        else:
            cmds.addAttr(each,ln="trigger", at="long")
            cmds.setAttr('%s.trigger' %each,e=True,keyable=True)
            cmds.setAttr('%s.trigger' %each,-1)
    
    min=cmds.playbackOptions(q=True,min=True)
    max=cmds.playbackOptions(q=True,max=True)
    activeObjects=[]
    for i in range (int(min),int(max)):
        randDist=random.uniform(2,3.5)
        for each in insideRoad:
            objPos=cmds.xform(each,q=True,rp=True,ws=True)
            obstaclePos=list(cmds.getAttr('%s.t' %obs,t=i)[0])
            dist=distance(objPos,obstaclePos)
            if dist<=random.uniform(2,3.5):
                cmds.setAttr('%s.trigger' %each,i)
                activeObjects.append(each)
    
    for active in activeObjects:
        frame=cmds.getAttr('%s.trigger' %active)
        x=bullet[active]
        cmds.setKeyframe('%s.bodyType' %x , t=frame ,v=2 )
        cmds.setKeyframe('%s.bodyType' %x , t=frame-1 ,v=0 )
    Interesting, possibly. Are you using opengl? Why aren't you using numpy? Is there an extremely dense object causing up to point in two directions? Is it a perspective affect? Need more information.

    Maybe you just need this link:
    http://cs.fit.edu/~wds/classes/cse5255/thesis/viewTrans/viewTrans.html#Alter
    Last edited by b49P23TIvg; November 9th, 2013 at 08:30 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo