Colored code?

How do I post code that is colored? I noticed the code is now colored on forum ,but it stays the same color when I try to post.

Here’s one where it’s colored

Wild guess based on experiences, it depends on what you use to declare something as code.

Markup ``` & ```
BBCode [ code ] & [/ code ]

It seems Markup does colors, but BBCode doesn’t.

I copied the same code from this thread. I pasted code, then select it all, and then selected the “Preformatted text” button. We don’t have code option like old forum.

You can see all code from this thread is also color.

    bl_info = {   
     "name": "addon name",   
     "author": "author",   
     "version": (1, 0),   
     "blender": (2, 7, 9),   
     "location": "",   
     "description": "",
     "warning": "",   
     "wiki_url": "",   
     "tracker_url": "",
     "category": "OBJECT"}

    import bpy

    # Addon Prefs
    class AddonPreferences (bpy.types.AddonPreferences):
        bl_idname = __name__
                                                                                                      
        enumprop_pref = bpy.props.EnumProperty(
            name = "enumprop_pref",
            items = [('ONE','One', ''),('TWO','Two','')],
            default = 'ONE'
        )

    # Register Addon Prefs
    bpy.utils.register_class (AddonPreferences)



    # Rest of the Addon
    class PanelSettings (bpy.types.PropertyGroup):
        enumprop = bpy.props.EnumProperty(
            name = "enumprop",
            items = [('ONE','One', ''),('TWO','Two','')],
            default = bpy.context.user_preferences.addons[__name__].preferences.enumprop_pref
        )
            


    # Register Addon
    def register():
        bpy.utils.register_class (PanelSettings)

    def unregister ():
        bpy.utils.unregister_class (PanelSettings)
      
    if __name__ == "__main__":   
        register()

No no no. Put a line with ``` (that is 3 grave accents, the ones that go like \ , alone in the line) above the code, and another such line below. That seems to work. And in some cases, wrapping with [ code ][/ code ] too, but it seems to apply different colors or go without color. Preformatted could be something else (and dumber).

Accents:

color

Blue word.

Tags:

color

Plain color (white in the dark theme).

Accents, different text:

bl_info = {
  "name": "addon name"
}

Some parts have color.

Tags, different text:

bl_info = {
  "name": "addon name"
}

Some parts have color.

WTF. Weird.

Now what I think you did: writing, selecting and hitting the Preformatted “button”:

color

No color, but text slightly to the right, like half the width of the c letter.

What about the one that looks like code and works with ``` or [ code ]?

bl_info = {
  "name" = "addon name"
}

Plain, just the slight offset to the right again.

Conclusion: write ``` (Markdown) or [ code ] (BBcode) and check the side preview uses colors. Maybe it’s something that needs to be configured by admins so all three work the same way.

1 Like

OH… you have to manually add [ code] and [/code]. I edited my other reply and it now has color.

@bartv
Is the code tag things something that can be added as a button to the reply box?

I’ve put a note on my todo list to look at this with low priority.

1 Like

from https://meta.discourse.org/t/syntax-highlighting-of-code-blocks/7242/3

import bpy
import utils
from math import pi , sin , cos , abs

verts = list()
faces = list()

def sphere_mesh( m , n , a , b ) :
    for col in xrange( m ) :
        for row in xrange( n ) :
            i = row * pi * 2 / n
            j = col * pi * 2 / m
            x = sin( i ) * cos( j )
            y = sin( j )
            z = cos( i ) * cos( j )
            point = ( abs( x ) ** da * sgn( x ) 
            , abs( y ) ** db * sgn( y ) 
            , abs( z ) ** da * sgn( z ) ) 
            point = surface(u, v)
            verts.append(point)
            # Connect first and last 
            # vertices on the u and v axis
            rowNext = (row + 1) % n
            colNext = (col + 1) % m
            # Indices for each qued
            faces.append(((col*n) + rowNext, 
            (colNext*n) + rowNext, 
            (colNext*n) + row, (col*n) + row))
    #print('verts : ' + str(len(verts)))
    #print('faces : ' + str(len(faces)))
    # Create mesh and object
    mesh = bpy.data.meshes.new(name+'Mesh')
    obj  = bpy.data.objects.new(name, mesh)
    obj.location = origin
    # Link object to scene
    bpy.context.scene.objects.link(obj)
    # Create mesh from given verts and faces
    mesh.from_pydata(verts, [], faces)
    #Update mesh with new data
    mesh.update(calc_edges=True)
    return obj




if __name__ == '__main__' :
    # Remove all elements
    utils.removeAll()
    # Create camera
    bpy.ops.object.add(type='CAMERA', 
    location=(0, -3.5, 0))
    cam = bpy.context.object
    cam.rotation_euler = Euler((pi/2, 0, 0), 'XYZ')
    # Make this the current camera
    bpy.context.scene.camera = cam
    # Create lamps
    utils.rainbowLights()
    # Create object and its material
    sphere = sphere_mesh( 12 , 12 , 2 , 2 )
    utils.setSmooth( sphere , 3 )
    utils.renderToFolder( 'rendering' , 
    'test_sphere' , 500 , 500 )

I copy-pasta’d the code from your post within:

```python
    <-code-here->
```

Frankly this deserves a whole chapter of its own in the getting started with discourse wiki-post.

Testing ```

bl_info = {
  "name": "addon name"
}

OK, that seems to work too.

On my iPad there doesn’t seem to be the ` ,so you have to do [code]. Although, more than likely most people won’t be posted code on a iPad.