Created
November 9, 2022 10:30
-
-
Save Eterea/dd2e107d14697827d3fb0c30bd9116b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sd | |
# Import the required classes, tools and other sd stuff. | |
from sd.tools import io | |
from sd.ui.graphgrid import * | |
from sd.api.sbs.sdsbscompgraph import * | |
from sd.api.sdgraphobjectpin import * | |
from sd.api.sdgraphobjectframe import * | |
from sd.api.sdgraphobjectcomment import * | |
from sd.api.sdproperty import SDPropertyCategory | |
from sd.api.sdvalueserializer import SDValueSerializer | |
# Import OrderedDict because order in our dictionaries are important | |
from collections import OrderedDict | |
#--------------------------------------------------------------------------------------------------------- | |
# VERSION A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A | |
#--------------------------------------------------------------------------------------------------------- | |
# Get the application and UI manager object. | |
ctx = sd.getContext() | |
app = ctx.getSDApplication() | |
uiMgr = app.getQtForPythonUIMgr() | |
# Get the current graph and grid size | |
sdSBSCompGraph = uiMgr.getCurrentGraph() | |
cGridSize = GraphGrid.sGetFirstLevelSize() | |
# Get the currently selected nodes. | |
selection = uiMgr.getCurrentGraphSelectedNodes() | |
size = selection.getSize() | |
# I define these nodes, but order of selection does NOT exist in Designer | |
nodeA = selection.getItem(0) | |
nodeB = selection.getItem(1) | |
roundN = 2 # Overall round value for floats. Change this to 3 or 4 for extra accuracy | |
# Crete both Ordered Dictionaries for REFERENCE/MODIFIED nodes, and for DIFFERENCES | |
refmod_dict = OrderedDict() | |
differ_dict = OrderedDict() | |
for index, node in enumerate(selection): | |
definition = node.getDefinition() | |
nodeId = node.getIdentifier() | |
refmod_dict[index] = OrderedDict() | |
# Create a list of each property category enumeration item. | |
categories = [ | |
SDPropertyCategory.Annotation, | |
SDPropertyCategory.Input, | |
SDPropertyCategory.Output | |
] | |
# Get node properties for each property category. | |
for category in categories: | |
props = definition.getProperties(category) | |
# Get the label and identifier of each property. | |
for prop in props: | |
label = prop.getLabel() | |
# Special cases for Rotation/Angle, to note that is Turns (not degrees) | |
if 'Rotation' in label: | |
label = 'Rot-Turns' | |
elif 'Angle' in label: | |
label = 'Angle-Turns' | |
# Get the value for the currently accessed property. | |
value = node.getPropertyValue(prop) | |
if value: | |
value = SDValueSerializer.sToString(value) # This gives a convoluted result, poor readability | |
# ----------------------------------------------------------------------------- | |
# Dirty cleaner for convoluted value strings. And also for rounding floats. | |
# Example, to convert from: | |
# ('Position Random', 'SDValueFloat2(float2(0.17365,0.3249))') | |
# to a more simple and readable: | |
# ('Position Random', ('0.17', '0.32')) | |
if 'SDValueEnum' in value: | |
value = value[-2] | |
elif 'SDValueInt(int(' in value: | |
value = value.replace('SDValueInt(int(','').replace('))','') | |
elif 'SDValueInt2(int2(' in value: | |
value = value.replace('SDValueInt2(int2(','').replace('))','') | |
elif 'SDValueFloat(float(' in value: | |
value = value.replace('SDValueFloat(float(','').replace('))','') | |
value = str(round(float(value), roundN)) | |
elif 'SDValueFloat2(float2(' in value: | |
value = value.replace('SDValueFloat2(float2(','').replace('))','') | |
value0 = value.split(',')[0] | |
value1 = value.split(',')[1] | |
value = str(round(float(value0), roundN)), str(round(float(value1), roundN)) | |
elif 'SDValueFloat3(float3(' in value: | |
value = value.replace('SDValueFloat3(float3(','').replace('))','') | |
value0 = value.split(',')[0] | |
value1 = value.split(',')[1] | |
value2 = value.split(',')[2] | |
value = str(round(float(value0), roundN)), str(round(float(value1), roundN)), str(round(float(value2), roundN)) | |
elif 'SDValueFloat4(float4(' in value: | |
value = value.replace('SDValueFloat4(float4(','').replace('))','') | |
value0 = value.split(',')[0] | |
value1 = value.split(',')[1] | |
value2 = value.split(',')[2] | |
value3 = value.split(',')[3] | |
value = str(round(float(value0), roundN)), str(round(float(value1), roundN)), str(round(float(value2), roundN)), str(round(float(value3), roundN)) | |
elif 'SDValueBool(bool(' in value: | |
value = value.replace('SDValueBool(bool(','').replace('))','') | |
elif 'SDValueString(string(' in value: | |
value = value.replace('SDValueString(string(','').replace('))','') | |
elif 'SDValueTexture(SDTexture(' in value: | |
value = value.replace('SDValueTexture(SDTexture(','').replace('))','') | |
elif 'SDValueColorRGBA(ColorRGBA(' in value: | |
value = value.replace('SDValueColorRGBA(ColorRGBA(','').replace('))','') | |
else: | |
value = 'UNKNOW' | |
# ----------------------------------------------------------------------------- | |
refmod_dict[index].update({label: value}) # Add our label/value combos to dictionaries | |
print('Len Dict 0 = %s' % len(refmod_dict[0])) | |
print('Len Dict 1 = %s' % len(refmod_dict[1])) | |
for key, value in refmod_dict[0].items(): | |
if key not in refmod_dict[1]: | |
differ_dict.update({key: value}) | |
else: | |
if value != refmod_dict[1][key]: | |
differ_dict.update({key: value}) | |
differ_list = list(differ_dict.items()) # Convert Ordered Dictionary to list | |
# Clean the resulting list for simpler and better readability, breaking lines and removing some characters | |
differ_str = '\n'.join(map(str, differ_list)) # To convert to various lines | |
differ_str = differ_str.replace("(","").replace(")","").replace("'","") # Extra step to remove (') characters | |
print(differ_str) | |
# Create New Comment attached to Node, using the order hack | |
sdGraphObjectComment = SDGraphObjectComment.sNewAsChild(nodeA) | |
sdGraphObjectComment.setPosition(float2(-cGridSize*0.5, cGridSize*0.5)) | |
sdGraphObjectComment.setDescription('%s' % differ_str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment