2
$\begingroup$

I would like to add this kind of curve property that I've seen in different Blender's operators to my custom operator:

enter image description here

(This one shows when we interpolate strokes in Grease Pencil). In other softwares is called something like Curve2D. Basically making a custom curve that I could later evaluate its value depending x (f(x) = y).

I tried right click to see the source code, but it wasn't available. So I'm here asking.

Any idea?

EDIT:

I found this answer. (Changing it a bit so the curve is drawn in an operator rather than a panel):

def myNodeTree():
    if 'TestCurveData' not in bpy.data.node_groups:
        ng = bpy.data.node_groups.new('TestCurveData', 'ShaderNodeTree')
        # ng.fake_user = True
        ng.use_fake_user = True
    return bpy.data.node_groups['TestCurveData'].nodes

curve_node_mapping = {}
def myCurveData(curve_name):
    if curve_name not in curve_node_mapping:
        cn = myNodeTree().new('ShaderNodeRGBCurve')
        curve_node_mapping[curve_name] = cn.name
    return myNodeTree()[curve_node_mapping[curve_name]]

class OT_with_curve(bpy.types.Operator):
    bl_idname = "test.curve_property"
    bl_label = "test"
    bl_options = {'REGISTER','UNDO'}


    def draw(self, context):
        self.layout.template_curve_mapping(myCurveData('TestOne'), "mapping")
        
def register():
    bpy.utils.register_class(OT_with_curve)

Now the curve shows. But when I try to modify it, I get the error:

    Python: Traceback (most recent call last):
  in:
    layout.template_curve_mapping(myCurveData('TestOne'), "mapping")
  in myCurveData
    cn = myNodeTree().new('ShaderNodeRGBCurve')
  in myNodeTree
    ---ng.fake_user = True---
      ng.use_fake_user = True---
AttributeError: Writing to ID classes in this context is not allowed: TestCurveData, NodeTree datablock, error setting ShaderNodeTree.fake_user

I get that is not possible to modify it in that context. I tried calling myNodeTree() in invoke():

def invoke(self, context: Context, event: Event):
    myNodeTree()
    return self.execute(context)

Again, I can see the curve, but when I edit it, it dissapears. After seeing in the console, I've seen:

    in myCurveData
    return myNodeTree()[curve_node_mapping[curve_name]]
KeyError: 'bpy_prop_collection[key]: key "RGB Curves" not found'

And Im a bit lost. Shouldn't it exist?

Any idea?

$\endgroup$

0

You must log in to answer this question.

Browse other questions tagged .