Bending body part via script

SloshSlosh Posts: 2,391

I am trying to write this bit of code, but cannot find any references to acceptable syntax. I would like to do the following:

While having Genesis 2 Female selected (or any part of her body), I would like to bend her forearm, say 16.5 units. So I need to be able to tell the script that G2F is selected, find her left forearm, get it's current z rotation and assign that to a variable.

Please help. The current documentation is incomplete, and I don't know what to do or where to find some examples.

Thanks,
Slosh

Comments

  • SloshSlosh Posts: 2,391
    edited December 1969

    Never mind, I figured it out. Thanks anyway!

  • SloshSlosh Posts: 2,391
    edited December 1969

    OK, I'm still having a problem. In my script, I can apply an action to the selected figure, for example Genesis. I can make her move in the Z direction 1 unit. What I would like to do is select Genesis, then have the script assign her forearm to a variable, which I would then bend via a function. At this point, I don't know how to tell the script to select her forearm.

    I am using Scene.findNodeByLabel("Left Forearm") which does select her forearm. The problem is, by using "Scene", the script applies the function to the first listed object in the scene, not the selected object. More specifically, if I have 2 Genesis figures in the scene, no matter which Genesis I have selected, it applies the forearm bend to the first listed Genesis. How do I use the "findNodeByLabel" command on the selected figure?

    This is for a script that will be very useful, so I would really like to get it worked out. The script works great if you only have one figure in the scene, but we all know that is often not the case when setting up our scenes.

  • Richard HaseltineRichard Haseltine Posts: 101,516
    edited December 1969

    Use DzNode.findNodeChild( name, true ) on the selected figure - name being the name, not the label, of the bone you want and the true telling it to scan through the figure's hierarchy rather than just looking at the immediate children (which would be the hip only).

  • SloshSlosh Posts: 2,391
    edited December 1969

    Thank you, Richard. Also, the array info you gave me the other day worked out great. I have a new issue, which I can't figure out. When I use a function to add a value to the ZRotation of a bone, the math is not working out. Specifically, I have a bone whose ZRotate (Bend) is originally set at (-11.86) and I want to add (16.31) to it, which should make the new value (4.45), but instead it is adding (4.45) to (-11.86) and giving me (-7.41). How is that happening? I do notice that the bone already has a set minimum limit of -10, but I turned limits off for the figure.

    Other rotations, such as Twist and Side-Side are computing properly, and sometimes Bend does, too. But other times, it comes out wrong. Any ideas?

  • millighostmillighost Posts: 261
    edited December 1969

    Slosh said:
    OK, I'm still having a problem. In my script, I can apply an action to the selected figure, for example Genesis. I can make her move in the Z direction 1 unit. What I would like to do is select Genesis, then have the script assign her forearm to a variable, which I would then bend via a function. At this point, I don't know how to tell the script to select her forearm.

    I am using Scene.findNodeByLabel("Left Forearm") which does select her forearm. The problem is, by using "Scene", the script applies the function to the first listed object in the scene, not the selected object. More specifically, if I have 2 Genesis figures in the scene, no matter which Genesis I have selected, it applies the forearm bend to the first listed Genesis. How do I use the "findNodeByLabel" command on the selected figure?

    This is for a script that will be very useful, so I would really like to get it worked out. The script works great if you only have one figure in the scene, but we all know that is often not the case when setting up our scenes.


    If you have the skeleton for the selected figure, you can do something like this:
    
    /*
     * get the selected figures
     */
    var figs = Scene.getSelectedSkeletonList ();
    /*
     * search for the left hand in all selected figures
     */
    for (var fig_idx = 0; fig_idx != figs.length; ++ fig_idx) {
     var bone = figs[fig_idx].findBoneByLabel ("lHand");
     /*
      * do some complicated action with the left hand here....
      */
     print ("found a hand: ", bone.name);
    }
    

    Richard's method with the findNodeChild() would work also, but is different, because it searches the whole hierarchy. That means it will also search in child objects that do not belong to the currently selected figure (e.g. a clothing item also might have a "Left Hand"), so even if you have only one selected figure, you might have multiple "Left Hands". Which might be a problem or not, depending on the circumstances and what you want to do with it.
  • SloshSlosh Posts: 2,391
    edited December 1969

    I think I love you, millighost. That worked perfectly! Thank you so much.

    I want to thank Richard, too. Although his method did not work for me, the script did not like me using DzNode. I'm sure I used it wrong, but thankfully millighost's method workd.

    Anyone have any ideas on the funky math?

  • millighostmillighost Posts: 261
    edited December 1969

    Slosh said:
    Thank you, Richard. Also, the array info you gave me the other day worked out great. I have a new issue, which I can't figure out. When I use a function to add a value to the ZRotation of a bone, the math is not working out. Specifically, I have a bone whose ZRotate (Bend) is originally set at (-11.86) and I want to add (16.31) to it, which should make the new value (4.45), but instead it is adding (4.45) to (-11.86) and giving me (-7.41). How is that happening? I do notice that the bone already has a set minimum limit of -10, but I turned limits off for the figure.

    Other rotations, such as Twist and Side-Side are computing properly, and sometimes Bend does, too. But other times, it comes out wrong. Any ideas?


    How do you add the to the rotation? To simulate something like the user does when sliding the sliders, it should look more or less like this:
    
    /*
     * add 10 to the (local) rotation around Y:
     */
    c = node.getYRotControl ();
    c.setValue (c.getValue () + 10);
    
  • Richard HaseltineRichard Haseltine Posts: 101,516
    edited December 1969

    The figure is a DzNode, so you'd just use the findChildNode function on that - no need to create a separate DzNode (if that's what you were doing). Though millighost's alternative is more efficient anyway.

    As for the weird maths, is there any ERC on the parameter with the master control set?

  • SloshSlosh Posts: 2,391
    edited December 1969

    Thanks to millighost, my script is doing exactly what I wanted it to do, as well as overcoming that "math" problem, which stemmed from the raw values being factored into the equation.

    I wish there was a developers' chatroom. I feel like I could learn a lot from talking to people like millighost and Richard. Sometimes PM and forum posts are a bit like snail mail and it would be great to share ideas and knowledge on a more direct level.

    But until that day, I am still grateful for the forum and helpful people.

  •  

    Slosh said:

    OK, I'm still having a problem. In my script, I can apply an action to the selected figure, for example Genesis. I can make her move in the Z direction 1 unit. What I would like to do is select Genesis, then have the script assign her forearm to a variable, which I would then bend via a function. At this point, I don't know how to tell the script to select her forearm.

     

    I am using Scene.findNodeByLabel("Left Forearm") which does select her forearm. The problem is, by using "Scene", the script applies the function to the first listed object in the scene, not the selected object. More specifically, if I have 2 Genesis figures in the scene, no matter which Genesis I have selected, it applies the forearm bend to the first listed Genesis. How do I use the "findNodeByLabel" command on the selected figure?

    This is for a script that will be very useful, so I would really like to get it worked out. The script works great if you only have one figure in the scene, but we all know that is often not the case when setting up our scenes.


    If you have the skeleton for the selected figure, you can do something like this:
     
    /* * get the selected figures */var figs = Scene.getSelectedSkeletonList ();/* * search for the left hand in all selected figures */for (var fig_idx = 0; fig_idx != figs.length; ++ fig_idx) { var bone = figs[fig_idx].findBoneByLabel ("lHand"); /*  * do some complicated action with the left hand here....  */ print ("found a hand: ", bone.name);}

    Richard's method with the findNodeChild() would work also, but is different, because it searches the whole hierarchy. That means it will also search in child objects that do not belong to the currently selected figure (e.g. a clothing item also might have a "Left Hand"), so even if you have only one selected figure, you might have multiple "Left Hands". Which might be a problem or not, depending on the circumstances and what you want to do with it.

     

    My aim is to select rCollar bone from the entire body then move it. So I am using help from the discussion here. I can move the select the bone and run the script using Scene.getSelectedNodeList();. But I want the script to automatically find the bone and do the processing and save my result.

    When I am trying to use the statement  print ("found a hand: ", bone.name);

    I am getting an error 

    Script Error: Line 21

    TypeError: Result of expression 'bone' [null] is not an object.
    Stack Trace: ()@:21

     

    I dont know how to correct it.

     

  • us

    if ( bone.inherits( "DzBone" ) {

    print ("found a hand: ", bone.name);

    }

    to check you have a valid retunrn object before trying to do anything with it. I try to check everything, even if there's not much chance of its failing, to be on the safe side.

  • rbtwhizrbtwhiz Posts: 2,267
    edited February 2016

    That still assumes that the bone variable is not undefined or null. You'll want to at least check that the bone variable is a truthy value before you attempt to call inherits on it:

    if( bone && bone.inherits("DzBone")){	// ... }

    You can go a little further and even test for inequality with undefined and null, or use a try...catch statement:

    if( bone !== undefined && bone !== null && bone.inherits("DzBone")){	// ... }

    -Rob

    Post edited by rbtwhiz on
Sign In or Register to comment.