2
$\begingroup$

Bear with me, I am still new to python in blender, I apologize, I am not very good with words or is capable of explaining the issues I am having or solutions I am looking for. So instead of struggling with words, I'll use illustration at most of time.

I started off with creating Panel that displays two pointerPropteries coming from Array and Curve modifier.

enter image description here

...and the script.

class PROJECTX(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "PANEL"
    bl_idname = "POINTERTEST"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "PJTX"

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        col = layout.column()
        

        col.prop(bpy.context.active_object.modifiers[r"Array"],'curve', text=r"Curve")
        col.prop(bpy.context.active_object.modifiers[r"Curve"], 'object', text=r"Curve Object")

Now, Instead of two PointerProperies inside UI panel, I was hoping I could use single pointer. I was wondering if its possible to add operation that can have single pointer from Panel to assign direct to both modifiers' (array and curve) pointer at once whenever it pick curve in the scene? If so, how could I write a script for it?

enter image description here

I was looking at layout.prop_search() which I picked up from https://blender.stackexchange.com/a/101301/142719 and https://docs.blender.org/api/current/bpy.types.UILayout.html and thought maybe this can be used in Panel and assign directly to pointers in both modifiers whenever it pick curve in scene. Unfortunately I must admit I don't have better insight on .prop_Search() or PointerProperites since there aren't sufficent information on this. I was hoping I could collect some information to get me started. Any suggestions? Thanks in advance

$\endgroup$
2
  • 1
    $\begingroup$ Try to create a update() function in your Properties. It will run the function when the Properties value changed. $\endgroup$
    – X Y
    Mar 24, 2022 at 18:02
  • $\begingroup$ I am not sure I get you... can you elaborate? $\endgroup$
    – sirrus
    Mar 24, 2022 at 19:33

2 Answers 2

3
$\begingroup$

Sync Data:

import bpy
from bpy.app.handlers import persistent

@persistent
def upd_fn(dummy):
    try:
        print("-- update --")
        mds = bpy.context.active_object.modifiers
        mds[r"Curve"].object = mds[r"Array"].curve
    except:
        pass

class PROJECTX(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "PANEL"
    bl_idname = "POINTERTEST"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "PJTX"

    def draw(self, context):
        layout = self.layout
        scene = bpy.context.scene
        col = layout.column()
        col.prop(bpy.context.active_object.modifiers[r"Array"],'curve', text=r"Curve")


bpy.app.handlers.depsgraph_update_post.append(upd_fn)

def register():
    bpy.utils.register_class(PROJECTX)

def unregister():
    bpy.utils.unregister_class(PROJECTX)

if __name__ == "__main__":
    register()
$\endgroup$
2
  • 1
    $\begingroup$ yes, it is updated. $\endgroup$
    – X Y
    Mar 24, 2022 at 22:03
  • $\begingroup$ Thank you for sharing... Very interesting, Sync data is something that I'd look into. $\endgroup$
    – sirrus
    Mar 25, 2022 at 14:13
3
$\begingroup$

You can add a custom property to bpy.types.Object that holds a pointer to a curve, and use the update method to dynamically update the object's modifiers.

import bpy


def update_my_curve(self, context):
    array = self.modifiers.get("Array")
    if array:
        array.curve = self.my_curve
    curve =  self.modifiers.get("Curve")
    if curve:
        curve.object = self.my_curve


class MOD_PT_Curve(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "PANEL"
    bl_idname = "POINTERTEST"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "PJTX"

    def draw(self, context):
        self.layout.prop(context.active_object, "my_curve")
        

def register():
    bpy.utils.register_class(MOD_PT_Curve)
    bpy.types.Object.my_curve = bpy.props.PointerProperty(
        type=bpy.types.Object, 
        update=update_my_curve, 
        name="My Curve",
        poll=lambda self, object: object.type == "CURVE",  # Filter only curve objects
        )
        
if __name__ == "__main__":
    register()
$\endgroup$
1
  • 1
    $\begingroup$ Very insightful and interesting! That's something I've always want to look into for a long time. Thank you for taking time to share with me. $\endgroup$
    – sirrus
    Mar 25, 2022 at 15:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .