FYI, this is based on my old question in previous post Is it possible to use single pointer to assign on multiple modifers' Pointer Properties at once?. Now, for my learning and experiement purpose, I wanted to continue to use same script except adding few new string inside. I've encountered some issues and strugging find solution.
The goal was to get active object (Sphere) simultaneously aligned to curve's origin location after picked up curve with eyedropper.
With this single string that pasted inside blender editor and run test...
bpy.context.view_layer.objects.active.location = bpy.context.view_layer.objects.active.my_curve.location
...It was succesful, the sphere got aligned to curve's origin location.
After adding almost exacly same string inside Panel class (class MOD_PT_Curve(bpy.types.Panel
) , It didn't work. I am getting error...
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")
## NEWLY ADDED STRING -------------------------------------
active_obj = bpy.context.view_layer.objects.active
picked_curve = bpy.context.view_layer.objects.active.my_curve
active_obj.location = picked_curve.location
## -------------------------------------------------------
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()
...And the error from console show
AttributeError: Writing to ID classes in this context is not allowed: Sphere, Object datablock, error setting Object.location
Yes, I know and I believe it was incorrect way to do this, since I am still learning blender-python. I wasn't sure what other methods I could use to solve this. I need help and your help would be greatly appreciated.