1
$\begingroup$

Why this lattice property from "properties panel" can't be copied? enter image description here however i can copy another property e.g object location to "My Panel"

here the simple script what i've done

import bpy


class HelloWorldPanel(bpy.types.Panel):
    bl_idname = "MY_PT_Panel"
    bl_label = "My Panel"
    bl_category = "Tool"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):
        layout = self.layout

        
        lat = context.lattice
        row = layout.row()
        row.prop(lat, "points_u", text="Resolution U")
        
        obj = context.object
        row = layout.row()
        row.prop(obj, "location")


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


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


if __name__ == "__main__":
    register()

please help does i'm missing something?

$\endgroup$

1 Answer 1

1
$\begingroup$

context.lattice only exists in PROPERTIES area

There are different context members available in different spaces in blender. Always look for this in code copied from the properties space. In this case context.lattice for the data part of a lattice object, similarly context.speaker

A rule of thumb test to check a context member via py console.

>>> C.lattice
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
AttributeError: 'Context' object has no attribute 'lattice'

Can set a context pointer in the UI, have added to code below and commented out. Look for this in the properties space UI python file.

Or simply set it from the data of a lattice type context object

lat = context.object.data

The poll method ensures the context object is only of type lattice for the panel to poll and hence be drawn.

import bpy


class HelloWorldPanel(bpy.types.Panel):
    bl_idname = "MY_PT_Panel"
    bl_label = "My Panel"
    bl_category = "Tool"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"


    @classmethod
    def poll(cls, context):
        ob = context.object
        return ob and ob.type == 'LATTICE'
    
    def draw(self, context):
        layout = self.layout

        # set a context pointer
        #layout.context_pointer_set("lattice", context.object.data)
        #lat = context.lattice
        # or use the object data
        lat = context.object.data
        row = layout.row()
        row.prop(lat, "points_u", text="Resolution U")
        
        obj = context.object
        row = layout.row()
        row.prop(obj, "location")


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


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


if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

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