Hi man! Glad to help!
Disclaimer: i moved away from the project because i actually changed job, and moreover in later iterations i was actually moving to cv2.
Anyway i digged out what i recall SHOULD be the latest working script i was up to. I based that upon what the real masters told me just in this post, so i'm just glad to help!
Let me know if something is not working, i can try to give you some more code if needed (this script was part of a more complex project, and frankly i dont remember well if this would be a stand alone).
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 25 09:43:06 2019
@author: Simone Lodi
"""
import mmapi
### from mmRemote import *
from mmRemote import mmRemote
import mm
import numpy as np
def GetCamCoord(continuos=False, FileName='default_output', verbose=False, world=False):
remote = mmRemote()
if continuos:
remote.connect()
while True:
###Variables:
f = mmapi.frame3f()
w = mmapi.frame3f()
target = mmapi.vec3f()
cam_info = mmapi.camera_info()
origin = mmapi.vec3f()
camTarget = mmapi.vec3f()
###Get camera data:
cmd2 = mmapi.StoredCommands()
cam_key = cmd2.CameraControl_QueryCamera()
remote.runCommand(cmd2)
cmd2.CameraControl_QueryCameraResult(cam_key,f,target,cam_info)
remote.runCommand(cmd2)
# Converting tangents to degree angles
Tans = (f.tan2_x,f.tan2_y,f.tan2_z)
Angle_Rad = np.arctan2(Tans)
Angle_Deg = Angle_Rad*180/np.pi
###Output data:
# DirOut = INSERT YOUR DIRECTORY;
if world:
### Conversion to 'real' World coordinates:
WoX = mm.toW(remote,f.origin_x)
WoY = mm.toW(remote,f.origin_y)
WoZ = mm.toW(remote,f.origin_z)
WtX = mm.toW(remote,target.x)
WtY = mm.toW(remote,target.y)
WtZ = mm.toW(remote,target.z)
Coord = [(WoX,WoY,WoZ),
(WtX,WtY,WtZ),
Angle_Deg]
else:
Coord = [(f.origin_x,f.origin_y,f.origin_z),
(target.x, target.y, target.z),
Angle_Deg]
if verbose:
print("RAW OUTPUT of QueryCamera in MM's normalized Scene coordinates:")
print("Cam coordinates: ", [f.origin_x,f.origin_y,f.origin_z])
print("Cam normal direction: ", [f.normal_x,f.normal_y,f.normal_z])
print("Cam left direction: " , [f.tan1_x,f.tan1_y,f.tan1_z])
print("Cam up direction: ", [f.tan2_x,f.tan2_y,f.tan2_z])
print("Target of the cams center line: " , [target.x, target.y, target.z ])
print("Field of View angle: " , cam_info.horzFOVDegrees)
print("Width of 3D scene (pixels): " , cam_info.width)
print("Height of 3D scene (pixels): " , cam_info.height)
##Needed data to set the cam back to a stored position:
print("CONVERTED to World coordinates:")
print("Cam coordinates (World): ", [origin.x, origin.y, origin.z ])
print("Target coordinates (World): ", [camTarget.x, camTarget.y, camTarget.z ])
print("Up direction normal: ", [f.tan2_x, f.tan2_y, f.tan2_z])
print(Coord)
remote.shutdown()
else:
remote.connect()
###Variables:
f = mmapi.frame3f()
w = mmapi.frame3f()
target = mmapi.vec3f()
cam_info = mmapi.camera_info()
origin = mmapi.vec3f()
camTarget = mmapi.vec3f()
###Get camera data:
cmd2 = mmapi.StoredCommands()
cam_key = cmd2.CameraControl_QueryCamera()
remote.runCommand(cmd2)
cmd2.CameraControl_QueryCameraResult(cam_key,f,target,cam_info)
remote.runCommand(cmd2)
###Output data:
DirOut = INSERT YOUR DIRECTORY;
# Coord = (Cam coordinates, Target coordinates, Cam up direction)
# Converting tangents to degree angles
Tans = (f.tan2_x,f.tan2_y,f.tan2_z)
Angle_Rad_xy = np.arctan2(f.tan2_x,f.tan2_y)
Angle_Rad_xz = np.arctan2(f.tan2_x,f.tan2_z)
Angle_Rad_yz = np.arctan2(f.tan2_y,f.tan2_z)
Angle_Deg_xy = Angle_Rad_xy*180/np.pi
Angle_Deg_xz = Angle_Rad_xz*180/np.pi
Angle_Deg_yz = Angle_Rad_yz*180/np.pi
Angle_Deg = [Angle_Deg_xy, Angle_Deg_xz, Angle_Deg_yz]
if world:
### Conversion to 'real' World coordinates:
# w.origin.x = mm.toW(remote,f.origin_x)
# w.origin.y = mm.toW(remote,f.origin_y)
# w.origin.z = mm.toW(remote,f.origin_z)
#
# w.target.x = mm.toW(remote,target.x)
# w.target.y = mm.toW(remote,target.y)
# w.target.z = mm.toW(remote,target.z)
WoX = mm.toW(remote,f.origin_x)
WoY = mm.toW(remote,f.origin_y)
WoZ = mm.toW(remote,f.origin_z)
WtX = mm.toW(remote,target.x)
WtY = mm.toW(remote,target.y)
WtZ = mm.toW(remote,target.z)
Coord = [(WoX,WoY,WoZ),
(WtX,WtY,WtZ),
Angle_Deg]
else:
Coord = [(f.origin_x,f.origin_y,f.origin_z),
(target.x, target.y, target.z),
Angle_Deg]
if verbose:
print("RAW OUTPUT of QueryCamera in MM's normalized Scene coordinates:")
print("Cam coordinates: ", [f.origin_x,f.origin_y,f.origin_z])
print("Cam normal direction: ", [f.normal_x,f.normal_y,f.normal_z])
print("Cam left direction: " , [f.tan1_x,f.tan1_y,f.tan1_z])
print("Cam up direction: ", [f.tan2_x,f.tan2_y,f.tan2_z])
print("Target of the cams center line: " , [target.x, target.y, target.z ])
print("Field of View angle: " , cam_info.horzFOVDegrees)
print("Width of 3D scene (pixels): " , cam_info.width)
print("Height of 3D scene (pixels): " , cam_info.height)
##Needed data to set the cam back to a stored position:
print("CONVERTED to World coordinates:")
print("Cam coordinates (World): ", [origin.x, origin.y, origin.z ])
print("Target coordinates (World): ", [camTarget.x, camTarget.y, camTarget.z ])
print("Up direction normal: ", [f.tan2_x, f.tan2_y, f.tan2_z])
CoordStr = str(Coord[0])+"\n"+str(Coord[1])+"\n"+str(Coord[2])+"\n"
Outfile = open(DirOut+FileName+'.txt','a+')
Outfile.write(CoordStr+'\n')
Outfile.close()
# Closing connectin to MeshMixer
remote.shutdown()
ACoord = np.around(Coord)
return Coord, ACoord
#[Coord,ACoord] = GetCamCoord(continuos=False, verbose=False, world=True)
#print(ACoord)