## Recommended Posts

Hope this isn't a newbie question but I didn't see anything about it when I searched. I have a camera rotated in space and I need to convert the direction the camera is facing into a vector. Does anyone know how I would do this?

Thanks

##### Share on other sites

Hope this isn't a newbie question but I didn't see anything about it when I searched. I have a camera rotated in space and I need to convert the direction the camera is facing into a vector. Does anyone know how I would do this?

Thanks

Hi,TonyL,Maybe the easiest trick is by using the "Look at" of your camera,that is,let the camera face the null object,then subtract the position of your camera from the null,the result is the facing vector.

more rigorously,the facing vector=R(z)R(y)R(x)[0 0 -1]T

R(z),R(y),R(x) is the roation matrix about the z,y,x respectively,[0 0 -1]T is the camera original facing direction. but I would not like to get through such pain way ##### Share on other sites

Hi,TonyL,Maybe the easiest trick is by using the "Look at" of your camera,that is,let the camera face the null object,then subtract the position of your camera from the null,the result is the facing vector.

Actually (I didn't explain this before), the null object in your example is essentially what I'm solving for. I want to have an object in the dead center of the camera's field of view and directly facing the camera. We'll get the distance from the camera from a user-controlled slider. The idea is if I can get the direction it's looking at in terms of a normalized vector, then I can use the distance and position of the camera to determine that object's position in space. This centering is just for initial (and final) placement, after that the camera will orbit the object (right now a grid of cubes) and the cubes will reconfigure to again face the camera and be in the middle of the shot.

more rigorously,the facing vector=R(z)R(y)R(x)[0 0 -1]T

R(z),R(y),R(x) is the roation matrix about the z,y,x respectively,[0 0 -1]T is the camera original facing direction. but I would not like to get through such pain way I don't quite understand your equation, could you explain this a little more? How would this look as an Hscript or Python equation inside Houdini, for example?

##### Share on other sites

Actually (I didn't explain this before), the null object in your example is essentially what I'm solving for. I want to have an object in the dead center of the camera's field of view and directly facing the camera. We'll get the distance from the camera from a user-controlled slider. The idea is if I can get the direction it's looking at in terms of a normalized vector, then I can use the distance and position of the camera to determine that object's position in space. This centering is just for initial (and final) placement, after that the camera will orbit the object (right now a grid of cubes) and the cubes will reconfigure to again face the camera and be in the middle of the shot.

I don't quite understand your equation, could you explain this a little more? How would this look as an Hscript or Python equation inside Houdini, for example?

Hello, Tony! Please look the example file.

the main function is writen in 'Expressions' Tab of 'aliases and variables' menu

here is the code

```vector nd(float x,float y, float z)
{
nx =-(cos(z)*sin(y)*sin(x) - sin(z)*cos(x));
ny =-(sin(z)*sin(y)*sin(x) + cos(z)*cos(x));
nz =-(cos(y)*sin(x));
return vector3(nx, ny, nz);
}
```

this the same what was said upper. Rotating 'xform1' SOP => setting the direction of 'line3' SOP

Hope this helps!

Edited by mic
• 1

##### Share on other sites

Hello, Tony! Please look the example file.

the main function is writen in 'Expressions' Tab of 'aliases and variables' menu

here is the code

```vector nd(float x,float y, float z)
{
nx =-(cos(z)*sin(y)*sin(x) - sin(z)*cos(x));
ny =-(sin(z)*sin(y)*sin(x) + cos(z)*cos(x));
nz =-(cos(y)*sin(x));
return vector3(nx, ny, nz);
}
```

this the same what was said upper. Rotating 'xform1' SOP => setting the direction of 'line3' SOP

Hope this helps!

forgot to attach the file...

rotations_to_vector.hipnc

##### Share on other sites

Thanks mic for your code, I was finally able to try it out in my project (my wife and I were putting on a gathering that took all my time the last few days). It's definitely closer, that's for sure. The equations for X and Z seem to be working, but Y isn't there yet, which you can check out in the attached file.

I'm using the degree to vector expression to help place objects in space at a specific point of the camera's field of view (right now it's dead center). I have a distance variable in the data node of gridobject1 which is affecting a sphere. When working correctly the sphere should be right in the middle of cam1's field of view, and would only appear to get bigger or smaller depending on the distance variable. As you can see, as you increase distance the sphere drops off the bottom of the screen.

Also, right now z rotation is zero, but if you change that you'll notice that X (and I think Z as well) will also be off.

box_transition_02.hipnc

##### Share on other sites

As much as I love doing all the math myself, I found it easier to just accomplish this using some linear algebra in a Python Object. In the attached scene I quickly wrote a simple object that just takes a pointed to camera and a distance, and transforms whatever is inside/connected to the object along the along the vector of the camera to the specified distance.

box_transition_hom.hipnc

##### Share on other sites

Hey, I'm all for letting the computer do the math, that's fantastic! I think that's what I'm looking for! I'd like to get more into Python, is it possible to see the Python code that set this up?

Thanks,

Tony

##### Share on other sites

As much as I love doing all the math myself, I found it easier to just accomplish this using some linear algebra in a Python Object.

I must be missing something here. Why can't you just use a Fetch object to fetch the camera transform and then just put -ch("distance") into the sphere object's tz?

##### Share on other sites

You can view it in the Code tab of the Operator Type Properties window.

That's great, thank you so much for taking the time to help me out!

##### Share on other sites

One last thing. I noticed the digital asset is embedded. Is there a way to un-embed the asset or save it out to disk so I can put it in my original file? And once in that file, is there a way to re-embed it?

##### Share on other sites

Wait, I think I figured that out. Copy Operator Type, then Save to Library and create a new otl, then go to file to bring it into, make an instance, copy again, and in save to Library enter "embedded".

##### Share on other sites

Hello, Tony! Please look the example file.

the main function is writen in 'Expressions' Tab of 'aliases and variables' menu

here is the code

```vector nd(float x,float y, float z)
{
nx =-(cos(z)*sin(y)*sin(x) - sin(z)*cos(x));
ny =-(sin(z)*sin(y)*sin(x) + cos(z)*cos(x));
nz =-(cos(y)*sin(x));
return vector3(nx, ny, nz);
}
```

this the same what was said upper. Rotating 'xform1' SOP => setting the direction of 'line3' SOP

Hope this helps!

I know this is an old thread, but thanks for that example. Here it is in Python for anyone else in the future. If the code is in the hou.session module (Python Source Editor window) then it can be called on parameter expressions.

```import math
import hou

def angleToVector(rx,ry,rz,length):
vx = -(math.cos(rz) * math.sin(ry) * math.sin(rx) - math.sin(rz) * math.cos(rx))
vy = -(math.sin(rz) * math.sin(ry) * math.sin(rx) + math.cos(rz) * math.cos(rx))
vz = -(math.cos(ry) * math.sin(rx))
return hou.Vector3(vx,vy,vz) * length
```

On a parameter expression it can be called like this for each component of a vector. In this case I used it for gravity in a DOP. The fourth argument is a length multiplier, so I wanted the force to be 400 in the example instead of the unit length vector.

```# For X axis parameter
hou.session.angleToVector(0,0,-25,400)

# For Y axis parameter
hou.session.angleToVector(0,0,-25,400)

# For Z axis parameter
hou.session.angleToVector(0,0,-25,400)
```

## Create an account

Register a new account