From 28604c46a137c1288cc7a494b36ed72e44a0ab8b Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Wed, 10 Feb 2016 15:09:45 +0100 Subject: Cycles: Make Blender importer more forward compatible Basically the idea is to make code robust against extending enum options in the future by falling back to a known safe default setting when RNA is set to something unknown. While this approach solves the issues similar to T47377, but it wouldn't really help when/if any of the RNA values gets ever deprecated and removed. There'll be no simple solution to that apart from defining explicit mapping from RNA value to Cycles one. Another part which isn't so great actually is that we now have to have some enum guards and give some explicit values to the enum items, but we can live with that perhaps. Reviewers: dingto, juicyfruit, lukasstockner97, brecht Reviewed By: brecht Differential Revision: https://developer.blender.org/D1785 --- intern/cycles/blender/blender_shader.cpp | 74 +++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 15 deletions(-) (limited to 'intern/cycles/blender/blender_shader.cpp') diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp index b82e7dd47b7..3e490ed7584 100644 --- a/intern/cycles/blender/blender_shader.cpp +++ b/intern/cycles/blender/blender_shader.cpp @@ -51,6 +51,50 @@ void BlenderSync::find_shader(BL::ID& id, } } +/* RNA translation utilities */ + +static VolumeSampling get_volume_sampling(PointerRNA& ptr) +{ + return (VolumeSampling)get_enum(ptr, + "volume_sampling", + VOLUME_NUM_SAMPLING, + VOLUME_SAMPLING_DISTANCE); +} + +static VolumeInterpolation get_volume_interpolation(PointerRNA& ptr) +{ + return (VolumeInterpolation)get_enum(ptr, + "volume_interpolation", + VOLUME_NUM_INTERPOLATION, + VOLUME_INTERPOLATION_LINEAR); +} + +static int validate_enum_value(int value, int num_values, int default_value) +{ + if(value >= num_values) { + return default_value; + } + return value; +} + +template +static InterpolationType get_image_interpolation(NodeType b_node) +{ + int value = b_node.interpolation(); + return (InterpolationType)validate_enum_value(value, + INTERPOLATION_NUM_TYPES, + INTERPOLATION_LINEAR); +} + +template +static ExtensionType get_image_extension(NodeType b_node) +{ + int value = b_node.extension(); + return (ExtensionType)validate_enum_value(value, + EXTENSION_NUM_TYPES, + EXTENSION_REPEAT); +} + /* Graph */ static BL::NodeSocket get_node_output(BL::Node& b_node, const string& name) @@ -653,14 +697,14 @@ static ShaderNode *add_node(Scene *scene, scene->image_manager->tag_reload_image( image->filename, image->builtin_data, - (InterpolationType)b_image_node.interpolation(), - (ExtensionType)b_image_node.extension()); + get_image_interpolation(b_image_node), + get_image_extension(b_image_node)); } } image->color_space = ImageTextureNode::color_space_enum[(int)b_image_node.color_space()]; image->projection = ImageTextureNode::projection_enum[(int)b_image_node.projection()]; - image->interpolation = (InterpolationType)b_image_node.interpolation(); - image->extension = (ExtensionType)b_image_node.extension(); + image->interpolation = get_image_interpolation(b_image_node); + image->extension = get_image_extension(b_image_node); image->projection_blend = b_image_node.projection_blend(); BL::TexMapping b_texture_mapping(b_image_node.texture_mapping()); get_tex_mapping(&image->tex_mapping, b_texture_mapping); @@ -696,14 +740,15 @@ static ShaderNode *add_node(Scene *scene, /* TODO(sergey): Does not work properly when we change builtin type. */ if(b_image.is_updated()) { - scene->image_manager->tag_reload_image(env->filename, - env->builtin_data, - (InterpolationType)b_env_node.interpolation(), - EXTENSION_REPEAT); + scene->image_manager->tag_reload_image( + env->filename, + env->builtin_data, + get_image_interpolation(b_env_node), + EXTENSION_REPEAT); } } env->color_space = EnvironmentTextureNode::color_space_enum[(int)b_env_node.color_space()]; - env->interpolation = (InterpolationType)b_env_node.interpolation(); + env->interpolation = get_image_interpolation(b_env_node); env->projection = EnvironmentTextureNode::projection_enum[(int)b_env_node.projection()]; BL::TexMapping b_texture_mapping(b_env_node.texture_mapping()); get_tex_mapping(&env->tex_mapping, b_texture_mapping); @@ -824,8 +869,7 @@ static ShaderNode *add_node(Scene *scene, point_density->filename = b_point_density_node.name(); point_density->space = PointDensityTextureNode::space_enum[(int)b_point_density_node.space()]; - point_density->interpolation = - (InterpolationType)b_point_density_node.interpolation(); + point_density->interpolation = get_image_interpolation(b_point_density_node); point_density->builtin_data = b_point_density_node.ptr.data; /* 1 - render settings, 0 - vewport settings. */ @@ -1200,8 +1244,8 @@ void BlenderSync::sync_materials(bool update_all) shader->use_mis = get_boolean(cmat, "sample_as_light"); shader->use_transparent_shadow = get_boolean(cmat, "use_transparent_shadow"); shader->heterogeneous_volume = !get_boolean(cmat, "homogeneous_volume"); - shader->volume_sampling_method = (VolumeSampling)get_enum(cmat, "volume_sampling"); - shader->volume_interpolation_method = (VolumeInterpolation)get_enum(cmat, "volume_interpolation"); + shader->volume_sampling_method = get_volume_sampling(cmat); + shader->volume_interpolation_method = get_volume_interpolation(cmat); shader->set_graph(graph); shader->tag_update(scene); @@ -1231,8 +1275,8 @@ void BlenderSync::sync_world(bool update_all) /* volume */ PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles"); shader->heterogeneous_volume = !get_boolean(cworld, "homogeneous_volume"); - shader->volume_sampling_method = (VolumeSampling)get_enum(cworld, "volume_sampling"); - shader->volume_interpolation_method = (VolumeInterpolation)get_enum(cworld, "volume_interpolation"); + shader->volume_sampling_method = get_volume_sampling(cworld); + shader->volume_interpolation_method = get_volume_interpolation(cworld); } else if(b_world) { ShaderNode *closure, *out; -- cgit v1.2.3