0
$\begingroup$

So I should be missing something simple. (Amateur here...) I am testing snippets of code to create an addon.

The idea is to create markers on the timeline from a list.

The code below works in the python console but not in the text editor

scene = bpy.data.scenes['Scene']
for j in range(len(markerLIST)):
    scene.timeline_markers.new(markerLIST[j], frame=j)

Can someone explain to me why that is? And how it fix it?

Thanks

$\endgroup$
6
  • $\begingroup$ If you read the python console start message you will notice that some modules and some variables have been imported for our convenience. eg bpy and C = bpy.context. You need to include your imports in the text editor. Too see errors blender.stackexchange.com/questions/6173/… $\endgroup$
    – batFINGER
    Nov 27, 2019 at 6:12
  • $\begingroup$ also consider using for j, name in enumerate(markerlist): to enumerate without using range and length methods. And using scene = bpy.context.scene will use the current (context) scene no matter its name. $\endgroup$
    – batFINGER
    Nov 27, 2019 at 6:27
  • $\begingroup$ Thanks for the reply. I look for the module that corresponds to the markers $\endgroup$
    – Sharl
    Nov 27, 2019 at 6:33
  • $\begingroup$ scene = bpy.data.scenes doesn't work without the ['Scene']. Maybe I am missing something. $\endgroup$
    – Sharl
    Nov 27, 2019 at 6:44
  • $\begingroup$ I am missing where you got the idea to use scene = bpy.data.scenes I suggested bpy.context.scene $\endgroup$
    – batFINGER
    Nov 27, 2019 at 6:48

1 Answer 1

1
$\begingroup$

If you read the python console start message you will notice that some modules and some variables are builtin or have been imported for our convenience. eg bpy and C = bpy.context.

PYTHON INTERACTIVE CONSOLE 3.7.3 (default, Apr  3 2019, 19:16:38)  [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]

Command History:     Up/Down Arrow
Cursor:              Left/Right Home/End
Remove:              Backspace/Delete
Execute:             Enter
Autocomplete:        Ctrl-Space
Zoom:                Ctrl +/-, Ctrl-Wheel
Builtin Modules:     bpy, bpy.data, bpy.ops, bpy.props, bpy.types, bpy.context, bpy.utils, bgl, blf, mathutils
Convenience Imports: from mathutils import *; from math import *
Convenience Variables: C = bpy.context, D = bpy.data

You need to include your imports in the text editor. To see errors Where does console output go

also consider using for j, name in enumerate(markerlist): to enumerate without using range and length methods. And using scene = bpy.context.scene will use the current (context) scene no matter its name.

import bpy # NEED THIS

scene = bpy.context.scene # < the context scene

markerLIST = ["foo", "bar"]

for j, name in enumerate(markerLIST):
    scene.timeline_markers.new(name, frame=j)

Consider giving the list both the name and frame

import bpy # NEED THIS

scene = bpy.context.scene # < the context scene

markerLIST = [(1, "foo"),
              (20, "bar"),
        ]

for j, name in markerLIST:
    scene.timeline_markers.new(name, frame=j)
$\endgroup$

You must log in to answer this question.

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