public static bool HasAttribute(this HoudiniGeo geo, string attrName, HoudiniGeoAttributeOwner owner) { if (owner == HoudiniGeoAttributeOwner.Any) { return(geo.attributes.Any(a => a.name == attrName)); } return(geo.attributes.Any(a => a.owner == owner && a.name == attrName)); }
private static void ParseAttributes(HoudiniGeo geo, JToken attributesValueToken) { // "attributes",[ // "vertexattributes",[ // [(attribute obj)], // [(attribute obj)], // ... // ], // "pointattributes",[ // [(attribute obj)], // [(attribute obj)], // ... // ], // ... // ], if (ATTRIBUTES_TO_PARSE == null) { ATTRIBUTES_TO_PARSE = new Dictionary <string, HoudiniGeoAttributeOwner>(); ATTRIBUTES_TO_PARSE.Add("vertexattributes", HoudiniGeoAttributeOwner.Vertex); ATTRIBUTES_TO_PARSE.Add("pointattributes", HoudiniGeoAttributeOwner.Point); ATTRIBUTES_TO_PARSE.Add("primitiveattributes", HoudiniGeoAttributeOwner.Primitive); ATTRIBUTES_TO_PARSE.Add("globalattributes", HoudiniGeoAttributeOwner.Detail); } Dictionary <string, JToken> attributeTokensDict = ArrayKeyValueToDictionary(attributesValueToken.Children().ToArray()); // Parse each attribute group var geoAttributes = new List <HoudiniGeoAttribute>(); foreach (var attrKeyVal in ATTRIBUTES_TO_PARSE) { string attrGroupKey = attrKeyVal.Key; HoudiniGeoAttributeOwner attrOwner = attrKeyVal.Value; JToken groupValueToken; if (attributeTokensDict.TryGetValue(attrGroupKey, out groupValueToken)) { // Parse each attribute in group foreach (var attributeToken in groupValueToken.Children()) { var attribute = ParseSingleAttribute(attributeToken, attrOwner); if (attribute != null) { geoAttributes.Add(attribute); } } } } geo.attributes = geoAttributes.ToArray(); }
public static bool TryGetAttribute(this HoudiniGeo geo, string attrName, HoudiniGeoAttributeType type, HoudiniGeoAttributeOwner owner, out HoudiniGeoAttribute attr) { if (owner == HoudiniGeoAttributeOwner.Any) { attr = geo.attributes.FirstOrDefault(a => a.type == type && a.name == attrName); } else { attr = geo.attributes.FirstOrDefault(a => a.owner == owner && a.type == type && a.name == attrName); } return(attr != null); }
private static HoudiniGeoAttribute ParseSingleAttribute(JToken attrToken, HoudiniGeoAttributeOwner owner) { // NUMERIC // [ // [ // "scope","public", // "type","numeric", // "name","P", <- Extract This // "options",{ // "type":{ // "type":"string", // "value":"hpoint" // } // } // ], // [ // "size",4, <- Extract This // "storage","fpreal32", <- Extract This // "defaults",[ // "size",4, // "storage","fpreal64", // "values",[0,0,0,1] // ], // "values",[ // "size",4, // "storage","fpreal32", // "tuples",[[-0.5,-0.5,-0.5,1],[0.5,-0.5,-0.5,1],...] <- Extract This // ] // ] // ] // STRING // [ // [ // "scope","public", // "type","string", // "name","varmap", // "options",{ // } // ], // [ // "size",1, // "storage","int32", // "strings",["SHIT_INT -> SHIT_INT"], // "indices",[ // "size",1, // "storage","int32", // "arrays",[[0]] // ] // ] // ] JToken[] childBlockTokens = attrToken.Children().ToArray(); JToken headerToken = childBlockTokens[0]; JToken bodyToken = childBlockTokens[1]; var geoAttribute = new HoudiniGeoAttribute(); geoAttribute.owner = owner; // Parse header block Dictionary <string, JToken> headerBlockDict = ArrayKeyValueToDictionary(headerToken.Children().ToArray()); geoAttribute.name = headerBlockDict["name"].Value <string>(); string valueType = headerBlockDict["type"].Value <string>(); // Parse body block Dictionary <string, JToken> valuesBlockDict = ArrayKeyValueToDictionary(bodyToken.Children().ToArray()); geoAttribute.tupleSize = valuesBlockDict["size"].Value <int>(); // Parse Numeric types if (valueType == "numeric") { // Get storage type (float, int) string storageType = valuesBlockDict["storage"].Value <string>(); geoAttribute.type = AttributeTypeStrToEnumValue(storageType); if (geoAttribute.type == HoudiniGeoAttributeType.Invalid) { Debug.LogWarning("HoudiniGeoFileParser: unsuppored numeric storage type " + valueType); return(null); } // Get all values Dictionary <string, JToken> valuesDict = ArrayKeyValueToDictionary(valuesBlockDict["values"].Children().ToArray()); if (geoAttribute.type == HoudiniGeoAttributeType.Float) { int tupleSize = valuesDict["size"].Value <int>(); string valuesKey = (tupleSize == 1) ? "arrays" : "tuples"; geoAttribute.floatValues = valuesDict[valuesKey].Children().SelectMany(t => t.Values <float>()).ToArray(); } else if (geoAttribute.type == HoudiniGeoAttributeType.Integer) { geoAttribute.intValues = valuesDict["arrays"].Children().SelectMany(t => t.Values <int>()).ToArray(); } } // Parse String types else if (valueType == "string") { geoAttribute.type = HoudiniGeoAttributeType.String; Dictionary <string, JToken> indicesDict = ArrayKeyValueToDictionary(valuesBlockDict["indices"].Children().ToArray()); string[] stringValues = valuesBlockDict["strings"].Values <string>().ToArray(); int[] indices = indicesDict["arrays"].Children().SelectMany(t => t.Values <int>()).ToArray(); geoAttribute.stringValues = indices.Select(i => (i >= 0 && i < stringValues.Length) ? stringValues[i] : "").ToArray(); } // Unexpected type? else { Debug.LogWarning("HoudiniGeoFileParser: unsuppored attribute valueType " + valueType); return(null); } return(geoAttribute); }