0

I am writing a python program to replace all the walls I currently have in a Maya scene. The program should duplicate an existing wall I have in the scene, then replace every wall in the scene with it. The script properly duplicates the desired wall (Walls_Options:Wall_v*). Unfortunately, the program isn't placing them correctly. It seems to be placing them along the Y axis at random for some reason. It also doesn't rotate them at all. Any help is greatly appreciated.

from pymel.core import *
import random as rnd

wallChoices = ls("Walls_Options:Wall_v*", et="transform") #ls("newBrick", et="transform")

oldWalls = ls("Brick", et="transform")

for oldWall in oldWalls:
    select(oldWall, r=True)
    makeIdentity(apply=True, t=1, r=1, s=1, n=0)
    thisWallPos = xform(oldWall, query=True, worldSpace=True, t=True)
    thisWallRot = xform(oldWall, query=True, worldSpace=True, ro=True)
    print(oldWall)
    print("position is")
    print(thisWallPos)
    #print("rotation is")
    #print(thisWallRot)

    newThisWall = duplicate(wallChoices[0])
    xform(newThisWall, query=False, worldSpace=True, t=thisWallPos)



    ######not rotating correctly yet....
    xform(newThisWall, query=False, worldSpace=True, rt=thisWallRot)



    select(newThisWall, r=True)
    move(thisWallPos)
    rotate(thisWallRot)
    delete(oldWall)
    print(newThisWall)
    print(xform(newThisWall, query=True, worldSpace=True, rp=True))

1 Answers1

0

OK, so you're looping through your old walls, and you are using makeIdentity to freeze their transforms.

Now imagine you have an old wall that's rotated 90 degrees in x. Once you freeze its transforms the wall will stay where it's at but its rotations will bake down to 0. Now when you query its x rotation, it returns 0, as if it never rotated in the first place (even though it is!). So when you make your new wall and apply rotation on it, you're simply applying 0 in all x, y, and z axes. That's why you're not getting any rotation.

As for position, when you use makeIdentity on transforms it bakes all the translations to 0, except it transfers the old values into its pivots. You can see this by selecting your old wall and switching over to the Attribute Editor>Pivots>Local Space. If any of these values are not 0 then it means the object has an offset in its pivots. And using xform doesn't take these values into account when your query translation, so you end up with a weird offset. Now if you're using Maya 2016 or higher you can use matchTransform and that will take pivots into account. Otherwise you have to make another call with xform(q=True, pivots=True) and include these values in your calculation.

The easiest solution right now is to comment out makeIdentity so you don't freeze transforms on the old walls at all. That way your rotation stays intact, and you aren't creating pivot offsets.

Green Cell
  • 4,677
  • 2
  • 18
  • 49