KeyShot Forum

Technical discussions => Extending KeyShot => Scripting => Topic started by: jbeau on July 22, 2019, 12:27:13 pm

Title: resetting object transforms
Post by: jbeau on July 22, 2019, 12:27:13 pm
How do I compare matrices and apply?
If I don't compare, I have success but I don't see any changes applied.

Code: [Select]
root = lux.getSceneTree()
for node in root.find("test"):
     noTrans = luxmath.Matrix.makeIdentity().val()
     #compare object matrix to default matrix
     if node.getTransform() != noTrans:
          #if object matrix is not default, apply default transforms
          node.applyTransform(noTrans)
     else:
          print('transform set to default already:', node)

Is there a way to use the reset transform, similar to the button option in position?
I can't seem to split the result of getTransform() to return the matrix only for comparison.
Thanks,
J
Title: Re: resetting object transforms
Post by: jbeau on July 22, 2019, 08:29:22 pm
Just doing some quick testing...
"externalShell_grp" has an X- transform of 10 units.

Code: [Select]
root = lux.getSceneTree()
val = luxmath.Matrix().makeIdentity().val()
print('val:', val)
for node in root.find(name = "externalShell_grp"):
     name = node.getName()
     print('nodeName:', name)
     print('/n')

     print('node:', node)
     print('/n')

     getTrans = node.getTransform()
     print('getTrans:', getTrans) 
     print('/n')

     Matrix = luxmath.Matrix(val).makeIdentity()
     print('Matrix:', Matrix) 
     
     node.applyTransform(Matrix)

returns:
val: (1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0)
nodeName: externalShell_grp
/n
node: <lux.SceneNode object at 0x12c808768, name: "externalShell_grp", kind: Group>
/n
getTrans: <luxmath.Matrix object at 0x12c808840>
(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 10.0, 0.0, 0.0, 1.0)
/n
Matrix: <luxmath.Matrix object at 0x12c808708>
(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0)

If val() is just a tuple, maybe I can use that to compare?
It prints successful but the transforms don't get updated. What am I doing wrong?
Title: Re: resetting object transforms
Post by: jbeau on July 22, 2019, 08:45:18 pm
Comparing tuples worked using val() :)

Code: [Select]
root = lux.getSceneTree()
val = luxmath.Matrix().makeIdentity().val()
print('val:', val)
for node in root.find(name = "externalShell_grp"):
     getTrans = node.getTransform().val()
     if getTrans == val:
          print(node.getName(), "has default transforms.")
     else:
          print(node.getName(), "does not have default transforms")

Now how to apply them...
Title: Re: resetting object transforms
Post by: jbeau on July 22, 2019, 11:06:58 pm
Boom 8) working reset for translate only:
Code: [Select]
root = lux.getSceneTree()
for node in root.find(name = "replaceObjectNameHere"):
     #get transformations for scale, rotation and translate
     getTrans = node.getTransform().getTransformation()

     #isolate translations
     translation = list(getTrans)[-1].val()

     #not sure if .mul() is the best way to reset, any ideas? maybe .invert()
     Matrix = luxmath.Matrix().makeIdentity().translate(luxmath.Vector(translation).mul(-1))
     node.applyTransform(Matrix)
Title: Re: resetting object transforms
Post by: jbeau on July 27, 2019, 01:18:27 pm
Are there any flags for resetting local transforms?
Similar to scene/object/position/reset.
I've been able to get position and scale, but absolute rotate values are converted to something completely different:(
Title: Re: resetting object transforms
Post by: Jérémy on November 07, 2019, 09:19:23 am
I'm trying to reset object to his initial position without success...
I tested with setTransformation only and with Rotate then Translate but none of this works.

How can i code the reset button?

Code: [Select]
root = lux.getSceneTree()
for ch in root.find(name = "test_node"):
    # Get the transformation matrix of node
    matrice = ch.getTransform()

    # Get the scale vector, rotation and translation of matrix
    tup_vector = matrice.getTransformation()

    scale_vector = tup_vector[0].mul(-1).val()
    rotation_vector = tup_vector[1].mul(-1).val()
    transformation_vector = tup_vector[2].mul(-1).val()
                           
    #print(scale_vector)
    #print(rotation_vector)
    #print(transformation_vector)
                           
    Matrix = luxmath.Matrix().makeIdentity().setTransformation(scale_vector, rotation_vector, transformation_vector)
    ch.applyTransform(Matrix)
                       
    #Matrix = luxmath.Matrix().makeIdentity().rotate(rotation_vector)
    #ch.applyTransform(Matrix)
                           
    #Matrix = luxmath.Matrix().makeIdentity().translate(transformation_vector)
    #ch.applyTransform(Matrix)