def exportNukeDewarpNode(id_cam,offset,nuke_path):
	id_lens 	= tde4.getCameraLens(id_cam)
	model 	= tde4.getLensLDModel(id_lens)
	num_frames 	= tde4.getCameraNoFrames(id_cam)
	w_fb_cm = tde4.getLensFBackWidth(id_lens)
	h_fb_cm = tde4.getLensFBackHeight(id_lens)
	lco_x_cm = tde4.getLensLensCenterX(id_lens)
	lco_y_cm = tde4.getLensLensCenterY(id_lens)
	pxa = tde4.getLensPixelAspect(id_lens)
# xa,xb,ya,yb in unit coordinates, in this order.
	xa_unit,xb_unit,ya_unit,yb_unit = tde4.getCameraFOV(id_cam)
		
	f = open(nuke_path,"w")
	try:
		f.write('# Created by 3DEqualizer4 using Export Nuke Distortion Nodes export script\n')
		f.write("LD" + nukify_name(model) + ' {\n')
		f.write(' direction undistort\n')
################################
# focal length                 #
################################
		if is_focal_length_dynamic(id_cam):
# write focal length curve if dynamic
#			print 'dynamic focal length'
			f.write(' tde4_focal_length_cm {{curve ')	
			for frame in range(num_frames):
# Internally, frames start at 1.
				focal = tde4.getCameraFocalLength(id_cam,frame + 1)
				f.write ('x%i %.7f ' % (frame + offset,focal))
			f.write('}}\n')
		else:
# write static focal length otherwise
#			print 'static focal length'
			f.write(' tde4_focal_length_cm %.7f \n' % tde4.getCameraFocalLength(id_cam,1))
################################
# focus distance               #
################################
# For Release 1 this function return False, so no problem with getCameraFocus.
		if is_focus_distance_dynamic(id_cam):
# write focus distance curve if dynamic
#			print 'dynamic focus distance'
			f.write(' tde4_custom_focus_distance_cm {{curve ')	
			for frame in range(num_frames):
# Internally, frames start at 1.
				focus = tde4.getCameraFocus(id_cam,frame + 1)
				f.write ('x%i %.7f ' % (frame + offset,focus))
			f.write('}}\n')
		else:
			try:
# write static focus distance otherwise
				f.write(' tde4_custom_focus_distance_cm %.7f \n' % tde4.getCameraFocus(id_cam,1))
			except:
# For Release 1 we simply write out the default value to Nuke.
				f.write(' tde4_custom_focus_distance_cm 100.0 \n')
################################
# built-in parameters          #
################################
# the remaining five built-in parameters
		f.write(' tde4_filmback_width_cm %.7f \n' % w_fb_cm)
		f.write(' tde4_filmback_height_cm %.7f \n' % h_fb_cm)
		f.write(' tde4_lens_center_offset_x_cm %.7f \n' % lco_x_cm)
		f.write(' tde4_lens_center_offset_y_cm %.7f \n' % lco_y_cm)
		f.write(' tde4_pixel_aspect %.7f \n' % pxa)
################################
# field-of-view                #
################################
		f.write(' field_of_view_xa_unit %.7f \n' % xa_unit)
		f.write(' field_of_view_xb_unit %.7f \n' % xb_unit)
		f.write(' field_of_view_ya_unit %.7f \n' % ya_unit)
		f.write(' field_of_view_yb_unit %.7f \n' % yb_unit)
		
# write distortion parameters
#
# dynamic distortion
		dyndistmode = get_dynamic_distortion_mode(id_lens)

		old_api = True
		try:
			for para in getLDmodelParameterList(model):
				tde4.getLensLDAdjustableParameter(id_lens, para, 1)
				break
		except:
			old_api = False

		if old_api:
# dynamic focal length (zoom)
			if dyndistmode=="DISTORTION_DYNAMIC_FOCAL_LENGTH":
#				print 'dynamic lens distortion, focal length'
				for para in getLDmodelParameterList(model):
					f.write(' ' + nukify_name(para) + ' {{curve ')	
					for frame in range(num_frames):
# Internally, frames start at 1.
						focal = tde4.getCameraFocalLength(id_cam,frame + 1)
						f.write ('x%i %.7f ' % (frame + offset,tde4.getLensLDAdjustableParameter(id_lens,para,focal)))
					f.write('}}\n')
# dynamic focus distance
			if dyndistmode=="DISTORTION_DYNAMIC_FOCUS_DISTANCE":
#				print 'dynamic lens distortion, focus distance'
				for para in getLDmodelParameterList(model):
					f.write(' ' + nukify_name(para) + ' {{curve ')	
					for frame in range(num_frames):
# Older Releases do not have Focus-methods.
						try:
# Internally, frames start at 1.
							focus = tde4.getCameraFocus(id_cam,frame + 1)
						except:
							focus = 100.0
						f.write('x%i %.7f ' % (frame + offset,tde4.getLensLDAdjustableParameter(id_lens,para,focus)))
					f.write('}}\n')
# static distortion
			if dyndistmode=="DISTORTION_STATIC":
#				print 'static lens distortion'
				for para in getLDmodelParameterList(model):
					f.write(' ' + nukify_name(para) + ' %.7f \n'%tde4.getLensLDAdjustableParameter(id_lens,para,1))
		else:
# new API
			if dyndistmode=="DISTORTION_STATIC":
#				print 'static lens distortion'
				for para in getLDmodelParameterList(model):
					f.write(' ' + nukify_name(para) + ' %.7f \n'%tde4.getLensLDAdjustableParameter(id_lens,para,1,1))
			else:
#				print 'dynamic lens distortion,'
# dynamic
				for para in getLDmodelParameterList(model):
					f.write(' ' + nukify_name(para) + ' {{curve ')	
					for frame in range(num_frames):
# Internally, frames start at 1.
						focal = tde4.getCameraFocalLength(id_cam,frame + 1)
						focus = tde4.getCameraFocus(id_cam,frame + 1)
						f.write('x%i %.7f ' % (frame + offset,tde4.getLensLDAdjustableParameter(id_lens,para,focal,focus)))
#						print "%i[3DE4] -> %i[Nuke]" % (frame + tde4.getCameraFrameOffset(id_cam),frame + offset)
					f.write('}}\n')


		
		f.write(' name LD_3DE4_' + decode_entities(tde4.getCameraName(id_cam)) + '\n')
		f.write('}\n')

	finally:	
		f.close()	
def exportNukeDewarpNode(id_cam,offset,nuke_path):
	id_lens 	= tde4.getCameraLens(id_cam)
	model 	= tde4.getLensLDModel(id_lens)
	num_frames 	= tde4.getCameraNoFrames(id_cam)
	w_fb_cm = tde4.getLensFBackWidth(id_lens)
	h_fb_cm = tde4.getLensFBackHeight(id_lens)
	lco_x_cm = tde4.getLensLensCenterX(id_lens)
	lco_y_cm = tde4.getLensLensCenterY(id_lens)
	pxa = tde4.getLensPixelAspect(id_lens)
# xa,xb,ya,yb in unit coordinates, in this order.
	xa_unit,xb_unit,ya_unit,yb_unit = tde4.getCameraFOV(id_cam)
		
	f = open(nuke_path,"w")
	try:
		f.write('# Created by 3DEqualizer4 using Export Nuke Distortion Nodes export script\n')
		f.write("LD" + nukify_name(model) + ' {\n')
		f.write(' direction undistort\n')
################################
# focal length                 #
################################
		if is_focal_length_dynamic(id_cam):
# write focal length curve if dynamic
#			print 'dynamic focal length'
			f.write(' tde4_focal_length_cm {{curve ')	
			for frame in range(num_frames):
# Internally, frames start at 1.
				focal = tde4.getCameraFocalLength(id_cam,frame + 1)
				f.write ('x%i %.7f ' % (frame + offset,focal))
			f.write('}}\n')
		else:
# write static focal length otherwise
#			print 'static focal length'
			f.write(' tde4_focal_length_cm %.7f \n' % tde4.getCameraFocalLength(id_cam,1))
################################
# focus distance               #
################################
# For Release 1 this function return False, so no problem with getCameraFocus.
		if is_focus_distance_dynamic(id_cam):
# write focus distance curve if dynamic
#			print 'dynamic focus distance'
			f.write(' tde4_custom_focus_distance_cm {{curve ')	
			for frame in range(num_frames):
# Internally, frames start at 1.
				focus = tde4.getCameraFocus(id_cam,frame + 1)
				f.write ('x%i %.7f ' % (frame + offset,focus))
			f.write('}}\n')
		else:
			try:
# write static focus distance otherwise
				f.write(' tde4_custom_focus_distance_cm %.7f \n' % tde4.getCameraFocus(id_cam,1))
			except:
# For Release 1 we simply write out the default value to Nuke.
				f.write(' tde4_custom_focus_distance_cm 100.0 \n')
################################
# built-in parameters          #
################################
# the remaining five built-in parameters
		f.write(' tde4_filmback_width_cm %.7f \n' % w_fb_cm)
		f.write(' tde4_filmback_height_cm %.7f \n' % h_fb_cm)
		f.write(' tde4_lens_center_offset_x_cm %.7f \n' % lco_x_cm)
		f.write(' tde4_lens_center_offset_y_cm %.7f \n' % lco_y_cm)
		f.write(' tde4_pixel_aspect %.7f \n' % pxa)
################################
# field-of-view                #
################################
		f.write(' field_of_view_xa_unit %.7f \n' % xa_unit)
		f.write(' field_of_view_xb_unit %.7f \n' % xb_unit)
		f.write(' field_of_view_ya_unit %.7f \n' % ya_unit)
		f.write(' field_of_view_yb_unit %.7f \n' % yb_unit)
		
# write distortion parameters
#
# dynamic distortion
		dyndistmode = get_dynamic_distortion_mode(id_lens)

		old_api = True
		try:
			for para in getLDmodelParameterList(model):
				tde4.getLensLDAdjustableParameter(id_lens, para, 1)
				break
		except:
			old_api = False

		if old_api:
# dynamic focal length (zoom)
			if dyndistmode=="DISTORTION_DYNAMIC_FOCAL_LENGTH":
#				print 'dynamic lens distortion, focal length'
				for para in getLDmodelParameterList(model):
					f.write(' ' + nukify_name(para) + ' {{curve ')	
					for frame in range(num_frames):
# Internally, frames start at 1.
						focal = tde4.getCameraFocalLength(id_cam,frame + 1)
						f.write ('x%i %.7f ' % (frame + offset,tde4.getLensLDAdjustableParameter(id_lens,para,focal)))
					f.write('}}\n')
# dynamic focus distance
			if dyndistmode=="DISTORTION_DYNAMIC_FOCUS_DISTANCE":
#				print 'dynamic lens distortion, focus distance'
				for para in getLDmodelParameterList(model):
					f.write(' ' + nukify_name(para) + ' {{curve ')	
					for frame in range(num_frames):
# Older Releases do not have Focus-methods.
						try:
# Internally, frames start at 1.
							focus = tde4.getCameraFocus(id_cam,frame + 1)
						except:
							focus = 100.0
						f.write('x%i %.7f ' % (frame + offset,tde4.getLensLDAdjustableParameter(id_lens,para,focus)))
					f.write('}}\n')
# static distortion
			if dyndistmode=="DISTORTION_STATIC":
#				print 'static lens distortion'
				for para in getLDmodelParameterList(model):
					f.write(' ' + nukify_name(para) + ' %.7f \n'%tde4.getLensLDAdjustableParameter(id_lens,para,1))
		else:
# new API
			if dyndistmode=="DISTORTION_STATIC":
#				print 'static lens distortion'
				for para in getLDmodelParameterList(model):
					f.write(' ' + nukify_name(para) + ' %.7f \n'%tde4.getLensLDAdjustableParameter(id_lens,para,1,1))
			else:
#				print 'dynamic lens distortion,'
# dynamic
				for para in getLDmodelParameterList(model):
					f.write(' ' + nukify_name(para) + ' {{curve ')	
					for frame in range(num_frames):
# Internally, frames start at 1.
						focal = tde4.getCameraFocalLength(id_cam,frame + 1)
						focus = tde4.getCameraFocus(id_cam,frame + 1)
						f.write('x%i %.7f ' % (frame + offset,tde4.getLensLDAdjustableParameter(id_lens,para,focal,focus)))
#						print "%i[3DE4] -> %i[Nuke]" % (frame + tde4.getCameraFrameOffset(id_cam),frame + offset)
					f.write('}}\n')


		
		f.write(' name LD_3DE4_' + decode_entities(tde4.getCameraName(id_cam)) + '\n')
		f.write('}\n')

	finally:	
		f.close()	
예제 #3
0
def _generate_v1(point_group,
                 camera,
                 points,
                 start_frame=None,
                 undistort=False):
    """
    Generate the UV file format contents, using a basic ASCII format.

    Each point will store:
    - Point name
    - X, Y position (in UV coordinates, per-frame)
    - Point weight (per-frame)

    :param point_group: The 3DE Point Group containing 'points'
    :type point_group: str

    :param camera: The 3DE Camera containing 2D 'points' data.
    :type camera: str

    :param points: The list of 3DE Points representing 2D data to
                   save.
    :type points: list of str

    :param start_frame: The frame number to be considered at
                       'first frame'. Defaults to 1001 if
                       set to None.
    :type start_frame: None or int

    :param undistort: Should we apply undistortion to the 2D points
                      data? Yes or no.
    :type undistort: bool

    :returns: A ASCII format string, with the UV Track data in it.
    :rtype: str
    """
    assert isinstance(point_group, basestring)
    assert isinstance(camera, basestring)
    assert isinstance(points, (list, tuple))
    assert start_frame is None or isinstance(start_frame, int)
    assert isinstance(undistort, bool)
    if start_frame is None:
        start_frame = 1001
    data_str = ''
    cam_num_frames = tde4.getCameraNoFrames(camera)
    camera_fov = tde4.getCameraFOV(camera)

    if len(points) == 0:
        return data_str

    frame0 = int(start_frame)
    frame0 -= 1

    data_str += '{0:d}\n'.format(len(points))

    for point in points:
        name = tde4.getPointName(point_group, point)
        c2d = tde4.getPointPosition2DBlock(point_group, point, camera, 1,
                                           cam_num_frames)
        valid_mode = _get_point_valid_mode(point_group, point)

        # Write per-frame position data
        num_valid_frame = 0
        pos_list = []
        weight_list = []
        frame = 1  # 3DE starts at frame '1' regardless of the 'start-frame'.
        for v in c2d:
            if v[0] == -1.0 or v[1] == -1.0:
                # No valid data here.
                frame += 1
                continue

            # Does the 2D point go outside the camera FOV? Is that ok?
            valid = tde4.isPointPos2DValid(point_group, point, camera, frame)
            if valid == 0:
                # No valid data here.
                frame += 1
                continue

            # Check if we're inside the FOV / Frame or not.
            valid_pos = _is_valid_position(v, camera_fov, valid_mode)
            if valid_pos is False:
                frame += 1
                continue

            # Number of points with valid positions
            num_valid_frame += 1

            f = frame + frame0
            if undistort is True:
                v = tde4.removeDistortion2D(camera, frame, v)
            weight = _get_point_weight(point_group, point, camera, frame)

            pos_list.append((f, v))
            weight_list.append((f, weight))
            frame += 1

        # add data
        data_str += name + '\n'
        data_str += '{0:d}\n'.format(num_valid_frame)
        for pos_data, weight_data in zip(pos_list, weight_list):
            f = pos_data[0]
            v = pos_data[1]
            w = weight_data[1]
            assert f == weight_data[0]
            data_str += '%d %.15f %.15f %.8f\n' % (f, v[0], v[1], w)

    return data_str
예제 #4
0
def _generate_v2_v3_and_v4(point_group,
                           camera,
                           points,
                           version=None,
                           **kwargs):
    """
    Generate the UV file format contents, using JSON format.

    Set the individual _generate_v2 or _generate_v3 functions for
    details of what is stored.

    :param point_group: The 3DE Point Group containing 'points'
    :type point_group: str

    :param camera: The 3DE Camera containing 2D 'points' data.
    :type camera: str

    :param points: The list of 3DE Points representing 2D data to
                   save.
    :type points: list of str

    :param version: The version of file to generate,
                    UV_TRACK_FORMAT_VERSION_2 or
                    UV_TRACK_FORMAT_VERSION_3.
    :type version: int

    :param start_frame: Format v2 and v3; The frame number to be
                        considered at 'first frame'.
                        Defaults to 1001 if set to None.
    :type start_frame: None or int

    :param undistort: Format v2; Should we apply undistortion to the 2D
                      points data? Yes or no.
    :type undistort: bool

    :returns: A JSON format string, with the UV Track data in it.
    :rtype: str
    """
    assert isinstance(point_group, basestring)
    assert isinstance(camera, basestring)
    assert isinstance(points, (list, tuple))
    assert isinstance(version, (int, long))
    assert version in [
        UV_TRACK_FORMAT_VERSION_2, UV_TRACK_FORMAT_VERSION_3,
        UV_TRACK_FORMAT_VERSION_4
    ]

    start_frame = kwargs.get('start_frame')
    assert start_frame is None or isinstance(start_frame, int)
    if start_frame is None:
        start_frame = 1001

    undistort = None
    if version == UV_TRACK_FORMAT_VERSION_2:
        undistort = kwargs.get('undistort')
        assert isinstance(undistort, bool)

    data = None
    if version == UV_TRACK_FORMAT_VERSION_2:
        data = UV_TRACK_HEADER_VERSION_2.copy()
    elif version == UV_TRACK_FORMAT_VERSION_3:
        data = UV_TRACK_HEADER_VERSION_3.copy()
    elif version == UV_TRACK_FORMAT_VERSION_4:
        data = UV_TRACK_HEADER_VERSION_4.copy()
    else:
        raise ValueError("Version number is invalid; %r" % version)

    cam_num_frames = tde4.getCameraNoFrames(camera)
    camera_fov = tde4.getCameraFOV(camera)

    if len(points) == 0:
        return ''

    frame0 = int(start_frame)
    frame0 -= 1

    data['num_points'] = len(points)
    data['is_undistorted'] = None
    if version == UV_TRACK_FORMAT_VERSION_2:
        data['is_undistorted'] = bool(undistort)

    data['points'] = []
    for point in points:
        point_data = {}

        # Query point information
        name = tde4.getPointName(point_group, point)
        uid = None
        if SUPPORT_PERSISTENT_ID is True:
            uid = tde4.getPointPersistentID(point_group, point)
        point_set = tde4.getPointSet(point_group, point)
        point_set_name = None
        if point_set is not None:
            point_set_name = tde4.getSetName(point_group, point_set)
        point_data['name'] = name
        point_data['id'] = uid
        point_data['set_name'] = point_set_name
        valid_mode = _get_point_valid_mode(point_group, point)

        # Get the 3D point position
        if version in [UV_TRACK_FORMAT_VERSION_3, UV_TRACK_FORMAT_VERSION_4]:
            point_data['3d'] = _get_3d_data_from_point(point_group, point)

        # Write per-frame position data
        frame = 1  # 3DE starts at frame '1' regardless of the 'start frame'.
        point_data['per_frame'] = []
        pos_block = tde4.getPointPosition2DBlock(point_group, point, camera, 1,
                                                 cam_num_frames)
        for pos in pos_block:
            if pos[0] == -1.0 or pos[1] == -1.0:
                # No valid data here.
                frame += 1
                continue

            # Is the 2D point obscured?
            valid = tde4.isPointPos2DValid(point_group, point, camera, frame)
            if valid == 0:
                # No valid data here.
                frame += 1
                continue

            # Check if we're inside the FOV / Frame or not.
            valid_pos = _is_valid_position(pos, camera_fov, valid_mode)
            if valid_pos is False:
                frame += 1
                continue

            pos_undist = pos
            if undistort is True or undistort is None:
                pos_undist = tde4.removeDistortion2D(camera, frame, pos)
            weight = _get_point_weight(point_group, point, camera, frame)

            f = frame + frame0
            frame_data = {'frame': f, 'pos': pos_undist, 'weight': weight}
            if version in [
                    UV_TRACK_FORMAT_VERSION_3, UV_TRACK_FORMAT_VERSION_4
            ]:
                frame_data['pos_dist'] = pos
            point_data['per_frame'].append(frame_data)
            frame += 1

        data['points'].append(point_data)

    if version == UV_TRACK_FORMAT_VERSION_4:
        lens = tde4.getCameraLens(camera)
        data['camera'] = _generate_camera_data(camera, lens, frame0)

    data_str = json.dumps(data)
    return data_str
예제 #5
0
def _remove_rs_from_2d_point(point_group, camera, frame, input_2d, depth):
    """
    Correct Rolling Shutter for the given input_2d point data, on frame.

    :param point_group: Camera Point Group for camera.
    :param camera: The camera to use for rolling shutter calculations.
    :param frame: The 2D point's frame number (in internal 3DE frame numbers).
    :param input_2d: Input 2D data.
    :param depth: The content distance to calculate rolling shutter at.

    :return: 2D point with corrected position.
    :rtype: [float, float]
    """
    assert isinstance(input_2d, vl_sdv.vec2d)
    num_frames = tde4.getCameraNoFrames(camera)
    if num_frames == 1:
        return input_2d

    # Static camera and lens values.
    camera_fps = tde4.getCameraFPS(camera)
    camera_fov = tde4.getCameraFOV(camera)
    lens = tde4.getCameraLens(camera)
    fbw = tde4.getLensFBackWidth(lens)
    fbh = tde4.getLensFBackHeight(lens)
    lcox = tde4.getLensLensCenterX(lens)
    lcoy = tde4.getLensLensCenterY(lens)
    rs_time_shift = tde4.getCameraRollingShutterTimeShift(camera)
    rs_value = rs_time_shift * camera_fps

    # Sample at previous frame
    prev_pos = vl_sdv.vec3d(0, 0, 0)
    prev_frame = frame - 1
    if frame > 1:
        prev_pos = _convert_2d_to_3d_point_undistort(
            point_group, camera,
            fbw, fbh, lcox, lcoy,
            camera_fov,
            prev_frame, input_2d, depth)

    # Sample at next frame
    next_pos = vl_sdv.vec3d(0, 0, 0)
    next_frame = frame + 1
    if frame < num_frames:
        next_pos = _convert_2d_to_3d_point_undistort(
            point_group, camera,
            fbw, fbh, lcox, lcoy,
            camera_fov,
            next_frame, input_2d, depth)

    # Sample at current frame
    curr_pos = _convert_2d_to_3d_point_undistort(
        point_group, camera,
        fbw, fbh, lcox, lcoy,
        camera_fov,
        frame, input_2d, depth)

    # Blend previous, next and current frame values based on the
    # position of the 2D point vertical position and the rolling
    # shutter value.
    if frame == 1:
        prev_pos = curr_pos + (curr_pos - next_pos)
    if frame == num_frames:
        next_pos = curr_pos + (curr_pos - prev_pos)
    t = rs_value * (1.0 - input_2d[1])
    curr_pos = _apply_rs_correction(-t, prev_pos, curr_pos, next_pos)

    # Back-projection
    focal = tde4.getCameraFocalLength(camera, frame)
    r3d = vl_sdv.mat3d(tde4.getPGroupRotation3D(point_group, camera, frame))
    p3d = vl_sdv.vec3d(tde4.getPGroupPosition3D(point_group, camera, frame))
    d = r3d.trans() * (curr_pos - p3d)
    p2d = [0, 0]
    p2d[0] = (d[0] * focal / (-d[2] * fbw)) + (lcox / fbw) + 0.5
    p2d[1] = (d[1] * focal / (-d[2] * fbh)) + (lcoy / fbh) + 0.5
    p = tde4.applyDistortion2D(camera, frame, p2d)
    left, right, bottom, top = camera_fov
    p = vl_sdv.vec2d((p[0] * (right - left)) + left,
                     (p[1] * (top - bottom)) + bottom)
    v = (input_2d + (input_2d - p)).list()
    return v
예제 #6
0
def generateNukeNode(cam, direction, offset=0, index=0):
    lens = tde4.getCameraLens(cam)
    model = tde4.getLensLDModel(lens)
    num_frames = tde4.getCameraNoFrames(cam)
    w_fb_cm = tde4.getLensFBackWidth(lens)
    h_fb_cm = tde4.getLensFBackHeight(lens)
    lco_x_cm = tde4.getLensLensCenterX(lens)
    lco_y_cm = tde4.getLensLensCenterY(lens)
    pxa = tde4.getLensPixelAspect(lens)
    # xa,xb,ya,yb in unit coordinates, in this order.
    fov = tde4.getCameraFOV(cam)

    print 'camera: ', tde4.getCameraName(cam)
    print 'offset:', offset
    print 'lens:', tde4.getLensName(lens)
    print 'model: ', model

    nukeNode = []

    nukeNode.append('# Created by 3DEqualizer4 ')
    nukeNode.append('# using Export Nuke Distortion Nodes export script')
    nukeNode.append("LD" + tools.nukify_name(model) + ' {')
    nukeNode.append(' direction ' + direction)

    # write focal length curve if dynamic
    if tde4.getCameraZoomingFlag(cam):
        print 'dynamic focal length'
        dynFocalLength = []

        for frame in range(1, num_frames + 1):
            dynFocalLength.append('x%i' % (frame + offset))
            dynFocalLength.append(' %.7f ' %
                                  tde4.getCameraFocalLength(cam, frame))
        focalLenghtStr = "".join(dynFocalLength)
        nukeNode.append(' tde4_focal_length_cm {{curve ' + focalLenghtStr +
                        ' }}')

# write static focal length else
    else:
        print 'static focal length'
        focalLenghtStr = ' tde4_focal_length_cm %.7f '
        focalLenghtStr = focalLenghtStr % tde4.getCameraFocalLength(cam, 1)
        nukeNode.append(focalLenghtStr)

# write focus distance curve if dynamic
    try:
        if tde4.getCameraFocusMode(cam) == "FOCUS_DYNAMIC":
            print 'dynamic focus distance'
            dynFocusDistance = []
            for frame in range(1, num_frames + 1):
                dynFocusDistance.append('x%i' % (frame + offset))
                dynFocusDistance.append(' %.7f ' %
                                        tde4.getCameraFocus(cam, frame))
            focusDStr = "".join(dynFocusDistance)
            toWrite = ' tde4_custom_focus_distance_cm {{curve ' + focusDStr + '}}'
            nukeNode.append(toWrite)
    except:
        # For 3DE4 Release 1:
        pass
# write static focus distance else
    else:
        print 'static focus distance'
        try:
            cameraFocus = ' tde4_custom_focus_distance_cm %.7f '
            cameraFocus = cameraFocus % tde4.getCameraFocus(cam, 1)
            nukeNode.append(cameraFocus)
        except:
            # For 3DE4 Release 1:
            nukeNode.append(' tde4_custom_focus_distance_cm 100.0 ')


# write camera
    nukeNode.append(' tde4_filmback_width_cm %.7f ' % w_fb_cm)
    nukeNode.append(' tde4_filmback_height_cm %.7f ' % h_fb_cm)
    nukeNode.append(' tde4_lens_center_offset_x_cm %.7f ' % lco_x_cm)
    nukeNode.append(' tde4_lens_center_offset_y_cm %.7f ' % lco_y_cm)
    nukeNode.append(' tde4_pixel_aspect %.7f ' % pxa)
    nukeNode.append(' field_of_view_xa_unit %.7f ' % fov[0])
    nukeNode.append(' field_of_view_ya_unit %.7f ' % fov[2])
    nukeNode.append(' field_of_view_xb_unit %.7f ' % fov[1])
    nukeNode.append(' field_of_view_yb_unit %.7f ' % fov[3])
    nukeNode.append(' filter Simon')

    # dynamic distortion
    try:
        dyndistmode = tde4.getLensDynamicDistortionMode(lens)
    except:
        # For 3DE4 Release 1:
        if tde4.getLensDynamicDistortionFlag(lens) == 1:
            dyndistmode = "DISTORTION_DYNAMIC_FOCAL_LENGTH"
        else:
            dyndistmode = "DISTORTION_STATIC"

    if dyndistmode == "DISTORTION_STATIC":
        print 'static lens distortion'
        for para in (tools.getLDmodelParameterList(model)):
            distStr = ' %.7f ' % tde4.getLensLDAdjustableParameter(
                lens, para, 1, 1)
            nukeNode.append(' ' + tools.nukify_name(para) + distStr)
    else:
        print 'dynamic lens distortion,'
        # dynamic
        for para in (tools.getLDmodelParameterList(model)):

            lensCurve = []
            for frame in range(1, num_frames + 1):
                focal = tde4.getCameraFocalLength(cam, frame)
                focus = tde4.getCameraFocus(cam, frame)
                lensCurve.append('x%i' % (frame + offset))
                frameParam = tde4.getLensLDAdjustableParameter(
                    lens, para, focal, focus)
                lensCurve.append(' %.7f ' % frameParam)
            lensCurveStr = ' {{curve ' + "".join(lensCurve) + ' }}'
            nukeNode.append(' ' + tools.nukify_name(para) + lensCurveStr)

    nameNode = 'name ' + getLDNodeName(cam, direction, offset, index)
    nukeNode.append(nameNode)
    nukeNode.append('}')

    return nukeNode
예제 #7
0
def _generate_v2(point_group,
                 camera,
                 points,
                 start_frame=None,
                 undistort=False):
    """
    Generate the UV file format contents, using JSON format.

    :param point_group: The 3DE Point Group containing 'points'
    :type point_group: str

    :param camera: The 3DE Camera containing 2D 'points' data.
    :type camera: str

    :param points: The list of 3DE Points representing 2D data to
                   save.
    :type points: list of str

    :param start_frame: The frame number to be considered at
                       'first frame'. Defaults to 1001 if
                       set to None.
    :type start_frame: None or int

    :param undistort: Should we apply undistortion to the 2D points
                      data? Yes or no.
    :type undistort: bool

    Each point will store:
    - Point name
    - X, Y position (in UV coordinates, per-frame)
    - Point weight (per-frame)
    - Point Set name
    - Point 'Persistent ID'
    """
    assert isinstance(point_group, basestring)
    assert isinstance(camera, basestring)
    assert isinstance(points, (list, tuple))
    assert start_frame is None or isinstance(start_frame, int)
    assert isinstance(undistort, bool)
    if start_frame is None:
        start_frame = 1001
    data = UV_TRACK_HEADER_VERSION_2.copy()
    cam_num_frames = tde4.getCameraNoFrames(camera)
    camera_fov = tde4.getCameraFOV(camera)

    if len(points) == 0:
        return ''

    frame0 = int(start_frame)
    frame0 -= 1

    data['num_points'] = len(points)
    data['is_undistorted'] = bool(undistort)

    data['points'] = []
    for point in points:
        point_data = {}

        # Query point information
        name = tde4.getPointName(point_group, point)
        uid = None
        if SUPPORT_PERSISTENT_ID is True:
            uid = tde4.getPointPersistentID(point_group, point)
        point_set = tde4.getPointSet(point_group, point)
        point_set_name = None
        if point_set is not None:
            point_set_name = tde4.getSetName(point_group, point_set)
        point_data['name'] = name
        point_data['id'] = uid
        point_data['set_name'] = point_set_name
        valid_mode = _get_point_valid_mode(point_group, point)

        # Write per-frame position data
        frame = 1  # 3DE starts at frame '1' regardless of the 'start frame'.
        point_data['per_frame'] = []
        pos_block = tde4.getPointPosition2DBlock(point_group, point, camera, 1,
                                                 cam_num_frames)
        for pos in pos_block:
            if pos[0] == -1.0 or pos[1] == -1.0:
                # No valid data here.
                frame += 1
                continue

            # Is the 2D point obscured?
            valid = tde4.isPointPos2DValid(point_group, point, camera, frame)
            if valid == 0:
                # No valid data here.
                frame += 1
                continue

            # Check if we're inside the FOV / Frame or not.
            valid_pos = _is_valid_position(pos, camera_fov, valid_mode)
            if valid_pos is False:
                frame += 1
                continue

            if undistort is True:
                pos = tde4.removeDistortion2D(camera, frame, pos)
            weight = _get_point_weight(point_group, point, camera, frame)

            f = frame + frame0
            frame_data = {'frame': f, 'pos': pos, 'weight': weight}
            point_data['per_frame'].append(frame_data)
            frame += 1

        data['points'].append(point_data)

    data_str = json.dumps(data)
    return data_str
def exportNukeDewarpNode(cam, offset, nuke_path):
    lens = tde4.getCameraLens(cam)
    model = tde4.getLensLDModel(lens)
    num_frames = tde4.getCameraNoFrames(cam)
    w_fb_cm = tde4.getLensFBackWidth(lens)
    h_fb_cm = tde4.getLensFBackHeight(lens)
    lco_x_cm = tde4.getLensLensCenterX(lens)
    lco_y_cm = tde4.getLensLensCenterY(lens)
    pxa = tde4.getLensPixelAspect(lens)
    # xa,xb,ya,yb in unit coordinates, in this order.
    fov = tde4.getCameraFOV(cam)

    print 'camera: ', tde4.getCameraName(cam)
    print 'offset:', offset
    print 'lens:', tde4.getLensName(lens)
    print 'model: ', model

    f = open(nuke_path, "w")
    try:
        f.write(
            '# Created by 3DEqualizer4 using Export Nuke Distortion Nodes export script\n'
        )
        f.write("LD" + nukify_name(model) + ' {\n')
        f.write(' direction undistort\n')

        # write focal length curve if dynamic
        if tde4.getCameraZoomingFlag(cam):
            print 'dynamic focal length'
            f.write(' tde4_focal_length_cm {{curve ')
            for frame in range(1, num_frames + 1):
                f.write('x%i' % (frame + offset))
                f.write(' %.7f ' % tde4.getCameraFocalLength(cam, frame))
            f.write('}}\n')
# write static focal length else
        else:
            print 'static focal length'
            f.write(' tde4_focal_length_cm %.7f \n' %
                    tde4.getCameraFocalLength(cam, 1))
# write focus distance curve if dynamic
        try:
            if tde4.getCameraFocusMode(cam) == "FOCUS_DYNAMIC":
                print 'dynamic focus distance'
                f.write(' tde4_custom_focus_distance_cm {{curve ')
                for frame in range(1, num_frames + 1):
                    f.write('x%i' % (frame + offset))
                    f.write(' %.7f ' % tde4.getCameraFocus(cam, frame))
                f.write('}}\n')
        except:
            # For 3DE4 Release 1:
            pass
# write static focus distance else
        else:
            print 'static focus distance'
            try:
                f.write(' tde4_custom_focus_distance_cm %.7f \n' %
                        tde4.getCameraFocus(cam, 1))
            except:
                # For 3DE4 Release 1:
                f.write(' tde4_custom_focus_distance_cm 100.0 \n')
# write camera
        f.write(' tde4_filmback_width_cm %.7f \n' % w_fb_cm)
        f.write(' tde4_filmback_height_cm %.7f \n' % h_fb_cm)
        f.write(' tde4_lens_center_offset_x_cm %.7f \n' % lco_x_cm)
        f.write(' tde4_lens_center_offset_y_cm %.7f \n' % lco_y_cm)
        f.write(' tde4_pixel_aspect %.7f \n' % pxa)
        f.write(' field_of_view_xa_unit %.7f \n' % fov[0])
        f.write(' field_of_view_ya_unit %.7f \n' % fov[2])
        f.write(' field_of_view_xb_unit %.7f \n' % fov[1])
        f.write(' field_of_view_yb_unit %.7f \n' % fov[3])

        # write distortion parameters
        #
        # dynamic distortion
        try:
            dyndistmode = tde4.getLensDynamicDistortionMode(lens)
        except:
            # For 3DE4 Release 1:
            if tde4.getLensDynamicDistortionFlag(lens) == 1:
                dyndistmode = "DISTORTION_DYNAMIC_FOCAL_LENGTH"
            else:
                dyndistmode = "DISTORTION_STATIC"

        old_api = True
        try:
            tde4.getLensLDAdjustableParameter(lens, para, 1)
        except:
            old_api = False

        if old_api:
            if dyndistmode == "DISTORTION_DYNAMIC_FOCAL_LENGTH":
                print 'dynamic lens distortion, focal length'
                # dynamic focal length (zoom)
                for para in (getLDmodelParameterList(model)):
                    f.write(' ' + nukify_name(para) + ' {{curve ')
                    for frame in range(1, num_frames + 1):
                        focal = tde4.getCameraFocalLength(cam, frame)
                        f.write('x%i' % (frame + offset))
                        f.write(' %.7f ' % tde4.getLensLDAdjustableParameter(
                            lens, para, focal))
                    f.write('}}\n')

            if dyndistmode == "DISTORTION_DYNAMIC_FOCUS_DISTANCE":
                print 'dynamic lens distortion, focus distance'
                # dynamic focus distance
                for para in (getLDmodelParameterList(model)):
                    f.write(' ' + nukify_name(para) + ' {{curve ')
                    for frame in range(1, num_frames + 1):
                        # Older Releases do not have Focus-methods.
                        try:
                            focus = tde4.getCameraFocus(cam, frame)
                        except:
                            focus = 100.0
                        f.write('x%i' % (frame + offset))
                        f.write(' %.7f ' % tde4.getLensLDAdjustableParameter(
                            lens, para, focus))
                    f.write('}}\n')

# static distortion
            if dyndistmode == "DISTORTION_STATIC":
                print 'static lens distortion'
                for para in (getLDmodelParameterList(model)):
                    f.write(' ' + nukify_name(para) + ' %.7f \n' %
                            tde4.getLensLDAdjustableParameter(lens, para, 1))
        else:
            # new API
            if dyndistmode == "DISTORTION_STATIC":
                print 'static lens distortion'
                for para in (getLDmodelParameterList(model)):
                    f.write(
                        ' ' + nukify_name(para) + ' %.7f \n' %
                        tde4.getLensLDAdjustableParameter(lens, para, 1, 1))
            else:
                print 'dynamic lens distortion,'
                # dynamic
                for para in (getLDmodelParameterList(model)):
                    f.write(' ' + nukify_name(para) + ' {{curve ')
                    for frame in range(1, num_frames + 1):
                        focal = tde4.getCameraFocalLength(cam, frame)
                        focus = tde4.getCameraFocus(cam, frame)
                        f.write('x%i' % (frame + offset))
                        f.write(' %.7f ' % tde4.getLensLDAdjustableParameter(
                            lens, para, focal, focus))
                    f.write('}}\n')

        f.write(' name LD_3DE4_' + decode_entities(tde4.getCameraName(cam)) +
                '\n')
        f.write('}\n')

    finally:
        f.close()
예제 #9
0
 def fov(self):
     return tde4.getCameraFOV(self._cam_id)