[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24105] trunk/blender/source/blender/ makesrna: Bugfix #19729: Color Ramps are not animatable (Part 1)

Joshua Leung aligorith at gmail.com
Tue Oct 27 11:14:03 CET 2009


Revision: 24105
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24105
Author:   aligorith
Date:     2009-10-27 11:14:02 +0100 (Tue, 27 Oct 2009)

Log Message:
-----------
Bugfix #19729: Color Ramps are not animatable (Part 1)

This first part of the fix makes it possible to animate ramp settings by making sure that the paths for ramps and their elements can be determined. While the code for constructing the path to the ramps is relatively simple, the code for the elements is a bit more involved :/

However, this commit only fixes the paths, but most of the ramp settings still cannot be keyframed directly from the UI buttons/widgets (i.e. from Material/Texture buttons) since the buttons still use the old layouts.

Modified Paths:
--------------
    trunk/blender/source/blender/makesrna/RNA_access.h
    trunk/blender/source/blender/makesrna/intern/rna_access.c
    trunk/blender/source/blender/makesrna/intern/rna_texture.c

Modified: trunk/blender/source/blender/makesrna/RNA_access.h
===================================================================
--- trunk/blender/source/blender/makesrna/RNA_access.h	2009-10-27 09:38:15 UTC (rev 24104)
+++ trunk/blender/source/blender/makesrna/RNA_access.h	2009-10-27 10:14:02 UTC (rev 24105)
@@ -673,6 +673,7 @@
 void RNA_property_collection_next(CollectionPropertyIterator *iter);
 void RNA_property_collection_end(CollectionPropertyIterator *iter);
 int RNA_property_collection_length(PointerRNA *ptr, PropertyRNA *prop);
+int RNA_property_collection_lookup_index(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *t_ptr);
 int RNA_property_collection_lookup_int(PointerRNA *ptr, PropertyRNA *prop, int key, PointerRNA *r_ptr);
 int RNA_property_collection_lookup_string(PointerRNA *ptr, PropertyRNA *prop, const char *key, PointerRNA *r_ptr);
 

Modified: trunk/blender/source/blender/makesrna/intern/rna_access.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_access.c	2009-10-27 09:38:15 UTC (rev 24104)
+++ trunk/blender/source/blender/makesrna/intern/rna_access.c	2009-10-27 10:14:02 UTC (rev 24105)
@@ -1660,6 +1660,25 @@
 		IDP_ResizeIDPArray(idprop, 0);
 }
 
+int RNA_property_collection_lookup_index(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *t_ptr)
+{
+	CollectionPropertyIterator iter;
+	int index= 0;
+	
+	RNA_property_collection_begin(ptr, prop, &iter);
+	for(index=0; iter.valid; RNA_property_collection_next(&iter), index++) {
+		if (iter.ptr.data == t_ptr->data)
+			break;
+	}
+	RNA_property_collection_end(&iter);
+	
+	/* did we find it? */
+	if (iter.valid)
+		return index;
+	else
+		return -1;
+}
+
 int RNA_property_collection_lookup_int(PointerRNA *ptr, PropertyRNA *prop, int key, PointerRNA *r_ptr)
 {
 	CollectionPropertyRNA *cprop= (CollectionPropertyRNA*)prop;

Modified: trunk/blender/source/blender/makesrna/intern/rna_texture.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_texture.c	2009-10-27 09:38:15 UTC (rev 24104)
+++ trunk/blender/source/blender/makesrna/intern/rna_texture.c	2009-10-27 10:14:02 UTC (rev 24105)
@@ -26,7 +26,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "RNA_access.h"
 #include "RNA_define.h"
 #include "RNA_types.h"
 
@@ -52,6 +51,10 @@
 
 #ifdef RNA_RUNTIME
 
+#include "MEM_guardedalloc.h"
+
+#include "RNA_access.h"
+
 #include "BKE_depsgraph.h"
 #include "BKE_texture.h"
 #include "BKE_main.h"
@@ -174,18 +177,9 @@
 		
 		/* get an iterator for this property, and try to find the relevant index */
 		if (prop) {
-			CollectionPropertyIterator iter;
-			int index= 0;
+			int index= RNA_property_collection_lookup_index(&id_ptr, prop, ptr);
 			
-			RNA_property_collection_begin(ptr, prop, &iter);
-			for(index=0; iter.valid; RNA_property_collection_next(&iter), index++) {
-				if (iter.ptr.data == ptr->id.data)
-					break;
-			}
-			RNA_property_collection_end(&iter);
-			
-			/* did we find it? */
-			if (iter.valid)
+			if (index >= 0)
 				return BLI_sprintfN("textures[%d]", index);
 		}
 	}
@@ -327,15 +321,110 @@
 	return item;
 }
 
+static char *rna_ColorRamp_path(PointerRNA *ptr)
+{
+	/* handle the cases where a single datablock may have 2 ramp types */
+	if (ptr->id.data) {
+		ID *id= ptr->id.data;
+		
+		switch (GS(id->name)) {
+			case ID_MA:	/* material has 2 cases - diffuse and specular */ 
+			{
+				Material *ma= (Material*)id;
+				
+				if (ptr->data == ma->ramp_col) 
+					return BLI_strdup("diffuse_ramp");
+				else if (ptr->data == ma->ramp_spec)
+					return BLI_strdup("specular_ramp");
+			}
+				break;
+		}
+	}
+	
+	/* everything else just uses 'color_ramp' */
+	return BLI_strdup("color_ramp");
+}
+
+static char *rna_ColorRampElement_path(PointerRNA *ptr)
+{
+	PointerRNA ramp_ptr;
+	PropertyRNA *prop;
+	char *path = NULL;
+	int index;
+	
+	/* helper macro for use here to try and get the path 
+	 *	- this calls the standard code for getting a path to a texture...
+	 */
+#define COLRAMP_GETPATH \
+	{ \
+		prop= RNA_struct_find_property(&ramp_ptr, "elements"); \
+		if (prop) { \
+			index= RNA_property_collection_lookup_index(&ramp_ptr, prop, ptr); \
+			if (index >= 0) { \
+				char *texture_path= rna_ColorRamp_path(&ramp_ptr); \
+				path= BLI_sprintfN("%s.elements[%d]", texture_path, index); \
+				MEM_freeN(texture_path); \
+			} \
+		} \
+	}
+	
+	/* determine the path from the ID-block to the ramp */
+	// FIXME: this is a very slow way to do it, but it will have to suffice...
+	if (ptr->id.data) {
+		ID *id= ptr->id.data;
+		
+		switch (GS(id->name)) {
+			case ID_MA: /* 2 cases for material - diffuse and spec */
+			{
+				Material *ma= (Material *)id;
+				
+				/* try diffuse first */
+				if (ma->ramp_col) {
+					RNA_pointer_create(id, &RNA_ColorRamp, ma->ramp_col, &ramp_ptr);
+					COLRAMP_GETPATH;
+				}
+				/* try specular if not diffuse */
+				if (!path && ma->ramp_spec) {
+					RNA_pointer_create(id, &RNA_ColorRamp, ma->ramp_spec, &ramp_ptr);
+					COLRAMP_GETPATH;
+				}
+			}
+				break;
+			
+			// TODO: node trees need special attention
+			case ID_NT: 
+			{
+				// FIXME: we'll probably have to loop over nodes until we find one that uses the color ramp
+			}
+				break;
+			
+			default: /* everything else should have a "color_ramp" property */
+			{
+				/* create pointer to the ID block, and try to resolve "color_ramp" pointer */
+				RNA_id_pointer_create(id, &ramp_ptr);
+				if (RNA_path_resolve(&ramp_ptr, "color_ramp", &ramp_ptr, &prop)) {
+					COLRAMP_GETPATH;
+				}
+			}
+		}
+	}
+	
+	/* cleanup the macro we defined */
+#undef COLRAMP_GETPATH
+	
+	return path;
+}
+
 #else
 
 static void rna_def_color_ramp_element(BlenderRNA *brna)
 {
 	StructRNA *srna;
 	PropertyRNA *prop;
-
+	
 	srna= RNA_def_struct(brna, "ColorRampElement", NULL);
 	RNA_def_struct_sdna(srna, "CBData");
+	RNA_def_struct_path_func(srna, "rna_ColorRampElement_path");
 	RNA_def_struct_ui_text(srna, "Color Ramp Element", "Element defining a color at a position in the color ramp.");
 
 	prop= RNA_def_property(srna, "color", PROP_FLOAT, PROP_COLOR);
@@ -366,6 +455,7 @@
 
 	srna= RNA_def_struct(brna, "ColorRamp", NULL);
 	RNA_def_struct_sdna(srna, "ColorBand");
+	RNA_def_struct_path_func(srna, "rna_ColorRamp_path");
 	RNA_def_struct_ui_text(srna, "Color Ramp", "Color ramp mapping a scalar value to a color.");
 
 	prop= RNA_def_property(srna, "elements", PROP_COLLECTION, PROP_COLOR);





More information about the Bf-blender-cvs mailing list